Git Product home page Git Product logo

bitcoinaddress's Introduction

Bitcoin Address

Bitcoin Wallet Address Generator

This is a simple Bitcoin non-deterministic wallet address generator coded in Python 3. It generates a Private Key in different formats (hex, wif and compressed wif) and corresponding Public Addresses, raw, P2WPKH addresses starting with prefix 1, P2SH addresses starting with prefix 3 as part of Segwit soft fork and Bech32 addresses with prefix bc1 P2WPKH and P2WSH.

Installation

pip install bitcoinaddress

Usage

Example 1 - Mainnet
from bitcoinaddress import Wallet

wallet = Wallet()
print(wallet)
Output:
Private Key HEX: 03902e4f09664bc177fe4e090dcd9906b432b50f15fb6151984475c1c75c35b6
Private Key WIF: 5HqrbgkWPqBy6dvCE7FoUiMuiCfFPRdtRsyi6NuCM2np8qBZxq5
Private Key WIF compressed: KwLdv6T2jmhQbswnYrcL9KZHerTpVyjozp1JNjfP5QuD3GchCwCc 
            
Public Key: 04c5389a31ce6149c28ba20d14db8540b2319e5a65000a2919fbf7a6296e7840b53f883a9483fb7f2b43f3eacd857c904d1b70ecc168571b64d8f1ab82b57eea88 
Public Key compressed: 02c5389a31ce6149c28ba20d14db8540b2319e5a65000a2919fbf7a6296e7840b5

Public Address 1: 1Bu6YxH64nfvhdDsYNEP8PftoBMqgusdPS   
Public Address 1 compressed: 18i5PtPisxbGiGGEviW7HPcnfNPmcsscwH   
Public Address 3: 38dRrGx5YbrnRWuWcJv5i2XHjYUnHE2wvv  
Public Address bc1 P2WPKH: bc1q2jxe5azr6zmhk3258av7ul6cqtu4eu4mps8f4p    
Public Address bc1 P2WSH: bc1qdveuf0egtfdnd2fnsp0lzfukn2e58czf8323ky6xt8ydew4ecfcqv3ettx  
Example 2 - Testnet
from bitcoinaddress import Wallet

wallet = Wallet(testnet=True)
print(wallet)
Output:
Private Key HEX: 064f8f0bebfa2f65db003b56bc911535614f2764799bc89091398c1aed82e884
Private Key WIF: 91dhN38UTmqGtd3zG1GnDdnyivAP5LnWJQyyj7V7pqthirHAj4X
Private Key WIF compressed: cMny9rPzDAt58r8BjECeamPwN1eQSAKrKrrVNsd78AoCjcWxuVym 
            
Public Key: 04f7a01e30388dea9673db8cdb48b985441db785382efbcecc05abac079a6304818a907f886b0d0518e345a0288a6f1e09072f2b11d4ccb75bc67ec6c71dfef800 
Public Key compressed: 02f7a01e30388dea9673db8cdb48b985441db785382efbcecc05abac079a630481

Public Address 1: mwdHSyBBHMbcy8rogvzssvDrGyffRo3amQ   
Public Address 1 compressed: n4VzeGfAyZGR7xCXiKmABvnKXojTFJrmKH   
Public Address 3: 2MtJ3jPSD2AYgbF25fq9cm1aUCPhSmWUMcJ  
Public Address bc1 P2WPKH: tb1qlsw8qqe2aa2avzn9t9nsfjy8kwwgarwcfvfqlh    
Public Address bc1 P2WSH: tb1qp53qkcsth8ffuvr00cnlg5hde03aszzeq7y0layklhd4nwkxzejs99tlh3
Example 3 - Import Private Key
from bitcoinaddress import Wallet

wallet = Wallet('5HqrbgkWPqBy6dvCE7FoUiMuiCfFPRdtRsyi6NuCM2np8qBZxq5')
print(wallet)
Output:
Private Key HEX: 03902e4f09664bc177fe4e090dcd9906b432b50f15fb6151984475c1c75c35b6
Private Key WIF: 5HqrbgkWPqBy6dvCE7FoUiMuiCfFPRdtRsyi6NuCM2np8qBZxq5
Private Key WIF compressed: KwLdv6T2jmhQbswnYrcL9KZHerTpVyjozp1JNjfP5QuD3GchCwCc 
            
Public Key: 04c5389a31ce6149c28ba20d14db8540b2319e5a65000a2919fbf7a6296e7840b53f883a9483fb7f2b43f3eacd857c904d1b70ecc168571b64d8f1ab82b57eea88 
Public Key compressed: 02c5389a31ce6149c28ba20d14db8540b2319e5a65000a2919fbf7a6296e7840b5

