Skip to content

Commit 6847173

Browse files
authored
Merge pull request #2042 from xutruth/main
fix encodePacked DynamicBytes
2 parents 84c9fe9 + b1ad81b commit 6847173

File tree

4 files changed

+32
-7
lines changed

4 files changed

+32
-7
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
1212
* Fix Sign method [#2033](https://github.com/hyperledger/web3j/pull/2033)
1313
* Revert 2031 and 2033 [#2034](https://github.com/hyperledger/web3j/pull/2034)
1414
* Web3j release fix [2037](https://github.com/hyperledger/web3j/pull/2037)
15+
* Fix encodePacked DynamicBytes [2042](https://github.com/hyperledger/web3j/pull/2042)
1516

1617
### Features
1718

abi/src/main/java/org/web3j/abi/TypeEncoder.java

+7-2
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,11 @@ public static String encode(Type parameter) {
101101
*/
102102
public static String encodePacked(Type parameter) {
103103
if (parameter instanceof Utf8String) {
104-
return removePadding(encode(parameter), parameter);
104+
//removePadding can also be used, but is not necessary
105+
return Numeric.toHexStringNoPrefix(((Utf8String) parameter).getValue().getBytes(StandardCharsets.UTF_8));
105106
} else if (parameter instanceof DynamicBytes) {
106-
return encode(parameter).substring(64);
107+
//removePadding can also be used, but is not necessary
108+
return Numeric.toHexStringNoPrefix(((DynamicBytes) parameter).getValue());
107109
} else if (parameter instanceof DynamicArray) {
108110
return arrayEncodePacked((DynamicArray) parameter);
109111
} else if (parameter instanceof StaticArray) {
@@ -140,6 +142,9 @@ static String removePadding(String encodedValue, Type parameter) {
140142
int length =
141143
((Utf8String) parameter).getValue().getBytes(StandardCharsets.UTF_8).length;
142144
return encodedValue.substring(64, 64 + length * 2);
145+
}
146+
if (parameter instanceof DynamicBytes) {
147+
return encodedValue.substring(64, 64 + ((DynamicBytes) parameter).getValue().length * 2);
143148
} else {
144149
throw new UnsupportedOperationException(
145150
"Type cannot be encoded: " + parameter.getClass());

abi/src/test/java/org/web3j/abi/DefaultFunctionEncoderTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import org.web3j.abi.datatypes.generated.Bytes10;
3434
import org.web3j.abi.datatypes.generated.Uint256;
3535
import org.web3j.abi.datatypes.generated.Uint32;
36+
import org.web3j.utils.Numeric;
3637

3738
import static org.junit.jupiter.api.Assertions.assertEquals;
3839

@@ -151,6 +152,19 @@ public void testEncodeConstructorPacked_multipleParameters() {
151152
new Address("0x663e27AdC18d862dA9A82f060310621D379e469a"),
152153
new Uint256(BigInteger.TEN),
153154
new Bytes10("1234567890".getBytes()))));
155+
assertEquals(
156+
"0000004501000102030405",
157+
FunctionEncoder.encodeConstructorPacked(
158+
Arrays.asList(
159+
new Uint32(BigInteger.valueOf(69)),
160+
new Bool(true),
161+
new DynamicBytes((new byte[]{0, 1, 2, 3, 4, 5})))));
162+
assertEquals(
163+
"12000102030405",
164+
FunctionEncoder.encodeConstructorPacked(
165+
Arrays.asList(
166+
new DynamicBytes(Numeric.hexStringToByteArray("0x12")),
167+
new DynamicBytes((new byte[]{0, 1, 2, 3, 4, 5})))));
154168
}
155169

156170
@Test

abi/src/test/java/org/web3j/abi/TypeEncoderPackedTest.java

+10-5
Original file line numberDiff line numberDiff line change
@@ -905,17 +905,22 @@ public void testStaticArrayEncodePacked() {
905905
public void testDynamicBytesEncodePacked() {
906906
DynamicBytes dynamicBytes = new DynamicBytes(new byte[] {0, 1, 2, 3, 4, 5});
907907
assertEquals(
908-
"0001020304050000000000000000000000000000000000000000000000000000",
908+
"000102030405",
909909
TypeEncoder.encodePacked(dynamicBytes));
910910

911-
DynamicBytes empty = new DynamicBytes(new byte[] {0});
911+
DynamicBytes zero = new DynamicBytes(new byte[] {0});
912912
assertEquals(
913-
"0000000000000000000000000000000000000000000000000000000000000000",
913+
"00",
914+
TypeEncoder.encodePacked(zero));
915+
916+
DynamicBytes empty = new DynamicBytes(new byte[] {});
917+
assertEquals(
918+
"",
914919
TypeEncoder.encodePacked(empty));
915920

916921
DynamicBytes dave = new DynamicBytes("dave".getBytes());
917922
assertEquals(
918-
"6461766500000000000000000000000000000000000000000000000000000000",
923+
"64617665",
919924
TypeEncoder.encodePacked(dave));
920925

921926
DynamicBytes loremIpsum =
@@ -942,7 +947,7 @@ public void testDynamicBytesEncodePacked() {
942947
+ "756c6c612070617269617475722e204578636570746575722073696e74206f63"
943948
+ "63616563617420637570696461746174206e6f6e2070726f6964656e742c2073"
944949
+ "756e7420696e2063756c706120717569206f666669636961206465736572756e"
945-
+ "74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e000000"),
950+
+ "74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e"),
946951
TypeEncoder.encodePacked(loremIpsum));
947952
}
948953

0 commit comments

Comments
 (0)