Search in sources :

Example 1 with ECKeyPair

use of org.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.

the class Ethereum 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();
    Thread.sleep(3000);
    System.out.println("开始测试...");
    System.out.println("===================================================================");
    logger.info("初始化AOMP的ChannelEthereumService");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    // 使用AMOP消息信道初始化web3j
    Web3j web3 = Web3j.build(channelEthereumService);
    logger.info("调用web3的getBlockNumber接口");
    EthBlockNumber ethBlockNumber = web3.ethBlockNumber().sendAsync().get();
    logger.info("获取ethBlockNumber:{}", ethBlockNumber);
    // 初始化交易签名私钥
    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");
    // 部署合约
    Ok ok = Ok.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("Ok getContractAddress " + ok.getContractAddress());
    // 调用合约接口
    java.math.BigInteger Num = new BigInteger("999");
    Uint256 num = new Uint256(Num);
    TransactionReceipt receipt = ok.trans(num).get();
    System.out.println("receipt transactionHash" + receipt.getTransactionHash());
    // 查询合约数据
    num = ok.get().get();
    System.out.println("ok.get() " + num.getValue());
}
Also used : EthBlockNumber(org.bcos.web3j.protocol.core.methods.response.EthBlockNumber) ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) Service(org.bcos.channel.client.Service) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.bcos.web3j.protocol.Web3j) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) BigInteger(java.math.BigInteger) Uint256(org.bcos.web3j.abi.datatypes.generated.Uint256) Credentials(org.bcos.web3j.crypto.Credentials)

Example 2 with ECKeyPair

use of org.bcos.web3j.crypto.ECKeyPair 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 ECKeyPair

use of org.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.

the class SystemContractTools 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();
    ToolConf toolConf = context.getBean(ToolConf.class);
    System.out.println(LOGO);
    System.out.println("===================================================================");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    Web3j web3 = Web3j.build(channelEthereumService);
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Credentials credentials = Credentials.create(keyPair);
    BigInteger gasPrice = new BigInteger("30000000");
    BigInteger gasLimit = new BigInteger("30000000");
    SystemProxy systemProxy = SystemProxy.load(toolConf.getSystemProxyAddress(), web3, credentials, gasPrice, gasLimit);
    switch(args[0]) {
        case "SystemProxy":
            SystemProxyTools.processSystemProxy(systemProxy, web3, credentials, gasPrice, gasLimit);
            break;
        case "AuthorityFilter":
            AuthorityFilter authorityFilter = AuthorityFilter.load(getAction(systemProxy, "AuthorityFilter"), web3, credentials, gasPrice, gasLimit);
            AuthorityFilterTools.processAuthorityFilter(authorityFilter, args);
            break;
        case "NodeAction":
            NodeAction nodeAction = NodeAction.load(getAction(systemProxy, "NodeAction"), web3, credentials, gasPrice, gasLimit);
            NodeActionTools.processNodeAction(context, nodeAction, args);
            break;
        case "CAAction":
            CAAction caAction = CAAction.load(getAction(systemProxy, "CAActions"), web3, credentials, gasPrice, gasLimit);
            CAActionTools.processCAAction(context, caAction, args);
            break;
        case "ConfigAction":
            ConfigAction configAction = ConfigAction.load(getAction(systemProxy, "ConfigAction"), web3, credentials, gasPrice, gasLimit);
            ConfigActionTools.processConfigAction(configAction, args);
            break;
        default:
            break;
    }
    System.exit(0);
}
Also used : ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) Service(org.bcos.channel.client.Service) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) ApplicationContext(org.springframework.context.ApplicationContext) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) Web3j(org.bcos.web3j.protocol.Web3j) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) BigInteger(java.math.BigInteger) Credentials(org.bcos.web3j.crypto.Credentials)

Example 4 with ECKeyPair

use of org.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.

the class ECKey method main.

public static void main(String[] args) throws Exception {
    ECKeyPair keyPair = Keys.createEcKeyPair();
    logger.debug("public key", keyPair.getPublicKey());
    logger.debug("private key", keyPair.getPrivateKey());
    System.out.println("public key " + keyPair.getPublicKey());
    System.out.println("private key " + keyPair.getPrivateKey());
    System.out.println("serialize key " + Hex.toHexString(Keys.serialize(keyPair)));
    // public key 6005884739482598907019672016029935954035758996027051146272921018865015941269698926222431345309233458526942087465818124661687956402067203118790805113144306
    // private key 11695290896330592173013668505941497555094145434653626165899956696676058923570
    // serialize key
    keyPair = Keys.deserialize(Hex.decode("19db4cd14479981c3d7e785ec2412619885b5a7ffc438e6801474b962996023272ac27315da55056067973a3b58b27385bb9c919331df1751771016efcbe61a969458d6f7286b7a7107e4cd6e17b348c9df6c2b3fe9bf239555f90a78f8603f2"));
    System.out.println("public key " + keyPair.getPublicKey());
    System.out.println("private key " + Hex.toHexString(keyPair.getPrivateKey().toByteArray()));
    String str = "hello world";
    Sign.SignatureData sigData = Sign.signMessage(str.getBytes(), keyPair);
    BigInteger publicKey = Sign.signedMessageToKey(str.getBytes(), sigData);
    System.out.println("publicKey " + publicKey);
}
Also used : ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) BigInteger(java.math.BigInteger) Sign(org.bcos.web3j.crypto.Sign)

Example 5 with ECKeyPair

use of org.bcos.web3j.crypto.ECKeyPair in project web3sdk by FISCO-BCOS.

the class LoadTestRunner method main.

