Skip to content

Commit 5c4f05a

Browse files
committed
from_string back in
Signed-off-by: nadine.loepfe <[email protected]>
1 parent bd16399 commit 5c4f05a

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

src/hiero_sdk_python/crypto/public_key.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,26 @@ def from_bytes(cls, key_bytes: bytes):
5959
except Exception as e:
6060
raise ValueError(f"Failed to load public key (DER): {e}")
6161

62+
@classmethod
63+
def from_string(cls, key_str):
64+
"""
65+
Load a public key from a hex-encoded string.
66+
For Ed25519, expects 32 bytes. Raw bytes string for ECDSA is not supported for now.
67+
If the key is DER-encoded, tries to parse and detect Ed25519 vs ECDSA.
68+
Args:
69+
key_str (str): The hex-encoded public key string.
70+
Returns:
71+
PublicKey: A new instance of PublicKey.
72+
Raises:
73+
ValueError: If the key is invalid or unsupported.
74+
"""
75+
try:
76+
key_bytes = bytes.fromhex(key_str.removeprefix("0x"))
77+
except ValueError:
78+
raise ValueError("Invalid hex-encoded public key string.")
79+
80+
return cls.from_bytes(key_bytes)
81+
6282
def verify(self, signature: bytes, data: bytes) -> None:
6383
"""
6484
Verifies a signature for the given data using this public key.

0 commit comments

Comments
 (0)