java 离线生成ETH账户

我们采用bip44协议生成带助记词的账户,先看看生成的的结果包含了哪些

{
    "address":"****",       //地址
    "keystore":"****",      //kestore
    "mnemonic":****,        //助记词
    "mnemonicPath":"****",  //助记词根路径
    "privateKey":"****",    //私钥
    "publicKey":"****"      //公钥
}

这是我们创建的结果,最终怎么持久化就看业务需求了,这里建议存keystore就足够了。

首先我们需要导入依赖

 	   
            io.github.novacrypto
            BIP44
            0.0.3
        
        
            org.web3j
            core
            3.4.0
        
        
            org.bitcoinj
            bitcoinj-core
            0.14.6
            compile
        

用户这边输入密码,看代码

public void createEthWallet(String password) {
	//TODO 密码长度这边校验一下  一般大于8就可以了
	//默认根地址
	String path="m/44'/60'/0'/0/0";
	String[] pathArray =path.split("/");
	long creationTimeSeconds = System.currentTimeMillis() / 1000;
	DeterministicSeed ds = new DeterministicSeed(new SecureRandom(), 128, "", creationTimeSeconds);
        //根私钥
        byte[] seedBytes = ds.getSeedBytes();
        //助记词
        List mnemonic = ds.getMnemonicCode();
        try {
            //助记词种子
            byte[] mnemonicSeedBytes = MnemonicCode.INSTANCE.toEntropy(mnemonic);
            ECKeyPair mnemonicKeyPair = ECKeyPair.create(mnemonicSeedBytes);
            WalletFile walletFile = Wallet.createLight(password, mnemonicKeyPair);
            ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
            //存这个keystore 用完后删除
            String jsonStr = objectMapper.writeValueAsString(walletFile);
            //验证
            WalletFile checkWalletFile = objectMapper.readValue(jsonStr, WalletFile.class);
            ECKeyPair ecKeyPair = Wallet.decrypt(password, checkWalletFile);
            byte[] checkMnemonicSeedBytes = Numeric.hexStringToByteArray(ecKeyPair.getPrivateKey().toString(16));
            List checkMnemonic = MnemonicCode.INSTANCE.toMnemonic(checkMnemonicSeedBytes);
        } catch (MnemonicException.MnemonicLengthException | MnemonicException.MnemonicWordException | MnemonicException.MnemonicChecksumException | CipherException | IOException e) {
            logger.error("账号生成異常了!!!{}", e);
        }
        if (seedBytes == null) return;
        DeterministicKey dkKey = HDKeyDerivation.createMasterPrivateKey(seedBytes);
        for (int i = 1; i < pathArray.length; i++) {
            ChildNumber childNumber;
            if (pathArray[i].endsWith("'")) {
                int number = Integer.parseInt(pathArray[i].substring(0,
                        pathArray[i].length() - 1));
                childNumber = new ChildNumber(number, true);
            } else {
                int number = Integer.parseInt(pathArray[i]);
                childNumber = new ChildNumber(number, false);
            }
            dkKey = HDKeyDerivation.deriveChildKey(dkKey, childNumber);
        }
        ECKeyPair keyPair = ECKeyPair.create(dkKey.getPrivKeyBytes());
        EthWalletModel ethHDWallet = null;
        try {
		WalletFile walletFile = Wallet.createLight(password, keyPair);
		ObjectMapper objectMapper = ObjectMapperFactory.getObjectMapper();
		//keystore
		String jsonStr = objectMapper.writeValueAsString(walletFile);
		//私钥
		String privateKey=keyPair.getPrivateKey().toString(16);
		//公钥
		String publicKey=keyPair.getPublicKey().toString(16);
		//根地址
		String rpath = dkKey.getPathAsString();
		//地址
		String address="0x" + walletFile.getAddress();
        } catch (CipherException | JsonProcessingException e) {
            logger.error("創建账户異常了!!!{}", e);
        }
    }

整个流程就生成seed种子(随机生成助记词)—>私钥—>公钥

关于确定新算法可以参考https://blog.csdn.net/weixin_39842528/article/details/82224907

这样就离线生成了一个ETH帐号!

你可能感兴趣的:(区块链)