Tag: programming

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:

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:

2. Generate a private key using the random key function:

3. Display the private key on the screen:

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:

Now, with the public key, generate a bitcoin address. Do this by passing the public key that is generated to the pubtoaddr function:

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:

  1. Create three private keys:

2. Create three public keys from those private keys using the privtopub function:

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.

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:

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.