org.web3j.abi.datatypes.Bytes Java Examples

The following examples show how to use org.web3j.abi.datatypes.Bytes. 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: TypeDecoderTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticBytes() throws Exception {
    byte[] testbytes = new byte[] {0, 1, 2, 3, 4, 5};
    Bytes6 staticBytes = new Bytes6(testbytes);
    assertEquals(
            TypeDecoder.decodeBytes(
                    "0001020304050000000000000000000000000000000000000000000000000000",
                    Bytes6.class),
            (staticBytes));

    Bytes empty = new Bytes1(new byte[] {0});
    assertEquals(
            TypeDecoder.decodeBytes(
                    "0000000000000000000000000000000000000000000000000000000000000000",
                    Bytes1.class),
            (empty));

    Bytes dave = new Bytes4("dave".getBytes());
    assertEquals(
            TypeDecoder.decodeBytes(
                    "6461766500000000000000000000000000000000000000000000000000000000",
                    Bytes4.class),
            (dave));

    assertEquals(TypeDecoder.instantiateType("bytes6", testbytes), (new Bytes6(testbytes)));
}
 
Example #2
Source File: TypeEncoderTest.java    From web3j with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticBytes() {
    Bytes staticBytes = new Bytes6(new byte[] {0, 1, 2, 3, 4, 5});
    assertEquals(
            TypeEncoder.encodeBytes(staticBytes),
            ("0001020304050000000000000000000000000000000000000000000000000000"));

    Bytes empty = new Bytes1(new byte[] {0});
    assertEquals(
            TypeEncoder.encodeBytes(empty),
            ("0000000000000000000000000000000000000000000000000000000000000000"));

    Bytes ones = new Bytes1(new byte[] {127});
    assertEquals(
            TypeEncoder.encodeBytes(ones),
            ("7f00000000000000000000000000000000000000000000000000000000000000"));

    Bytes dave = new Bytes4("dave".getBytes());
    assertEquals(
            TypeEncoder.encodeBytes(dave),
            ("6461766500000000000000000000000000000000000000000000000000000000"));
}
 
