Skip to content

Commit

Permalink
from_string back in
Browse files Browse the repository at this point in the history
Signed-off-by: nadine.loepfe <[email protected]>
  • Loading branch information
nadineloepfe committed Feb 12, 2025
1 parent bd16399 commit 5c4f05a
Showing 1 changed file with 20 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/hiero_sdk_python/crypto/public_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,26 @@ def from_bytes(cls, key_bytes: bytes):
except Exception as e:
raise ValueError(f"Failed to load public key (DER): {e}")

@classmethod
def from_string(cls, key_str):
"""
Load a public key from a hex-encoded string.
For Ed25519, expects 32 bytes. Raw bytes string for ECDSA is not supported for now.
If the key is DER-encoded, tries to parse and detect Ed25519 vs ECDSA.
Args:
key_str (str): The hex-encoded public key string.
Returns:
PublicKey: A new instance of PublicKey.
Raises:
ValueError: If the key is invalid or unsupported.
"""
try:
key_bytes = bytes.fromhex(key_str.removeprefix("0x"))
except ValueError:
raise ValueError("Invalid hex-encoded public key string.")

return cls.from_bytes(key_bytes)

def verify(self, signature: bytes, data: bytes) -> None:
"""
Verifies a signature for the given data using this public key.
Expand Down

0 comments on commit 5c4f05a

Please sign in to comment.