From 1ff5d05200c75ba2c351cd1b75b675a4c7b2748b Mon Sep 17 00:00:00 2001 From: Mike Hearn Date: Wed, 26 Mar 2014 20:29:38 +0100 Subject: [PATCH] HD wallets: add a Wallet.getKeyChainSeed method. --- .../java/com/google/bitcoin/core/Wallet.java | 16 ++++++++++++++++ .../java/com/google/bitcoin/core/WalletTest.java | 6 ++++++ 2 files changed, 22 insertions(+) diff --git a/core/src/main/java/com/google/bitcoin/core/Wallet.java b/core/src/main/java/com/google/bitcoin/core/Wallet.java index 32948c59..f1a18d42 100644 --- a/core/src/main/java/com/google/bitcoin/core/Wallet.java +++ b/core/src/main/java/com/google/bitcoin/core/Wallet.java @@ -573,6 +573,22 @@ public class Wallet extends BaseTaggableObject implements Serializable, BlockCha return findKeyFromPubKey(pubkey) != null; } + /** + * Returns the immutable seed for the current active HD chain. + * @throws com.google.bitcoin.core.ECKey.MissingPrivateKeyException if the seed is unavailable (watching wallet) + */ + public DeterministicSeed getKeyChainSeed() { + lock.lock(); + try { + DeterministicSeed seed = keychain.getActiveKeyChain().getSeed(); + if (seed == null) + throw new ECKey.MissingPrivateKeyException(); + return seed; + } finally { + lock.unlock(); + } + } + //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // // Serialization support diff --git a/core/src/test/java/com/google/bitcoin/core/WalletTest.java b/core/src/test/java/com/google/bitcoin/core/WalletTest.java index 18c9e437..57c3d9b0 100644 --- a/core/src/test/java/com/google/bitcoin/core/WalletTest.java +++ b/core/src/test/java/com/google/bitcoin/core/WalletTest.java @@ -98,6 +98,12 @@ public class WalletTest extends TestWithWallet { super.tearDown(); } + @Test + public void getSeedAsWords1() { + // Can't verify much here as the wallet is random each time. We could fix the RNG for the unit tests and solve. + assertEquals(12, wallet.getKeyChainSeed().toMnemonicCode().size()); + } + @Test public void basicSpending() throws Exception { basicSpendingCommon(wallet, myAddress, new ECKey().toAddress(params), false);