Python Blockchain Transaction Class

Python Blockchain Transaction Class

In this chapter, let’s create a Transaction class so that a client can send money to someone. Note that a client can be both a sender and a receiver of money. When you want to receive money, some other sender creates a transaction and specifies your public address. We define the initialization of the Transaction class as follows.

def __init__(self, sender, recipient, value):
self.sender = sender
self.recipient = recipient
self.value = value
self.time = datetime.datetime.now()

The init method requires three parameters – the sender’s public key, the recipient’s public key, and the amount to be sent. These are stored in instance variables for use by other methods. We also create a variable to store the transaction time.

Next, we write a utility method called to_dict to merge all four instance variables into a single dictionary object. This simply allows the entire transaction information to be accessed through a single variable.

As you know from the previous tutorial, the first block in a blockchain is the genesis block. The genesis block contains the first transaction initiated by the creator of the blockchain. This person’s identity may be kept secret, as in Bitcoin. Therefore, when this first transaction is created, the creator can simply send their identity as Genesis. Therefore, when creating the dictionary, we check if the sender is Genesis. If so, we simply assign some string value to the identity variable; otherwise, we assign the sender’s identity to the identity variable.

if self.sender == "Genesis":
identity = "Genesis"
else:
identity = self.sender.identity

We use the following line of code to build the dictionary

return collections.OrderedDict({
'sender': identity,
'recipient': self.recipient,
'value': self.value,
'time' : self.time})

to_dict The full code for the method is shown below

def to_dict(self):
if self.sender == "Genesis":
identity = "Genesis"
else:
identity = self.sender.identity

return collections.OrderedDict({
'sender': identity,
'recipient': self.recipient,
'value': self.value,
'time': self.time})

Finally, we sign this dictionary object using the sender’s private key. As before, we use the built-in PKI with the SHA algorithm. The resulting signature is decoded to obtain an ASCII representation for printing and storage in our blockchain. The code for the sign_transaction method is shown here —

def sign_transaction(self):
private_key = self.sender._private_key
signer = PKCS1_v1_5.new(private_key)
h = SHA.new(str(self.to_dict()).encode('utf8'))
return binascii.hexlify(signer.sign(h)).decode('ascii')

We will now test the Transaction class.

Testing the Transaction Class

To do this, we will create two users named Dinesh and Ramesh. Dinesh will send 5 TPCoins to Ramesh. To do this, we first create clients named Dinesh and Ramesh.

Dinesh = Client()
Ramesh = Client()

Remember, when you instantiate a Client class, client-specific public key and private key are created. Since Dinesh wants to pay Ramesh, he will need Ramesh’s public key, which is obtained using the client’s identity property.

Therefore, we will use the following code to create a transaction instance.

t = Transaction(
Dinesh,
Ramesh.identity,
5.0
)

Note that the first argument is the sender, the second is the receiver’s public key, and the third is the amount to be transferred. The sign_transaction method retrieves the sender’s private key from the first argument to sign the transaction.

After the transaction object is created, you will sign it by calling its sign_transaction method. This method returns the generated signature in a printable format. We use the following two lines of code to generate and print the signature.

signature = t.sign_transaction()
print (signature)

When you run the above code, you will see output similar to this:

7c7e3c97629b218e9ec6e86b01f9abd8e361fd69e7d373c38420790b655b9abe3b575e343c7
13703ca1aee781acd7157a0624db3d57d7c2f1172730ee3f45af943338157f899965856f6b0
0e34db 240b62673ad5a08c8e490f880b568efbc36035cae2e748f1d802d5e8e66298be826f5
c6363dc511222fb2416036ac04eb972

Now that our basic infrastructure for creating clients and transactions is in place, we will have multiple clients and multiple transactions, just like in real life.

Leave a Reply

Your email address will not be published. Required fields are marked *