简介:

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

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

欢迎star

support Android sdk >= 14

- 如何使用

  1. 先添加bip44forandroidlibrary的依赖

implementation ‘party.loveit:bip44forandroidlibrary:1.0.7’

  1. 生成 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());

//m / purpose’ / coin_type’ / account’ / change / address_index
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
  1. 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
/**
* 离线签名eth
*
* @param to//转账的钱包地址
* @param nonce//获取到的交易次数
* @param gasPrice
* @param gasLimit
* @param value //转账的值
* @return
*/
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");
}
//把十进制的转换成ETH的Wei, 1ETH = 10^18 Wei
BigDecimal realValue = Convert.toWei(value, Convert.Unit.ETHER);
RawTransaction rawTransaction = RawTransaction.createEtherTransaction(nonce, gasPrice, gasLimit, to, realValue.toBigIntegerExact());
//手续费= (gasPrice * gasLimit ) / 10^18 ether

Credentials credentials = Credentials.create(privateKey);
//使用TransactionEncoder对RawTransaction进行签名操作
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
// //转换成0x开头的字符串
return Numeric.toHexString(signedMessage);
}
  1. 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
/**
* 离线签名eth
*
* @param contractAddress//合约地址
* @param to//转账的钱包地址
* @param nonce//获取到的交易次数
* @param gasPrice
* @param gasLimit
* @param value //转账的值
* @return
*/
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");
}
//因为每个代币可以规定自己的小数位, 所以实际的转账值=数值 * 10^小数位
BigDecimal realValue = BigDecimal.valueOf(value * Math.pow(10.0, decimal));

//0xa9059cbb代表某个代币的转账方法hex(transfer) + 对方的转账地址hex + 转账的值的hex
String data = "0xa9059cbb" + Numeric.toHexStringNoPrefixZeroPadded(Numeric.toBigInt(to), 64) + Numeric.toHexStringNoPrefixZeroPadded(realValue.toBigInteger(), 64);
RawTransaction rawTransaction = RawTransaction.createTransaction(nonce, gasPrice, gasLimit, contractAddress, data);
//手续费= (gasPrice * gasLimit ) / 10^18 ether

Credentials credentials = Credentials.create(privateKey);
//使用TransactionEncoder对RawTransaction进行签名操作
byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
//转换成0x开头的字符串
return Numeric.toHexString(signedMessage);
}

- How to dependencies

  1. Maven
1
2
3
4
5
6
<dependency>
<groupId>party.loveit</groupId>
<artifactId>ethforandroidlibrary</artifactId>
<version>1.0.1</version>
<type>pom</type>
</dependency>
  1. Gradle
1
2
3
4
5
compile 'party.loveit:ethforandroidlibrary:1.0.1'

or

implementation 'party.loveit:ethforandroidlibrary:1.0.1'
  1. Ivy
1
2
3
<dependency org='party.loveit' name='ethforandroidlibrary' rev='1.0.1'>
<artifact name='ethforandroidlibrary' ext='pom' ></artifact>
</dependency>