简介:
这是一个轻量级的eth库,支持eth的私钥,公钥,地址的生成,和eth及其智能合约的转账离线签名操作
项目地址:https://github.com/wypeng2012/ETHForAndroid
欢迎star
support Android sdk >= 14
- 如何使用
- 先添加bip44forandroidlibrary的依赖
implementation ‘party.loveit:bip44forandroidlibrary:1.0.7’
- 生成 ETH相关的公钥私钥等
code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| List<String> words = Bip44Utils.generateMnemonicWords(MainActivity.this); Log.e("TAG", "words: " + words.toString());
BigInteger priKey = Bip44Utils.getPathPrivateKey(words, "m/44'/60'/0'/0/0");
ECKeyPair ecKeyPair = ECKeyPair.create(priKey);
String publicKey = ecKeyPair.getPublicKeyToString(); Log.e("TAG", "publicKey: " + publicKey);
String privateKey = ecKeyPair.getPrivateKeyToString(); Log.e("TAG", "privateKey: " + privateKey);
String address = "0x" + Keys.getAddress(ecKeyPair); Log.e("TAG", "address: " + address);
|
result:
1 2 3 4
| 2018-12-06 14:04:54.053 6857-7056/party.loveit.ethforandroid E/TAG: words: [kite, portion, actress, prize, multiply, odor, mobile, social, refuse, fruit, tunnel, smart] 2018-12-06 14:04:54.648 6857-7056/party.loveit.ethforandroid E/TAG: publicKey: 0x7eac8e97e93d250630ff507c4339a4950f9b194d0951ac7b4b260c61521613f77b13dc8621cd0691f711c134ba5693a7460dc5231c98636ce6f727d4e725596a 2018-12-06 14:04:54.648 6857-7056/party.loveit.ethforandroid E/TAG: privateKey: 0xc3f0b16731bc42ae07ff0304ba1172e6414dcd8de7a612e1ec281c109181f3d9 2018-12-06 14:04:54.653 6857-7056/party.loveit.ethforandroid E/TAG: address: 0x4a7484c7c9ed536ef428e48240ad5707b21dc6e8
|
- eth转账离线签名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
|
public static String signedEthTransactionData(String privateKey, String to, BigInteger nonce, BigInteger gasPrice, BigInteger gasLimit, String value) throws Exception { if (words == null || words.size() != 12) { throw new RuntimeException("please generateMnemonic first"); } BigDecimal realValue = Convert.toWei(value, Convert.Unit.ETHER); RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, realValue.toBigIntegerExact());
Credentials credentials = Credentials.create(privateKey); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); return Numeric.toHexString(signedMessage); }
|
- eth智能合约转账离线签名
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
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);
Credentials credentials = Credentials.create(privateKey); byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials); return Numeric.toHexString(signedMessage); }
|
- How to dependencies
- Maven
1 2 3 4 5 6
| <dependency> <groupId>party.loveit</groupId> <artifactId>ethforandroidlibrary</artifactId> <version>1.0.1</version> <type>pom</type> </dependency>
|
- Gradle
1 2 3 4 5
| compile 'party.loveit:ethforandroidlibrary:1.0.1'
or
implementation 'party.loveit:ethforandroidlibrary:1.0.1'
|
- Ivy
1 2 3
| <dependency org='party.loveit' name='ethforandroidlibrary' rev='1.0.1'> <artifact name='ethforandroidlibrary' ext='pom' ></artifact> </dependency>
|