public static void main(String[] args) throws Exception {
    logger.debug("初始化");
    ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
    Service service = context.getBean(Service.class);
    service.run();
    System.out.println("3s后开始测试...");
    Thread.sleep(1000);
    System.out.println("2s后开始测试...");
    Thread.sleep(1000);
    System.out.println("1s后开始测试...");
    Thread.sleep(1000);
    System.out.println("开始测试");
    System.out.println("===================================================================");
    ChannelEthereumService channelEthereumService = new ChannelEthereumService();
    channelEthereumService.setChannelService(service);
    channelEthereumService.setTimeout(10000);
    Web3j web3 = Web3j.build(channelEthereumService);
    Thread.sleep(2000);
    // 先部署一个测试合约
    ECKeyPair keyPair = Keys.createEcKeyPair();
    Credentials credentials = Credentials.create(keyPair);
    RawTransactionManager rawTransactionManager = new RawTransactionManager(web3, credentials);
    java.math.BigInteger gasPrice = new BigInteger("30000000");
    java.math.BigInteger gasLimit = new BigInteger("30000000");
    java.math.BigInteger initialWeiValue = new BigInteger("0");
    LoadTest loadTest = LoadTest.deploy(web3, credentials, gasPrice, gasLimit, initialWeiValue).get();
    System.out.println("LoadTest getContractAddress " + loadTest.getContractAddress());
    // 发送交易
    for (Integer i = 0; i < 10; ++i) {
        Thread txThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        Thread.sleep(1000);
                        // TransactionReceipt receipt = loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1)).get();
                        loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1));
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        txThread.start();
    }
    // 读取合约
    for (Integer i = 0; i < 10; ++i) {
        Thread callThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        Address origin = loadTest._origin().get();
                        Address sender = loadTest._sender().get();
                        logger.info("storage sender:{} origin:{}", sender.toString(), origin.toString());
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        callThread.start();
    }
    // 200个线程,发链上链下消息
    for (Integer i = 0; i < 10; ++i) {
        Thread messageThread = new Thread() {

            public void run() {
                try {
                    while (true) {
                        ChannelRequest request = new ChannelRequest();
                        request.setToTopic("test");
                        request.setFromOrg("WB");
                        request.setToOrg("EB");
                        request.setMessageID(service.newSeq());
                        StringBuffer sb = new StringBuffer();
                        for (int i = 0; i < 500; ++i) {
                            sb.append("hello world!:");
                        }
                        request.setContent(sb.toString() + request.getMessageID());
                        request.setTimeout(2000);
                        // ChannelResponse response = service.sendChannelMessage2(request);
                        logger.info("发送链上链下消息:{}", request.getMessageID());
                        ChannelResponse response = service.sendChannelMessage(request);
                        logger.info("收到链上链下响应:{} {}", response.getMessageID(), response.getErrorCode());
                    }
                } catch (Exception e) {
                    logger.error("系统错误", e);
                }
            }
        };
        messageThread.start();
    }
    for (int i = 0; i < 10; ++i) {
        TransactionReceipt receipt = loadTest.addCounter(new Utf8String(UUID.randomUUID().toString()), new Int256(1)).get();
        Address origin = loadTest._origin().get();
        Address sender = loadTest._sender().get();
        List<Type> addresses = loadTest.constGetInf().get();
        logger.info("const sender:{} origin:{}", (((Address) addresses.get(0)).toString()), (((Address) addresses.get(1)).toString()));
        logger.info("storage sender:{} origin:{}", sender.toString(), origin.toString());
    }
}
Also used : ChannelRequest(org.bcos.channel.dto.ChannelRequest) 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) BigInteger(java.math.BigInteger) ECKeyPair(org.bcos.web3j.crypto.ECKeyPair) TransactionReceipt(org.bcos.web3j.protocol.core.methods.response.TransactionReceipt) Service(org.bcos.channel.client.Service) ChannelEthereumService(org.bcos.web3j.protocol.channel.ChannelEthereumService) RawTransactionManager(org.bcos.web3j.tx.RawTransactionManager) ChannelResponse(org.bcos.channel.dto.ChannelResponse) BigInteger(java.math.BigInteger) Utf8String(org.bcos.web3j.abi.datatypes.Utf8String) Int256(org.bcos.web3j.abi.datatypes.generated.Int256) Web3j(org.bcos.web3j.protocol.Web3j) Type(org.bcos.web3j.abi.datatypes.Type) BigInteger(java.math.BigInteger) Credentials(org.bcos.web3j.crypto.Credentials)

Aggregations

BigInteger (java.math.BigInteger)5 ECKeyPair (org.bcos.web3j.crypto.ECKeyPair)5 Service (org.bcos.channel.client.Service)4 Credentials (org.bcos.web3j.crypto.Credentials)4 Web3j (org.bcos.web3j.protocol.Web3j)4 ChannelEthereumService (org.bcos.web3j.protocol.channel.ChannelEthereumService)4 ApplicationContext (org.springframework.context.ApplicationContext)4 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)4 Address (org.bcos.web3j.abi.datatypes.Address)2 Type (org.bcos.web3j.abi.datatypes.Type)2 Utf8String (org.bcos.web3j.abi.datatypes.Utf8String)2 Uint256 (org.bcos.web3j.abi.datatypes.generated.Uint256)2 TransactionReceipt (org.bcos.web3j.protocol.core.methods.response.TransactionReceipt)2 ChannelRequest (org.bcos.channel.dto.ChannelRequest)1 ChannelResponse (org.bcos.channel.dto.ChannelResponse)1 Bool (org.bcos.web3j.abi.datatypes.Bool)1 Int256 (org.bcos.web3j.abi.datatypes.generated.Int256)1 Sign (org.bcos.web3j.crypto.Sign)1 EthBlockNumber (org.bcos.web3j.protocol.core.methods.response.EthBlockNumber)1 RawTransactionManager (org.bcos.web3j.tx.RawTransactionManager)1