如何使用Web3j生成私钥和地址,而不只是创建密钥存储JSON文件?

一个我提供的方法,通过将结果privatekey导入到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
}

原文《以太坊常见问题和错误》中的:
http://cw.hubwiz.com/card/c/ethereum-FAQ/1/1/5/

另外推荐几个很受欢迎全网稀缺的互动教程:

  • web3j,主要是针对java和android程序员围绕web3j库进行区块链以太坊开发的讲解。
  • python以太坊,主要是针对python围绕web3.py进行区块链以太坊应用开发的讲解。
  • php以太坊,主要是介绍使用php进行智能合约开发交互,进行账号创建、交易、转账、代币开发以及过滤器和事件等内容。
  • 以太坊开发,主要是介绍使用node.js、mongodb、区块链、ipfs实现去中心化电商DApp实战,适合进阶。
  • 以太坊教程,主要介绍智能合约与dapp应用开发,适合入门。

猜你喜欢

转载自blog.csdn.net/mongo_node/article/details/81094551