连接钱包分为2步
- 接入以太坊客户端
/*******连接以太坊客户端**************/
private void connectETHClient() throws IOException {
//连接方式1:使用infura 提供的客户端
//mainnet https://mainnet.infura.io/v3/2b86c426683f4a6095fd175fe931d799
web3j = Web3j.build(new HttpService(Environment.RPC_URL));// TODO: 2018/4/10 节点更改为自己的或者主网
//连接方式2:使用本地客户端
//web3j = Web3j.build(new HttpService("127.0.0.1:7545"));
//测试是否连接成功
String web3ClientVersion = web3j.web3ClientVersion().send().getWeb3ClientVersion();
log.info("version=" + web3ClientVersion);
}
- 加载用户钱包(加载用户钱包分为,文件json加载,私钥加载,助记词加载3种)
此次用私钥加载,助记词和文件,代码中有,可自行使用
/********根据私钥加载钱包 **********/
private void loadWalletByPrivateKey(String inputPrivateKey) {
credentials = Credentials.create(inputPrivateKey);
String address = credentials.getAddress();
BigInteger publicKey = credentials.getEcKeyPair().getPublicKey();
BigInteger privateKey = credentials.getEcKeyPair().getPrivateKey();
log.info("address=" + address);
log.info("public key=" + publicKey);
log.info("private key=" + privateKey);
tempAddress = address;
}
运行结果 web3j demo项目git地址: https://github.com/jambestwick/we3jdemo
|