Skip to content

Commit

Permalink
Added function signature helpers to Decoder.java
Browse files Browse the repository at this point in the history
  • Loading branch information
wkennedy committed Jul 16, 2024
1 parent 87a3c5b commit faf84e6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/main/java/com/github/wkennedy/abi/Decoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,22 @@ public List<DecodedLog> decodeLogs(String data) {
return decodeLogs(logs);
}

public static String getFunctionSignature(byte[] data) {
byte[] methodBytes = Arrays.copyOf(data, 4);
return HEX_PREFIX + Hex.encodeHexString(methodBytes);
}

public static String getFunctionSignature(String data) {
String noPrefix = data.replaceFirst(HEX_PREFIX, "");
byte[] decodedDataInBytes;
try {
decodedDataInBytes = Hex.decodeHex(noPrefix.toUpperCase());
return getFunctionSignature(decodedDataInBytes);
} catch (DecoderException e) {
throw new RuntimeException(e);
}
}

private Log[] fromJson(String data) {
try {
return objectMapper.readValue(data, Log[].class);
Expand Down
5 changes: 5 additions & 0 deletions src/test/java/com/github/wkennedy/abi/DecoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,11 @@ public void testDecodeFunctionTupleContainingDynamicTypes() throws IOException {
// assertEquals(DecodedFunctions.MULTICALL, decodedFunction.getName());
}

@Test
public void testGetFunctionSignature() {
String functionSignature = Decoder.getFunctionSignature("0xac9650d80000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000100000000000000000000000000000000000000000000000000000000000000008413ead5620000000000000000000000005c8cd1c2f2997f7a041026cc29de8177b4c6d8ec00000000000000000000000089e54f174ca5ff39cf53ab58004158e2ca012eac0000000000000000000000000000000000000000000000000000000000000bb8000000000000000000000000000000000035f2482336c0d4c2ba6e94faa1d66f000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000164883164560000000000000000000000005c8cd1c2f2997f7a041026cc29de8177b4c6d8ec00000000000000000000000089e54f174ca5ff39cf53ab58004158e2ca012eac0000000000000000000000000000000000000000000000000000000000000bb8fffffffffffffffffffffffffffffffffffffffffffffffffffffffffff2764c00000000000000000000000000000000000000000000000000000000000a11a8000000000000000000000000000000000000000000000000000000e8d4a510000000000000000000000000000000000000000000000a56d35c029fd16645e079000000000000000000000000000000000000000000000000000000e840308c030000000000000000000000000000000000000000000a503344abc0fbe23670910000000000000000000000005a2b5cb4ce921abd65f0c66c2c839894bfc2076c000000000000000000000000000000000000000000000000000000006244356a00000000000000000000000000000000000000000000000000000000");
assertEquals("0x73b80d49777f0c32d45a0a5a7c3487eb9e8da2c93922540c260cdafc3e81a165", functionSignature);
}

@Test
public void testDecodeMulticall() throws IOException {
Expand Down

0 comments on commit faf84e6

Please sign in to comment.