|
1 | 1 | package org.arkecosystem.crypto.utils; |
2 | 2 |
|
3 | 3 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 4 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
4 | 5 |
|
5 | 6 | import java.math.BigInteger; |
6 | 7 | import java.util.Arrays; |
@@ -62,6 +63,120 @@ void it_should_expose_read_uint_as_static() { |
62 | 63 | assertEquals(BigInteger.valueOf(32), AbiDecoder.readUInt(bytes, 0)); |
63 | 64 | } |
64 | 65 |
|
| 66 | + @Test |
| 67 | + void it_should_expose_decode_signed_negative_number_as_static() { |
| 68 | + byte[] bytes = |
| 69 | + Numeric.hexStringToByteArray( |
| 70 | + "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"); |
| 71 | + |
| 72 | + assertEquals("-1", AbiDecoder.decodeNumber(bytes, 0, 256, true)[0]); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void it_should_expose_decode_string_as_static() { |
| 77 | + byte[] bytes = |
| 78 | + Numeric.hexStringToByteArray( |
| 79 | + "0000000000000000000000000000000000000000000000000000000000000020" |
| 80 | + + "000000000000000000000000000000000000000000000000000000000000000b" |
| 81 | + + "68656c6c6f20776f726c64000000000000000000000000000000000000000000"); |
| 82 | + |
| 83 | + Object[] result = AbiDecoder.decodeString(bytes, 0); |
| 84 | + |
| 85 | + assertEquals("hello world", result[0]); |
| 86 | + assertEquals(32, result[1]); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + void it_should_expose_decode_dynamic_bytes_as_static() { |
| 91 | + byte[] bytes = |
| 92 | + Numeric.hexStringToByteArray( |
| 93 | + "0000000000000000000000000000000000000000000000000000000000000020" |
| 94 | + + "0000000000000000000000000000000000000000000000000000000000000004" |
| 95 | + + "deadbeef00000000000000000000000000000000000000000000000000000000"); |
| 96 | + |
| 97 | + Object[] result = AbiDecoder.decodeDynamicBytes(bytes, 0); |
| 98 | + |
| 99 | + assertEquals("0xdeadbeef", result[0]); |
| 100 | + assertEquals(32, result[1]); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + void it_should_expose_decode_fixed_bytes_as_static() { |
| 105 | + byte[] bytes = |
| 106 | + Numeric.hexStringToByteArray( |
| 107 | + "deadbeef00000000000000000000000000000000000000000000000000000000"); |
| 108 | + |
| 109 | + Object[] result = AbiDecoder.decodeFixedBytes(bytes, 0, 4); |
| 110 | + |
| 111 | + assertEquals("0xdeadbeef", result[0]); |
| 112 | + assertEquals(32, result[1]); |
| 113 | + } |
| 114 | + |
| 115 | + @Test |
| 116 | + void it_should_expose_decode_fixed_array_as_static() throws Exception { |
| 117 | + byte[] bytes = |
| 118 | + Numeric.hexStringToByteArray( |
| 119 | + "0000000000000000000000000000000000000000000000000000000000000001" |
| 120 | + + "0000000000000000000000000000000000000000000000000000000000000002" |
| 121 | + + "0000000000000000000000000000000000000000000000000000000000000003"); |
| 122 | + Map<String, Object> param = new HashMap<>(); |
| 123 | + param.put("type", "uint256"); |
| 124 | + |
| 125 | + Object[] result = AbiDecoder.decodeArray(bytes, 0, param, 3); |
| 126 | + |
| 127 | + assertEquals(Arrays.asList("1", "2", "3"), result[0]); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void it_should_expose_decode_dynamic_array_as_static() throws Exception { |
| 132 | + byte[] bytes = |
| 133 | + Numeric.hexStringToByteArray( |
| 134 | + "0000000000000000000000000000000000000000000000000000000000000020" |
| 135 | + + "0000000000000000000000000000000000000000000000000000000000000003" |
| 136 | + + "000000000000000000000000000000000000000000000000000000000000000a" |
| 137 | + + "0000000000000000000000000000000000000000000000000000000000000014" |
| 138 | + + "000000000000000000000000000000000000000000000000000000000000001e"); |
| 139 | + Map<String, Object> param = new HashMap<>(); |
| 140 | + param.put("type", "uint256"); |
| 141 | + |
| 142 | + Object[] result = AbiDecoder.decodeArray(bytes, 0, param, null); |
| 143 | + |
| 144 | + assertEquals(Arrays.asList("10", "20", "30"), result[0]); |
| 145 | + } |
| 146 | + |
| 147 | + @Test |
| 148 | + void it_should_expose_decode_static_tuple_as_static() throws Exception { |
| 149 | + byte[] bytes = |
| 150 | + Numeric.hexStringToByteArray( |
| 151 | + "000000000000000000000000000000000000000000000000000000000000002a" |
| 152 | + + "000000000000000000000000512f366d524157bcf734546eb29a6d687b762255"); |
| 153 | + Map<String, Object> uintComponent = new HashMap<>(); |
| 154 | + uintComponent.put("type", "uint256"); |
| 155 | + uintComponent.put("name", "amount"); |
| 156 | + Map<String, Object> addrComponent = new HashMap<>(); |
| 157 | + addrComponent.put("type", "address"); |
| 158 | + addrComponent.put("name", "recipient"); |
| 159 | + Map<String, Object> param = new HashMap<>(); |
| 160 | + param.put("components", Arrays.asList(uintComponent, addrComponent)); |
| 161 | + |
| 162 | + Object[] result = AbiDecoder.decodeTuple(bytes, 0, param); |
| 163 | + |
| 164 | + Map<String, Object> tuple = (Map<String, Object>) result[0]; |
| 165 | + assertEquals("42", tuple.get("amount")); |
| 166 | + assertEquals("0x512F366D524157BcF734546eB29a6d687B762255", tuple.get("recipient")); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + void it_should_throw_when_function_selector_is_not_found() { |
| 171 | + String unknownSelector = "deadbeef"; |
| 172 | + |
| 173 | + Exception thrown = |
| 174 | + assertThrows( |
| 175 | + Exception.class, () -> decoder.decodeFunctionData("0x" + unknownSelector)); |
| 176 | + |
| 177 | + assertEquals("Function selector not found in ABI: " + unknownSelector, thrown.getMessage()); |
| 178 | + } |
| 179 | + |
65 | 180 | @Test |
66 | 181 | void it_should_decode_vote_payload() throws Exception { |
67 | 182 | String functionName = "vote"; |
|
0 commit comments