org.web3j.abi.datatypes.DynamicBytes Java Examples

The following examples show how to use org.web3j.abi.datatypes.DynamicBytes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: UtilsTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetTypeName() throws ClassNotFoundException {
    assertThat(Utils.getTypeName(new TypeReference<Uint>(){}), is("uint256"));
    assertThat(Utils.getTypeName(new TypeReference<Int>(){}), is("int256"));
    assertThat(Utils.getTypeName(new TypeReference<Ufixed>(){}), is("ufixed256"));
    assertThat(Utils.getTypeName(new TypeReference<Fixed>(){}), is("fixed256"));

    assertThat(Utils.getTypeName(new TypeReference<Uint64>(){}), is("uint64"));
    assertThat(Utils.getTypeName(new TypeReference<Int64>(){}), is("int64"));
    assertThat(Utils.getTypeName(new TypeReference<Bool>(){}), is("bool"));
    assertThat(Utils.getTypeName(new TypeReference<Utf8String>(){}), is("string"));
    assertThat(Utils.getTypeName(new TypeReference<DynamicBytes>(){}), is("bytes"));

    assertThat(Utils.getTypeName(
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5){}),
            is("uint256[5]"));
    assertThat(Utils.getTypeName(
            new TypeReference<DynamicArray<Uint>>(){}),
            is("uint256[]"));
}
 
Example #2
Source File: DepositContract.java    From teku with Apache License 2.0 6 votes vote down vote up
public RemoteFunctionCall<TransactionReceipt> deposit(
    byte[] pubkey,
    byte[] withdrawal_credentials,
    byte[] signature,
    byte[] deposit_data_root,
    BigInteger weiValue) {
  final org.web3j.abi.datatypes.Function function =
      new org.web3j.abi.datatypes.Function(
          FUNC_DEPOSIT,
          Arrays.<Type>asList(
              new org.web3j.abi.datatypes.DynamicBytes(pubkey),
              new org.web3j.abi.datatypes.DynamicBytes(withdrawal_credentials),
              new org.web3j.abi.datatypes.DynamicBytes(signature),
              new org.web3j.abi.datatypes.generated.Bytes32(deposit_data_root)),
          Collections.<TypeReference<?>>emptyList());
  return executeRemoteCallTransaction(function, weiValue);
}
 
Example #3
Source File: SolidityFunctionWrapperTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetNativeType() {
    assertThat(getNativeType(TypeName.get(Address.class)),
            equalTo(TypeName.get(String.class)));
    assertThat(getNativeType(TypeName.get(Uint256.class)),
            equalTo(TypeName.get(BigInteger.class)));
    assertThat(getNativeType(TypeName.get(Int256.class)),
            equalTo(TypeName.get(BigInteger.class)));
    assertThat(getNativeType(TypeName.get(Utf8String.class)),
            equalTo(TypeName.get(String.class)));
    assertThat(getNativeType(TypeName.get(Bool.class)),
            equalTo(TypeName.get(Boolean.class)));
    assertThat(getNativeType(TypeName.get(Bytes32.class)),
            equalTo(TypeName.get(byte[].class)));
    assertThat(getNativeType(TypeName.get(DynamicBytes.class)),
            equalTo(TypeName.get(byte[].class)));
}
 
Example #4
Source File: AbiTypesMapperGenerator.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
private MethodSpec.Builder addTypes(MethodSpec.Builder builder, String packageName) {
    builder = addStatement(builder, packageName,
            Address.TYPE_NAME, Address.class.getSimpleName());

    builder = addStatement(builder, packageName,
            Bool.TYPE_NAME, Bool.class.getSimpleName());

    builder = addStatement(builder, packageName,
            Utf8String.TYPE_NAME, Utf8String.class.getSimpleName());

    builder = addStatement(builder, packageName,
            DynamicBytes.TYPE_NAME, DynamicBytes.class.getSimpleName());

    // TODO: Fixed array & dynamic array support
    return builder;
}
 
