web3j的基础用法-2多种创建钱包

创建钱包

分为3方式

  1. 创建助记词钱包,并返回助记词到指定文件
  public static boolean createWallet(String filePath, String destFile) throws CipherException, IOException {
        FileUtil.createOrExistsDir(new File(filePath));
        Bip39Wallet bip39Wallet = WalletUtils.generateBip39Wallet("", new File(filePath));//助记词钱包生成
        bip39Wallet.getMnemonic();
        System.out.println("生成助记词:" + bip39Wallet.getMnemonic());
        return FileUtil.writeFileFromLineString(new File(destFile), bip39Wallet.getMnemonic(), true);
    }

  1. 创建完整类型的钱包

    /**
     * return fileName
     **/
    public static String createWalletFull(String password, String destFile) throws CipherException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
        FileUtil.createOrExistsDir(new File(destFile));
        return WalletUtils.generateFullNewWalletFile(password, new File(destFile));
    }
  1. 创建一个简单钱包

     /**
     * return fileName
     ***/
    public static String createWalletLight(String password, String destFile) throws CipherException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, NoSuchProviderException, IOException {
        FileUtil.createOrExistsDir(new File(destFile));
        return WalletUtils.generateLightNewWalletFile(password, new File(destFile));
    }

备注:简单钱包和完整钱包在于生成公公私钥的规则上区别

    public static String generateWalletFile(
            String password, ECKeyPair ecKeyPair, File destinationDirectory, boolean useFullScrypt)
            throws CipherException, IOException {

        WalletFile walletFile;
        if (useFullScrypt) {
            walletFile = Wallet.createStandard(password, ecKeyPair);
        } else {
            walletFile = Wallet.createLight(password, ecKeyPair);
        }

        String fileName = getWalletFileName(walletFile);
        File destination = new File(destinationDirectory, fileName);

        objectMapper.writeValue(destination, walletFile);

        return fileName;
    }

在这里插入图片描述

进一步深入了解加密机制,我们转到 ScryptKdfParams

在这里插入图片描述
我们看到了这些参数:

  • P the bytes of the pass phrase. 密码短语的字节数
  • S 盐值 字节串 salt
  • N 参数 n 和 r 决定了占用的内存区域大小和哈希迭代次数(占用的内存大小为 128⋅n⋅r bytes,迭代次数为 2⋅n⋅r),所以可以修改的参数是 n。
  • r 参数 r 决定了连续读大小(sequential read size),通常不应该修改。
  • p Parallelization parameter. Must be a positive integer less than or equal to
    Integer.MAX_VALUE / (128 * r * 8).P 口令,一字节串
  • dkLen the length of the key to generate 要生成的密钥的长度

这些就是生成密钥的关键逻辑参数,也是破解的参数

在这里插入图片描述

在这里插入图片描述
想深入了解 :请看 《什么是以太坊私钥储存(Keystore)文件》

github web3jdemo工程地址:https://github.com/jambestwick/we3jdemo

在这里插入图片描述

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值