Search in sources :

Example 1 with Bytes

use of org.fisco.bcos.web3j.abi.datatypes.Bytes in project web3sdk by FISCO-BCOS.

the class ABIObject method newObject.

// clone itself
public ABIObject newObject() {
    ABIObject abiObject = new ABIObject(this.type);
    // value
    abiObject.setValueType(this.getValueType());
    abiObject.setName(this.getName());
    if (this.getNumericValue() != null) {
        abiObject.setNumericValue(new NumericType(this.getNumericValue().getTypeAsString(), this.getNumericValue().getValue()) {

            @Override
            public boolean dynamicType() {
                return false;
            }

            @Override
            public int offset() {
                return 1;
            }
        });
    }
    if (this.getBoolValue() != null) {
        abiObject.setBoolValue(new Bool(this.getBoolValue().getValue()));
    }
    if (this.getStringValue() != null) {
        abiObject.setStringValue(new Utf8String(this.getStringValue().getValue()));
    }
    if (this.getDynamicBytesValue() != null) {
        abiObject.setDynamicBytesValue(new DynamicBytes(this.getDynamicBytesValue().getValue()));
    }
    if (this.getAddressValue() != null) {
        abiObject.setAddressValue(new Address(this.getAddressValue().toUint160()));
    }
    if (this.getBytesValue() != null) {
        abiObject.setBytesValue(new Bytes(this.getBytesValue().getValue().length, this.getBytesValue().getValue()));
    }
    // list
    abiObject.setListType(this.getListType());
    abiObject.setListLength(this.getListLength());
    if (this.getListValueType() != null) {
        abiObject.setListValueType(this.getListValueType().newObject());
    }
    if (this.listValues != null) {
        for (ABIObject obj : this.listValues) {
            abiObject.listValues.add(obj.newObject());
        }
    }
    // tuple
    if (this.structFields != null) {
        for (ABIObject obj : this.structFields) {
            abiObject.structFields.add(obj.newObject());
        }
    }
    return abiObject;
}
Also used : NumericType(org.fisco.bcos.web3j.abi.datatypes.NumericType) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bytes(org.fisco.bcos.web3j.abi.datatypes.Bytes) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Address(org.fisco.bcos.web3j.abi.datatypes.Address) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool)

Example 2 with Bytes

use of org.fisco.bcos.web3j.abi.datatypes.Bytes in project web3sdk by FISCO-BCOS.

the class ABICodecJsonWrapper method encode.

public ABIObject encode(ABIObject template, List<String> inputs) throws IOException {
    ABIObject abiObject = template.newObject();
    // check parameters match
    if (inputs.size() != abiObject.getStructFields().size()) {
        errorReport("arguments size", String.valueOf(abiObject.getStructFields().size()), String.valueOf(inputs.size()));
    }
    for (int i = 0; i < abiObject.getStructFields().size(); ++i) {
        ABIObject argObject = abiObject.getStructFields().get(i).newObject();
        String value = inputs.get(i);
        switch(argObject.getType()) {
            case VALUE:
                {
                    try {
                        switch(argObject.getValueType()) {
                            case BOOL:
                                {
                                    argObject.setBoolValue(new Bool(Boolean.valueOf(value)));
                                    break;
                                }
                            case UINT:
                                {
                                    argObject.setNumericValue(new Uint256(Numeric.decodeQuantity(value)));
                                    break;
                                }
                            case INT:
                                {
                                    argObject.setNumericValue(new Int256(Numeric.decodeQuantity(value)));
                                    break;
                                }
                            case ADDRESS:
                                {
                                    argObject.setAddressValue(new Address(value));
                                    break;
                                }
                            case BYTES:
                                {
                                    // Binary data requires base64 encoding
                                    byte[] bytesValue = Base64.getDecoder().decode(value);
                                    argObject.setBytesValue(new Bytes(bytesValue.length, bytesValue));
                                    break;
                                }
                            case DBYTES:
                                {
                                    // Binary data requires base64 encoding
                                    byte[] bytesValue = Base64.getDecoder().decode(value);
                                    argObject.setDynamicBytesValue(new DynamicBytes(bytesValue));
                                    break;
                                }
                            case STRING:
                                {
                                    argObject.setStringValue(new Utf8String(value));
                                    break;
                                }
                            default:
                                {
                                    throw new UnsupportedOperationException("Unrecognized valueType: " + argObject.getValueType());
                                }
                        }
                    } catch (Exception e) {
                        logger.error(" e: ", e);
                        errorReport("ROOT", e.getMessage());
                    }
                    break;
                }
            case STRUCT:
            case LIST:
                {
                    JsonNode argNode = objectMapper.readTree(value.getBytes());
                    argObject = encodeNode("ROOT", argObject, argNode);
                    break;
                }
        }
        abiObject.getStructFields().set(i, argObject);
    }
    return abiObject;
}
Also used : Address(org.fisco.bcos.web3j.abi.datatypes.Address) JsonNode(com.fasterxml.jackson.databind.JsonNode) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) IOException(java.io.IOException) InvalidParameterException(java.security.InvalidParameterException) Int256(org.fisco.bcos.web3j.abi.datatypes.generated.Int256) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bytes(org.fisco.bcos.web3j.abi.datatypes.Bytes) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) Uint256(org.fisco.bcos.web3j.abi.datatypes.generated.Uint256)

