edgecase
Author: StJohn Piano
Published: 2020-07-20
Datafeed Article 155
This article has been digitally signed by Edgecase Datafeed.
498 words - 237 lines - 6 pages




Contents


- Description
- Assets
- Notes
- Recipe
- Example






Description


This recipe describes a method of generating a standard Bitcoin address from a private key.






Assets

Asset: A library of functions for handling standard Bitcoin data types.
bitcoin_functions_2.py [paywalled]

Asset: An implementation of RIPEMD-160, written by Björn Edström.
bjorn_edstrom_ripemd160.py [paywalled]

Asset: A Python implementation of ECDSA cryptography, written by Peter Pearson.
ecdsa-0.10.tar.gz [paywalled]

Asset: A script that generates a standard Bitcoin address from a private key.
generate_bitcoin_address_4.py [paywalled]

Asset: A Python implementation of SHA256. Author unknown.
pypy_sha256.py [paywalled]



Notes

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 :
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.



Recipe


Initial conditions:

- 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:
15e7e195332b0aa8a684dc3be1f29dd04daa0a5434c11a8b9e6ad180df076ae4



1) Create a work directory.


2) Browse to the Assets section of this article and download all the linked assets.

List of assets:
- bitcoin_functions_2.py
- bjorn_edstrom_ripemd160.py
- ecdsa-0.10.tar.gz
- generate_bitcoin_address_4.py
- pypy_sha256.py



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

and
##### END CONTROLS



6) Set the variable
private_key_type
to
"hex_bytes"
. By default, it is already set to this value.


7) Set the variable
private_key_hex_bytes
to be the private key.


8) Open a terminal and change directory to the work directory.


9) Run the following command:
python generate_bitcoin_address_4.py


The output should contain the Bitcoin address that corresponds to your private key.








Example


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