edgecase
For most of its being, mankind did not know what things are made of, but could only guess. With the growth of worldken, we began to learn, and today we have a beholding of stuff and work that watching bears out, both in the workstead and in daily life.
~ Poul Anderson
Author: StJohn Piano
Published: 2018-11-14
Datafeed Article 69
This article has been digitally signed by Edgecase Datafeed.
524 words - 200 lines - 5 pages




Parts

- 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.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 Python implementation of SHA256.
pypy_sha256.py [paywalled]

Asset: A script that generates a standard Bitcoin address from a private key.
generate_bitcoin_address_3.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

This recipe was originally published in the article Creating and signing a standard raw Bitcoin transaction: Iteration #2, in the section Recipes For Using Various Downloadable Assets, in the part "Recipe 2: generate_bitcoin_address_3.py". The recipe shown here has been edited for republication.



Recipe

Initial conditions:
- You must have Python 2.7.x installed on your computer. These code assets were developed / tested under Python 2.7.13 running on Mac OS X 10.6.8 (Snow Leopard), and should run successfully on other versions of Python 2.7.
- You must already have a Bitcoin private key (ideally 32 bytes). A 32-byte private key will be 64 hex characters long. When used as a control value in this recipe, it must be a 64-character string that consists only of hex characters (the ten digit characters "0123456789" and the lower-case alphabetical characters "abcdef").

1) Create a work directory.

2) Browse to the Assets part of this article and download all the linked assets. List of assets:
- bitcoin_functions.py
- bjorn_edstrom_ripemd160.py
- ecdsa-0.10.tar.gz
- generate_bitcoin_address_3.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 should produce a new directory named "ecdsa-0.10" in the work directory. The directory "ecdsa-0.10" should contain a directory named "ecdsa". Copy the "ecdsa" directory into the work directory.

5) Open the file generate_bitcoin_address_3.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"
.

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_3.py


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



Example

aineko:work stjohnpiano$ python generate_bitcoin_address_3.py


### START GENERATION OF BITCOIN ADDRESS

Private key (hex bytes): a26e15954d2dafcee70eeaaa084eab8a4c1a30b0f71a42be4d8da20123bff121
Private key (32 hex bytes): a26e15954d2dafcee70eeaaa084eab8a4c1a30b0f71a42be4d8da20123bff121
Private key (WIF): 5K3pdySk3c2jz3i7jiULrMZN5JSUSEEF1M1bbetRRVAbE2vdEv9
Bitcoin address: 1AGygbyEFYduWkkmZbbvirgS9kuBBMLJCP

### END GENERATION OF BITCOIN ADDRESS