Search in sources :

Example 1 with Type

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

the class CAActionTools method processCAAction.

static void processCAAction(ApplicationContext ctx, CAAction caActions, String[] args) {
    try {
        switch(args[1]) {
            case "update":
            case "updateStatus":
                if (args.length < 3) {
                    System.out.println("Please input: ca.json");
                    break;
                }
                System.out.println("ca.json=" + args[2]);
                Resource template = ctx.getResource(args[2]);
                InputStream ksInputStream = template.getInputStream();
                ObjectMapper mapper = new ObjectMapper();
                mapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
                CaInfo caInfo = mapper.readValue(ksInputStream, CaInfo.class);
                if (args[1].equals("update")) {
                    caActions.update(new Utf8String(caInfo.getHash()), new Utf8String(caInfo.getPubkey()), new Utf8String(caInfo.getOrgname()), new Uint256(caInfo.getNotbefore()), new Uint256(caInfo.getNotafter()), new Uint8(caInfo.getStatus()), new Utf8String(caInfo.getWhitelist()), new Utf8String(caInfo.getBlacklist())).get();
                } else {
                    caActions.updateStatus(new Utf8String(caInfo.getHash()), new Uint8(caInfo.getStatus())).get();
                }
            case "all":
                Uint256 len = caActions.getHashsLength().get();
                System.out.println("HashsLength= " + len);
                for (int i = 0; i < len.getValue().intValue(); i++) {
                    System.out.println("----------CA " + i + "---------");
                    Utf8String hash = caActions.getHash(new Uint256(i)).get();
                    System.out.println("hash=" + hash);
                    List<Type> ca = caActions.get(hash).get();
                    System.out.println("pubkey=" + ca.get(1));
                    System.out.println("orgname=" + ca.get(2));
                    System.out.println("notbefore=" + ca.get(3));
                    System.out.println("notafter=" + ca.get(4));
                    System.out.println("status=" + ca.get(5));
                    System.out.println("blocknumber=" + ca.get(6));
                    List<Type> iplist = caActions.getIp(hash).get();
                    System.out.println("whitelist=" + iplist.get(0));
                    System.out.println("blacklist=" + iplist.get(1));
                }
                break;
            default:
                break;
        }
    } catch (Exception e) {
        System.out.println(e);
    }
}
Also used : Utf8String(org.bcos.web3j.abi.datatypes.Utf8String) Uint8(org.bcos.web3j.abi.datatypes.generated.Uint8) Type(org.bcos.web3j.abi.datatypes.Type) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) Uint256(org.bcos.web3j.abi.datatypes.generated.Uint256) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with Type

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

the class InitSystemContract method main.

