Last active
March 30, 2022 22:47
-
-
Save Mleekko/ba44531b7af7a8af6c3a7b0de27fcf52 to your computer and use it in GitHub Desktop.
Encoding and Decoding messages in Radix transactions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.commons.codec.DecoderException; | |
import org.apache.commons.codec.binary.Hex; | |
import java.nio.charset.StandardCharsets; | |
public class MessageEncoder { | |
public static String decode(String message) throws DecoderException { | |
if (message.startsWith("0000")) { // sent through new API | |
byte[] bytes = Hex.decodeHex(message.substring(4).toCharArray()); | |
return new String(bytes, StandardCharsets.UTF_8); | |
} else if (message.startsWith("30303030")) { // sent through old API, | |
// you can skip this section if you are going to process only transactions sent after March 1 | |
byte[] bytes = Hex.decodeHex(message.toCharArray()); | |
return decode(new String(bytes, StandardCharsets.UTF_8)); | |
} else { | |
throw new DecoderException("Can't parse message: " + message); | |
} | |
} | |
public static String encode(String message) { | |
return "0000" + Hex.encodeHexString(message.getBytes(StandardCharsets.UTF_8)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment