How to Code a Blockchain in 6 Steps

Step 1 / Put Some Data in a Block

#Start with a basic block
transaction_1="Wallet#1 paid 1 bitcoin to Wallet#2"
genesis_block=(transaction_1)
print(genesis_block)

Step 2 / Prepare the Chains

a hash is the key ingredient to a blockchain

import hashlib # the library with the hash function we need# Create our hash function
def bit_hash(data):
string_data=str(data) # always use a string
formatted_data= string_data.encode('utf-8') # encode for hashing
bit_hash = hashlib.sha256(formatted_data).hexdigest() # hash it
return bit_hash # function outputs the hash

Step 3 / Add Chain Links to Our Block

#Create the genesis block hash
genesis_block_hash = bit_hash((0,transaction_1))
print(genesis_block_hash)
#Recreate the genesis block - this time with hashes
genesis_block=(0,transaction_1,genesis_block_hash)
print(genesis_block)

Step 4 / Create the Next Block

# Create the transaction for block 2
transaction_2 = "Wallet#1 paid 2 bitcoin to Wallet#2"
# Create the block 2 hash
block_2_hash = bit_hash((genesis_block_hash, transaction_2))
# Create block 2
block_2 = (genesis_block_hash,transaction_2,block_2_hash)
print(block_2)

Step 5 / Create a Function for Creating New Blocks

#Create a function that creates a new block and add a property to tell us what number block it is
def create_block(prior_block_hash,prior_block_number,transaction_data):
block_number = prior_block_number + 1
block_hash = bit_hash((prior_block_hash,block_number,transaction_data))
new_block= (prior_block_hash,block_number,transaction_data,block_hash)
return new_block

Step 6 / Use the Function to Print Blocks

#Print a genesis block
genesis_block = create_block(0,-1,"Wallet#1 paid 1 bitcoin to Wallet#2") # block numbering to start at zero
print(genesis_block)
#Print 3 more blocks
prior_block=genesis_block
for i in range(3):
prior_block_hash=prior_block[3] # 3rd element of prior block
prior_block_number=prior_block[1] # 1st element of prior block
next_block=create_block(prior_block_hash,prior_block_number,"Wallet#1 paid " + str(i+2)+" bitcoin to Wallet#2") # pay 1 more bitcoin in each block
print(next_block)
prior_block=next_block

Great — the blocks are chaining together. The hash of each block becomes the prior hash of the next block.

Conclusion

--

--

--

The guy who thinks all life’s answers are in the metadata, hiding. When I do find answers, I share them here | Chief Data Officer

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Custom UIView with XIB file

The 6 W’s of Running a Disaster Simulation

Protected methods in ruby

Use Cases of AWS SQS

Generate Graph API Access Token in SharePoint Provided hosted app

We Helped 42 Developers Get $772,441 In Salary Raises And Promotions, Here Is What We Learned

Message Queues in AWS SQS

HOW TO BUY $APP TOKEN STEP BY STEP USING TRUSTWALLET.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ari Cohen

Ari Cohen

The guy who thinks all life’s answers are in the metadata, hiding. When I do find answers, I share them here | Chief Data Officer

More from Medium

Classifying and Predicting Stock Market States Using HMM and XGBoost

Most Asked Interview Questions in Python/Django

python django illustration — interview questions

How to Build a Simple Cross platform Mobile App Using the Python Library Kivy

Developing a Money-Making Telegram Bot on Python — Pt 2.