Example #5
Source File: TypeDecoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
static <T extends Type> T decode(String input, int offset, Class<T> type) {
    if (NumericType.class.isAssignableFrom(type)) {
        return (T) decodeNumeric(input.substring(offset), (Class<NumericType>) type);
    } else if (Address.class.isAssignableFrom(type)) {
        return (T) decodeAddress(input.substring(offset));
    } else if (Bool.class.isAssignableFrom(type)) {
        return (T) decodeBool(input, offset);
    } else if (Bytes.class.isAssignableFrom(type)) {
        return (T) decodeBytes(input, offset, (Class<Bytes>) type);
    } else if (DynamicBytes.class.isAssignableFrom(type)) {
        return (T) decodeDynamicBytes(input, offset);
    } else if (Utf8String.class.isAssignableFrom(type)) {
        return (T) decodeUtf8String(input, offset);
    } else if (Array.class.isAssignableFrom(type)) {
        throw new UnsupportedOperationException(
                "Array types must be wrapped in a TypeReference");
    } else {
        throw new UnsupportedOperationException("Type cannot be encoded: " + type.getClass());
    }
}
 
Example #6
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
public RemoteCall<Tuple2<BigInteger, byte[]>> ABI(byte[] node, BigInteger contentTypes) {
    final Function function = new Function("ABI",
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node),
            new org.web3j.abi.datatypes.generated.Uint256(contentTypes)),
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<DynamicBytes>() {}));
    return new RemoteCall<Tuple2<BigInteger, byte[]>>(
            new Callable<Tuple2<BigInteger, byte[]>>() {
                @Override
                public Tuple2<BigInteger, byte[]> call() throws Exception {
                    List<Type> results = executeCallMultipleValueReturn(function);;
                    return new Tuple2<BigInteger, byte[]>(
                            (BigInteger) results.get(0).getValue(),
                            (byte[]) results.get(1).getValue());
                }
            });
}
 
Example #7
Source File: FunctionEncoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testFunctionMDynamicArrayEncode1() {
    Function function = new Function(
            "sam",
            Arrays.asList(
                new DynamicBytes("dave".getBytes()),
                new Bool(true),
                new DynamicArray<>(
                        new Uint(BigInteger.ONE),
                        new Uint(BigInteger.valueOf(2)),
                        new Uint(BigInteger.valueOf(3)))),
            Collections.<TypeReference<?>>emptyList()
    );

    assertThat(FunctionEncoder.encode(function),
            is("0xa5643bf2"
                    + "0000000000000000000000000000000000000000000000000000000000000060"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "00000000000000000000000000000000000000000000000000000000000000a0"
                    + "0000000000000000000000000000000000000000000000000000000000000004"
                    + "6461766500000000000000000000000000000000000000000000000000000000"
                    + "0000000000000000000000000000000000000000000000000000000000000003"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "0000000000000000000000000000000000000000000000000000000000000002"
                    + "0000000000000000000000000000000000000000000000000000000000000003"));
}
 
Example #8
Source File: UtilsTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTypeName() throws ClassNotFoundException {
    assertThat(Utils.getTypeName(new TypeReference<Uint>(){}), is("uint256"));
    assertThat(Utils.getTypeName(new TypeReference<Int>(){}), is("int256"));
    assertThat(Utils.getTypeName(new TypeReference<Ufixed>(){}), is("ufixed256"));
    assertThat(Utils.getTypeName(new TypeReference<Fixed>(){}), is("fixed256"));

    assertThat(Utils.getTypeName(new TypeReference<Uint64>(){}), is("uint64"));
    assertThat(Utils.getTypeName(new TypeReference<Int64>(){}), is("int64"));
    assertThat(Utils.getTypeName(new TypeReference<Bool>(){}), is("bool"));
    assertThat(Utils.getTypeName(new TypeReference<Utf8String>(){}), is("string"));
    assertThat(Utils.getTypeName(new TypeReference<DynamicBytes>(){}), is("bytes"));

    assertThat(Utils.getTypeName(
            new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5){}),
            is("uint256[5]"));
    assertThat(Utils.getTypeName(
            new TypeReference<DynamicArray<Uint>>(){}),
            is("uint256[]"));
}
 
Example #9
Source File: SolidityFunctionWrapperTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testGetNativeType() {
    assertThat(getNativeType(TypeName.get(Address.class)),
            equalTo(TypeName.get(String.class)));
    assertThat(getNativeType(TypeName.get(Uint256.class)),
            equalTo(TypeName.get(BigInteger.class)));
    assertThat(getNativeType(TypeName.get(Int256.class)),
            equalTo(TypeName.get(BigInteger.class)));
    assertThat(getNativeType(TypeName.get(Utf8String.class)),
            equalTo(TypeName.get(String.class)));
    assertThat(getNativeType(TypeName.get(Bool.class)),
            equalTo(TypeName.get(Boolean.class)));
    assertThat(getNativeType(TypeName.get(Bytes32.class)),
            equalTo(TypeName.get(byte[].class)));
    assertThat(getNativeType(TypeName.get(DynamicBytes.class)),
            equalTo(TypeName.get(byte[].class)));
}
 
