Java Code Examples for org.web3j.abi.datatypes.Type#MAX_BYTE_LENGTH

The following examples show how to use org.web3j.abi.datatypes.Type#MAX_BYTE_LENGTH . 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 client-sdk-java with Apache License 2.0 6 votes vote down vote up
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
    try {
        byte[] inputByteArray = Numeric.hexStringToByteArray(input);
        int typeLengthAsBytes = getTypeLengthInBytes(type);

        byte[] resultByteArray = new byte[typeLengthAsBytes + 1];

        if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
            resultByteArray[0] = inputByteArray[0];  // take MSB as sign bit
        }

        int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
        System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);

        BigInteger numericValue = new BigInteger(resultByteArray);
        return type.getConstructor(BigInteger.class).newInstance(numericValue);

    } catch (NoSuchMethodException | SecurityException
            | InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example 2
Source File: FunctionEncoder.java    From client-sdk-java with Apache License 2.0 6 votes vote down vote up
private static String encodeParameters(List<Type> parameters, StringBuilder result) {
    int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH;
    StringBuilder dynamicData = new StringBuilder();

    for (Type parameter:parameters) {
        String encodedValue = TypeEncoder.encode(parameter);

        if (TypeEncoder.isDynamic(parameter)) {
            String encodedDataOffset = TypeEncoder.encodeNumeric(
                    new Uint(BigInteger.valueOf(dynamicDataOffset)));
            result.append(encodedDataOffset);
            dynamicData.append(encodedValue);
            dynamicDataOffset += encodedValue.length() >> 1;
        } else {
            result.append(encodedValue);
        }
    }
    result.append(dynamicData);

    return result.toString();
}
 
Example 3
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
    try {
        byte[] inputByteArray = Numeric.hexStringToByteArray(input);
        int typeLengthAsBytes = getTypeLengthInBytes(type);

        byte[] resultByteArray = new byte[typeLengthAsBytes + 1];

        if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
            resultByteArray[0] = inputByteArray[0];  // take MSB as sign bit
        }

        int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
        System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);

        BigInteger numericValue = new BigInteger(resultByteArray);
        return type.getConstructor(BigInteger.class).newInstance(numericValue);

    } catch (NoSuchMethodException | SecurityException
            | InstantiationException | IllegalAccessException
            | IllegalArgumentException | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example 4
Source File: FunctionEncoder.java    From etherscan-explorer with GNU General Public License v3.0 6 votes vote down vote up
private static String encodeParameters(List<Type> parameters, StringBuilder result) {
    int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH;
    StringBuilder dynamicData = new StringBuilder();

    for (Type parameter:parameters) {
        String encodedValue = TypeEncoder.encode(parameter);

        if (TypeEncoder.isDynamic(parameter)) {
            String encodedDataOffset = TypeEncoder.encodeNumeric(
                    new Uint(BigInteger.valueOf(dynamicDataOffset)));
            result.append(encodedDataOffset);
            dynamicData.append(encodedValue);
            dynamicDataOffset += encodedValue.length() >> 1;
        } else {
            result.append(encodedValue);
        }
    }
    result.append(dynamicData);

    return result.toString();
}
 
Example 5
Source File: DefaultFunctionEncoder.java    From web3j with Apache License 2.0 6 votes vote down vote up
private static String encodeParameters(
        final List<Type> parameters, final StringBuilder result) {

    int dynamicDataOffset = getLength(parameters) * Type.MAX_BYTE_LENGTH;
    final StringBuilder dynamicData = new StringBuilder();

    for (Type parameter : parameters) {
        final String encodedValue = TypeEncoder.encode(parameter);

        if (TypeEncoder.isDynamic(parameter)) {
            final String encodedDataOffset =
                    TypeEncoder.encodeNumeric(new Uint(BigInteger.valueOf(dynamicDataOffset)));
            result.append(encodedDataOffset);
            dynamicData.append(encodedValue);
            dynamicDataOffset += encodedValue.length() >> 1;
        } else {
            result.append(encodedValue);
        }
    }
    result.append(dynamicData);

    return result.toString();
}
 
Example 6
Source File: TypeDecoder.java    From client-sdk-java 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 7
Source File: TypeDecoder.java    From etherscan-explorer with GNU General Public License v3.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 8
Source File: TypeDecoder.java    From web3j with Apache License 2.0 5 votes vote down vote up
static <T extends NumericType> T decodeNumeric(String input, Class<T> type) {
    try {
        byte[] inputByteArray = Numeric.hexStringToByteArray(input);
        int typeLengthAsBytes = getTypeLengthInBytes(type);

        byte[] resultByteArray = new byte[typeLengthAsBytes + 1];

        if (Int.class.isAssignableFrom(type) || Fixed.class.isAssignableFrom(type)) {
            resultByteArray[0] = inputByteArray[0]; // take MSB as sign bit
        }

        int valueOffset = Type.MAX_BYTE_LENGTH - typeLengthAsBytes;
        System.arraycopy(inputByteArray, valueOffset, resultByteArray, 1, typeLengthAsBytes);

        BigInteger numericValue = new BigInteger(resultByteArray);
        return type.getConstructor(BigInteger.class).newInstance(numericValue);

    } catch (NoSuchMethodException
            | SecurityException
            | InstantiationException
            | IllegalAccessException
            | IllegalArgumentException
            | InvocationTargetException e) {
        throw new UnsupportedOperationException(
                "Unable to create instance of " + type.getName(), e);
    }
}
 
Example 9
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;
    }
}