Skip to content

Commit

Permalink
encodePacked DynamicBytes
Browse files Browse the repository at this point in the history
Signed-off-by: hamburger <1210062377@qq.com>
  • Loading branch information
xutruth committed Apr 18, 2024
1 parent b552fee commit 20882df
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
2 changes: 1 addition & 1 deletion abi/src/main/java/org/web3j/abi/TypeEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public static String encodePacked(Type parameter) {
if (parameter instanceof Utf8String) {
return removePadding(encode(parameter), parameter);
} else if (parameter instanceof DynamicBytes) {
return encode(parameter).substring(64);
return Numeric.toHexStringNoPrefix(((DynamicBytes) parameter).getValue());
} else if (parameter instanceof DynamicArray) {
return arrayEncodePacked((DynamicArray) parameter);
} else if (parameter instanceof StaticArray) {
Expand Down
14 changes: 14 additions & 0 deletions abi/src/test/java/org/web3j/abi/DefaultFunctionEncoderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.web3j.abi.datatypes.generated.Bytes10;
import org.web3j.abi.datatypes.generated.Uint256;
import org.web3j.abi.datatypes.generated.Uint32;
import org.web3j.utils.Numeric;

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

Expand Down Expand Up @@ -151,6 +152,19 @@ public void testEncodeConstructorPacked_multipleParameters() {
new Address("0x663e27AdC18d862dA9A82f060310621D379e469a"),
new Uint256(BigInteger.TEN),
new Bytes10("1234567890".getBytes()))));
assertEquals(
"0000004501000102030405",
FunctionEncoder.encodeConstructorPacked(
Arrays.asList(
new Uint32(BigInteger.valueOf(69)),
new Bool(true),
new DynamicBytes((new byte[]{0, 1, 2, 3, 4, 5})))));
assertEquals(
"12000102030405",
FunctionEncoder.encodeConstructorPacked(
Arrays.asList(
new DynamicBytes(Numeric.hexStringToByteArray("0x12")),
new DynamicBytes((new byte[]{0, 1, 2, 3, 4, 5})))));
}

@Test
Expand Down
15 changes: 10 additions & 5 deletions abi/src/test/java/org/web3j/abi/TypeEncoderPackedTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -905,17 +905,22 @@ public void testStaticArrayEncodePacked() {
public void testDynamicBytesEncodePacked() {
DynamicBytes dynamicBytes = new DynamicBytes(new byte[] {0, 1, 2, 3, 4, 5});
assertEquals(
"0001020304050000000000000000000000000000000000000000000000000000",
"000102030405",
TypeEncoder.encodePacked(dynamicBytes));

DynamicBytes empty = new DynamicBytes(new byte[] {0});
DynamicBytes zero = new DynamicBytes(new byte[] {0});
assertEquals(
"0000000000000000000000000000000000000000000000000000000000000000",
"00",
TypeEncoder.encodePacked(zero));

DynamicBytes empty = new DynamicBytes(new byte[] {});
assertEquals(
"",
TypeEncoder.encodePacked(empty));

DynamicBytes dave = new DynamicBytes("dave".getBytes());
assertEquals(
"6461766500000000000000000000000000000000000000000000000000000000",
"64617665",
TypeEncoder.encodePacked(dave));

DynamicBytes loremIpsum =
Expand All @@ -942,7 +947,7 @@ public void testDynamicBytesEncodePacked() {
+ "756c6c612070617269617475722e204578636570746575722073696e74206f63"
+ "63616563617420637570696461746174206e6f6e2070726f6964656e742c2073"
+ "756e7420696e2063756c706120717569206f666669636961206465736572756e"
+ "74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e000000"),
+ "74206d6f6c6c697420616e696d20696420657374206c61626f72756d2e"),
TypeEncoder.encodePacked(loremIpsum));
}

Expand Down

0 comments on commit 20882df

Please sign in to comment.