Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix encodePacked DynamicBytes #2042

Merged
merged 13 commits into from
Apr 25, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ See [Conventional Commits](https://conventionalcommits.org) for commit guideline
* Fix Sign method [#2033](https://github.com/hyperledger/web3j/pull/2033)
* Revert 2031 and 2033 [#2034](https://github.com/hyperledger/web3j/pull/2034)
* Web3j release fix [2037](https://github.com/hyperledger/web3j/pull/2037)
* Fix encodePacked DynamicBytes [2042](https://github.com/hyperledger/web3j/pull/2042)

### Features

Expand Down
9 changes: 7 additions & 2 deletions abi/src/main/java/org/web3j/abi/TypeEncoder.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,11 @@ public static String encode(Type parameter) {
*/
public static String encodePacked(Type parameter) {
if (parameter instanceof Utf8String) {
return removePadding(encode(parameter), parameter);
//removePadding can also be used, but is not necessary
return Numeric.toHexStringNoPrefix(((Utf8String) parameter).getValue().getBytes(StandardCharsets.UTF_8));
} else if (parameter instanceof DynamicBytes) {
return encode(parameter).substring(64);
//removePadding can also be used, but is not necessary
return Numeric.toHexStringNoPrefix(((DynamicBytes) parameter).getValue());
} else if (parameter instanceof DynamicArray) {
return arrayEncodePacked((DynamicArray) parameter);
} else if (parameter instanceof StaticArray) {
Expand Down Expand Up @@ -140,6 +142,9 @@ static String removePadding(String encodedValue, Type parameter) {
int length =
((Utf8String) parameter).getValue().getBytes(StandardCharsets.UTF_8).length;
return encodedValue.substring(64, 64 + length * 2);
}
if (parameter instanceof DynamicBytes) {
return encodedValue.substring(64, 64 + ((DynamicBytes) parameter).getValue().length * 2);
} else {
throw new UnsupportedOperationException(
"Type cannot be encoded: " + parameter.getClass());
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
Loading