Go-Ethereum

使用 web3j 生成私鑰和地址

  • May 15, 2020

web3j如何使用而不是創建密鑰​​庫 json 文件來生成私鑰和地址

下面是我的方法,通過將結果私鑰導入 MetaMask 並獲得與預期相同的地址來驗證。

private static JSONObject process(String seed){

        JSONObject processJson = new JSONObject();

        try {
           ECKeyPair ecKeyPair = Keys.createEcKeyPair();
           BigInteger privateKeyInDec = ecKeyPair.getPrivateKey();

           String sPrivatekeyInHex = privateKeyInDec.toString(16);

           WalletFile aWallet = Wallet.createLight(seed, ecKeyPair);
           String sAddress = aWallet.getAddress();


           processJson.put("address", "0x" + sAddress);
           processJson.put("privatekey", sPrivatekeyInHex);


       } catch (CipherException e) {
           //
       } catch (InvalidAlgorithmParameterException e) {
           //
       } catch (NoSuchAlgorithmException e) {
           //
       } catch (NoSuchProviderException e) {
           //
       } 

       return processJson;
}


main(){  // unit test 
   String seed = UUID.randomUUID().toString();
   JSONObject result = process(seed); // get a json containing private key and address
}

引用自:https://ethereum.stackexchange.com/questions/41072