Skip to content

Instantly share code, notes, and snippets.

@logbon72
Created June 11, 2020 20:00
Show Gist options
  • Save logbon72/2cb71fc88a84ca9ed7ea7c20c863951a to your computer and use it in GitHub Desktop.
Save logbon72/2cb71fc88a84ca9ed7ea7c20c863951a to your computer and use it in GitHub Desktop.
DecodeBinanceSignedData is a way to decode signed Binance data in Java.
import com.binance.dex.api.client.TransactionConverter;
import com.binance.dex.api.client.domain.broadcast.Transaction;
import com.binance.dex.api.client.encoding.message.MessageType;
import com.binance.dex.api.proto.StdTx;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.protobuf.CodedInputStream;
import org.bouncycastle.util.encoders.Hex;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;
public class DecodeBinanceSignedData {
@Test
public void aminoUnWrap() throws IOException {
byte[] data = Hex.decode("db01f0625dee0a65ce6dc0430a14b6561dcc104130059a7c08f48c64610c1f6f9064122b423635363144434331303431333030353941374330384634384336343631304331463646393036342d31311a0b4254432d3543345f424e42200228013080c2d72f3880989abc044001126e0a26eb5ae9872103baf53d1424f8ea83d03a82f6d157b5401c4ea57ffb8317872e15a19fc9b7ad7b1240e79a6606d28cf07b9cc6f566b524a5282b13beccc3162376c79f392620c95a447b19f64e761e22a7a3bc311a780e7d9fdd521e2f7edec25308c5bac6aa1c0a311801200a");
CodedInputStream cis = CodedInputStream.newInstance(data);
long tag = cis.readUInt64();
System.out.println("Tag: " + tag + " --> " + cis.getTotalBytesRead());
byte[] typeBytes = MessageType.StdTx.getTypePrefixBytes();
byte[] typeRead = cis.readRawBytes(typeBytes.length);
System.out.println("Type = " + Hex.toHexString(typeRead));
byte[] txData = cis.readRawBytes((int) (tag - typeRead.length));
StdTx tx = StdTx.parseFrom(txData);
TransactionConverter converter = new TransactionConverter("bnb");
List<Transaction> transactions = tx.getMsgsList().stream().map((msg) -> {
return converter.convert(msg.toByteArray());
})
.collect(Collectors.toList());
Assertions.assertFalse(transactions.isEmpty());
System.out.println("transaction: " + new ObjectMapper().writeValueAsString(transactions));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment