public static Credentials loadBip44Credentials(String password, String mnemonic, boolean testNet) { byte[] seed = MnemonicUtils.generateSeed(mnemonic, password); Bip32ECKeyPair masterKeypair = Bip32ECKeyPair.generateKeyPair(seed); Bip32ECKeyPair bip44Keypair = generateBip44KeyPair(masterKeypair, testNet); return Credentials.create(bip44Keypair); } }
/** * 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 testGenerateBip44Wallets() throws Exception { Bip39Wallet wallet = Bip44WalletUtils.generateBip44Wallet(PASSWORD, tempDir); byte[] seed = MnemonicUtils.generateSeed(wallet.getMnemonic(), PASSWORD); Bip32ECKeyPair masterKeypair = Bip32ECKeyPair.generateKeyPair(seed); Bip32ECKeyPair bip44Keypair = Bip44WalletUtils.generateBip44KeyPair(masterKeypair); Credentials credentials = Credentials.create(bip44Keypair); assertEquals(credentials, Bip44WalletUtils.loadBip44Credentials(PASSWORD, wallet.getMnemonic())); }
private void testGenerated(String seed, String expectedPriv, String expectedPub, int[] path) { Bip32ECKeyPair pair = Bip32ECKeyPair.generateKeyPair(Numeric.hexStringToByteArray(seed)); assertNotNull(pair); pair = Bip32ECKeyPair.deriveKeyPair(pair, path); assertNotNull(pair); assertEquals(expectedPriv, Base58.encode(addChecksum(serializePrivate(pair)))); assertEquals(expectedPub, Base58.encode(addChecksum(serializePublic(pair)))); }
@SuppressWarnings("checkstyle:LineLength") @Test public void generateBip44KeyPair() { String mnemonic = "spider elbow fossil truck deal circle divert sleep safe report laundry above"; byte[] seed = MnemonicUtils.generateSeed(mnemonic, null); String seedStr = bytesToHex(seed); assertEquals("f0d2ab78b96acd147119abad1cd70eb4fec4f0e0a95744cf532e6a09347b08101213b4cbf50eada0eb89cba444525fe28e69707e52aa301c6b47ce1c5ef82eb5", seedStr); Bip32ECKeyPair masterKeypair = Bip32ECKeyPair.generateKeyPair(seed); assertEquals("xprv9s21ZrQH143K2yA9Cdad5gjqHRC7apVUgEyYq5jXeXigDZ3PfEnps44tJprtMXr7PZivEsin6Qrbad7PuiEy4tn5jAEK6A3U46f9KvfRCmD", Base58.encode(addChecksum(serializePrivate(masterKeypair)))); Bip32ECKeyPair bip44Keypair = Bip44WalletUtils.generateBip44KeyPair(masterKeypair); assertEquals("xprv9zvpunws9gusoXVkmqAXWQm5z5hjR5kY3ifRGL7M8Kpjn8kRhavkGnFLjnFWPGGS2gAUw8rP33Lmj6SwZUpwy2mn2fXRYWzGa9WRTnE8DPz", Base58.encode(addChecksum(serializePrivate(bip44Keypair)))); assertEquals("xpub6DvBKJUkz4UB21aDsrhXsYhpY7YDpYUPQwb24iWxgfMiew5aF8EzpaZpb567bYYbMfUnPwFNuRYvVpMGQUcaGPMoXUEUZKFvx7LaU5b7zBD", Base58.encode(addChecksum(serializePublic(bip44Keypair)))); }
@SuppressWarnings("checkstyle:LineLength") @Test public void generateBip44KeyPairTestNet() { String mnemonic = "spider elbow fossil truck deal circle divert sleep safe report laundry above"; byte[] seed = MnemonicUtils.generateSeed(mnemonic, null); String seedStr = bytesToHex(seed); assertEquals("f0d2ab78b96acd147119abad1cd70eb4fec4f0e0a95744cf532e6a09347b08101213b4cbf50eada0eb89cba444525fe28e69707e52aa301c6b47ce1c5ef82eb5", seedStr); Bip32ECKeyPair masterKeypair = Bip32ECKeyPair.generateKeyPair(seed); assertEquals("xprv9s21ZrQH143K2yA9Cdad5gjqHRC7apVUgEyYq5jXeXigDZ3PfEnps44tJprtMXr7PZivEsin6Qrbad7PuiEy4tn5jAEK6A3U46f9KvfRCmD", Base58.encode(addChecksum(serializePrivate(masterKeypair)))); Bip32ECKeyPair bip44Keypair = Bip44WalletUtils.generateBip44KeyPair(masterKeypair, true); assertEquals("xprv9zhLxq63By3SX5hAMKnxjGy7L18bnn7GzDQv53eYYqeRX9M82riC1dqovamttwFpk2ZkDQxgcikBQzs1DTu2KShJJqnqgx83EftUB3k39uc", Base58.encode(addChecksum(serializePrivate(bip44Keypair)))); assertEquals("xpub6DghNLcw2LbjjZmdTMKy6Quqt2y6CEq8MSLWsS4A7BBQPwgGaQ2SZSAHmsrqBVxLegjW2mBfcvDBhpeEqCmucTTPJiNLHQkiDuKwHs9gEtk", Base58.encode(addChecksum(serializePublic(bip44Keypair)))); }