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;
}
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;
}
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);
}
}
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;
}
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;
}
Aggregations