Public Address 1: 1Bu6YxH64nfvhdDsYNEP8PftoBMqgusdPS   
Public Address 1 compressed: 18i5PtPisxbGiGGEviW7HPcnfNPmcsscwH   
Public Address 3: 38dRrGx5YbrnRWuWcJv5i2XHjYUnHE2wvv  
Public Address bc1 P2WPKH: bc1q2jxe5azr6zmhk3258av7ul6cqtu4eu4mps8f4p    
Public Address bc1 P2WSH: bc1qdveuf0egtfdnd2fnsp0lzfukn2e58czf8323ky6xt8ydew4ecfcqv3ettx
Example 4 - Check attributes
from bitcoinaddress import Wallet

wallet = Wallet()
print(wallet.key.__dict__)
print(wallet.key.__dict__['mainnet'].__dict__)
print(wallet.key.__dict__['testnet'].__dict__)
print(wallet.address.__dict__)
print(wallet.address.__dict__['mainnet'].__dict__)
print(wallet.address.__dict__['testnet'].__dict__)

License and other

This software is distributed under the terms of the MIT License. See the file 'LICENSE' in the root directory of the present distribution, or http://opensource.org/licenses/MIT.

Bech32 address scripts source from https://github.com/sipa/bech32/tree/master/ref/python

bitcoinaddress's People

Contributors

bjorn-qb avatar fortesp avatar jolly-pirate avatar streamofstars avatar teimoon avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

bitcoinaddress's Issues

Need explanation on Wallet()

Hi,
first of all, thank you very much for your job, It's fantastic.

Is it possible to use Wallet() with a defined private key in string format?
Can you provide a quickly example for this?

Thank you in advance.

Openssl removed ripemd160 - issues on Ubuntu22

Type "help", "copyright", "credits" or "license" for more information.
>>> from bitcoinaddress import Wallet
>>> wallet = Wallet()
Traceback (most recent call last):
  File "/usr/lib/python3.10/hashlib.py", line 160, in __hash_new
    return _hashlib.new(name, data, **kwargs)
