- Foundations of Blockchain
- Koshik Raj
- 239字
- 2021-07-02 14:17:19
Creating an identity
As we have covered digital signatures and the public-private key in the previous chapter, we are already aware of the properties and security of digital signatures. To recap, they provide a way for a node that possesses the private key to sign a message to prove their identity. This can then be verified by anyone who has access to the distributed public key.
An identity is created by generating a public-private key pair. This is similar to creating an account in the blockchain network. The following code shows how a public-private key pair is generated by the ecdsa Python package using an elliptic curve:
import binascii from ecdsa import SigningKey, VerifyingKey, SECP256k1, keys class Wallet: def __init__(self): self.private_key = None self.public_key = None def generate_key_pair(self): sk = SigningKey.generate(curve=SECP256k1) self.private_key = binascii.b2a_hex(sk.to_string()).decode() self.public_key =
binascii.b2a_hex(sk.get_verifying_key().to_string()).decode()
The key pair is generated using a special elliptic curve, secp256k1, which is also used in Bitcoin's digital signature generation. The following lines of code will create a public-private key pair:
account = Wallet() account.generate_key_pair() print("Generated public key: %s" % account.public_key)
print("Generated private key: %s" % account.private_key)
A 64-character (256-bit) private key and a 128-character public key are generated in hexadecimal format as follows:
Generated public key:
b7f5edffe6d3532ed743e07c4de5551c2d7476a4053221999ce40edec2607bb4ef
7ecb9fc6ecf735fd3802fada56c42e18474f8bad269a965f95863f9fc38158 Generated private key:
6eb9035be1dabd01fadcb6a9f92946decc868046184c7810a43806eb6cc46237
The private key is always kept secret, and the public key is used to generate the public address of the user and is then embedded in the transaction.