mcdallas / cryptotools

MIT License
206 stars 80 forks source link

How to derive all possible segwit addresses from xpub #28

Closed academe-01 closed 2 years ago

academe-01 commented 2 years ago

Hello, I need derive all possible addresses from Ledger xPub.

I've tried this:

from cryptotools import Xpub

MY_XPUB = 'xpub-as-shown-in-ledger'

key = Xpub.decode(MY_XPUB)

pubkey0 = key/0/0
print(pubkey0.address('P2WPKH'))  # Tell the wallet that you want a P2WPKH address

but it gives only 1 address. Any idea ?

Thanks.

mcdallas commented 2 years ago

hey @academe-01 just change the key/0/0 to key/0/1 to get the second address etc. Or just do a for loop to get the first X addresses

academe-01 commented 2 years ago

hey @mcdallas ! thanks for help! any idea if my ledger show "freshAddressPath": "49'/0'/0'/0/1" and it's segwit from what range should i start loop?

big thanks!

mcdallas commented 2 years ago

You should start from /0/1 since that’s the path ledger shows. For BIP49 addresses (starting with 3) use .address(‘ P2WPKH-P2SH’) instead of ‘P2WPKH` in your example