开发者社区> 问答> 正文

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

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

展开
收起
景凌凯 2020-03-18 18:49:50 1808 0
2 条回答
写回答
取消 提交回答
  • 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 }

    2020-03-21 14:36:33
    赞同 展开评论 打赏
  • 有点尴尬唉 你要寻找的东西已经被吃掉啦!

    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 }

    2020-03-18 18:50:04
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
Web应用系统性能优化 立即下载
高性能Web架构之缓存体系 立即下载
PWA:移动Web的现在与未来 立即下载