以太坊ETH开发3(助记词、账户导入、账户导出)

生成助记词

    public void nem(String pwd) {
        StringBuilder sb = new StringBuilder();
        byte[] entropy = new byte[Words.TWELVE.byteLength()];
        new SecureRandom().nextBytes(entropy);
        new MnemonicGenerator(English.INSTANCE)
                .createMnemonic(entropy, sb::append);
        String mnemonic = sb.toString();
        System.out.println("mnemonic:" + mnemonic);
        List mnemonicList = Arrays.asList(mnemonic.split(" "));
        byte[] seed = new SeedCalculator()
                .withWordsFromWordList(English.INSTANCE)
                .calculateSeed(mnemonicList, pwd);
        ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
        String privateKey = ecKeyPair.getPrivateKey().toString(16);
        String publicKey = ecKeyPair.getPublicKey().toString(16);
        String address = Keys.getAddress(publicKey);
        System.out.println("privateKey:" + privateKey);
        System.out.println("publicKey:" + publicKey);
        System.out.println("address:" + address);
    }
  • 创建账户并返回助记词

  /**
     * 创建账户
     *
     * @param pwd
     * @return mnemonic :助记词
     * privateKey:私钥
     * publicKey:公钥
     * address:地址
     * accountFilePath:账户文件路径
     */
    public Map createAccount(String pwd) {
        Map resultMap = new LinkedHashMap();
        //String filePath = System.getProperty("user.home") + File.separator + "keystore";
        StringBuilder sb = new StringBuilder();
        byte[] entropy = new byte[Words.TWELVE.byteLength()];
        new SecureRandom().nextBytes(entropy);
        new MnemonicGenerator(English.INSTANCE)
                .createMnemonic(entropy, sb::append);
        String mnemonic = sb.toString();
        System.out.println("mnemonic:" + mnemonic);
        List mnemonicList = Arrays.asList(mnemonic.split(" "));
        byte[] seed = new SeedCalculator()
                .withWordsFromWordList(English.INSTANCE)
                .calculateSeed(mnemonicList, pwd);
        ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
        String privateKey = ecKeyPair.getPrivateKey().toString(16);
        String publicKey = ecKeyPair.getPublicKey().toString(16);
        String address = "0x" + Keys.getAddress(publicKey);
        //创建钱包地址与密钥
        String fileName = null;
        try {
            fileName = WalletUtils.generateWalletFile(pwd, ecKeyPair, new File(filePath), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (fileName == null) {
            return null;
        }
        String accountFilePath = filePath + File.separator + fileName;
        resultMap.put("mnemonic", mnemonic);
        resultMap.put("privateKey", privateKey);
        resultMap.put("publicKey", publicKey);
        resultMap.put("address", address);
        resultMap.put("accountFilePath", accountFilePath);
        return resultMap;
    }
  • 根据助记词导入


/**
     * 根据助记词导入
     *
     * @param pwd      : 钱包密码
     * @param mnemonic :助记词
     * @return
     */
    public Map importByMnemonic(String pwd, String mnemonic) {
        Map resultMap = new LinkedHashMap();
        List mnemonicList = Arrays.asList(mnemonic.split(" "));
        byte[] seed = new SeedCalculator()
                .withWordsFromWordList(English.INSTANCE)
                .calculateSeed(mnemonicList, pwd);
        ECKeyPair ecKeyPair = ECKeyPair.create(Sha256.sha256(seed));
        String privateKey = ecKeyPair.getPrivateKey().toString(16);
        String publicKey = ecKeyPair.getPublicKey().toString(16);
        String address = "0x" + Keys.getAddress(publicKey);
        //创建钱包地址与密钥
        String fileName = null;
        try {
            fileName = WalletUtils.generateWalletFile(pwd, ecKeyPair, new File(filePath), false);
        } catch (Exception e) {
            e.printStackTrace();
        }
        if (fileName == null) {
            return null;
        }
        String accountFilePath = filePath + File.separator + fileName;
        resultMap.put("mnemonic", mnemonic);
        resultMap.put("privateKey", privateKey);
        resultMap.put("publicKey", publicKey);
        resultMap.put("address", address);
        resultMap.put("accountFilePath", accountFilePath);
        return resultMap;
    }

导出账户

    /**
     * 导出账户
     * @param walletFilePath : 账户完整路径,包括文件名
     * @param password : 密码
     * @return
     */
    public Map export(String walletFilePath, String password) {
        Map resultMap = new LinkedHashMap();
        Credentials credentials = loadAccount(walletFilePath, password);
        ECKeyPair ecKeyPair = credentials.getEcKeyPair();
        boolean useFullScrypt = false;
        WalletFile walletFile = null;
        try {
            if (useFullScrypt) {
                walletFile = Wallet.createStandard(password, ecKeyPair);
            } else {
                walletFile = Wallet.createLight(password, ecKeyPair);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        String fileNameEx = getWalletFileName(walletFile);
        System.out.println("walletFile:" + JSON.toJSONString(walletFile));
        System.out.println("fileNameEx:" + fileNameEx);
        resultMap.put("walletFile", walletFile);
        resultMap.put("fileName", fileNameEx);
        return resultMap;
    }

maven依赖:

        
        
            io.github.novacrypto
            BIP39
            0.1.9
        
        
            io.github.novacrypto
            BIP32
            0.0.9
        
        
            io.github.novacrypto
            BIP44
            0.0.3
        
        
        
            io.github.novacrypto
            SHA256
            0.0.1
        
        
            com.lambdaworks
            scrypt
            1.4.0
        
        
        
            io.github.novacrypto
            ToRuntime
            0.9.0
        
        
        
            com.madgag.spongycastle
            core
            1.58.0.0
        
        
        
            org.web3j
            core
            3.5.0
        

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