Author

Topic: How to get the master private key from a Wallet object? (Read 965 times)

HCP
legendary
Activity: 2086
Merit: 4363
From DeterministicKeyChain.java:
Quote
* A watching wallet is not instantiated using the public part of the master key as you may imagine. Instead, you
 * need to take the account key (first child of the master key) and provide the public part of that to the watching
 * wallet instead. You can do this by calling {@link #getWatchingKey()} and then serializing it with
 * {@link org.bitcoinj.crypto.DeterministicKey#serializePubB58(org.bitcoinj.core.NetworkParameters)}. The resulting "xpub..." string encodes
 * sufficient information about the account key to create a watching chain via
 * {@link org.bitcoinj.crypto.DeterministicKey#deserializeB58(org.bitcoinj.crypto.DeterministicKey, String, org.bitcoinj.core.NetworkParameters)}
 * (with null as the first parameter) and then
 * {@link DeterministicKeyChain#DeterministicKeyChain(org.bitcoinj.crypto.DeterministicKey)}.


 *

which leads to this a bit further down in DeterministicKeyChain.java:

Quote
    /**
     *

An alias for getKeyByPath(getAccountPath()).


     *
     *

Use this when you would like to create a watching key chain that follows this one, but can't spend money from it.
     * The returned key can be serialized and then passed into {@link #watch(org.bitcoinj.crypto.DeterministicKey)}
     * on another system to watch the hierarchy.


     *
     *

Note that the returned key is not pubkey only unless this key chain already is: the returned key can still
     * be used for signing etc if the private key bytes are available.


     */
    public DeterministicKey getWatchingKey() {
        return getKeyByPath(getAccountPath());
    }

Hopefully that helps... Not 100% familiar with the bitcoinj code itself... but recently I have been poking around using python to extract data from MultiBit HD wallets...
newbie
Activity: 12
Merit: 0
Hi people.
I'm trying to dump the master private key from MultibitHD to monitor from other wallet, but I can't get it right.
What I do is restore a wallet and then try to get the private key from the Wallet object:

Code:
NetworkParameters PARAMS = MainNetParams.get();
DeterministicKeyChain kc = wallet.getActiveKeychain();
DeterministicKey rk = kc.getRootKey();
String key = rk.getPrivateKeyAsWiF(PARAMS);

I'm getting an exception saying java.lang.IllegalStateException: Private key bytes not available

Any help would be greatly appreciated.

Alternatively, if someone knows how to properly derive the master key from the seed using a third party tool, that'd help!
Jump to: