CSOC-WEB3-Crypto assignment
in this assignment we use hashlib , os , ecdsa ,ecdsa.util libraries. I install ecdsa . other was built in.
then in module there are 3 parts
-
who create hash TXID using sha256, 2 times
-
we create a digital signature using ECDSA over SECP256k1 curve
-
then we Verifies that the given signature was created by the owner of the public key
here crypto_demo is module with all functions. and test_demo is testing .
in module create_tx_hash(tx_data : bytes)-> str converts a msg like transaction to TXID , this uses 2 type SHA-256 hashing . then we use [::-1].hex() to reverse the bytes we reverse it because reversed double SHA256 hash is bitcoin style TXID .
here we see reversed version.
now
sign_transaction(private_key:int,tx_hash:bytes)->tuple this uses ecdsa to generate signature . signature is made of 2 numbers . so we make tuple and write like r , s
this shows how persons signing a transaction "owns the private key of wallet".
then
verify_signature(pubkey:bytes,sig:tuple, tx_hash:bytes)-> bool uses public key and check signed correctly or not . returns true if signature is valid.
test_demo work
1)generate random private-public key pair 2)make fake message 3)generate TXID using double SHA256 4) sign msg 6)verify