Learn Bitcoin-Python #1 - Installation guide, Basic commands

learn_bitcoin-python.png

Hi Guys!!...

This is a tutorial series based on "Learn Bitcoin through programming with Python".

In this we will do multiple stuffs starting from creating wallet addresses to building python-based apps.
For this, we don't have to download the entire bitcoin-blockchain. We shall be fetching the API provided by Blockchain.info.

Let's get started....

Installation Guide

Follow the steps:

  • Step-1: Install the Python directly via Anaconda
  • Step-2: Now, pip library is installed. If not, use conda install pip in your command prompt (for Windows).
  • Step-3: Use the command pip install bitcoin to install bitcoin in your system.
    Bitcoin-python installation.png
    In my case, the Bitcoin is already installed.

NOTE: The best part in this installation is that we can fetch the Bitcoin related data w/o being a full node i.e. downloading the entire Bitcoin-blockchain (approx. 150 GB size till date). Please find here for recent size.

bitcoin-blockchain size _ Dec 2017.png
This Py-bitcoin fetches the data from "Blockchain.info" API.

Demo

Here, Python coding can be done in 2 ways: -

  • Command prompt -
    bticoin python_coding in cmd.png

  • Jupyter notebook -
    bticoin python_coding in jupyter notebook.png

Now, the notebook opens in the browser as follows:
bticoin python_coding in jupyter notebook_2.png

use commands in the notebook -
bticoin python_coding in jupyter notebook_3.png

Please, find the code below:

# import bitcoin 
from bitcoin import*

# Generate private key
my_private_key = random_key()
print("My private key: " + my_private_key)

# Generate public key
my_public_key = privtopub(my_private_key)
print("My public key: " + my_public_key) 

Basic commands

Here, are a few basic commands to use Bitcoin protocol.
First, we need to import the bitcoin library like this -

# import bitcoin 
from bitcoin import*
  • Generate private key
my_private_key = random_key()
print("My private key: " + my_private_key)

bitcoin_private_key.png

  • Generate public key
my_public_key = privtopub(my_private_key)
print("My public key: " + my_public_key) 

bitcoin_public_key.png

  • Create a bitcoin wallet address. Unlike Email id - Each time bitcoin address can be generated for each transaction.
my_address = pubtoaddr(my_public_key)
print("My address: " + my_address)

bitcoin_address.png

Multi-signature wallet

This require multiple private-public keys.
Application: In organizations, suppose there is a account accessed by multiple members. Suppose, someone wants to make any transaction using that wallet address. In this case, we can use multi-signature wallet in order to unanimously decide on transactions happening from the company's wallet address. Hence, it would requrie the signature of each member. Therefore, account security is enhanced.

  • Generate private keys
# Create private keys
private_key1 = random_key()
private_key2 = random_key()
private_key3 = random_key()
  • Generate public keys
# Create public keys
public_key1 = privtopub(private_key1)
public_key2 = privtopub(private_key2)
public_key3 = privtopub(private_key3)
  • Create Multi signature wallet
my_multi_sig = mk_multisig_script(public_key1, public_key2, public_key3, 2, 3)
my_multi_address = scriptaddr(my_multi_sig)
print("My multi signature wallet address: " + my_multi_address)

bitcoin_multi-sig_address.png

History of a transaction

Through this function, we will see the bitcoin received (unit is in satoshi)

  • Here, a valid address has few transactions. For more click here.
    blockchain_info_tx_1.png

Code -

# a valid address 
valid_address = "13bUJhSbGLvkLK2xHf3mUqR7eXhQ1Uzg8Z"
print(history(valid_address))

bitcoin_tx_history_1.png

  • Here, another valid address has nearly 380 transactions. For more click here.
    blockchain_info_tx_380.png

Code -

# a valid address 
valid_address = "1Nh7uHdvY6fNwtQtM1G5EZAFPLC33B59rB"
print(history(valid_address))

bitcoin_tx_history_380.png

That's it for now.

Stay tuned for more such tutorials.

View in Github



Posted on Utopian.io - Rewarding Open Source Contributors

H2
H3
H4
3 columns
2 columns
1 column
Join the conversation now
Logo
Center