Python blockchain client class
Python Blockchain Client Class
Client class generates a private key and a public key using the built-in Python RSA algorithm. Interested readers can refer to this tutorial for an RSA implementation. During object initialization, we create the private and public keys and store their values in instance variables.
self._private_key = RSA.generate(1024, random)
self._public_key = self._private_key.publickey()
Note that you should not lose your private key. For record-keeping, copy the generated private key to a secure external storage device, or simply write its ASCII representation on a piece of paper.
The generated public key will be used as the client’s identity. For this purpose, 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')
This identity is unique to each client and publicly available. Anyone can use this identity to send you cryptocurrency, and it will be added to your wallet.
Client class’s complete code 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 the Client
Now, we’ll write code that illustrates how to use the Client class –