Transaction of BitCoins through python programming

Shivam Vatshayan
2 min readJan 23, 2022

--

Photo by Kanchanara on Unsplash

Implementation and development of blockchain as:

Step 1: Importing of cryptographic algorithms

Step 2: Create a new block listing key/value pairs of block information in a JSON object. Reset the list of pending transactions & append the newest block to the chain

Step 3: Search the blockchain for the most recent block.

Step4: Add a transaction with relevant info to the ‘blockpool’ — list of pending tx’s.

Step5: Receive one block. Turn it into a string, turn that into Unicode (for hashing). Hash with SHA256 encryption, then translate the Unicode into a hexidecimal string.

Step 6: Testing Transaction between given friends.

import hashlibimport jsonfrom time import timeclass block_chain(object):
def __init__(self):
self.chain = []
self.pending_transactions = []

self.new_block(previous_hash="1", proof=100)

# Create a new block listing key/value pairs of block information in a JSON object. Reset the list of pending transactions & append the newest block to the chain.

def new_block(self, proof, previous_hash=None):
block = {
'index': len(self.chain) + 1,
'timestamp': time(),
'transactions': self.pending_transactions,
'proof': proof,
'previous_hash': previous_hash or self.hash(self.chain[-1]),
}
self.pending_transactions = []
self.chain.append(block)

return block

#Write Functions to Create New Transactions & Get the Last Block

#Search the blockchain for the most recent block.


@property
def last_block(self):

return self.chain[-1]

# Add a transaction with relevant info to the 'blockpool' - list of pending tx's.

def new_transaction(self, sender, recipient, amount):
transaction = {
'sender': sender,
'recipient': recipient,
'amount': amount
}
self.pending_transactions.append(transaction)
return self.last_block['index'] + 1

# receive one block. Turn it into a string, turn that into Unicode (for hashing). Hash with SHA256 encryption, then translate the Unicode into a hexidecimal string.

def hash(self, block):
string_object = json.dumps(block, sort_keys=True)
block_string = string_object.encode()

raw_hash = hashlib.sha256(block_string)
hex_hash = raw_hash.hexdigest()

return hex_hash

Testing Transaction

blockchain = block_chain()
t1 = blockchain.new_transaction("Sally", "Moana", '2 BTC')
t2 = blockchain.new_transaction("Moana", "Sally", '3 BTC')
t3 = blockchain.new_transaction("Sally", "Ariana", '4 BTC')
blockchain.new_block(12345)

t4 = blockchain.new_transaction("Moana", "Bella", '1 BTC')
t5 = blockchain.new_transaction("Bella", "Laura", '2 BTC')
t6 = blockchain.new_transaction("Laura", "Moana", '3 BTC')
blockchain.new_block(6789)

print("Genesis block: ", blockchain.chain)

Output:

Genesis block:  [{'index': 1, 'timestamp': 1642625625.7074268, 'transactions': [], 'proof': 100, 'previous_hash': '1'}, {'index': 2, 'timestamp': 1642625625.707519, 'transactions': [{'sender': 'Sally', 'recipient': 'Moana', 'amount': '2 BTC'}, {'sender': 'Moana', 'recipient': 'Sally', 'amount': '3 BTC'}, {'sender': 'Sally', 'recipient': 'Ariana', 'amount': '4 BTC'}], 'proof': 12345, 'previous_hash': '0df623b0fda42e2676a0b7bdd0c38f930f729a098a504602e00e889f717bbd57'}, {'index': 3, 'timestamp': 1642625625.707684, 'transactions': [{'sender': 'Moana', 'recipient': 'Bella', 'amount': '1 BTC'}, {'sender': 'Bella', 'recipient': 'Laura', 'amount': '2 BTC'}, {'sender': 'Laura', 'recipient': 'Moana', 'amount': '3 BTC'}], 'proof': 6789, 'previous_hash': '1f2590bd7b60ec31617ca449a01d0c3c3df6a613470db3a2155f66c50c8d9a1c'}]

Hi there,

Contact for Blockchain Project Queries/Help:

Mail : vatshayan007@gmail.com

Github : https://github.com/Vatshayan/Transfer-of-Bitcoins

Computer Science Projects Help : https://www.cse-projects.com

Thanks:)

--

--

Shivam Vatshayan
Shivam Vatshayan

Written by Shivam Vatshayan

Blockchain Developer || Project Developer || Software Engineer

No responses yet