Published in


Cryptocurrency Development in Python Using Blockchain Lists (Part 1)

Image by WorldSpectrum from Pixabay
Image by WorldSpectrum from Pixabay
  • BlockValue
  • main
  • getPreviousHash()
  • returnBlock()
  • setHash()
  • runData()
  • mine()
  • newTransactionData()
  • verifyChain()
import time
import CryptoFunctions
diff = 3
nonce = 0
number = 1
arrayOfValues = []
currentArray = []
currentIndex = 0
previous_hash = "0" *64
senderData = ["Sender_One", "Sender_Two", "Sender_Three", "Sender_Four", "Sender_Five"]recipientData = ["Recieve_One", "Recieve_Two", "Recieve_Three", "Recieve_Four", "Recieve_Five"]amountData = ["1", "2", "3", "4", "5"]transactionIndex = 0
from hashlib import sha256
class BlockValue:def __init__()def new_data()
def __init__(self, index, transactions, timestamp, previous_hash, number, transactionIndexUpdated):def new_data(self):
  • The index argument will be used to track the index of the larger array, “arrayOfValues.”
  • The transactions are a placeholder but are an array that will be composed of the current sender, recipient, and numeric data for each block.
  • The timestamp argument will be used to identify the time of formation.
  • The previous_hash argument is used for verifying a continuous chain. The previous_chain of the next chain should be the same hash as the current block hash.
  • The number argument is used to identify which block that is currently being referenced, not in terms of an index value but rather numerically.
  • The transactionIndexUpdated argument is used to identify which index the transaction data is being pulled from.
def __init__(self, index, transactions, timestamp, previous_hash, number, transactionIndexUpdated):self.index = index
self.transactions = transactions
self.timestamp = timestamp
self.previous_hash = previous_hash
self.nonce = 0
self.number = number
self.transactionIndex = transactionIndexUpdated
def new_data(self):
timestamp = time.time()
transactions = CryptoFunctions.newTransactionData(
amountData[self.transactionIndex], [])
currentArray, number = CryptoFunctions.runData(self.index,
transactions, timestamp, previous_hash, nonce, self.number,

return currentArray, number
  • The timestamp function will be used to retrieve the time in which the block was fully formulated.
  • The transactions will retrieve the current transaction data from the senderData, recipientData, and the amountData using the “newTransactionData” function.
def getPreviousHash(currentIndex, arrayOfValues):
if (currentIndex == 0):
return "0" * 64
array = arrayOfValues[currentIndex - 1]
return array[1]
def returnBlock(arrayIndex, arrayOfValues):
arraySubsection = arrayOfValues[arrayIndex]
newValue = ("Block Number: " + str(arraySubsection[0]) +
"\nHash: " + str(arraySubsection[1]) + "\nPrevious: "
+ str(arraySubsection[2]) + "\nTransactional Data: " +
str(arraySubsection[3]) + "\nNonce: " +
str(arraySubsection[4]) + "\nTimestamp: " +
str(arraySubsection[5]) + "\n")
return (newValue)
def setHash(*args):
hashing_text = ""
hashing = sha256()
for arg in args:
hashing_text += str(arg)
return hashing.hexdigest()
def mine(transactions, nonce, hash, number, previous_hash, timestamp, currentIndex, diff):while (hash[:diff] != "0" * diff):
nonce += 1
hash = setHash(transactions,timestamp,previous_hash, nonce,
return (nonce, hash)
def newTransactionData(sender, recipient, amount, values):

values.extend([sender, recipient, amount])
return values
def verifyChain (arrayOfValues, lengthData):
if (lengthData > 1):
for x in range(lengthData-1):
currentArray = arrayOfValues[x]
secondaryArray = arrayOfValues[x+1]
if (currentArray[1] == secondaryArray[2]):
return (True)
def runData(currentIndex, transactions, timestamp, previous_hash, nonce, number, arrayOfValues, diff):previous_hash = getPreviousHash(currentIndex, arrayOfValues)hash = setHash(transactions, timestamp, previous_hash, nonce, 
nonce, hash = mine(transactions, nonce, hash, number,
previous_hash, timestamp, currentIndex, diff)
arrayOfValues = [number, hash, previous_hash, transactions,
nonce, timestamp]
number += 1return arrayOfValues, number
  • As seen in the BlockValue class, after retrieving the transactions data, the BlockValue function will execute “runData” using all the valid arguments.
  • Within this definition, the first method that occurs is that the previous_hash data is pulled using the getPreviousHash method.
  • Then, the hash is retrieved using the resulting valid arguments.
  • The hash will most likely not fit the difficulty. Thus, it must be mined repeatedly using the mine function.
  • After the previous_hash, nonce, and hash values are retrieved, the arrayOfValues can be updated.
  • The block number should also be updated for previous iterations.
  • Lastly, return arrayOfValues and the updated number.
class main:for value in range(len(senderData)):

genisisBlock = BlockValue(currentIndex, [], time.time(),
"0" * 64, number, transactionIndex)

currentArray, number = genisisBlock.new_data()
currentIndex += 1transactionIndex += 1print(CryptoFunctions.verifyChain(arrayOfValues,
  • Within the main class, we will iterate based on the number of values within the senderData range.
  • The primary block without any modification will be set under the conditions of genisisBlock.
  • The arrayOfValues will append the currentArray to make the collection of lists.
  • At the end of each iteration print the currentArray.
if __name__ == '__main__':



Everything connected with Tech & Code. Follow to join our 1M+ monthly readers

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
Ali Fakhry

Reinforcement learning, artificial intelligence, and software. NYU.