1. 程式人生 > >一個針對Android的輕量級的ETH庫

一個針對Android的輕量級的ETH庫

簡介:

這是一個輕量級的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:

                    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:

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轉賬離線簽名
    /**
     * 離線簽名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智慧合約轉賬離線簽名
/**
     * 離線簽名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
<dependency>
  <groupId>party.loveit</groupId>
  <artifactId>ethforandroidlibrary</artifactId>
  <version>1.0.1</version>
  <type>pom</type>
</dependency>
  1. Gradle
compile 'party.loveit:ethforandroidlibrary:1.0.1'

or

implementation 'party.loveit:ethforandroidlibrary:1.0.1'

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