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.
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.
This Py-bitcoin fetches the data from "Blockchain.info" API.
Demo
Here, Python coding can be done in 2 ways: -
Command prompt -
Jupyter notebook -
Now, the notebook opens in the browser as follows:
use commands in the notebook -
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)
- Generate public key
my_public_key = privtopub(my_private_key)
print("My public key: " + my_public_key)
- 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)
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)
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.
Code -
# a valid address
valid_address = "13bUJhSbGLvkLK2xHf3mUqR7eXhQ1Uzg8Z"
print(history(valid_address))
- Here, another valid address has nearly 380 transactions. For more click here.
Code -
# a valid address
valid_address = "1Nh7uHdvY6fNwtQtM1G5EZAFPLC33B59rB"
print(history(valid_address))
That's it for now.
Stay tuned for more such tutorials.
View in Github
Posted on Utopian.io - Rewarding Open Source Contributors
Come and learn how AI processes images :)
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
Suggestions
Get Noticed!
Community-Driven Witness!
I am the first and only Steem Community-Driven Witness. Participate on Discord. Lets GROW TOGETHER!
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