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

The following examples show how to use org.fisco.bcos.web3j.abi.datatypes.BytesType. 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-Front with Apache License 2.0 5 votes vote down vote up
static String decodeBytes(BytesType bytesType) {
    byte[] value = bytesType.getValue();
    int length = value.length;
    int mod = length % MAX_BYTE_LENGTH;

    byte[] dest;
    if (mod != 0) {
        int padding = MAX_BYTE_LENGTH - mod;
        dest = new byte[length + padding];
        System.arraycopy(value, 0, dest, 0, length);
    } else {
        dest = value;
    }
    return Numeric.toHexString(dest);
}
 
Example #2
Source File: BytesUtils.java    From WeBASE-Collect-Bee with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public static String bytesTypeListToString(Object typeList) {
    List<BytesType> list = (List<BytesType>) typeList;
    List<String> stringList = list.stream().map(t -> t.getValue())
            .map(b -> StrUtil.str(b, Charset.defaultCharset())).collect(Collectors.toList());
    return JacksonUtils.toJson(stringList);
}
 
Example #3
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);
    }
}