@Test public void generateMnemonicShouldGenerateExpectedMnemonicWords() { String actualMnemonic = MnemonicUtils.generateMnemonic(initialEntropy); assertEquals(mnemonic, actualMnemonic); }
/** * Generates a BIP-39 compatible Ethereum wallet. The private key for the wallet can * be calculated using following algorithm: * <pre> * Key = SHA-256(BIP_39_SEED(mnemonic, password)) * </pre> * * @param password Will be used for both wallet encryption and passphrase for BIP-39 seed * @param destinationDirectory The directory containing the wallet * @return A BIP-39 compatible Ethereum wallet * @throws CipherException if the underlying cipher is not available * @throws IOException if the destination cannot be written to */ public static Bip39Wallet generateBip39Wallet(String password, File destinationDirectory) throws CipherException, IOException { byte[] initialEntropy = new byte[16]; secureRandom.nextBytes(initialEntropy); String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy); byte[] seed = MnemonicUtils.generateSeed(mnemonic, password); ECKeyPair privateKey = ECKeyPair.create(sha256(seed)); String walletFile = generateWalletFile(password, privateKey, destinationDirectory, false); return new Bip39Wallet(walletFile, mnemonic); }
/** * Generates a BIP-44 compatible Ethereum wallet on top of BIP-39 generated seed. * * @param password Will be used for both wallet encryption and passphrase for BIP-39 seed * @param destinationDirectory The directory containing the wallet * @param testNet should use the testNet derive path * @return A BIP-39 compatible Ethereum wallet * @throws CipherException if the underlying cipher is not available * @throws IOException if the destination cannot be written to */ public static Bip39Wallet generateBip44Wallet(String password, File destinationDirectory, boolean testNet) throws CipherException, IOException { byte[] initialEntropy = new byte[16]; SecureRandomUtils.secureRandom().nextBytes(initialEntropy); String mnemonic = MnemonicUtils.generateMnemonic(initialEntropy); byte[] seed = MnemonicUtils.generateSeed(mnemonic, null); Bip32ECKeyPair masterKeypair = Bip32ECKeyPair.generateKeyPair(seed); Bip32ECKeyPair bip44Keypair = generateBip44KeyPair(masterKeypair, testNet); String walletFile = generateWalletFile(password, bip44Keypair, destinationDirectory, false); return new Bip39Wallet(walletFile, mnemonic); }
@Test public void testShouldProduceTheSameEntropy() { final byte[] expected = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15}; final byte[] actual = MnemonicUtils.generateEntropy( MnemonicUtils.generateMnemonic(expected)); Assert.assertArrayEquals(expected, actual); }
@Test public void testShouldProduceTheSameMnemonic() { final String expected = "clinic excuse minimum until indoor flower fun concert inquiry letter audit patrol"; final String actual = MnemonicUtils.generateMnemonic( MnemonicUtils.generateEntropy(expected)); Assert.assertEquals(expected, actual); }