public static void main(String[] args) throws Exception {
    // 初始化Service
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("开始部署...");
    System.out.println("===================================================================");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    // init web3j
    Web3j web3 = Web3j.build(channelEthereumService);
    // 初始化交易签名私钥
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Credentials credentials = Credentials.create(keyPair);
    // 初始化交易参数
    java.math.BigInteger gasPrice = new BigInteger("30000000");
    java.math.BigInteger gasLimit = new BigInteger("30000000");
    java.math.BigInteger initialWeiValue = new BigInteger("0");
    ToolConf toolConf = context.getBean(ToolConf.class);
    SystemProxy systemProxy = SystemProxy.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("systemProxy getContractAddress " + systemProxy.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "SystemProxy.address", systemProxy.getContractAddress());
    CAAction caAction = CAAction.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue, new Address(systemProxy.getContractAddress())).get();
    System.out.println("caAction getContractAddress " + caAction.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "CAAction.address", caAction.getContractAddress());
    NodeAction nodeAction = NodeAction.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("nodeAction getContractAddress " + nodeAction.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "NodeAction.address", nodeAction.getContractAddress());
    ConfigAction configAction = ConfigAction.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("configAction getContractAddress " + configAction.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "ConfigAction.address", configAction.getContractAddress());
    FileInfoManager fileInfoManager = FileInfoManager.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("fileInfoManager getContractAddress " + fileInfoManager.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "fileInfoManager.address", fileInfoManager.getContractAddress());
    FileServerManager fileServerManager = FileServerManager.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("fileServerManager getContractAddress " + fileServerManager.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "fileServerManager.address", fileServerManager.getContractAddress());
    ContractAbiMgr contractAbiMgr = ContractAbiMgr.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("contractAbiMgr getContractAddress " + contractAbiMgr.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "ContractAbiMgr.address", contractAbiMgr.getContractAddress());
    AuthorityFilter authorityFilter = AuthorityFilter.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("authorityFilter getContractAddress " + authorityFilter.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "AuthorityFilter.address", authorityFilter.getContractAddress());
    authorityFilter.setName(new Utf8String("AuthorityFilter"));
    authorityFilter.setVersion(new Utf8String("1.0"));
    Group group = Group.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("group getContractAddress " + group.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "Group.address", group.getContractAddress());
    TransactionFilterChain transactionFilterChain = TransactionFilterChain.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("transactionFilterChain getContractAddress " + transactionFilterChain.getContractAddress());
    writeAddress(toolConf.getOutPutpath() + "transactionFilterChain.address", transactionFilterChain.getContractAddress());
    contractAbiMgr.addAbi(new Utf8String("ContractAbiMgr"), new Utf8String("ContractAbiMgr"), new Utf8String(""), new Utf8String(ContractAbiMgr.ABI), new Address(contractAbiMgr.getContractAddress()));
    contractAbiMgr.addAbi(new Utf8String("SystemProxy"), new Utf8String("SystemProxy"), new Utf8String(""), new Utf8String(SystemProxy.ABI), new Address(contractAbiMgr.getContractAddress()));
    contractAbiMgr.addAbi(new Utf8String("TransactionFilterChain"), new Utf8String("TransactionFilterChain"), new Utf8String(""), new Utf8String(TransactionFilterChain.ABI), new Address(contractAbiMgr.getContractAddress()));
    contractAbiMgr.addAbi(new Utf8String("AuthorityFilter"), new Utf8String("AuthorityFilter"), new Utf8String(""), new Utf8String(AuthorityFilter.ABI), new Address(contractAbiMgr.getContractAddress()));
    contractAbiMgr.addAbi(new Utf8String("Group"), new Utf8String("Group"), new Utf8String(""), new Utf8String(Group.ABI), new Address(contractAbiMgr.getContractAddress()));
    contractAbiMgr.addAbi(new Utf8String("CAAction"), new Utf8String("CAAction"), new Utf8String(""), new Utf8String(CAAction.ABI), new Address(contractAbiMgr.getContractAddress()));
    contractAbiMgr.addAbi(new Utf8String("ConfigAction"), new Utf8String("ConfigAction"), new Utf8String(""), new Utf8String(ConfigAction.ABI), new Address(contractAbiMgr.getContractAddress()));
    contractAbiMgr.addAbi(new Utf8String("NodeAction"), new Utf8String("NodeAction"), new Utf8String(""), new Utf8String(NodeAction.ABI), new Address(contractAbiMgr.getContractAddress()));
    transactionFilterChain.addFilter(new Address(authorityFilter.getContractAddress()));
    systemProxy.setRoute(new Utf8String("TransactionFilterChain"), new Address(transactionFilterChain.getContractAddress()), new Bool(false)).get();
    systemProxy.setRoute(new Utf8String("ConfigAction"), new Address(configAction.getContractAddress()), new Bool(false)).get();
    systemProxy.setRoute(new Utf8String("NodeAction"), new Address(nodeAction.getContractAddress()), new Bool(false)).get();
    systemProxy.setRoute(new Utf8String("CAAction"), new Address(caAction.getContractAddress()), new Bool(false)).get();
    systemProxy.setRoute(new Utf8String("ContractAbiMgr"), new Address(contractAbiMgr.getContractAddress()), new Bool(false)).get();
    systemProxy.setRoute(new Utf8String("FileInfoManager"), new Address(fileInfoManager.getContractAddress()), new Bool(false)).get();
    systemProxy.setRoute(new Utf8String("FileServerManager"), new Address(fileServerManager.getContractAddress()), new Bool(false)).get();
    System.out.println("合约部署完成 系统代理合约:" + systemProxy.getContractAddress());
    System.out.println("-----------------系统路由表----------------------");
    Uint256 routelength = systemProxy.getRouteSize().get();
    for (int i = 0; i < routelength.getValue().intValue(); i++) {
        Utf8String key = systemProxy.getRouteNameByIndex(new Uint256(i)).get();
        List<Type> route = systemProxy.getRoute(key).get();
        System.out.println(" " + i + ")" + key + "=>" + (route.get(0)) + "," + route.get(1).getValue() + "," + ((BigInteger) (route.get(2).getValue())).intValue());
        if (key.getValue().equals("TransactionFilterChain")) {
            TransactionFilterChain transactionFilterChain1 = TransactionFilterChain.load(route.get(0).toString(), web3, credentials, gasPrice, gasLimit);
            Uint256 filterlength = transactionFilterChain1.getFiltersLength().get();
            for (int j = 0; j < filterlength.getValue().intValue(); j++) {
                Address filter = transactionFilterChain1.getFilter(new Uint256(j)).get();
                TransactionFilterBase transactionFilterBase = TransactionFilterBase.load(filter.toString(), web3, credentials, gasPrice, gasLimit);
                Utf8String name = transactionFilterBase._name().get();
                Utf8String version = transactionFilterBase._version().get();
                System.out.println("       " + name + "=>" + version + "," + filter);
            }
        }
    }
    System.exit(0);
}
Also used : Address(org.bcos.web3j.abi.datatypes.Address) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Bool(org.bcos.web3j.abi.datatypes.Bool) BigInteger(java.math.BigInteger) ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) Service(org.bcos.channel.client.Service) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) Utf8String(org.bcos.web3j.abi.datatypes.Utf8String) Web3j(org.bcos.web3j.protocol.Web3j) Type(org.bcos.web3j.abi.datatypes.Type) BigInteger(java.math.BigInteger) Uint256(org.bcos.web3j.abi.datatypes.generated.Uint256) Credentials(org.bcos.web3j.crypto.Credentials)