Example #10
Source File: Utils.java    From web3j with Apache License 2.0 6 votes vote down vote up
static String getSimpleTypeName(Class<?> type) {
    String simpleName = type.getSimpleName().toLowerCase();

    if (type.equals(Uint.class)
            || type.equals(Int.class)
            || type.equals(Ufixed.class)
            || type.equals(Fixed.class)) {
        return simpleName + "256";
    } else if (type.equals(Utf8String.class)) {
        return "string";
    } else if (type.equals(DynamicBytes.class)) {
        return "bytes";
    } else {
        return simpleName;
    }
}
 
Example #11
Source File: TypeEncoder.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static String encode(Type parameter) {
    if (parameter instanceof NumericType) {
        return encodeNumeric(((NumericType) parameter));
    } else if (parameter instanceof Address) {
        return encodeAddress((Address) parameter);
    } else if (parameter instanceof Bool) {
        return encodeBool((Bool) parameter);
    } else if (parameter instanceof Bytes) {
        return encodeBytes((Bytes) parameter);
    } else if (parameter instanceof DynamicBytes) {
        return encodeDynamicBytes((DynamicBytes) parameter);
    } else if (parameter instanceof Utf8String) {
        return encodeString((Utf8String) parameter);
    } else if (parameter instanceof StaticArray) {
        return encodeArrayValues((StaticArray) parameter);
    } else if (parameter instanceof DynamicArray) {
        return encodeDynamicArray((DynamicArray) parameter);
    } else {
        throw new UnsupportedOperationException(
                "Type cannot be encoded: " + parameter.getClass());
    }
}
 
Example #12
Source File: AbiTypesMapperGenerator.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private MethodSpec.Builder addTypes(MethodSpec.Builder builder, String packageName) {
    builder = addStatement(builder, packageName,
            Address.TYPE_NAME, Address.class.getSimpleName());

    builder = addStatement(builder, packageName,
            Bool.TYPE_NAME, Bool.class.getSimpleName());

    builder = addStatement(builder, packageName,
            Utf8String.TYPE_NAME, Utf8String.class.getSimpleName());

    builder = addStatement(builder, packageName,
            DynamicBytes.TYPE_NAME, DynamicBytes.class.getSimpleName());

    // TODO: Fixed array & dynamic array support
    return builder;
}
 
Example #13
Source File: TypeEncoder.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public static String encode(Type parameter) {
    if (parameter instanceof NumericType) {
        return encodeNumeric(((NumericType) parameter));
    } else if (parameter instanceof Address) {
        return encodeAddress((Address) parameter);
    } else if (parameter instanceof Bool) {
        return encodeBool((Bool) parameter);
    } else if (parameter instanceof Bytes) {
        return encodeBytes((Bytes) parameter);
    } else if (parameter instanceof DynamicBytes) {
        return encodeDynamicBytes((DynamicBytes) parameter);
    } else if (parameter instanceof Utf8String) {
        return encodeString((Utf8String) parameter);
    } else if (parameter instanceof StaticArray) {
        return encodeArrayValues((StaticArray) parameter);
    } else if (parameter instanceof DynamicArray) {
        return encodeDynamicArray((DynamicArray) parameter);
    } else {
        throw new UnsupportedOperationException(
                "Type cannot be encoded: " + parameter.getClass());
    }
}
 
Example #14
Source File: TypeDecoderTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
@Test
public void multiDimArrays() throws Exception {
    byte[] bytes1d = new byte[] {1, 2, 3};
    byte[][] bytes2d = new byte[][] {bytes1d, bytes1d, bytes1d};
    final byte[][][] bytes3d = new byte[][][] {bytes2d, bytes2d, bytes2d};

    assertEquals(TypeDecoder.instantiateType("bytes", bytes1d), (new DynamicBytes(bytes1d)));

    Type twoDim = TypeDecoder.instantiateType("uint256[][3]", bytes2d);
    assertTrue(twoDim instanceof StaticArray3);
    StaticArray3<DynamicArray<Uint256>> staticArray3 =
            (StaticArray3<DynamicArray<Uint256>>) twoDim;
    assertEquals(staticArray3.getComponentType(), DynamicArray.class);
    DynamicArray<Uint256> row1 = staticArray3.getValue().get(1);
    assertEquals(row1.getValue().get(2), new Uint256(3));

    Type threeDim = TypeDecoder.instantiateType("uint256[][3][3]", bytes3d);
    assertTrue(threeDim instanceof StaticArray3);
    StaticArray3<StaticArray3<DynamicArray<Uint256>>> staticArray3StaticArray3 =
            (StaticArray3<StaticArray3<DynamicArray<Uint256>>>) threeDim;
    assertEquals(staticArray3StaticArray3.getComponentType(), StaticArray3.class);
    row1 = staticArray3StaticArray3.getValue().get(1).getValue().get(1);
    assertEquals(row1.getValue().get(1), (new Uint256(2)));
}
 