Example 3 with Bytes

use of org.fisco.bcos.web3j.abi.datatypes.Bytes in project web3sdk by FISCO-BCOS.

the class TypeDecoder method decodeBytes.

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);
    }
}
Also used : Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Uint(org.fisco.bcos.web3j.abi.datatypes.Uint) InvocationTargetException(java.lang.reflect.InvocationTargetException) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bytes(org.fisco.bcos.web3j.abi.datatypes.Bytes)

Example 4 with Bytes

use of org.fisco.bcos.web3j.abi.datatypes.Bytes in project web3sdk by FISCO-BCOS.

the class ResultEntity method typeToObject.

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;
}
Also used : NumericType(org.fisco.bcos.web3j.abi.datatypes.NumericType) Address(org.fisco.bcos.web3j.abi.datatypes.Address) ArrayList(java.util.ArrayList) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) Array(org.fisco.bcos.web3j.abi.datatypes.Array) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bytes(org.fisco.bcos.web3j.abi.datatypes.Bytes) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) NumericType(org.fisco.bcos.web3j.abi.datatypes.NumericType) Type(org.fisco.bcos.web3j.abi.datatypes.Type) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with Bytes

use of org.fisco.bcos.web3j.abi.datatypes.Bytes in project web3sdk by FISCO-BCOS.

the class ABICodecJsonWrapper method encodeNode.

