44python examples/keys_public_ecdsa.py
55
66"""
7- from cryptography .hazmat .primitives .asymmetric import ec
7+ from cryptography .hazmat .primitives .asymmetric import ec , utils
88from cryptography .hazmat .primitives import hashes
99from cryptography .exceptions import InvalidSignature
10- from hiero_sdk_python .crypto .public_key import PublicKey
10+ from hiero_sdk_python .crypto .public_key import PublicKey , keccak256
1111
1212def example_load_compressed_ecdsa () -> None :
1313 """
1414 Demonstrate creating a PublicKey object from a compressed 33-byte ECDSA hex.
1515 """
1616 # A mock 33-byte compressed hex:
17- compressed_pubkey = bytes .fromhex ("0281c2e57fecef82ff4f546dece3684acb6e2fe12a97af066348de81ccaf05d0a4" )
18-
17+ compressed_pubkey = bytes .fromhex (
18+ "0281c2e57fecef82ff4f546dece3684acb6e2fe12a97af066348de81ccaf05d0a4"
19+ )
20+
1921 # 1) Construct via the specialized from_bytes_ecdsa()
2022 pubk_obj = PublicKey .from_bytes_ecdsa (compressed_pubkey ) # or from_bytes
2123 print ("Loaded ECDSA PublicKey (compressed) =" , pubk_obj )
22-
24+
2325 # 2) Convert it back to compressed hex
2426 compressed_hex = pubk_obj .to_string_ecdsa ()
2527 print ("Back to compressed hex:" , compressed_hex )
26-
28+
2729def example_load_uncompressed_ecdsa_from_hex () -> None :
2830 """
2931 Demonstrate creating an ECDSA (secp256k1) public key from an uncompressed 65-byte hex string.
3032 """
3133 # Uncompressed secp256k1 public keys start with 0x04 and are 65 bytes total.
3234 uncompressed_hex = (
3335 "04"
34- "18a5fcc2a9af70f6248efa1a2b0cc7d6cf973f43ae6c041ff35a1a3f7d947ba6 "
35- "15ba91825331ad2ce55d44469d4e874a997e3888e20e2d50322d52c365cad7f3e "
36+ "0abe0517fcf06e0c160ca821aa2909945752e08169f46c984cb6b02076a3b "
37+ "29513f047e5c13770101c321f332157377d2862b7c7ed14eedca3978b3b9d007659 "
3638 )
37-
39+
3840 # 1) Load directly from a hex string using the specialized from_string_ecdsa().
3941 pubk_obj = PublicKey .from_string_ecdsa (uncompressed_hex ) # or from_string
4042 print ("Loaded uncompressed ECDSA PublicKey from hex:" , pubk_obj )
41-
43+
4244 # 2) Convert to compressed raw bytes or hex:
4345 compressed_bytes = pubk_obj .to_bytes_ecdsa () #or to_bytes_raw
44- print ("Compressed ECDSA bytes (len={}):" . format ( len (compressed_bytes )), compressed_bytes .hex ())
46+ print (f "Compressed ECDSA bytes (len={ len (compressed_bytes )} ): { compressed_bytes .hex ()} " )
4547
4648def example_verify_ecdsa_signature () -> None :
4749 """
@@ -53,11 +55,11 @@ def example_verify_ecdsa_signature() -> None:
5355
5456 # 1) Wrap in the PublicKey class
5557 pubk_obj = PublicKey (public_key )
56-
58+
5759 # 2) Sign some data
5860 data = b"Hello ECDSA"
59- signature = private_key .sign (data , ec .ECDSA (hashes .SHA256 ()))
60-
61+ signature = private_key .sign (keccak256 ( data ) , ec .ECDSA (utils . Prehashed ( hashes .SHA256 () )))
62+
6163 # 3) Verify with pubk_obj
6264 try :
6365 pubk_obj .verify (signature , data )
0 commit comments