How to derive segwit addresses from an extended public key?

1

I tried to use the following code:

    DeterministicKey key = DeterministicKey.deserializeB58(null , "xpub...", MAINNET);



    DeterministicHierarchy hierarchy = new DeterministicHierarchy(key)
;
    DeterministicKey chain = hierarchy.deriveChild(key.getPath(), false, false, new ChildNumber(0, false));
    DeterministicKey index = hierarchy.deriveChild(chain.getPath(), false, false, new ChildNumber(0, false));
    SegwitAddress segwitAddress = SegwitAddress.fromHash(MAINNET, index.getPubKeyHash());

But it doesn't return correct address. Any help here

Black Panther

Posted 2018-06-11T18:57:22.830

Reputation: 35

Answers

1

Zpub's would be needed for native segwit addresses. And current bitcoinj master branch does not support Zpub, it support xpub and xpub's would always derive to legacy addresses. Give a try to Segwit branch that may helps. Also take a look at this

Zombie

Posted 2018-06-11T18:57:22.830

Reputation: 528

0

Your problem seems similar to the given link:

https://groups.google.com/forum/#!topic/bitcoinj/F7smdvlCbnE

Try to use bitcoinjs for the same extended public key. or try the provided solutions.

Hope this will work in your case.

FairyRosie

Posted 2018-06-11T18:57:22.830

Reputation: 86

how to use bitcoinjs for extended public key in java?Black Panther 2018-06-12T11:42:17.147

0

You can use this Python library.

from btctools import Xpub

>>> extended = Xpub.decode('xpub123...')
>>> child = extended/0/0
>>> child.key.to_address('P2WPKH')
'bc1q0xedk29m3z0ja0l5xl2avclrhkd8y9z5au5wyt'

Mike D

Posted 2018-06-11T18:57:22.830

Reputation: 1 571

how can use this python lib in java ?Black Panther 2018-06-12T20:25:38.557