Skip to content

Commit db1d5e8

Browse files
committed
fix: ambiguous key signing but needs refining
1 parent 391a62f commit db1d5e8

31 files changed

+574
-181
lines changed

examples/account_create.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ def create_new_account():
1818
client = Client(network)
1919

2020
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
21-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
21+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
2222
client.set_operator(operator_id, operator_key)
2323

24-
new_account_private_key = PrivateKey.generate()
24+
new_account_private_key = PrivateKey.generate_ed25519()
2525
new_account_public_key = new_account_private_key.public_key()
2626

2727
transaction = (

examples/keys.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
#!/usr/bin/env python3
2+
"""
3+
example_keys.py
4+
5+
Demonstrates how to generate Ed25519/ECDSA private keys,
6+
convert them to/from raw and DER encodings, and sign/verify messages
7+
using the new explicit loaders in the hiero_sdk_python.crypto module.
8+
"""
9+
10+
from hiero_sdk_python.crypto.private_key import PrivateKey
11+
from hiero_sdk_python.crypto.public_key import PublicKey
12+
13+
14+
def example_ed25519_raw():
15+
print("=== Ed25519 (Raw) ===")
16+
# Generate Ed25519 private key
17+
priv_ed = PrivateKey.generate_ed25519()
18+
print("Ed25519 Private Key (raw hex):", priv_ed.to_string_raw())
19+
20+
# Derive the public key
21+
pub_ed = priv_ed.public_key()
22+
print("Ed25519 Public Key (raw hex):", pub_ed.to_string_raw())
23+
24+
# Reload the private key from its raw hex
25+
priv_ed_loaded = PrivateKey.from_string_ed25519(priv_ed.to_string_raw())
26+
# Reload the public key from its raw hex
27+
pub_ed_loaded = PublicKey.from_string_ed25519(pub_ed.to_string_raw())
28+
29+
# Sign and verify
30+
message = b"Hello, Ed25519!"
31+
signature = priv_ed.sign(message)
32+
try:
33+
pub_ed.verify(signature, message)
34+
print("Ed25519 signature verified successfully.")
35+
except Exception as e:
36+
print("Ed25519 signature verification failed:", e)
37+
38+
# Sanity checks
39+
print("Private keys match?" , priv_ed_loaded.to_string_raw() == priv_ed.to_string_raw())
40+
print("Public keys match?" , pub_ed_loaded.to_string_raw() == pub_ed.to_string_raw())
41+
print()
42+
43+
44+
def example_ed25519_der():
45+
print("=== Ed25519 (DER) ===")
46+
# Generate Ed25519 private key
47+
priv_ed = PrivateKey.generate_ed25519()
48+
print("Ed25519 Private Key (DER hex):", priv_ed.to_string_der())
49+
50+
# Derive the public key and show DER
51+
pub_ed = priv_ed.public_key()
52+
print("Ed25519 Public Key (DER hex):", pub_ed.to_string_der())
53+
54+
# Reload the private key from DER hex
55+
priv_ed_loaded = PrivateKey.from_string_der(priv_ed.to_string_der())
56+
# Reload the public key from DER hex
57+
pub_ed_loaded = PublicKey.from_string_der(pub_ed.to_string_der())
58+
59+
# Sign and verify
60+
message = b"Hello, Ed25519 in DER!"
61+
signature = priv_ed.sign(message)
62+
try:
63+
pub_ed.verify(signature, message)
64+
print("DER-based Ed25519 signature verified successfully.")
65+
except Exception as e:
66+
print("DER-based Ed25519 signature verification failed:", e)
67+
68+
# Sanity checks
69+
print("Private keys match?" , priv_ed_loaded.to_string_der() == priv_ed.to_string_der())
70+
print("Public keys match?" , pub_ed_loaded.to_string_der() == pub_ed.to_string_der())
71+
print()
72+
73+
74+
def example_ecdsa_raw():
75+
print("=== ECDSA (Raw) ===")
76+
# Generate ECDSA private key
77+
priv_ecdsa = PrivateKey.generate_ecdsa()
78+
print("ECDSA Private Key (raw hex):", priv_ecdsa.to_string_raw())
79+
80+
# Derive the public key
81+
pub_ecdsa = priv_ecdsa.public_key()
82+
print("ECDSA Public Key (raw hex):", pub_ecdsa.to_string_raw())
83+
84+
# Reload the private key from raw hex
85+
priv_ecdsa_loaded = PrivateKey.from_string_ecdsa(priv_ecdsa.to_string_raw())
86+
# Reload the public key from raw hex
87+
pub_ecdsa_loaded = PublicKey.from_string_ecdsa(pub_ecdsa.to_string_raw())
88+
89+
# Sign and verify
90+
message = b"Hello, ECDSA!"
91+
signature = priv_ecdsa.sign(message)
92+
try:
93+
pub_ecdsa.verify(signature, message)
94+
print("ECDSA signature verified successfully.")
95+
except Exception as e:
96+
print("ECDSA signature verification failed:", e)
97+
98+
# Sanity checks
99+
print("Private keys match?" , priv_ecdsa_loaded.to_string_raw() == priv_ecdsa.to_string_raw())
100+
print("Public keys match?" , pub_ecdsa_loaded.to_string_raw() == pub_ecdsa.to_string_raw())
101+
print()
102+
103+
104+
def example_ecdsa_der():
105+
print("=== ECDSA (DER) ===")
106+
# Generate ECDSA private key
107+
priv_ecdsa = PrivateKey.generate_ecdsa()
108+
print("ECDSA Private Key (DER hex):", priv_ecdsa.to_string_der())
109+
110+
# Derive the public key and show DER
111+
pub_ecdsa = priv_ecdsa.public_key()
112+
print("ECDSA Public Key (DER hex):", pub_ecdsa.to_string_der())
113+
114+
# Reload the private key from DER hex
115+
priv_ecdsa_loaded = PrivateKey.from_string_der(priv_ecdsa.to_string_der())
116+
# Reload the public key from DER hex
117+
pub_ecdsa_loaded = PublicKey.from_string_der(pub_ecdsa.to_string_der())
118+
119+
# Sign and verify
120+
message = b"Hello, ECDSA in DER!"
121+
signature = priv_ecdsa.sign(message)
122+
try:
123+
pub_ecdsa.verify(signature, message)
124+
print("DER-based ECDSA signature verified successfully.")
125+
except Exception as e:
126+
print("DER-based ECDSA signature verification failed:", e)
127+
128+
# Sanity checks
129+
print("Private keys match?" , priv_ecdsa_loaded.to_string_der() == priv_ecdsa.to_string_der())
130+
print("Public keys match?" , pub_ecdsa_loaded.to_string_der() == pub_ecdsa.to_string_der())
131+
print()
132+
133+
134+
def main():
135+
example_ed25519_raw()
136+
example_ed25519_der()
137+
example_ecdsa_raw()
138+
example_ecdsa_der()
139+
140+
141+
if __name__ == "__main__":
142+
main()

examples/query_balance.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,11 @@ def create_account_and_transfer():
2222
client = Client(network)
2323

2424
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
25-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
25+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
2626
client.set_operator(operator_id, operator_key)
2727

2828
# Create new account
29-
new_account_private_key = PrivateKey.generate()
29+
new_account_private_key = PrivateKey.generate_ed25519()
3030
new_account_public_key = new_account_private_key.public_key()
3131
transaction = AccountCreateTransaction(
3232
key=new_account_public_key,

examples/query_receipt.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ def query_receipt():
2020
client = Client(network)
2121

2222
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
23-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
23+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
2424
recipient_id = AccountId.from_string(os.getenv('RECIPIENT_ID'))
2525
amount = 10
2626

examples/query_topic_info.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
def query_topic_info():
1616
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
17-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
17+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
1818
topic_id = TopicId.from_string(os.getenv('TOPIC_ID'))
1919

2020
network = Network(network='testnet')

examples/token_associate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ def associate_token():
1818
client = Client(network)
1919

2020
recipient_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
21-
recipient_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
21+
recipient_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
2222
token_id = TokenId.from_string('TOKEN_ID') # Update as needed
2323

2424
client.set_operator(recipient_id, recipient_key)

examples/token_create_fungible_finite.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def create_token_fungible_finite():
4141

4242
# Operator credentials (must be present)
4343
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
44-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
44+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
4545

4646
# Optional Token Keys
47-
admin_key = PrivateKey.from_string(os.getenv('ADMIN_KEY'))# Optional
48-
supply_key = PrivateKey.from_string(os.getenv('SUPPLY_KEY')) # Optional
49-
freeze_key = PrivateKey.from_string(os.getenv('FREEZE_KEY')) # Optional
47+
admin_key = PrivateKey.from_string_ed25519(os.getenv('ADMIN_KEY'))# Optional
48+
supply_key = PrivateKey.from_string_ed25519(os.getenv('SUPPLY_KEY'))# Optional
49+
freeze_key = PrivateKey.from_string_ed25519(os.getenv('FREEZE_KEY'))# Optional
5050

5151
# Set the operator for the client
5252
client.set_operator(operator_id, operator_key)

examples/token_create_fungible_infinite.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def create_token_fungible_infinite():
4141

4242
# Operator credentials (must be present)
4343
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
44-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
44+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
4545

4646
# Optional Token Keys
47-
admin_key = PrivateKey.from_string(os.getenv('ADMIN_KEY'))# Optional
48-
supply_key = PrivateKey.from_string(os.getenv('SUPPLY_KEY')) # Optional
49-
freeze_key = PrivateKey.from_string(os.getenv('FREEZE_KEY')) # Optional
47+
admin_key = PrivateKey.from_string_ed25519(os.getenv('ADMIN_KEY'))# Optional
48+
supply_key = PrivateKey.from_string_ed25519(os.getenv('SUPPLY_KEY'))# Optional
49+
freeze_key = PrivateKey.from_string_ed25519(os.getenv('FREEZE_KEY'))# Optional
5050

5151
# Set the operator for the client
5252
client.set_operator(operator_id, operator_key)

examples/token_create_nft_finite.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def create_token_nft():
4141

4242
# Operator credentials (must be present)
4343
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
44-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
44+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
4545

4646
# Optional Token Keys
47-
admin_key = PrivateKey.from_string(os.getenv('ADMIN_KEY'))# Optional
48-
supply_key = PrivateKey.from_string(os.getenv('SUPPLY_KEY')) # Optional
49-
freeze_key = PrivateKey.from_string(os.getenv('FREEZE_KEY')) # Optional
47+
admin_key = PrivateKey.from_string_ed25519(os.getenv('ADMIN_KEY'))# Optional
48+
supply_key = PrivateKey.from_string_ed25519(os.getenv('SUPPLY_KEY'))# Optional
49+
freeze_key = PrivateKey.from_string_ed25519(os.getenv('FREEZE_KEY'))# Optional
5050

5151
# Set the operator for the client
5252
client.set_operator(operator_id, operator_key)

examples/token_create_nft_infinite.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,12 @@ def create_token_nft_infinite():
4141

4242
# Operator credentials (must be present)
4343
operator_id = AccountId.from_string(os.getenv('OPERATOR_ID'))
44-
operator_key = PrivateKey.from_string(os.getenv('OPERATOR_KEY'))
44+
operator_key = PrivateKey.from_string_ed25519(os.getenv('OPERATOR_KEY'))
4545

4646
# Optional Token Keys
47-
admin_key = PrivateKey.from_string(os.getenv('ADMIN_KEY'))# Optional
48-
supply_key = PrivateKey.from_string(os.getenv('SUPPLY_KEY')) # Optional
49-
freeze_key = PrivateKey.from_string(os.getenv('FREEZE_KEY')) # Optional
47+
admin_key = PrivateKey.from_string_ed25519(os.getenv('ADMIN_KEY'))# Optional
48+
supply_key = PrivateKey.from_string_ed25519(os.getenv('SUPPLY_KEY'))# Optional
49+
freeze_key = PrivateKey.from_string_ed25519(os.getenv('FREEZE_KEY'))# Optional
5050

5151
# Set the operator for the client
5252
client.set_operator(operator_id, operator_key)

0 commit comments

Comments
 (0)