This recipe describes a method of generating a standard Bitcoin address from a private key.
Asset: A library of functions for handling standard Bitcoin data types.
Asset: An implementation of RIPEMD-160, written by Björn Edström.
Asset: A Python implementation of ECDSA cryptography, written by Peter Pearson.
Asset: A script that generates a standard Bitcoin address from a private key.
Asset: A Python implementation of SHA256. Author unknown.
My working definition of a standard address:
- An uncompressed single-signature Pay-To-Public-Key-Hash (P2PKH) address.
Standard addresses start with the character '1'.
This recipe assumes that you have already generated a private key (32 bytes). The following article describes one way to do this:
Recipe for generating entropy bytes using dice #2
Previous version of this recipe:
Recipe for generating a Bitcoin address
The code used in this recipe has been run in Python 2.7.5 on CentOS 7.6 and in Python 2.7.13 on Mac OS X 10.6.8. It should run successfully in Python 2.7.x.
- You must already have a Bitcoin private key (ideally 32 bytes, but can be fewer). A 32-byte private key will be 64 hex characters. Hex characters consist of the ten digit characters "0123456789" and the lower-case alphabetical characters "abcdef".
Example Bitcoin private key:
1) Create a work directory.
2) Browse to the Assets section of this article and download all the linked assets.
List of assets:
3) Move these assets into the work directory.
4) Unpack the zipped tape archive file ecdsa-0.10.tar.gz. This can be done by opening a terminal, changing directory to the work directory, and running the following command:
tar -zxvf ecdsa-0.10.tar.gz
This unpacking will produce a new directory named "ecdsa-0.10" in the work directory. The directory "ecdsa-0.10" will contain a directory named "ecdsa". Copy the "ecdsa" directory into the work directory.
5) Open the file generate_bitcoin_address_4.py in a text editor. Scroll to lines 71-77, which should be the section of text that lies between
##### START CONTROLS
##### END CONTROLS
6) Set the variable
"hex_bytes". By default, it is already set to this value.
7) Set the variable
private_key_hex_bytesto be the private key.
8) Open a terminal and change directory to the work directory.
9) Run the following command:
The output should contain the Bitcoin address that corresponds to your private key.
I've used the example private key shown earlier.
aineko:work stjohnpiano$ python generate_bitcoin_address_4.py
### START GENERATION OF BITCOIN ADDRESS
Private key (hex bytes): 15e7e195332b0aa8a684dc3be1f29dd04daa0a5434c11a8b9e6ad180df076ae4
Private key (32 hex bytes): 15e7e195332b0aa8a684dc3be1f29dd04daa0a5434c11a8b9e6ad180df076ae4
Private key (WIF): 5Hyw8omWJ9NxPXDzk6unt8kM28oanScP4msfUvt3L9nPt8wZdVf
Bitcoin address: 14gLgyVBiK7i7gtPLLYcVCAKfBuj1rL9JT
### END GENERATION OF BITCOIN ADDRESS