Example 3 with Type

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

the class FunctionEncoder method encodeParameters.

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();
}
Also used : Type(org.bcos.web3j.abi.datatypes.Type) Uint(org.bcos.web3j.abi.datatypes.Uint) Uint(org.bcos.web3j.abi.datatypes.Uint)

Example 4 with Type

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

the class FunctionReturnDecoder method build.

private static List<Type> build(String input, List<TypeReference<Type>> outputParameters) {
    List<Type> results = new ArrayList<>(outputParameters.size());
    int offset = 0;
    for (TypeReference<?> typeReference : outputParameters) {
        try {
            Class<Type> type = (Class<Type>) typeReference.getClassType();
            int hexStringDataOffset = getDataOffset(input, offset, type);
            Type result;
            if (DynamicArray.class.isAssignableFrom(type)) {
                result = TypeDecoder.decodeDynamicArray(input, hexStringDataOffset, typeReference);
                offset += MAX_BYTE_LENGTH_FOR_HEX_STRING;
            } else if (StaticArray.class.isAssignableFrom(type)) {
                int length = ((TypeReference.StaticArrayTypeReference) typeReference).getSize();
                result = TypeDecoder.decodeStaticArray(input, hexStringDataOffset, typeReference, length);
                offset += length * MAX_BYTE_LENGTH_FOR_HEX_STRING;
            } else {
                result = TypeDecoder.decode(input, hexStringDataOffset, type);
                offset += MAX_BYTE_LENGTH_FOR_HEX_STRING;
            }
            results.add(result);
        } catch (ClassNotFoundException e) {
            throw new UnsupportedOperationException("Invalid class reference provided", e);
        }
    }
    return results;
}
Also used : StaticArray(org.bcos.web3j.abi.datatypes.StaticArray) BytesType(org.bcos.web3j.abi.datatypes.BytesType) Type(org.bcos.web3j.abi.datatypes.Type) ArrayList(java.util.ArrayList)

