Ethereum Library in Python
============================================================ ====.
Introduction
————-
In
Library Code
————-
We will use the “cryptography” library, which is a popular and well -maintained library for cryptographic tasks in python. We will also use the HMAC
Library to Generate Mac (Authentication of Message Codes) for Ethereum Blockchain.
facility
————–
To install the required Libraries, Run the Following Commands:
`bash
Pip Install Cryptography
'
Library Code:
Python
Import an operating system
Of cryptography.hazmat. Primitive Import of Serialization
cryptography.hazmat.primitifs.asymetric Import EC
from cryptography.hazmat.backends Import default_backend
Import the Base64
Ethereumlibrary Class:
Def __init __ (self, private_key = none):
If Private_Key:
self.private_key = private_key
Other:
Generate a New Ec Keys for Each Instance of the Library
self.private_key = Ec.Generate_Private_Key (EC.SecP256K1 (), default_backend ())
DEF GENERATE_EC_KEYPAIR (SELF, PRIVE_KEY = None):
If Private_Key is None:
private_key = self.private_key
Return Ec.Generate_Private_Key (Private_Key, Default_backend ())
Def get_private_key (self):
Return Self.Private_Key
Def get_public_key (self):
Return self.private_key.public_key ()
def sign_message (self, message, public_key):
signature = public_key.sign (
Message,
E.dhparams (),
Signingalgorithm.Sha256 Asymmetrical ()
)))
Return Base64.B32encode (Signature)
Def Verify_signature (Self, Message, Signature, Private_Key):
to try:
public_key = self.private_key.public_key ()
Public_key.verify (
Signature,
Message,
Asymmetrical Verveykey.dhparam (),
)))
Real Return
Except:
go back
Example of Use:
Library = Ethereumlibrary ()
Generate a New Ec Keys for Each Instance of the Library
Keypair = Biblirary.Generate_ec_KeyPair ()
private_key = library.get_private_key (). Public_bytes (
Coding = serialization.ecoding.pem,
Format = serialization.privormat-trust-inless,
Encryption_Algorithm = Serialization.noencryption ()
)))
Sign a message with the public key
Message = B "Hello, World!"
signature = library.sign_message (message, keypair.public_key ())
print (library.get_private_key (). Public_bytes (
Coding = serialization.ecoding.pem,
Format = serialization.publicformat.subjectpubliceweyinfo
)))
Check the Signature Using the Private Key
Private_key.verify (
B "\ x00 \ x01 \ x02 \ x03",
signature
B "Hello, World!", Message #
Keypair.private_key
)))
'
Note that Significant memory.
Committed Messages
----------------
For Validation Messages, follow the Standard Professional Guidelines:
- Use Imperative Mood (for example, "Feat: Add the Ethereum Library")
- Keep the First Short Line (<72 Characters)
- Use the Present (for example, "Add a New Generation of EC Keys"))
Example of Validation Message:
'
Adding the Basic Ethereum Library to Python
` ‘