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

版权声明:本文为原创文章,未经同意不得转载。 https://blog.csdn.net/m0_37754981/article/details/82116887

生成助记词

    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依赖:

        <!-- https://mvnrepository.com/artifact/io.github.novacrypto/BIP39 -->
        <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>BIP39</artifactId>
            <version>0.1.9</version>
        </dependency>
        <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>BIP32</artifactId>
            <version>0.0.9</version>
        </dependency>
        <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>BIP44</artifactId>
            <version>0.0.3</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.github.novacrypto/SHA256 -->
        <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>SHA256</artifactId>
            <version>0.0.1</version>
        </dependency>
        <dependency>
            <groupId>com.lambdaworks</groupId>
            <artifactId>scrypt</artifactId>
            <version>1.4.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/io.github.novacrypto/ToRuntime -->
        <dependency>
            <groupId>io.github.novacrypto</groupId>
            <artifactId>ToRuntime</artifactId>
            <version>0.9.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.madgag.spongycastle/core -->
        <dependency>
            <groupId>com.madgag.spongycastle</groupId>
            <artifactId>core</artifactId>
            <version>1.58.0.0</version>
        </dependency>

        <dependency>
            <groupId>org.web3j</groupId>
            <artifactId>core</artifactId>
            <version>3.5.0</version>
        </dependency>

猜你喜欢

转载自blog.csdn.net/m0_37754981/article/details/82116887