Skip to content

Commit 7452cfa

Browse files
committed
feat: add DER encoding methods and update key type alias in key_utils
Signed-off-by: MonaaEid <monaa_eid@hotmail.com>
1 parent 7c53b5d commit 7452cfa

2 files changed

Lines changed: 13 additions & 31 deletions

File tree

src/hiero_sdk_python/utils/key_utils.py

Lines changed: 8 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -7,47 +7,30 @@
77

88
from __future__ import annotations
99

10-
from hiero_sdk_python.crypto.key import Key as SdkKey
11-
from hiero_sdk_python.crypto.private_key import PrivateKey
12-
from hiero_sdk_python.crypto.public_key import PublicKey
10+
from hiero_sdk_python.crypto.key import Key
1311
from hiero_sdk_python.hapi.services import basic_types_pb2
1412

1513

16-
# Type alias for keys that can be either PrivateKey or PublicKey
17-
Key = PrivateKey | PublicKey | SdkKey
18-
19-
2014
def key_to_proto(key: Key | None) -> basic_types_pb2.Key | None:
2115
"""
22-
Helper function to convert a key (PrivateKey or PublicKey) to protobuf Key format.
16+
Helper function to convert an SDK key to protobuf Key format.
2317
24-
This function handles the conversion of SDK key types to protobuf format:
25-
- If a PrivateKey is provided, its corresponding public key is extracted and converted.
26-
- If a PublicKey is provided, it is converted directly to protobuf.
27-
- If None is provided, None is returned.
18+
This function handles any concrete subclass of Key by delegating to its
19+
to_proto_key() implementation. If None is provided, None is returned.
2820
2921
Args:
30-
key (Optional[Key]): The key to convert (PrivateKey or PublicKey), or None
22+
key (Optional[Key]): The key to convert, or None
3123
3224
Returns:
3325
basic_types_pb2.Key (Optional): The protobuf key or None if key is None
3426
3527
Raises:
36-
TypeError: If the provided key is not a PrivateKey, PublicKey, or None.
28+
TypeError: If the provided key is not a Key instance or None.
3729
"""
3830
if key is None:
3931
return None
4032

41-
# If it's a PrivateKey, get the public key first, then convert to proto
42-
if isinstance(key, PrivateKey):
43-
return key.public_key()._to_proto()
44-
45-
# If it's a PublicKey, convert directly to proto
46-
if isinstance(key, PublicKey):
47-
return key._to_proto()
48-
49-
# Any other SDK Key implementation (e.g., KeyList / ThresholdKey wrapper)
50-
if isinstance(key, SdkKey):
33+
if isinstance(key, Key):
5134
return key.to_proto_key()
5235

53-
raise TypeError("Key must be of type PrivateKey or PublicKey, or another SDK Key implementation")
36+
raise TypeError("Key must be of type PrivateKey or PublicKey")

tests/unit/key_utils_test.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
import pytest
66

7+
from hiero_sdk_python.crypto.key import Key
78
from hiero_sdk_python.crypto.private_key import PrivateKey
89
from hiero_sdk_python.hapi.services import basic_types_pb2
9-
from hiero_sdk_python.utils.key_utils import Key, key_to_proto
10+
from hiero_sdk_python.utils.key_utils import key_to_proto
1011

1112

1213
pytestmark = pytest.mark.unit
@@ -75,18 +76,16 @@ def test_key_to_proto_with_invalid_string_raises_error():
7576
with pytest.raises(TypeError) as e:
7677
key_to_proto("this is not a key")
7778

78-
assert "Key must be of type PrivateKey or PublicKey" in str(e.value)
79+
assert "Key must be an instance" in str(e.value)
7980

8081

81-
def test_key_type_alias():
82-
"""Tests that the Key type alias works correctly."""
82+
def test_key_type_inheritance():
83+
"""Tests that Key typing accepts PrivateKey and PublicKey instances."""
8384
private_key = PrivateKey.generate_ed25519()
8485
public_key = private_key.public_key()
8586

86-
# Test that both PrivateKey and PublicKey can be assigned to Key type
8787
key1: Key = private_key
8888
key2: Key = public_key
8989

90-
# Both should work with key_to_proto
9190
assert key_to_proto(key1) is not None
9291
assert key_to_proto(key2) is not None

0 commit comments

Comments
 (0)