Developing Client
A client is somebody who holds TPCoins and transacts those for goods/services from other vendors on the network including his own. We should define a Client class for this purpose. To create a globally unique identification for the client, we use PKI (Public Key Infrastructure). In this chapter, let us talk about this in detail.
The client should be able to send money from his wallet to another known person. Similarly, the client should be able to accept money from a third party. For spending money, the client would create a transaction specifying the sender’s name and the amount to be paid. For receiving money, the client will provide his identity to the third party − essentially a sender of the money. We do not store the balance amount of money the client holds in his wallet. During a transaction, we will compute the actual balance to ensure that the client has sufficient balance to make the payment.
To develop the Client class and for the rest of the code in the project, we will need to import many Python libraries. These are listed below −
# import libraries import hashlib import random import string import json import binascii import numpy as np import pandas as pd import pylab as pl import logging import datetime import collections
In addition to the above standard libraries, we are going to sign our transactions, create hash of the objects, etc. For this, you will need to import the following libraries −
# following imports are required by PKI import Crypto import Crypto.Random from Crypto.Hash import SHA from Crypto.PublicKey import RSA from Crypto.Signature import PKCS1_v1_5
Python Blockchain – Client Class
The Client class generates the private and public keys by using the built-in Python RSA algorithm. The interested reader may refer to this tutorial for the implementation of RSA. During the object initialization, we create private and public keys and store their values in the instance variable.
self._private_key = RSA.generate(1024, random) self._public_key = self._private_key.publickey()
Note that you should never lose your private key. For record keeping, the generated private key may be copied on a secured external storage or you may simply write down the ASCII representation of it on a piece of paper.
The generated public key will be used as the client’s identity. For this, we define a property called identity that returns the HEX representation of the public key.
@property def identity(self): return binascii.hexlify(self._public_key.exportKey(format='DER')) .decode('ascii')
The identity is unique to each client and can be made publicly available. Anybody would be able to send virtual currency to you using this identity and it will get added to your wallet.
The full code for the Client class is shown here −
class Client: def __init__(self): random = Crypto.Random.new().read self._private_key = RSA.generate(1024, random) self._public_key = self._private_key.publickey() self._signer = PKCS1_v1_5.new(self._private_key) @property def identity(self): return binascii.hexlify(self._public_key.exportKey(format='DER')).decode('ascii')
Testing Client
Now, we will write code that will illustrate how to use the Client class −
Dinesh = Client() print (Dinesh.identity)
The above code creates an instance of Client and assigns it to the variable Dinesh. We print the public key of Dinesh by calling its identity method. The output is shown here −
30819f300d06092a864886f70d010101050003818d0030818902818100b547fafceeb131e07 0166a6b23fec473cce22c3f55c35ce535b31d4c74754fecd820aa94c1166643a49ea5f49f72 3181ff943eb3fdc5b2cb2db12d21c06c880ccf493e14dd3e93f3a9e175325790004954c34d3 c7bc2ccc9f0eb5332014937f9e49bca9b7856d351a553d9812367dc8f2ac734992a4e6a6ff6 6f347bd411d07f0203010001
Now, we let us move on to create a transaction in the next chapter.
This article has been published from the source link without modifications to the text. Only the headline has been changed.