mcdallas / cryptotools

MIT License
206 stars 80 forks source link

Probably more of a question than an issue #1

Closed page1 closed 6 years ago

page1 commented 6 years ago

Hi @mcdallas I'm interested in generating addresses for Audit purposes for Ledger Nano S. Currently I'm focusing on generating segwit addresses for bitcoin. In the ledger wallet I'm able to get access to an Xpub key and a path as shown in the image below.

screen shot 2018-07-03 at 12 17 29 pm

I then am following stack exchange link

But the address that is generated doesn't seem to match my expectation. Especially when I attempt to child = extended/49./0./0. then I get an error stating "Cannot derive a hardened key from an extended public key".

Have you used this code to generate addresses based on a Ledger xpub and path? I assume I'm missing something.

Thanks for any tips/pointers

mcdallas commented 6 years ago

Hi @page1, this behaviour is part of the specification. You cannot derive an extended public key from a hardened public key (that's the whole point of hardened keys).

So in your case if you want M/49'/0'/0' you need either m/49'/0' or m/49'/0'/0' (m is for root extended private key). In other words if you have a specific Xpub you can go down the derivation tree to any other child Xpub only through non-hardened paths.

There is a stack exchange answer that elaborates a bit more.

page1 commented 6 years ago

Thanks for quick reply @mcdallas On investigation of https://iancoleman.io/bip39/ it seems I can get things working with my xpub using the bip141 tab. So I guess it's time to learn about that bip and find a package that can help generate addresses in python.

mcdallas commented 6 years ago

@page1 it is not a matter of library, the specification has the same limitations for everybody. You can derive both hardened and nonhardened keys from an Xpriv. You can derive only nonhardened keys from an Xpub.

This is the equivalent to the bip141 tab from the site you linked

>>>  xpub = Xpub.decode('xpub661MyMwAqRbcFBmVA1iTyVodRRkY3NBes4YDbdGxc4bRtJxuobVzdZ5eWr72F1K4GZPxm9fNRSsdZaNU5ZDs4fgFxbVdV6ENxshVhYZzkTa')
>>> (xpub/0/0).key.to_address('P2WPKH-P2SH')
'34sZJhhRjhKcsEfpAqTCf9UJbWmhxFLrRY'