以太坊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依赖:

        <!-- 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>
  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值