java 離線生成ETH賬戶

我們採用bip44協議生成帶助記詞的賬戶,先看看生成的的結果包含了哪些

{
    "address":"****",       //地址
    "keystore":"****",      //kestore
    "mnemonic":****,        //助記詞
    "mnemonicPath":"****",  //助記詞根路徑
    "privateKey":"****",    //私鑰
    "publicKey":"****"      //公鑰
}

這是我們創建的結果,最終怎麼持久化就看業務需求了,這裏建議存keystore就足夠了。

首先我們需要導入依賴

 	   <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>BIP44</artifactId>
            <version>0.0.3</version>
        </dependency>
        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>3.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.bitcoinj</groupId>
            <artifactId>bitcoinj-core</artifactId>
            <version>0.14.6</version>
            <scope>compile</scope>
        </dependency>

用戶這邊輸入密碼,看代碼

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<String> 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<String> 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帳號!

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章