Programming Bitcoin with Python
Learn how to generate private and public keys, and how to create a multi-signature bitcoin address in this tutorial with python.
In order to get started with bitcoin using Python, you must install Python 3.x and the bitcoin Python library called Pi Bitcoin tools in the system.
The Pi Bitcoin tools library
To install the Pi Bitcoin tools library, open the command-line program and execute the following command:
pip install bitcoin
The best thing about this library is that it does not need to have a bitcoin node on your computer in order for you to start using it. It connects to the bitcoin network and pulls data from places such as Blockchain.info.
For a start, write the equivalent of a Hello World program for bitcoin in Python. In the hello_bitcoin.py script, the demonstration of a new bitcoin address is created using Python. Go through the following steps to run the program:
1. Import the bitcoin library:
#!/usr/bin/env python ''' Title - Hello Bitcoin This program demonstrates the creation of - private key, - public key - and a bitcoin address. ''' # import bitcoin from bitcoin import *
2. Generate a private key using the random key function:
my_private_key = random_key()
3. Display the private key on the screen:
print("Private Key: %s\n" % my_private_key)
How to generate private keys and public keys
With the private key, a public key is generated. Perform this step by passing the private key that was generated to the privtopub function, as shown here:
# Generate Public Key my_public_key = privtopub(my_private_key) print("Public Key: %s\n" % my_public_key)
Now, with the public key, generate a bitcoin address. Do this by passing the public key that is generated to the pubtoaddr function:
# Create a bitcoin address my_bitcoin_address = pubtoaddr(my_public_key) print("Bitcoin Address: %s\n" % my_bitcoin_address)
The following screenshot shows the private key, public key, and the bitcoin address that is generated:
Note that a bitcoin address is a single-use token. Just as people use email addresses to send and receive emails, you can use this bitcoin address to send and receive bitcoins. Unlike email addresses, however, people have several bitcoin addresses, and it is a must to use a unique address for every transaction.
Creating a multisignature bitcoin address
A multisignature address is an address that is associated with more than one private key. In this section, you’ll create three private keys. Multisignature addresses are useful in organizations where no single individual is trusted with authorising the spending of bitcoins.
Go through the following steps to create a multisignature bitcoin address:
- Create three private keys:
#!/usr/bin/env python ''' Title - Create multi-signature address This program demonstrates the creation of Multi-signature bitcoin address. ''' # import bitcoin from bitcoin import * # Create Private Keys my_private_key1 = random_key() my_private_key2 = random_key() my_private_key3 = random_key() print("Private Key1: %s" % my_private_key1) print("Private Key2: %s" % my_private_key2) print("Private Key3: %s" % my_private_key3) print('\n')
2. Create three public keys from those private keys using the privtopub function:
# Create Public keys my_public_key1 = privtopub(my_private_key1) my_public_key2 = privtopub(my_private_key2) my_public_key3 = privtopub(my_private_key3) print("Public Key1: %s" % my_public_key1) print("Public Key2: %s" % my_public_key2) print("Public Key3: %s" % my_public_key3) print('\n')
3. After generating the public keys, create the multisig by passing the three public keys to the mk_ multi-sig_script function. The resulting multisig is passed to the addr script function to create the multisignature bitcoin address.
# Create Multi-signature address my_multi_sig = mk_multisig_script(my_private_key1, my_private_key2, my_private_key3, 2,3) my_multi_address = scriptaddr(my_multi_sig) print("Multi signature address: %s" % my_multi_address)
4. Print the multisignature address and execute the script. The following screenshot shows the output for the multisig bitcoin address:
You can also look at the preexisting bitcoin addresses’ transactional history. You’ll need to first get a valid address from Blockchain.info.
The following screenshot shows the copied address of a bitcoin block:
Pass the copied address to the history function, as shown in the following code, along with the output to get the history of the bitcoin address, including the transactional information:
!/usr/bin/env python ''' Title - Bitcoin Transaction History This program demonstrates listing history of a bitcoin address. ''' # import bitcoin from bitcoin import * #View address transaction history a_valid_bitcoin_address = '329e5RtfraHHNPKGDMXNxtuS4QjZTXqBDg' print(history(a_valid_bitcoin_address))
Hope you found this article interesting. To learn more interesting stuff about bitcoins and Python, you can explore Hands-On Bitcoin Programming with Python. Written with an easy-to-understand approach in mind, Hands-On Bitcoin Programming with Python takes you through numerous practical examples to teach you to build software for mining and create bitcoin using Python.