private ABIObject encodeNode(String path, ABIObject template, JsonNode node) {
    ABIObject abiObject = template.newObject();
    switch(abiObject.getType()) {
        case VALUE:
            {
                if (!node.isValueNode()) {
                    errorReport(path, abiObject.getType().toString(), node.getNodeType().toString());
                }
                switch(template.getValueType()) {
                    case BOOL:
                        {
                            if (!node.isBoolean()) {
                                errorReport(path, template.getValueType().toString(), node.getNodeType().toString());
                            }
                            abiObject.setBoolValue(new Bool(node.asBoolean()));
                            break;
                        }
                    case INT:
                        {
                            if (!node.isNumber() && !node.isBigInteger()) {
                                errorReport(path, template.getValueType().toString(), node.getNodeType().toString());
                            }
                            if (node.isNumber()) {
                                abiObject.setNumericValue(new Int256(node.asLong()));
                            } else {
                                abiObject.setNumericValue(new Int256(node.bigIntegerValue()));
                            }
                            break;
                        }
                    case UINT:
                        {
                            if (!node.isNumber() && !node.isBigInteger()) {
                                errorReport(path, template.getValueType().toString(), node.getNodeType().toString());
                            }
                            if (node.isNumber()) {
                                abiObject.setNumericValue(new Uint256(node.asLong()));
                            } else {
                                abiObject.setNumericValue(new Uint256(node.bigIntegerValue()));
                            }
                            break;
                        }
                    case ADDRESS:
                        {
                            if (!node.isTextual()) {
                                errorReport(path, template.getValueType().toString(), node.getNodeType().toString());
                            }
                            try {
                                abiObject.setAddressValue(new Address(node.asText()));
                            } catch (Exception e) {
                                errorReport(path, "Invalid address, address value: " + node.asText());
                            }
                            break;
                        }
                    case BYTES:
                        {
                            if (!node.isTextual()) {
                                errorReport(path, template.getValueType().toString(), node.getNodeType().toString());
                            }
                            // Binary data requires base64 encoding
                            byte[] bytesValue = Base64.getDecoder().decode(node.asText());
                            abiObject.setBytesValue(new Bytes(bytesValue.length, bytesValue));
                            break;
                        }
                    case DBYTES:
                        {
                            if (!node.isTextual()) {
                                errorReport(path, template.getValueType().toString(), node.getNodeType().toString());
                            }
                            byte[] bytesValue = Base64.getDecoder().decode(node.asText());
                            abiObject.setDynamicBytesValue(new DynamicBytes(bytesValue));
                            break;
                        }
                    case STRING:
                        {
                            if (!node.isTextual()) {
                                errorReport(path, template.getValueType().toString(), node.getNodeType().toString());
                            }
                            abiObject.setStringValue(new Utf8String(node.asText()));
                            break;
                        }
                }
                break;
            }
        case LIST:
            {
                if (!node.isArray()) {
                    errorReport(path, abiObject.getType().toString(), node.getNodeType().toString());
                }
                if ((abiObject.getListType() == ListType.FIXED) && (node.size() != abiObject.getListLength())) {
                    errorReport("fixed list arguments size", String.valueOf(abiObject.getListLength()), String.valueOf(node.size()));
                }
                int i = 0;
                Iterator<JsonNode> iterator = node.iterator();
                while (iterator.hasNext()) {
                    abiObject.getListValues().add(encodeNode(path + ".<" + String.valueOf(i) + ">", abiObject.getListValueType(), iterator.next()));
                }
                break;
            }
        case STRUCT:
            {
                if (!node.isArray() && !node.isObject()) {
                    errorReport(path, abiObject.getType().toString(), node.getNodeType().toString());
                }
                if (node.size() != abiObject.getStructFields().size()) {
                    errorReport("struct arguments size", String.valueOf(abiObject.getListLength()), String.valueOf(node.size()));
                }
                if (node.isArray()) {
                    for (int i = 0; i < abiObject.getStructFields().size(); i++) {
                        ABIObject field = abiObject.getStructFields().get(i);
                        abiObject.getStructFields().set(i, encodeNode(path + "." + field.getName(), field, node.get(i)));
                    }
                } else {
                    for (int i = 0; i < abiObject.getStructFields().size(); ++i) {
                        ABIObject field = abiObject.getStructFields().get(i);
                        JsonNode structNode = node.get(field.getName());
                        if (structNode == null) {
                            errorReport(path, " Missing struct field, field name: " + field.getName());
                        }
                        abiObject.getStructFields().set(i, encodeNode(path + "." + field.getName(), field, structNode));
                    }
                }
                break;
            }
    }
    return abiObject;
}
Also used : Int256(org.fisco.bcos.web3j.abi.datatypes.generated.Int256) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Bytes(org.fisco.bcos.web3j.abi.datatypes.Bytes) Utf8String(org.fisco.bcos.web3j.abi.datatypes.Utf8String) DynamicBytes(org.fisco.bcos.web3j.abi.datatypes.DynamicBytes) Address(org.fisco.bcos.web3j.abi.datatypes.Address) Bool(org.fisco.bcos.web3j.abi.datatypes.Bool) Iterator(java.util.Iterator) JsonNode(com.fasterxml.jackson.databind.JsonNode) Uint256(org.fisco.bcos.web3j.abi.datatypes.generated.Uint256) IOException(java.io.IOException) InvalidParameterException(java.security.InvalidParameterException)

Aggregations

Bytes (org.fisco.bcos.web3j.abi.datatypes.Bytes)5 DynamicBytes (org.fisco.bcos.web3j.abi.datatypes.DynamicBytes)5 Utf8String (org.fisco.bcos.web3j.abi.datatypes.Utf8String)5 Address (org.fisco.bcos.web3j.abi.datatypes.Address)4 Bool (org.fisco.bcos.web3j.abi.datatypes.Bool)4 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 IOException (java.io.IOException)2 InvalidParameterException (java.security.InvalidParameterException)2 NumericType (org.fisco.bcos.web3j.abi.datatypes.NumericType)2 Int256 (org.fisco.bcos.web3j.abi.datatypes.generated.Int256)2 Uint256 (org.fisco.bcos.web3j.abi.datatypes.generated.Uint256)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 ArrayList (java.util.ArrayList)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Array (org.fisco.bcos.web3j.abi.datatypes.Array)1 Type (org.fisco.bcos.web3j.abi.datatypes.Type)1 Uint (org.fisco.bcos.web3j.abi.datatypes.Uint)1