以太坊常见问题和错误

在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

如何使用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
}