使用web3j加载以太坊智能合约如何指定证书Credentials?
我正在使用web3j库来生成一个用于我的solidity以太坊智能合约的Java封装包,我已经将该智能合约部署到区块链中,并且它已经被挖掘,现在我想使用Java封装包在Java中加载该以太坊智能合约。 java封装包是SimpleStorage,web3j已经生成了用于加载以太坊智能合约的以下方法签名: public static SimpleStorage load(String contractAddress, Web3j web3j, Credentials credentials, BigInteger gasPrice, BigInteger gasLimit) { return new SimpleStorage(contractAddress, web3j, credentials, gasPrice, gasLimit); } public static SimpleStorage load(String contractAddress, Web3j web3j, TransactionManager transactionManager, BigInteger gasPrice, BigInteger gasLimit) { return new SimpleStorage(contractAddress, web3j, transactionManager, gasPrice, gasLimit); } 如何提供Credentials对象?我有一个公钥、私钥和地址,它是我在区块链上部署的,但是Credentials对象需要一个ECKeyPair,它希望私钥和公钥是一个BigInteger,但是我的密钥只是字符串形式的。 加载参数可以传递给这个方法: public static Credentials create(String privateKey, String publicKey) { return create(new ECKeyPair(Numeric.toBigInt(privateKey), Numeric.toBigInt(publicKey))); } 如果我的公钥和私钥不能转换成BigInt,那么这将失败。 问题的解决 若要生成凭据,如果你有普通的公钥和私钥,则需要将它们转换为十六进制的表示形式,然后将它们传递给构造函数用于Credentials。 String hexPrivateKey = String.format("%040x", new BigInteger(1, privateKey.getBytes())); String hexPublicKey = String.format("%040x", new BigInteger(1, publicKey.getBytes())); Credentials creds = Credentials.create(hexPrivateKey, hexPublicKey); 其中privateKey和privateKey是包含你的私钥和公钥的字符串。 原文《以太坊常见问题和错误》中的: http://cw.hubwiz.com/card/c/ethereum-FAQ/1/1/13/ 另外推荐几个很受欢迎全网稀缺的互动教程: web3j,主要是针对java和android程序员围绕web3j库进行区块链以太坊开发的讲解。 python以太坊,主要是针对python围绕web3.py进行区块链以太坊应用开发的讲解。 php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。 以太坊开发,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。