Example #3
Source File: TypeDecoder.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
static <T extends Bytes> T decodeBytes(String input, int offset, Class<T> type) {
    try {
        String simpleName = type.getSimpleName();
        String[] splitName = simpleName.split(Bytes.class.getSimpleName());
        int length = Integer.parseInt(splitName[1]);
        int hexStringLength = length << 1;

        byte[] bytes = Numeric.hexStringToByteArray(
                input.substring(offset, offset + hexStringLength));
        return type.getConstructor(byte[].class).newInstance(bytes);
    } catch (NoSuchMethodException | SecurityException
            | InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example #4
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 #5
Source File: TypeDecoderTest.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
@Test
public void testStaticBytes() {
    Bytes6 staticBytes = new Bytes6(new byte[] { 0, 1, 2, 3, 4, 5 });
    assertThat(TypeDecoder.decodeBytes(
            "0001020304050000000000000000000000000000000000000000000000000000", Bytes6.class),
            is(staticBytes));

    Bytes empty = new Bytes1(new byte[] { 0 });
    assertThat(TypeDecoder.decodeBytes(
            "0000000000000000000000000000000000000000000000000000000000000000", Bytes1.class),
            is(empty));

    Bytes dave = new Bytes4("dave".getBytes());
    assertThat(TypeDecoder.decodeBytes(
            "6461766500000000000000000000000000000000000000000000000000000000", Bytes4.class),
            is(dave));
}
 
Example #6
Source File: TypeDecoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
static <T extends Bytes> T decodeBytes(String input, int offset, Class<T> type) {
    try {
        String simpleName = type.getSimpleName();
        String[] splitName = simpleName.split(Bytes.class.getSimpleName());
        int length = Integer.parseInt(splitName[1]);
        int hexStringLength = length << 1;

        byte[] bytes =
                Numeric.hexStringToByteArray(input.substring(offset, offset + hexStringLength));
        return type.getConstructor(byte[].class).newInstance(bytes);
    } catch (NoSuchMethodException
            | SecurityException
            | InstantiationException
            | IllegalAccessException
            | IllegalArgumentException
            | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example #7
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 #8
Source File: DefaultFunctionReturnDecoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
@SuppressWarnings("unchecked")
public <T extends Type> Type decodeEventParameter(
        String rawInput, TypeReference<T> typeReference) {

    String input = Numeric.cleanHexPrefix(rawInput);

    try {
        Class<T> type = typeReference.getClassType();

        if (Bytes.class.isAssignableFrom(type)) {
            Class<Bytes> bytesClass = (Class<Bytes>) Class.forName(type.getName());
            return TypeDecoder.decodeBytes(input, bytesClass);
        } else if (Array.class.isAssignableFrom(type)
                || BytesType.class.isAssignableFrom(type)
                || Utf8String.class.isAssignableFrom(type)) {
            return TypeDecoder.decodeBytes(input, Bytes32.class);
        } else {
            return TypeDecoder.decode(input, type);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("Invalid class reference provided", e);
    }
}
 
Example #9
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
static <T extends Bytes> T decodeBytes(String input, int offset, Class<T> type) {
    try {
        String simpleName = type.getSimpleName();
        String[] splitName = simpleName.split(Bytes.class.getSimpleName());
        int length = Integer.parseInt(splitName[1]);
        int hexStringLength = length << 1;

        byte[] bytes = Numeric.hexStringToByteArray(
                input.substring(offset, offset + hexStringLength));
        return type.getConstructor(byte[].class).newInstance(bytes);
    } catch (NoSuchMethodException | SecurityException
            | InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example #10
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 #11
Source File: TypeDecoderTest.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
@Test
public void testStaticBytes() {
    Bytes6 staticBytes = new Bytes6(new byte[] { 0, 1, 2, 3, 4, 5 });
    assertThat(TypeDecoder.decodeBytes(
            "0001020304050000000000000000000000000000000000000000000000000000", Bytes6.class),
            is(staticBytes));

    Bytes empty = new Bytes1(new byte[] { 0 });
    assertThat(TypeDecoder.decodeBytes(
            "0000000000000000000000000000000000000000000000000000000000000000", Bytes1.class),
            is(empty));

    Bytes dave = new Bytes4("dave".getBytes());
    assertThat(TypeDecoder.decodeBytes(
            "6461766500000000000000000000000000000000000000000000000000000000", Bytes4.class),
            is(dave));
}
 
Example #12
Source File: AbiTypesGenerator.java    From web3j with Apache License 2.0 5 votes vote down vote up
private <T extends Type> void generateBytesTypes(String path) throws IOException {
    String packageName = createPackageName(Bytes.class);
    ClassName className;

    for (int byteSize = 1; byteSize <= 32; byteSize++) {

        MethodSpec constructorSpec =
                MethodSpec.constructorBuilder()
                        .addModifiers(Modifier.PUBLIC)
                        .addParameter(byte[].class, "value")
                        .addStatement("super($L, $N)", byteSize, "value")
                        .build();

        className = ClassName.get(packageName, Bytes.class.getSimpleName() + byteSize);

        FieldSpec defaultFieldSpec =
                FieldSpec.builder(
                                className,
                                DEFAULT,
                                Modifier.PUBLIC,
                                Modifier.STATIC,
                                Modifier.FINAL)
                        .initializer("new $T(new byte[$L])", className, byteSize)
                        .build();

        TypeSpec bytesType =
                TypeSpec.classBuilder(className.simpleName())
                        .addJavadoc(CODEGEN_WARNING)
                        .superclass(Bytes.class)
                        .addModifiers(Modifier.PUBLIC)
                        .addField(defaultFieldSpec)
                        .addMethod(constructorSpec)
                        .build();

        write(packageName, bytesType, path);
    }
}
 
Example #13
Source File: TypeEncoder.java    From web3j with Apache License 2.0 5 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 DynamicStruct) {
        return encodeDynamicStruct((DynamicStruct) parameter);
    } else if (parameter instanceof DynamicArray) {
        return encodeDynamicArray((DynamicArray) parameter);
    } else if (parameter instanceof PrimitiveType) {
        return encode(((PrimitiveType) parameter).toSolidityType());
    } else {
        throw new UnsupportedOperationException(
                "Type cannot be encoded: " + parameter.getClass());
    }
}
 
Example #14
Source File: AbiTypesMapperGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private MethodSpec.Builder generateFixedBytesTypes(MethodSpec.Builder builder,
                                                   String packageName) {
    for (int byteSize = 1; byteSize <= 32; byteSize++) {
        builder = addStatement(builder, packageName,
                Bytes.TYPE_NAME + byteSize, Bytes.class.getSimpleName() + byteSize);
    }
    return builder;
}
 
Example #15
Source File: AbiTypesGenerator.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private void generate(String destinationDir) throws IOException {
    generateIntTypes(Int.class, destinationDir);
    generateIntTypes(Uint.class, destinationDir);

    // TODO: Enable once Solidity supports fixed types - see
    // https://github.com/ethereum/solidity/issues/409
    // generateFixedTypes(Fixed.class, destinationDir);
    // generateFixedTypes(Ufixed.class, destinationDir);

    generateBytesTypes(Bytes.class, destinationDir);
    generateStaticArrayTypes(StaticArray.class, destinationDir);
}
 
Example #16
Source File: TypeEncoderTest.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testStaticBytes() {
    Bytes staticBytes = new Bytes6(new byte[] { 0, 1, 2, 3, 4, 5 });
    assertThat(TypeEncoder.encodeBytes(staticBytes),
            is("0001020304050000000000000000000000000000000000000000000000000000"));

    Bytes empty = new Bytes1(new byte[] { 0 });
    assertThat(TypeEncoder.encodeBytes(empty),
            is("0000000000000000000000000000000000000000000000000000000000000000"));

    Bytes dave = new Bytes4("dave".getBytes());
    assertThat(TypeEncoder.encodeBytes(dave),
            is("6461766500000000000000000000000000000000000000000000000000000000"));
}
 
Example #17
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 5 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 #18
Source File: AbiTypesMapperGenerator.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private MethodSpec.Builder generateFixedBytesTypes(MethodSpec.Builder builder,
                                                   String packageName) {
    for (int byteSize = 1; byteSize <= 32; byteSize++) {
        builder = addStatement(builder, packageName,
                Bytes.TYPE_NAME + byteSize, Bytes.class.getSimpleName() + byteSize);
    }
    return builder;
}
 
Example #19
Source File: AbiTypesGenerator.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private void generate(String destinationDir) throws IOException {
    generateIntTypes(Int.class, destinationDir);
    generateIntTypes(Uint.class, destinationDir);

    // TODO: Enable once Solidity supports fixed types - see
    // https://github.com/ethereum/solidity/issues/409
    // generateFixedTypes(Fixed.class, destinationDir);
    // generateFixedTypes(Ufixed.class, destinationDir);

    generateBytesTypes(Bytes.class, destinationDir);
    generateStaticArrayTypes(StaticArray.class, destinationDir);
}
 
Example #20
Source File: TypeEncoderTest.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void testStaticBytes() {
    Bytes staticBytes = new Bytes6(new byte[] { 0, 1, 2, 3, 4, 5 });
    assertThat(TypeEncoder.encodeBytes(staticBytes),
            is("0001020304050000000000000000000000000000000000000000000000000000"));

    Bytes empty = new Bytes1(new byte[] { 0 });
    assertThat(TypeEncoder.encodeBytes(empty),
            is("0000000000000000000000000000000000000000000000000000000000000000"));

    Bytes dave = new Bytes4("dave".getBytes());
    assertThat(TypeEncoder.encodeBytes(dave),
            is("6461766500000000000000000000000000000000000000000000000000000000"));
}
 
Example #21
Source File: TypeDecoder.java    From client-sdk-java with Apache License 2.0 5 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 #22
Source File: FunctionReturnDecoder.java    From client-sdk-java with Apache License 2.0 4 votes vote down vote up
/**
 * <p>Decodes an indexed parameter associated with an event. Indexed parameters are individually
 * encoded, unlike non-indexed parameters which are encoded as per ABI-encoded function
 * parameters and return values.</p>
 *
 * <p>If any of the following types are indexed, the Keccak-256 hashes of the values are
 * returned instead. These are returned as a bytes32 value.</p>
 *
 * <ul>
 *     <li>Arrays</li>
 *     <li>Strings</li>
 *     <li>Bytes</li>
 * </ul>
 *
 * <p>See the
 * <a href="http://solidity.readthedocs.io/en/latest/contracts.html#events">
 * Solidity documentation</a> for further information.</p>
 *
 * @param rawInput ABI encoded input
 * @param typeReference of expected result type
 * @param <T> type of TypeReference
 * @return the decode value
 */
@SuppressWarnings("unchecked")
public static <T extends Type> Type decodeIndexedValue(
        String rawInput, TypeReference<T> typeReference) {
    String input = Numeric.cleanHexPrefix(rawInput);

    try {
        Class<T> type = typeReference.getClassType();

        if (Bytes.class.isAssignableFrom(type)) {
            return TypeDecoder.decodeBytes(input, (Class<Bytes>) Class.forName(type.getName()));
        } else if (Array.class.isAssignableFrom(type)
                || BytesType.class.isAssignableFrom(type)
                || Utf8String.class.isAssignableFrom(type)) {
            return TypeDecoder.decodeBytes(input, Bytes32.class);
        } else {
            return TypeDecoder.decode(input, type);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("Invalid class reference provided", e);
    }
}
 
Example #23
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 3 votes vote down vote up
static <T extends Bytes> T decodeBytes(String input, Class<T> type) {
    return decodeBytes(input, 0, type);
}
 
Example #24
Source File: FunctionReturnDecoder.java    From etherscan-explorer with GNU General Public License v3.0 3 votes vote down vote up
/**
 * <p>Decodes an indexed parameter associated with an event. Indexed parameters are individually
 * encoded, unlike non-indexed parameters which are encoded as per ABI-encoded function
 * parameters and return values.</p>
 *
 * <p>If any of the following types are indexed, the Keccak-256 hashes of the values are
 * returned instead. These are returned as a bytes32 value.</p>
 *
 * <ul>
 *     <li>Arrays</li>
 *     <li>Strings</li>
 *     <li>Bytes</li>
 * </ul>
 *
 * <p>See the
 * <a href="http://solidity.readthedocs.io/en/latest/contracts.html#events">
 * Solidity documentation</a> for further information.</p>
 *
 * @param rawInput ABI encoded input
 * @param typeReference of expected result type
 * @param <T> type of TypeReference
 * @return the decode value
 */
@SuppressWarnings("unchecked")
public static <T extends Type> Type decodeIndexedValue(
        String rawInput, TypeReference<T> typeReference) {
    String input = Numeric.cleanHexPrefix(rawInput);

    try {
        Class<T> type = typeReference.getClassType();

        if (Bytes.class.isAssignableFrom(type)) {
            return TypeDecoder.decodeBytes(input, (Class<Bytes>) Class.forName(type.getName()));
        } else if (Array.class.isAssignableFrom(type)
                || BytesType.class.isAssignableFrom(type)
                || Utf8String.class.isAssignableFrom(type)) {
            return TypeDecoder.decodeBytes(input, Bytes32.class);
        } else {
            return TypeDecoder.decode(input, type);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("Invalid class reference provided", e);
    }
}
 
Example #25
Source File: TypeDecoder.java    From web3j with Apache License 2.0 3 votes vote down vote up
static <T extends Bytes> T decodeBytes(String input, Class<T> type) {
    return decodeBytes(input, 0, type);
}
 
Example #26
Source File: TypeDecoder.java    From client-sdk-java with Apache License 2.0 3 votes vote down vote up
static <T extends Bytes> T decodeBytes(String input, Class<T> type) {
    return decodeBytes(input, 0, type);
}