Example 5 with Type

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

the class TypeDecoder method decodeDynamicArray.

@SuppressWarnings("unchecked")
static <T extends Type> T decodeDynamicArray(String input, int offset, TypeReference<T> typeReference) {
    int length = decodeUintAsInt(input, offset);
    BiFunction<List<T>, String, T> function = (elements, typeName) -> {
        if (elements.isEmpty()) {
            return (T) DynamicArray.empty(typeName);
        } else {
            return (T) new DynamicArray<>(elements);
        }
    };
    int valueOffset = offset + MAX_BYTE_LENGTH_FOR_HEX_STRING;
    return decodeArrayElements(input, valueOffset, typeReference, length, function);
}
Also used : Address(org.bcos.web3j.abi.datatypes.Address) Numeric(org.bcos.web3j.utils.Numeric) IntType(org.bcos.web3j.abi.datatypes.IntType) Bytes(org.bcos.web3j.abi.datatypes.Bytes) Int(org.bcos.web3j.abi.datatypes.Int) Ufixed(org.bcos.web3j.abi.datatypes.Ufixed) Utf8String(org.bcos.web3j.abi.datatypes.Utf8String) NumericType(org.bcos.web3j.abi.datatypes.NumericType) BiFunction(java.util.function.BiFunction) Array(org.bcos.web3j.abi.datatypes.Array) FixedPointType(org.bcos.web3j.abi.datatypes.FixedPointType) StandardCharsets(java.nio.charset.StandardCharsets) StaticArray(org.bcos.web3j.abi.datatypes.StaticArray) Type(org.bcos.web3j.abi.datatypes.Type) Uint(org.bcos.web3j.abi.datatypes.Uint) InvocationTargetException(java.lang.reflect.InvocationTargetException) ArrayList(java.util.ArrayList) List(java.util.List) ParameterizedType(java.lang.reflect.ParameterizedType) Bool(org.bcos.web3j.abi.datatypes.Bool) DynamicArray(org.bcos.web3j.abi.datatypes.DynamicArray) DynamicBytes(org.bcos.web3j.abi.datatypes.DynamicBytes) BigInteger(java.math.BigInteger) Fixed(org.bcos.web3j.abi.datatypes.Fixed) DynamicArray(org.bcos.web3j.abi.datatypes.DynamicArray) ArrayList(java.util.ArrayList) List(java.util.List) Utf8String(org.bcos.web3j.abi.datatypes.Utf8String) Uint(org.bcos.web3j.abi.datatypes.Uint)

Aggregations

Type (org.bcos.web3j.abi.datatypes.Type)11 Utf8String (org.bcos.web3j.abi.datatypes.Utf8String)8 BigInteger (java.math.BigInteger)5 Address (org.bcos.web3j.abi.datatypes.Address)4 Uint256 (org.bcos.web3j.abi.datatypes.generated.Uint256)4 ArrayList (java.util.ArrayList)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 InputStream (java.io.InputStream)2 ParameterizedType (java.lang.reflect.ParameterizedType)2 Service (org.bcos.channel.client.Service)2 Bool (org.bcos.web3j.abi.datatypes.Bool)2 StaticArray (org.bcos.web3j.abi.datatypes.StaticArray)2 Uint (org.bcos.web3j.abi.datatypes.Uint)2 Uint8 (org.bcos.web3j.abi.datatypes.generated.Uint8)2 Credentials (org.bcos.web3j.crypto.Credentials)2 ECKeyPair (org.bcos.web3j.crypto.ECKeyPair)2 Web3j (org.bcos.web3j.protocol.Web3j)2 ChannelEthereumService (org.bcos.web3j.protocol.channel.ChannelEthereumService)2 ApplicationContext (org.springframework.context.ApplicationContext)2 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)2