Introduction
In this tutorial, we are going to introduce Bitcoin using Python. We
will be using Python’s bitcoin
library, conveniently called bitcoin.
Prerequisites
To get started with Bitcoin using Python, we need,
A Computer which can run Python programming environment
A basic knowledge of Python or another scripting language
Ability to run commands and programs from a command line program
Setup your Computer
Install Python
Download and install python from
http://www.python.org/ Make sure to download
the Python 3.x as that’s the one we are going to be using in this
tutorial.
Install bitcoin python library
After you finish installing Python, open your command line program and
execute below command to install bitcoin python library
pip install bitcoin
Hello Bitcoin - Generate a Private key
We will start with a writing a “Hello World” equivalent of Bitcoin in
Python. To write your python, you can need a code or text editor which
supports writing in ASCII format. You cannot use MS Word of Wordpad for
this. You could use Notepad, but we recommend using Atom Code
Editor, a free code editor.
Open your favorite editor and type in below code. In this code, we are
first importing the bitcoin library. We are then generating a private
key using random_key function and we are then displaying the
private key on the screen.
from bitcoin import *
my_private_key = random_key()
print(my_private_key)
Save it as a .py file and then open your command line program and run
the above program like this.
python <program location and name>
This will print out a private key.
Generate a Public Key
Next we generate a public key. We do this by passing the private key we
generated to privtopub function
from bitcoin import *
my_private_key = random_key()
my_public_key = privtopub(my_private_key)
print(my_public_key
Create a bitcoin address
In the previous programs we generated private and public keys. Now, we
are going to real bitcoin part. In the below code, we are generating a
bitcoin address by passing the public key we generated to
pubtoaddr function.
from bitcoin import *
my_private_key = random_key()
my_public_key = privtopub(my_private_key)
my_bitcoin_address = pubtoaddr(my_public_key)
print(addr)
Create a Multi-signature Address
Next, we create a multi-signature bitcoin address. Multi-signature
address is an address that is associated with more than one private key.
So, we first create 3 public and private keys. We then create a
multi-sig by passing the 3 public keys to mk_multisig_script
function. Finally, the resulting multi-sig is passed to scriptaddr
function to create the multi signature bitcoin address.
from bitcoin import *
my_private_key1 = random_key()
print('Private Key 1: ' + my_private_key1)
my_public_key1 = privtopub(my_private_key1)
print('Public Key 1: ' + my_public_key1)
my_private_key2 = random_key()
print('Private Key 2: ' + my_private_key2)
my_public_key2 = privtopub(my_private_key2)
print('Public Key 2: ' + my_public_key2)
my_private_key3 = random_key()
print('Private Key 3: ' + my_private_key3)
my_public_key3 = privtopub(my_private_key3)
print('Public Key 3: ' + my_public_key3)
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-Address: ' + my_multi_address)
View address transaction history
We can also look at pre-existing bitcoin addresses’ transactional
history. We do this passing a valid bitcoin address to function
history.
from bitcoin import *
print(history(a_vaid_bitcoin_address))
If you don’t have an address, you can look one up from
Blockchain.
Conclusion
In this tutorial, we introduced bitcoin with python. We saw how to get
setup with python for bitcoin. We saw how to generate private key,
public key and a bitcoin address. We also saw how to create a multi
signature bitcoin address and how to look at transactional history of a
bitcoin address.
If you would like to dig deeper into Bitcoin and Blockchain Concepts using Python, check it out my Video Course on Packt
Top comments (1)
After I use this tutorial, I got private key. But it returns Error : Non-base58 character.
What do i have to resolve the issue?