-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathAbiDecoderTest.java
More file actions
232 lines (182 loc) · 8.78 KB
/
AbiDecoderTest.java
File metadata and controls
232 lines (182 loc) · 8.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
package org.arkecosystem.crypto.utils;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.web3j.utils.Numeric;
class AbiDecoderTest {
private AbiDecoder decoder;
@BeforeEach
void setUp() throws Exception {
decoder = new AbiDecoder();
}
@Test
void it_should_expose_decode_address_as_static() {
byte[] bytes =
Numeric.hexStringToByteArray(
"000000000000000000000000512f366d524157bcf734546eb29a6d687b762255");
Object[] result = AbiDecoder.decodeAddress(bytes, 0);
assertEquals("0x512F366D524157BcF734546eB29a6d687B762255", result[0]);
assertEquals(32, result[1]);
}
@Test
void it_should_expose_decode_bool_as_static() {
byte[] truthy =
Numeric.hexStringToByteArray(
"0000000000000000000000000000000000000000000000000000000000000001");
byte[] falsy =
Numeric.hexStringToByteArray(
"0000000000000000000000000000000000000000000000000000000000000000");
assertEquals(true, AbiDecoder.decodeBool(truthy, 0)[0]);
assertEquals(false, AbiDecoder.decodeBool(falsy, 0)[0]);
}
@Test
void it_should_expose_decode_number_as_static() {
byte[] bytes =
Numeric.hexStringToByteArray(
"00000000000000000000000000000000000000000000000000000000000000ff");
assertEquals("255", AbiDecoder.decodeNumber(bytes, 0, 256, false)[0]);
}
@Test
void it_should_expose_read_uint_as_static() {
byte[] bytes =
Numeric.hexStringToByteArray(
"0000000000000000000000000000000000000000000000000000000000000020");
assertEquals(BigInteger.valueOf(32), AbiDecoder.readUInt(bytes, 0));
}
@Test
void it_should_expose_decode_signed_negative_number_as_static() {
byte[] bytes =
Numeric.hexStringToByteArray(
"ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff");
assertEquals("-1", AbiDecoder.decodeNumber(bytes, 0, 256, true)[0]);
}
@Test
void it_should_expose_decode_string_as_static() {
byte[] bytes =
Numeric.hexStringToByteArray(
"0000000000000000000000000000000000000000000000000000000000000020"
+ "000000000000000000000000000000000000000000000000000000000000000b"
+ "68656c6c6f20776f726c64000000000000000000000000000000000000000000");
Object[] result = AbiDecoder.decodeString(bytes, 0);
assertEquals("hello world", result[0]);
assertEquals(32, result[1]);
}
@Test
void it_should_expose_decode_dynamic_bytes_as_static() {
byte[] bytes =
Numeric.hexStringToByteArray(
"0000000000000000000000000000000000000000000000000000000000000020"
+ "0000000000000000000000000000000000000000000000000000000000000004"
+ "deadbeef00000000000000000000000000000000000000000000000000000000");
Object[] result = AbiDecoder.decodeDynamicBytes(bytes, 0);
assertEquals("0xdeadbeef", result[0]);
assertEquals(32, result[1]);
}
@Test
void it_should_expose_decode_fixed_bytes_as_static() {
byte[] bytes =
Numeric.hexStringToByteArray(
"deadbeef00000000000000000000000000000000000000000000000000000000");
Object[] result = AbiDecoder.decodeFixedBytes(bytes, 0, 4);
assertEquals("0xdeadbeef", result[0]);
assertEquals(32, result[1]);
}
@Test
void it_should_expose_decode_fixed_array_as_static() throws Exception {
byte[] bytes =
Numeric.hexStringToByteArray(
"0000000000000000000000000000000000000000000000000000000000000001"
+ "0000000000000000000000000000000000000000000000000000000000000002"
+ "0000000000000000000000000000000000000000000000000000000000000003");
Map<String, Object> param = new HashMap<>();
param.put("type", "uint256");
Object[] result = AbiDecoder.decodeArray(bytes, 0, param, 3);
assertEquals(Arrays.asList("1", "2", "3"), result[0]);
}
@Test
void it_should_expose_decode_dynamic_array_as_static() throws Exception {
byte[] bytes =
Numeric.hexStringToByteArray(
"0000000000000000000000000000000000000000000000000000000000000020"
+ "0000000000000000000000000000000000000000000000000000000000000003"
+ "000000000000000000000000000000000000000000000000000000000000000a"
+ "0000000000000000000000000000000000000000000000000000000000000014"
+ "000000000000000000000000000000000000000000000000000000000000001e");
Map<String, Object> param = new HashMap<>();
param.put("type", "uint256");
Object[] result = AbiDecoder.decodeArray(bytes, 0, param, null);
assertEquals(Arrays.asList("10", "20", "30"), result[0]);
}
@Test
void it_should_expose_decode_static_tuple_as_static() throws Exception {
byte[] bytes =
Numeric.hexStringToByteArray(
"000000000000000000000000000000000000000000000000000000000000002a"
+ "000000000000000000000000512f366d524157bcf734546eb29a6d687b762255");
Map<String, Object> uintComponent = new HashMap<>();
uintComponent.put("type", "uint256");
uintComponent.put("name", "amount");
Map<String, Object> addrComponent = new HashMap<>();
addrComponent.put("type", "address");
addrComponent.put("name", "recipient");
Map<String, Object> param = new HashMap<>();
param.put("components", Arrays.asList(uintComponent, addrComponent));
Object[] result = AbiDecoder.decodeTuple(bytes, 0, param);
Map<String, Object> tuple = (Map<String, Object>) result[0];
assertEquals("42", tuple.get("amount"));
assertEquals("0x512F366D524157BcF734546eB29a6d687B762255", tuple.get("recipient"));
}
@Test
void it_should_throw_when_function_selector_is_not_found() {
String unknownSelector = "deadbeef";
Exception thrown =
assertThrows(
Exception.class, () -> decoder.decodeFunctionData("0x" + unknownSelector));
assertEquals("Function selector not found in ABI: " + unknownSelector, thrown.getMessage());
}
@Test
void it_should_decode_vote_payload() throws Exception {
String functionName = "vote";
List<Object> args = Arrays.asList("0x512F366D524157BcF734546eB29a6d687B762255");
String data = "0x6dd7d8ea000000000000000000000000512f366d524157bcf734546eb29a6d687b762255";
Map<String, Object> decodedData = decoder.decodeFunctionData(data);
Map<String, Object> expectedData = new HashMap<>();
expectedData.put("functionName", functionName);
expectedData.put("args", args);
assertEquals(expectedData, decodedData);
}
@Test
void it_should_decode_error_payload() throws Exception {
assertEquals("CallerIsNotValidator", decoder.decodeError("cd03235e"));
}
@Test
void it_should_decode_error_payload_with_hex_prefix() throws Exception {
assertEquals("CallerIsNotValidator", decoder.decodeError("0xcd03235e"));
}
@Test
void it_should_decode_error_with_parameters() throws Exception {
String selector = stripSelector("InvalidRange(uint256,uint256)");
assertEquals("InvalidRange", decoder.decodeError(selector));
}
@Test
void it_should_throw_when_error_selector_is_not_found() {
Exception thrown = assertThrows(Exception.class, () -> decoder.decodeError("123456ab"));
assertEquals("Function selector not found in ABI: 123456ab", thrown.getMessage());
}
@Test
void it_should_not_decode_function_selectors_as_errors() {
String voteSelector = "6dd7d8ea";
Exception thrown = assertThrows(Exception.class, () -> decoder.decodeError(voteSelector));
assertEquals("Function selector not found in ABI: " + voteSelector, thrown.getMessage());
}
private String stripSelector(String signature) {
String hash = org.web3j.crypto.Hash.sha3String(signature);
return hash.startsWith("0x") ? hash.substring(2, 10) : hash.substring(0, 8);
}
}