fortesp / bitcoinaddress

Bitcoin Wallet Address Generator
https://pypi.org/project/bitcoinaddress/
MIT License
144 stars 51 forks source link

How to use it programatically? #24

Open hasanparasteh opened 1 year ago

hasanparasteh commented 1 year ago

This library print the address data very pretty but when you want to use it's API to get some information out of it and get different addresses it become really frustrating..Please review and setup some helper functions to retrieve each address accordingly

wpolk commented 4 months ago

Yeah I need to print 100 different keys and addresses and it only prints the same ones each time I run it. How do I get it to print different keys so I don't have to run the python file 100 different times?

wpolk commented 4 months ago

SOLUTION: Use wallet = Wallet() every time you need new keys. Because whenever you get a private key using a function like wallet.key.__dict__['mainnet'].__dict__['wif'] for example, it is still referencing that same wallet variable that was declared earlier.

For example, this is my script:

from bitcoinaddress import Wallet

privs = []
pubs = []
addrs = []

i = -1
for i in range(100):
    wallet = Wallet()

    privs.append(wallet.key.__dict__['mainnet'].__dict__['wif'])
    pubs.append(wallet.address.__dict__['pubkey'])
    addrs.append(wallet.address.__dict__['mainnet'].__dict__['pubaddr1'])

for i in privs:
    print(i)
print()
for i in pubs:
    print(i)
print()
for i in addrs:
    print(i)

(At first i had the wallet = Wallet() before the for loop)

@hasanparasteh You need to use the last part of the documentation: https://github.com/fortesp/bitcoinaddress?tab=readme-ov-file#example-4---check-attributes to get specific data from the wallet. You can also use for loops to get multiple addresses.