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)文件》

经验分享 程序员 微信小程序 职场和发展