org.fisco.bcos.web3j.abi.datatypes.Bytes Java Examples

The following examples show how to use org.fisco.bcos.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: ContractTypeUtil.java    From WeBASE-Transaction with Apache License 2.0 6 votes vote down vote up
static <T extends Bytes> T encodeBytes(String input, Class<T> type) throws BaseException {
    try {
        String simpleName = type.getSimpleName();
        String[] splitName = simpleName.split(Bytes.class.getSimpleName());
        int length = Integer.parseInt(splitName[1]);

        byte[] byteValue = null;
        if (input.length() > length) {
            byteValue = input.substring(0, length).getBytes();
        } else {
            byteValue = input.getBytes();
        }
        byte[] byteValueLength = new byte[length];
        System.arraycopy(byteValue, 0, byteValueLength, 0, byteValue.length);

        return type.getConstructor(byte[].class).newInstance(byteValueLength);
    } catch (NoSuchMethodException | SecurityException | InstantiationException
            | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        log.error("encodeBytes failed.");
        throw new BaseException(201203,
                String.format("unable to create instance of type:%s", type.getName()));
    }
}
 
Example #2
Source File: TypeDecoder.java    From web3sdk 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 #3
Source File: TypeDecoder.java    From web3sdk 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: ContractTypeUtil.java    From WeBASE-Front with Apache License 2.0 5 votes vote down vote up
static <T extends Bytes> T encodeBytes(String input, Class<T> type) throws FrontException {
    try {
        byte[] bytes = Numeric.hexStringToByteArray(input);
        return type.getConstructor(byte[].class).newInstance(bytes);
    } catch (NoSuchMethodException | SecurityException | InstantiationException
            | IllegalAccessException | IllegalArgumentException | InvocationTargetException e) {
        log.error("encodeBytes failed.", e);
        throw new FrontException(ConstantCode.CONTRACT_TYPE_PARAM_ERROR.getCode(),
                String.format("unable to create instance of type:%s", type.getName()));
    }
}
 
Example #5
Source File: TopicTools.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static String byteNToTopic(byte[] b) {
    // byte[] can't be more than 32 byte
    if (b.length > 32) {
        throw new IllegalArgumentException("byteN can't be more than 32 byte");
    }

    Bytes bs = new Bytes(b.length, b);
    return Numeric.prependHexPrefix(TypeEncoder.encode(bs));
}
 
Example #6
Source File: ResultEntity.java    From web3sdk with Apache License 2.0 5 votes vote down vote up
public static Object typeToObject(Type type) {
    Object obj = null;
    if (type instanceof NumericType) { // uint int
        obj = ((NumericType) type).getValue();
    } else if (type instanceof Bool) { // bool
        obj = ((Bool) type).getValue();
    } else if (type instanceof Address) { // address
        obj = type.toString();
    } else if (type instanceof Bytes) { // bytes32
        obj = new String(((Bytes) type).getValue()).trim();
    } else if (type instanceof DynamicBytes) { // bytes
        obj = new String(((DynamicBytes) type).getValue()).trim();
    } else if (type instanceof Utf8String) { // string
        obj = ((Utf8String) type).getValue();
    } else if (type instanceof Array) { // T[] T[k]
        List<Object> r = new ArrayList<Object>();
        List l = ((Array) type).getValue();
        for (int i = 0; i < l.size(); ++i) {
            r.add(typeToObject((Type) l.get(i)));
        }

        obj = (Object) r;
    } else {
        obj = (Object) obj;
    }

    return obj;
}
 
Example #7
Source File: AbiTypesMapperGenerator.java    From web3sdk 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 #8
Source File: BytesUtils.java    From WeBASE-Collect-Bee with Apache License 2.0 4 votes vote down vote up
public static String bytesToString(Object obj) {
    return bytesTypeToString((Bytes) obj);
}
 
Example #9
Source File: BytesUtils.java    From WeBASE-Collect-Bee with Apache License 2.0 4 votes vote down vote up
public static String bytesTypeToString(Bytes b) {
    return StrUtil.str(b.getValue(), Charset.defaultCharset());
}
 
Example #10
Source File: BytesUtils.java    From WeBASE-Collect-Bee with Apache License 2.0 4 votes vote down vote up
public static String bytesListObjectToString(Object obj) {
    List<Bytes> list = (List<Bytes>) obj;
    return bytesListToString(list);
}
 
Example #11
Source File: BytesUtils.java    From WeBASE-Collect-Bee with Apache License 2.0 4 votes vote down vote up
public static String bytesListToString(List<Bytes> list) {
    return JacksonUtils
            .toJson(list.stream().map(b -> b.getValue()).map(b -> new String(b)).collect(Collectors.toList()));
}
 
Example #12
Source File: FunctionReturnDecoder.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
/**
 * 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>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.
 *
 * <ul>
 *   <li>Arrays
 *   <li>Strings
 *   <li>Bytes
 * </ul>
 *
 * <p>See the <a href="http://solidity.readthedocs.io/en/latest/contracts.html#events">Solidity
 * documentation</a> for further information.
 *
 * @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, 0, type);
        }
    } catch (ClassNotFoundException e) {
        throw new UnsupportedOperationException("Invalid class reference provided", e);
    }
}
 
Example #13
Source File: TypeDecoder.java    From web3sdk with Apache License 2.0 4 votes vote down vote up
static <T extends Bytes> T decodeBytes(String input, Class<T> type) {
    return decodeBytes(input, 0, type);
}