How can I get all the addresses associated with my public key with bitcoinlib?

1.3k Views Asked by At

I want to get all the addresses associated with my extended public key. I found how to do this on bitcoinlib's docs:

Initialize an Address object. Specify a public key, redeemscript or a hash.

>>> addr = Address('03715219f51a2681b7642d1e0e35f61e5288ff59b87d275be9eaf1a5f481dcdeb6', encoding='bech32', script_type='p2wsh')
>>> addr.address
'bc1qaehsuffn0stxmugx3z69z9hm6gnjd9qzeqlfv92cpf5adw63x4tsfl7vwl'

However I'm having issues getting Address() function to work, since my code:

from bitcoin import *  # using import * because not sure what else to import?
master = Address("my extended pub key", encoding='bech32', script_type='p2wpkh')
print(master.address)

Produces this error:

NameError: name 'Address' is not defined
2

There are 2 best solutions below

5
On BEST ANSWER
from bitcoin import *

Using import * is not a good practice. Not only does this import stuff that you don't need and you don't know what exactly it imports, it also does not import what you do need in this case.

According to the documentation you have linked, you need to use this import statement:

from bitcoinlib.keys import Address
0
On

If you want to use the bitcoinlib library and check the addresses associated with your extended public key, you should do as follows:

pip install bitcoinlib

After that, using the Python binary where this package was installed, open a Python terminal and say:

from bitcoinlib.keys import Address

Needless to say it is recommendable to use a virtualenv and also that import * is not a very good idea. See the following Q&A to get more details on it: