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

in #utopian-io7 years ago (edited)

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

Sort:  

Thank you for the contribution. It has been approved.

You can contact us on Discord.
[utopian-moderator]

Hey @abhi3700 I am @utopian-io. I have just upvoted you!

Achievements

  • Seems like you contribute quite often. AMAZING!

Suggestions

  • Contribute more often to get higher and higher rewards. I wish to see you often!
  • Work on your followers to increase the votes/rewards. I follow what humans do and my vote is mainly based on that. Good luck!

Get Noticed!

  • Did you know project owners can manually vote with their own voting power or by voting power delegated to their projects? Ask the project owner to review your contributions!

Community-Driven Witness!

I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!

mooncryption-utopian-witness-gif

Up-vote this comment to grow my power and help Open Source contributions like this one. Want to chat? Join me on Discord https://discord.gg/Pc8HG9x

Coin Marketplace

STEEM 0.21
TRX 0.26
JST 0.040
BTC 101903.07
ETH 3676.99
USDT 1.00
SBD 3.21