org.web3j.abi.datatypes.Array Java Examples

The following examples show how to use org.web3j.abi.datatypes.Array. 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: 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 #2
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 #3
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
private static <T extends Type> T decodeArrayElements(
        String input, int offset, TypeReference<T> typeReference, int length,
        BiFunction<List<T>, String, T> consumer) {

    try {
        Class<T> cls = Utils.getParameterizedTypeFromArray(typeReference);
        if (Array.class.isAssignableFrom(cls)) {
            throw new UnsupportedOperationException(
                    "Arrays of arrays are not currently supported for external functions, see"
                            + "http://solidity.readthedocs.io/en/develop/types.html#members");
        } else {
            List<T> elements = new ArrayList<>(length);

            for (int i = 0, currOffset = offset;
                    i < length;
                    i++, currOffset += getSingleElementLength(input, currOffset, cls)
                         * MAX_BYTE_LENGTH_FOR_HEX_STRING) {
                T value = decode(input, currOffset, cls);
                elements.add(value);
            }

            String typeName = Utils.getSimpleTypeName(cls);

            return consumer.apply(elements, typeName);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException(
                "Unable to access parameterized type " + typeReference.getType().getTypeName(),
                e);
    }
}
 
Example #4
Source File: TypeEncoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static <T extends Type> String encodeArrayValues(Array<T> value) {
    StringBuilder result = new StringBuilder();
    for (Type type : value.getValue()) {
        result.append(encode(type));
    }
    return result.toString();
}
 
Example #5
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
private static <T extends Type> T decodeArrayElements(
        String input,
        int offset,
        TypeReference<T> typeReference,
        int length,
        BiFunction<List<T>, String, T> consumer) {

    try {
        Class<T> cls = Utils.getParameterizedTypeFromArray(typeReference);
        if (Array.class.isAssignableFrom(cls)) {
            throw new UnsupportedOperationException(
                    "Arrays of arrays are not currently supported for external functions, see"
                            + "http://solidity.readthedocs.io/en/develop/types.html#members");
        } else {
            List<T> elements = new ArrayList<>(length);

            for (int i = 0, currOffset = offset;
                    i < length;
                    i++,
                            currOffset +=
                                    getSingleElementLength(input, currOffset, cls)
                                            * MAX_BYTE_LENGTH_FOR_HEX_STRING) {
                T value = decode(input, currOffset, cls);
                elements.add(value);
            }

            String typeName = Utils.getSimpleTypeName(cls);

            return consumer.apply(elements, typeName);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException(
                "Unable to access parameterized type " + typeReference.getType().getTypeName(),
                e);
    }
}
 
Example #6
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static List arrayToList(Object array) {
    int len = java.lang.reflect.Array.getLength(array);
    ArrayList<Object> rslt = new ArrayList<Object>(len);
    for (int i = 0; i < len; i++) {
        rslt.add(java.lang.reflect.Array.get(array, i));
    }
    return rslt;
}
 
Example #7
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static <T extends Array> T decode(
        String input, int offset, TypeReference<T> typeReference) {
    Class cls = ((ParameterizedType) typeReference.getType()).getRawType().getClass();
    if (StaticArray.class.isAssignableFrom(cls)) {
        return decodeStaticArray(input, offset, typeReference, 1);
    } else if (DynamicArray.class.isAssignableFrom(cls)) {
        return decodeDynamicArray(input, offset, typeReference);
    } else {
        throw new UnsupportedOperationException(
                "Unsupported TypeReference: "
                        + cls.getName()
                        + ", only Array types can be passed as TypeReferences");
    }
}
 
Example #8
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
public static Type instantiateType(TypeReference ref, Object value)
        throws NoSuchMethodException, IllegalAccessException, InvocationTargetException,
                InstantiationException, ClassNotFoundException {
    Class rc = ref.getClassType();
    if (Array.class.isAssignableFrom(rc)) {
        return instantiateArrayType(ref, value);
    }
    return instantiateAtomicType(rc, value);
}
 
Example #9
Source File: TypeEncoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
static <T extends Type> String encodeArrayValues(Array<T> value) {
    StringBuilder result = new StringBuilder();
    for (Type type:value.getValue()) {
        result.append(TypeEncoder.encode(type));
    }
    return result.toString();
}
 
Example #10
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 5 votes vote down vote up
public static <T extends Array> T decode(
        String input, int offset, TypeReference<T> typeReference) {
    Class cls = ((ParameterizedType) typeReference.getType()).getRawType().getClass();
    if (StaticArray.class.isAssignableFrom(cls)) {
        return decodeStaticArray(input, offset, typeReference, 1);
    } else if (DynamicArray.class.isAssignableFrom(cls)) {
        return decodeDynamicArray(input, offset, typeReference);
    } else {
        throw new UnsupportedOperationException("Unsupported TypeReference: "
                + cls.getName() + ", only Array types can be passed as TypeReferences");
    }
}
 
Example #11
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 #12
Source File: TypeEncoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
static <T extends Type> String encodeArrayValues(Array<T> value) {
    StringBuilder result = new StringBuilder();
    for (Type type:value.getValue()) {
        result.append(TypeEncoder.encode(type));
    }
    return result.toString();
}
 
Example #13
Source File: TypeDecoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
private static <T extends Type> T decodeArrayElements(
        String input, int offset, TypeReference<T> typeReference, int length,
        BiFunction<List<T>, String, T> consumer) {

    try {
        Class<T> cls = Utils.getParameterizedTypeFromArray(typeReference);
        if (Array.class.isAssignableFrom(cls)) {
            throw new UnsupportedOperationException(
                    "Arrays of arrays are not currently supported for external functions, see"
                            + "http://solidity.readthedocs.io/en/develop/types.html#members");
        } else {
            List<T> elements = new ArrayList<>(length);

            for (int i = 0, currOffset = offset;
                    i < length;
                    i++, currOffset += getSingleElementLength(input, currOffset, cls)
                         * MAX_BYTE_LENGTH_FOR_HEX_STRING) {
                T value = decode(input, currOffset, cls);
                elements.add(value);
            }

            String typeName = Utils.getSimpleTypeName(cls);

            return consumer.apply(elements, typeName);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException(
                "Unable to access parameterized type " + typeReference.getType().getTypeName(),
                e);
    }
}
 
Example #14
Source File: TypeDecoder.java    From client-sdk-java with Apache License 2.0 5 votes vote down vote up
public static <T extends Array> T decode(
        String input, int offset, TypeReference<T> typeReference) {
    Class cls = ((ParameterizedType) typeReference.getType()).getRawType().getClass();
    if (StaticArray.class.isAssignableFrom(cls)) {
        return decodeStaticArray(input, offset, typeReference, 1);
    } else if (DynamicArray.class.isAssignableFrom(cls)) {
        return decodeDynamicArray(input, offset, typeReference);
    } else {
        throw new UnsupportedOperationException("Unsupported TypeReference: "
                + cls.getName() + ", only Array types can be passed as TypeReferences");
    }
}
 
Example #15
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 #16
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 #17
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);
    }
}