Example #15
Source File: UtilsTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTypeName() {
    assertEquals(Utils.getTypeName(new TypeReference<Uint>() {}), ("uint256"));
    assertEquals(Utils.getTypeName(new TypeReference<Int>() {}), ("int256"));
    assertEquals(Utils.getTypeName(new TypeReference<Ufixed>() {}), ("ufixed256"));
    assertEquals(Utils.getTypeName(new TypeReference<Fixed>() {}), ("fixed256"));

    assertEquals(Utils.getTypeName(new TypeReference<Uint64>() {}), ("uint64"));
    assertEquals(Utils.getTypeName(new TypeReference<Int64>() {}), ("int64"));
    assertEquals(Utils.getTypeName(new TypeReference<Bool>() {}), ("bool"));
    assertEquals(Utils.getTypeName(new TypeReference<Utf8String>() {}), ("string"));
    assertEquals(Utils.getTypeName(new TypeReference<DynamicBytes>() {}), ("bytes"));

    assertEquals(
            Utils.getTypeName(
                    new TypeReference.StaticArrayTypeReference<StaticArray<Uint>>(5) {}),
            ("uint256[5]"));
    assertEquals(Utils.getTypeName(new TypeReference<DynamicArray<Uint>>() {}), ("uint256[]"));
}
 
Example #16
Source File: FunctionEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testFunctionMDynamicArrayEncode1() {
    Function function = new Function(
            "sam",
            Arrays.asList(
                new DynamicBytes("dave".getBytes()),
                new Bool(true),
                new DynamicArray<>(
                        new Uint(BigInteger.ONE),
                        new Uint(BigInteger.valueOf(2)),
                        new Uint(BigInteger.valueOf(3)))),
            Collections.<TypeReference<?>>emptyList()
    );

    assertThat(FunctionEncoder.encode(function),
            is("0xa5643bf2"
                    + "0000000000000000000000000000000000000000000000000000000000000060"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "00000000000000000000000000000000000000000000000000000000000000a0"
                    + "0000000000000000000000000000000000000000000000000000000000000004"
                    + "6461766500000000000000000000000000000000000000000000000000000000"
                    + "0000000000000000000000000000000000000000000000000000000000000003"
                    + "0000000000000000000000000000000000000000000000000000000000000001"
                    + "0000000000000000000000000000000000000000000000000000000000000002"
                    + "0000000000000000000000000000000000000000000000000000000000000003"));
}
 
Example #17
Source File: PublicResolver.java    From web3j with Apache License 2.0 6 votes vote down vote up
public RemoteCall<Tuple2<BigInteger, byte[]>> ABI(byte[] node, BigInteger contentTypes) {
    final Function function = new Function(FUNC_ABI, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node), 
            new org.web3j.abi.datatypes.generated.Uint256(contentTypes)), 
            Arrays.<TypeReference<?>>asList(new TypeReference<Uint256>() {}, new TypeReference<DynamicBytes>() {}));
    return new RemoteCall<Tuple2<BigInteger, byte[]>>(
            new Callable<Tuple2<BigInteger, byte[]>>() {
                @Override
                public Tuple2<BigInteger, byte[]> call() throws Exception {
                    List<Type> results = executeCallMultipleValueReturn(function);
                    return new Tuple2<BigInteger, byte[]>(
                            (BigInteger) results.get(0).getValue(), 
                            (byte[]) results.get(1).getValue());
                }
            });
}
 
Example #18
Source File: DepositContract.java    From teku with Apache License 2.0 5 votes vote down vote up
public RemoteFunctionCall<byte[]> get_deposit_count() {
  final org.web3j.abi.datatypes.Function function =
      new org.web3j.abi.datatypes.Function(
          FUNC_GET_DEPOSIT_COUNT,
          Arrays.<Type>asList(),
          Arrays.<TypeReference<?>>asList(new TypeReference<DynamicBytes>() {}));
  return executeRemoteCallSingleValueReturn(function, byte[].class);
}
 
