eth java,一个轻量级的Java实现的ETH库

简介:

这是一个轻量级的eth库,支持eth的私钥,公钥,地址的生成,和eth及其智能合约的转账离线签名操作

项目地址:https://github.com/wypeng2012/ETHLiteForJava

欢迎star

- 如何使用

first add Bip44ForJava dependencies

implementation ‘party.52it:Bip44ForJava:1.0.0’

生成 ETH

code:

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

路径私钥: a54faf1496634d32743ba74755b82f673c1309335e49e19cf527d17f3304899d

publicKey: 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 = Convert.toWei(value, Convert.Unit.ETHER);

RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, realValue.toBigIntegerExact());

//fee= (gasPrice * gasLimit ) / 10^18 ether

Credentials 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 = 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 ether

Credentials 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'

or

implementation 'party.52it:ETHLiteForJava:1.0.0'

Ivy

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值