简介: 这是一个轻量级的eth库,支持eth的私钥,公钥,地址的生成,和eth及其智能合约的转账离线签名操作
项目地址:https://github.com/wypeng2012/ETHLiteForJava- 如何使用
【区块链钱包相关|一个轻量级的Java实现的ETH库】欢迎star
- first add Bip44ForJava dependencies
implementation ‘party.52it:Bip44ForJava:1.0.0’
- 生成 ETH
System.out.println("************ 获取12个助记词中 ************");
List> words = Bip44Utils.generateMnemonicWords();
System.out.println("12个助记词: " + words.toString() + "\n");
System.out.println("************ 种子生成中 ************");
byte[] seed = Bip44Utils.getSeed(words);
System.out.println("种子: " + new BigInteger(1,seed).toString(16) + "\n");
System.out.println("************ 根据路径获取节点私钥中 ************");
BigInteger pri1 = Bip44Utils.getPathPrivateKey(words,"m/44'/60'/0'/0/0");
System.out.println("路径私钥: " + pri1.toString(16) + "\n");
ECKeyPair ecKeyPair = ECKeyPair.create(pri1);
String publicKey = ecKeyPair.getPublicKeyToString();
System.out.println("publicKey: " + publicKey);
String privateKey = ecKeyPair.getPrivateKeyToString();
System.out.println("privateKey: " + privateKey);
String address = "0x" + Keys.getAddress(ecKeyPair);
System.out.println("address: " + address);
result:
************ 获取12个助记词中 ************
12个助记词: [bottom, material, render, live, alcohol, fan, lab, monkey, laptop, leader, stay, light]************ 种子生成中 ************
PBKDF2 took {}678.9 ms
种子: 625becd48df47b1304a13daf6d6b1f5df735a1a3e0ecb1307afdbcee5c71d342296f4982528c91728c5eb01ad72ccd7beebac5b4b7c594095d39408dbb0d6ec8************ 根据路径获取节点私钥中 ************
PBKDF2 took {}29.31 ms
路径私钥: a54faf1496634d32743ba74755b82f673c1309335e49e19cf527d17f3304899dpublicKey: 0x7dcb498a35f9beb9e5b567055fbefc50380c5e7bf23cac9ef4ced3cce3acb52acf0e77b6b594a5f68838f6cd211ce824e301f6e2fdd458bf64ff43d4ae9b60ed
privateKey: 0xa54faf1496634d32743ba74755b82f673c1309335e49e19cf527d17f3304899d
address: 0x71e6ed40bdb631e1cd15046b6b053f80333ceece
- eth转账离线签名
public static String signedEthTransactionData(String privateKey, String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String value) throws Exception {//1ETH = 10^18 Wei
BigDecimal realValue = https://www.it610.com/article/Convert.toWei(value, Convert.Unit.ETHER);
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, realValue.toBigIntegerExact());
//fee= (gasPrice * gasLimit ) / 10^18 etherCredentials credentials = Credentials.create(privateKey);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
return Numeric.toHexString(signedMessage);
}
- eth智能合约转账离线签名
public static String signedEthContractTransactionData(String privateKey, String contractAddress, String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, Double value, Double decimal) throws Exception {
if (words == null || words.size() != 12) {
throw new RuntimeException("please generateMnemonic first");
}BigDecimal realValue = https://www.it610.com/article/BigDecimal.valueOf(value * Math.pow(10.0, decimal));
String data ="0xa9059cbb" + Numeric.toHexStringNoPrefixZeroPadded(Numeric.toBigInt(to), 64) + Numeric.toHexStringNoPrefixZeroPadded(realValue.toBigInteger(), 64);
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, contractAddress, data);
//fee= (gasPrice * gasLimit ) / 10^18 etherCredentials credentials = Credentials.create(privateKey);
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
return Numeric.toHexString(signedMessage);
}
- 远程依赖
- Maven
party.52it
ETHLiteForJava
1.0.0
- Gradle
compile 'party.52it:ETHLiteForJava:1.0.0'orimplementation 'party.52it:ETHLiteForJava:1.0.0'
- Ivy