Example #19
Source File: FunctionReturnDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testDecodeIndexedDynamicBytesValue() {
    DynamicBytes bytes = new DynamicBytes(new byte[]{ 1, 2, 3, 4, 5});
    String encoded = TypeEncoder.encodeDynamicBytes(bytes);
    String hash = Hash.sha3(encoded);

    assertThat(FunctionReturnDecoder.decodeIndexedValue(
            hash,
            new TypeReference<DynamicBytes>() {}),
            equalTo(new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example #20
Source File: DefaultFunctionReturnDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static <T extends Type> int getDataOffset(String input, int offset, Class<T> type) {
    if (DynamicBytes.class.isAssignableFrom(type)
            || Utf8String.class.isAssignableFrom(type)
            || DynamicArray.class.isAssignableFrom(type)) {
        return TypeDecoder.decodeUintAsInt(input, offset) << 1;
    } else {
        return offset;
    }
}
 
Example #21
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static DynamicBytes decodeDynamicBytes(String input, int offset) {
    int encodedLength = decodeUintAsInt(input, offset);
    int hexStringEncodedLength = encodedLength << 1;

    int valueOffset = offset + MAX_BYTE_LENGTH_FOR_HEX_STRING;

    String data = input.substring(valueOffset, valueOffset + hexStringEncodedLength);
    byte[] bytes = Numeric.hexStringToByteArray(data);

    return new DynamicBytes(bytes);
}
 
Example #22
Source File: TypeEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static <T extends Type> String encodeArrayValuesOffsets(DynamicArray<T> value) {
    StringBuilder result = new StringBuilder();
    boolean arrayOfBytes =
            !value.getValue().isEmpty() && value.getValue().get(0) instanceof DynamicBytes;
    boolean arrayOfString =
            !value.getValue().isEmpty() && value.getValue().get(0) instanceof Utf8String;
    if (arrayOfBytes || arrayOfString) {
        long offset = 0;
        for (int i = 0; i < value.getValue().size(); i++) {
            if (i == 0) {
                offset = value.getValue().size() * MAX_BYTE_LENGTH;
            } else {
                int bytesLength =
                        arrayOfBytes
                                ? ((byte[]) value.getValue().get(i - 1).getValue()).length
                                : ((String) value.getValue().get(i - 1).getValue()).length();
                int numberOfWords = (bytesLength + MAX_BYTE_LENGTH - 1) / MAX_BYTE_LENGTH;
                int totalBytesLength = numberOfWords * MAX_BYTE_LENGTH;
                offset += totalBytesLength + MAX_BYTE_LENGTH;
            }
            result.append(
                    Numeric.toHexStringNoPrefix(
                            Numeric.toBytesPadded(
                                    new BigInteger(Long.toString(offset)), MAX_BYTE_LENGTH)));
        }
    }
    return result.toString();
}
 
Example #23
Source File: FunctionReturnDecoderTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testDecodeIndexedDynamicBytesValue() {
    DynamicBytes bytes = new DynamicBytes(new byte[] {1, 2, 3, 4, 5});
    String encoded = TypeEncoder.encodeDynamicBytes(bytes);
    String hash = Hash.sha3(encoded);

    assertEquals(
            FunctionReturnDecoder.decodeIndexedValue(
                    hash, new TypeReference<DynamicBytes>() {}),
            (new Bytes32(Numeric.hexStringToByteArray(hash))));
}
 
Example #24
Source File: SolidityFunctionWrapperTest.java    From web3j with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetNativeType() {
    assertEquals(getNativeType(TypeName.get(Address.class)), (TypeName.get(String.class)));
    assertEquals(getNativeType(TypeName.get(Uint256.class)), (TypeName.get(BigInteger.class)));
    assertEquals(getNativeType(TypeName.get(Int256.class)), (TypeName.get(BigInteger.class)));
    assertEquals(getNativeType(TypeName.get(Utf8String.class)), (TypeName.get(String.class)));
    assertEquals(getNativeType(TypeName.get(Bool.class)), (TypeName.get(Boolean.class)));
    assertEquals(getNativeType(TypeName.get(Bytes32.class)), (TypeName.get(byte[].class)));
    assertEquals(getNativeType(TypeName.get(DynamicBytes.class)), (TypeName.get(byte[].class)));
}
 
Example #25
Source File: PublicResolver.java    From web3j with Apache License 2.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> setABI(byte[] node, BigInteger contentType, byte[] data) {
    final Function function = new Function(
            FUNC_SETABI, 
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node), 
            new org.web3j.abi.datatypes.generated.Uint256(contentType), 
            new org.web3j.abi.datatypes.DynamicBytes(data)), 
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #26
Source File: TypeEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static String encodeDynamicBytes(DynamicBytes dynamicBytes) {
    int size = dynamicBytes.getValue().length;
    String encodedLength = encode(new Uint(BigInteger.valueOf(size)));
    String encodedValue = encodeBytes(dynamicBytes);

    StringBuilder result = new StringBuilder();
    result.append(encodedLength);
    result.append(encodedValue);
    return result.toString();
}
 
Example #27
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static <T extends Type> int getSingleElementLength(String input, int offset, Class<T> type) {
    if (input.length() == offset) {
        return 0;
    } else if (DynamicBytes.class.isAssignableFrom(type)
            || Utf8String.class.isAssignableFrom(type)) {
        // length field + data value
        return (decodeUintAsInt(input, offset) / Type.MAX_BYTE_LENGTH) + 2;
    } else {
        return 1;
    }
}
 
Example #28
Source File: PublicResolver.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public RemoteCall<TransactionReceipt> setABI(byte[] node, BigInteger contentType, byte[] data) {
    Function function = new Function(
            "setABI",
            Arrays.<Type>asList(new org.web3j.abi.datatypes.generated.Bytes32(node),
            new org.web3j.abi.datatypes.generated.Uint256(contentType),
            new org.web3j.abi.datatypes.DynamicBytes(data)),
            Collections.<TypeReference<?>>emptyList());
    return executeRemoteCallTransaction(function);
}
 
Example #29
Source File: FunctionEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testFunctionMDynamicArrayEncode2() {
    Function function = new Function(
            "f",
            Arrays.asList(
                new Uint(BigInteger.valueOf(0x123)),
                new DynamicArray<>(
                        new Uint32(BigInteger.valueOf(0x456)),
                        new Uint32(BigInteger.valueOf(0x789))
                ),
                new Bytes10("1234567890".getBytes()),
                new DynamicBytes("Hello, world!".getBytes())),
            Collections.<TypeReference<?>>emptyList()
    );

    assertThat(FunctionEncoder.encode(function),
            is("0x8be65246"
                    + "0000000000000000000000000000000000000000000000000000000000000123"
                    + "0000000000000000000000000000000000000000000000000000000000000080"
                    + "3132333435363738393000000000000000000000000000000000000000000000"
                    + "00000000000000000000000000000000000000000000000000000000000000e0"
                    + "0000000000000000000000000000000000000000000000000000000000000002"
                    + "0000000000000000000000000000000000000000000000000000000000000456"
                    + "0000000000000000000000000000000000000000000000000000000000000789"
                    + "000000000000000000000000000000000000000000000000000000000000000d"
                    + "48656c6c6f2c20776f726c642100000000000000000000000000000000000000"
            ));
}
 
Example #30
Source File: SolidityFunctionWrapper.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static TypeName getNativeType(TypeName typeName) {

        if (typeName instanceof ParameterizedTypeName) {
            return getNativeType((ParameterizedTypeName) typeName);
        }

        String simpleName = ((ClassName) typeName).simpleName();

        if (simpleName.equals(Address.class.getSimpleName())) {
            return TypeName.get(String.class);
        } else if (simpleName.startsWith("Uint")) {
            return TypeName.get(BigInteger.class);
        } else if (simpleName.startsWith("Int")) {
            return TypeName.get(BigInteger.class);
        } else if (simpleName.equals(Utf8String.class.getSimpleName())) {
            return TypeName.get(String.class);
        } else if (simpleName.startsWith("Bytes")) {
            return TypeName.get(byte[].class);
        } else if (simpleName.equals(DynamicBytes.class.getSimpleName())) {
            return TypeName.get(byte[].class);
        } else if (simpleName.equals(Bool.class.getSimpleName())) {
            return TypeName.get(Boolean.class);  // boolean cannot be a parameterized type
        } else {
            throw new UnsupportedOperationException(
                    "Unsupported type: " + typeName
                            + ", no native type mapping exists.");
        }
    }