Creating a Deterministic Keychain from BIP32 master public key

4

I'm trying to create a bitcoinj determinstic key chain from a BIP32 master public key. This is the code that I have written so far:

val xpub = "xpub661MyMwAqRbcGiwzoCu93MuL6H91qANYUoYBwD4xSH8bRKJVNXsP1FUnNYiWUiu4trHbs3tavhbr4ih9bwNeVBmdQAnrA5SwSEVVCFza8dy"
val keyChainSeed : DeterministicKey =  DeterministicKey.deserializeB58(null,xpub, MainNetParams.get)
val w = Wallet.fromWatchingKey(MainNetParams.get, keyChainSeed)

but I get the following error:

No key found for absolute path M/0H.
java.lang.IllegalArgumentException: No key found for absolute path M/0H.
    at org.bitcoinj.crypto.DeterministicHierarchy.get(DeterministicHierarchy.java:94)
    at org.bitcoinj.crypto.DeterministicHierarchy.deriveChild(DeterministicHierarchy.java:152)
    at org.bitcoinj.wallet.DeterministicKeyChain.initializeHierarchyUnencrypted(DeterministicKeyChain.java:590)
    at org.bitcoinj.wallet.DeterministicKeyChain.<init>(DeterministicKeyChain.java:384)
    at org.bitcoinj.wallet.DeterministicKeyChain.<init>(DeterministicKeyChain.java:393)
    at org.bitcoinj.wallet.DeterministicKeyChain.watch(DeterministicKeyChain.java:444)
    at org.bitcoinj.wallet.DeterministicKeyChain.watch(DeterministicKeyChain.java:436)
    at org.bitcoinj.wallet.KeyChainGroup.<init>(KeyChainGroup.java:115)
    at org.bitcoinj.core.Wallet.fromWatchingKey(Wallet.java:260)
    at com.suredbits.core.bitcoin.WalletTest$$anonfun$2.apply$mcV$sp(WalletTest.scala:84)
    at com.suredbits.core.bitcoin.WalletTest$$anonfun$2.apply(WalletTest.scala:80)
    at com.suredbits.core.bitcoin.WalletTest$$anonfun$2.apply(WalletTest.scala:80)

I'm not sure why this isn't working, as bitcoinj supports BIP32. Can anyone spot what I am doing incorrectly?

Chris Stewart

Posted 2015-05-15T21:02:58.020

Reputation: 865

Answers

1

It looks like you're trying to get a key from a seed, rather than deriving a key using a path. This uses the default derivation settings, which attempt to derive a hardened key, which can't be done using an extended public key.

Try an explicit, non-hardened derivation instead:

val xpub = "xpub661MyMwAqRbcGiwzoCu93MuL6H91qANYUoYBwD4xSH8bRKJVNXsP1FUnNYiWUiu4trHbs3tavhbr4ih9bwNeVBmdQAnrA5SwSEVVCFza8dy"
val keyChainSeed : DeterministicKey =  DeterministicKey.deserializeB58(null,xpub, MainNetParams.get)
DeterministicKey key = HDKeyDerivation.deriveChildKey(
         keyChainSeed, new ChildNumber(0, false));
val w = Wallet.fromWatchingKey(MainNetParams.get, key);

P.S. You should read the BIP32 spec if you haven't already.

Nick ODell

Posted 2015-05-15T21:02:58.020

Reputation: 26 536

This did not work, I'm still getting the same error.Chris Stewart 2015-05-15T21:59:18.830