ValueError: [digital envelope routines] unsupported

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/.local/lib/python3.10/site-packages/bitcoinaddress/wallet.py", line 16, in __init__
    self.address = Address.of(self.key)
  File "/home/user/.local/lib/python3.10/site-packages/bitcoinaddress/address.py", line 88, in of
    address.generate()
  File "/home/user/.local/lib/python3.10/site-packages/bitcoinaddress/address.py", line 94, in generate
    self.mainnet.generate_publicaddress1()
  File "/home/user/.local/lib/python3.10/site-packages/bitcoinaddress/address.py", line 38, in generate_publicaddress1
    self.pubaddr1 = self.instance._generate_publicaddress1(Address.MainNet.PREFIX_A,
  File "/home/user/.local/lib/python3.10/site-packages/bitcoinaddress/address.py", line 107, in _generate_publicaddress1
    m = prefix_a + hash160(p).digest()
  File "/home/user/.local/lib/python3.10/site-packages/bitcoinaddress/util.py", line 20, in hash160
    return ripemd(hashlib.sha256(v).digest())
  File "/home/user/.local/lib/python3.10/site-packages/bitcoinaddress/util.py", line 24, in ripemd
    r = hashlib.new('ripemd160')
  File "/usr/lib/python3.10/hashlib.py", line 166, in __hash_new
    return __get_builtin_constructor(name)(data)
  File "/usr/lib/python3.10/hashlib.py", line 123, in __get_builtin_constructor
    raise ValueError('unsupported hash type ' + name) ```
    ```

ValueError: unsupported hash type ripemd160

Kernel: 5.15.0-33
Ubuntu 22.04 LTS
OpenSSL: OpenSSL 3.0.2 15 Mar 2022 (Library: OpenSSL 3.0.2 15 Mar 2022)
https://www.openssl.org/news/changelog.html

The low-level MD2, MD4, MD5, MDC2, RIPEMD160 and Whirlpool digest functions have been deprecated.

I would need some advice here ?

How to use it programatically?

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

'seed' is not stored as instance variable in __init__ method of Key class

First of all, thank you for sharing this library.

I noticed that the __init__ method of the Key class does not store the optionally passed seed value as an instance variable:

def __init__(self, seed=None):

Also, the generate() method does not try to use self.seed in case it was defined upon instantiation.

Consequently, when you do:

key = Key('some seed')
key.generate()

It will generate from a random seed instead of the value passed upon instantiation. For example here:

self.key.generate()

To work, it would need to look like this (currently):

key = Key()
key.generate('some seed')

(Unrelated: it would be interesting if the Address class would provide convenience functions to convert one format to another without having a private key. The use case would be: I have a bc1 address and want to know its base58 version starting with 1 etc. This means one can have an Address instance without having a private key, but at least one version of the public key.)

Actually it's not issue, it's 2 questions

First thank you so much for the library, really it's big effort from you:

Q1: Where is the wif or wifc for eth {bc1}. your code creates BTC uncompresed and compresed and privatekey for every one
is there is privatekey for bc1 address?

Q2. is this random number can bring real existing BTC address with balance ?

Q3. how can I have your package 2 time inside my pc. I need to edit the library files and keep original one?

Thank you again

Best Regards,

Need help

Good day,
would you mind providing a small example, how do I generate all possible BTC public addresses (P2PKH, P2SH, Bech32) using existing WIF private key, in string format?

p2wsh addresses

I'm looking for to double check some of my python code outputs and find that your outputs for p2wsh doesnt match this sample which I was guided:

https://bc-2.jp/tools/bech32demo/index.html

Could You check your metod for this and explain how there is difference.

Thanks in advance.

Add Hash160

hash160 (20byte) format is very important and missing from output.

Output is wrong

Instead of

Public Address bc1 P2WPKH: bc1qdveuf0egtfdnd2fnsp0lzfukn2e58czf8323ky6xt8ydew4ecfcqv3ettx    
Public Address bc1 P2WSH: bc1q2jxe5azr6zmhk3258av7ul6cqtu4eu4mps8f4p

it should be

Public Address bc1 P2WSH: bc1qdveuf0egtfdnd2fnsp0lzfukn2e58czf8323ky6xt8ydew4ecfcqv3ettx    
Public Address bc1 P2WPKH: bc1q2jxe5azr6zmhk3258av7ul6cqtu4eu4mps8f4p

Why all generated address in range 00 and how to change range to 0000

Could you help me please
Why all generated address hex start with 00? How to change it to start for example 0000 or 000000
I tried to to change key, address, key, seed but no success for making this change
Hex 160:

00C32DE061453296E71E9E9E9D5636E7E7FEE272C7
00BB9EB37303709E92C7D121F8751ECD97B5D407EB
00DFD584C7534C1B8E0BB41AF2C64DC2EE479A7895

Base58:

1Jo1ixjbSN9nzdavauqFNCsNPMBxANyErF
1J73UcLXkeTYXDq9qycDMwwUf1EVbNgh8Y
1MQXUKHP29jVBq6rrmiQzKv6w1kfcBWtdE

Thank you

Wallet generator failing

Cloned and just running the example immediately fails:

In [11]: from bitcoinaddress import Wallet

In [12]: wallet = Wallet()

AttributeError Traceback (most recent call last)
in
----> 1 wallet = Wallet()

/usr/lib/python3.9/site-packages/bitcoinaddress/wallet.py in init(self, hash_or_seed, testnet)
13 def init(self, hash_or_seed=None, testnet=False):
14 if hash_or_seed is None: hash_or_seed = Seed()
---> 15 self.key = Key.of(hash_or_seed)
16 self.address = Address.of(self.key)
17 self.testnet = testnet

/usr/lib/python3.9/site-packages/bitcoinaddress/key/key.py in of(obj)
57 key = Key()
58 if isinstance(obj, Seed):
---> 59 key._from_seed(obj)
60 else:
61 try:

/usr/lib/python3.9/site-packages/bitcoinaddress/key/key.py in _from_seed(self, seed)
77 self._generate_digest()
78 self._generate_hex()
---> 79 self.mainnet.generate_wif()
80 self.mainnet.generate_wif_compressed()
81 self.testnet.generate_wif()

/usr/lib/python3.9/site-packages/bitcoinaddress/key/key.py in generate_wif(self)
28
29 def generate_wif(self):
---> 30 self.wif = self.instance._generate_wif(Key.MainNet.PREFIX)
31
32 def generate_wif_compressed(self):

/usr/lib/python3.9/site-packages/bitcoinaddress/key/key.py in _generate_wif(self, prefix)
110 digest = prefix + self.digest
111 c = checksum(digest)
--> 112 return base58.b58encode(digest + c).decode('utf-8')
113
114 def _generate_wif_compressed(self, prefix, suffix):

AttributeError: 'str' object has no attribute 'decode'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.