Skip to content

Commit 01968e6

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 01968e6

2 files changed

Lines changed: 34 additions & 93 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/token_create_transaction_test.py

Lines changed: 26 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,16 @@ def generate_transaction_id(account_id_proto):
6464
return TransactionId(valid_start=tx_timestamp, account_id=account_id_proto)
6565

6666

67+
def _mock_private_key(public_key_bytes: bytes, signature: bytes) -> MagicMock:
68+
"""Create a PrivateKey mock that returns a stable protobuf key representation."""
69+
key = MagicMock(spec=PrivateKey)
70+
key.sign.return_value = signature
71+
key.public_key().to_bytes_raw.return_value = public_key_bytes
72+
key.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=public_key_bytes)
73+
key.to_proto_key.return_value = basic_types_pb2.Key(ed25519=public_key_bytes)
74+
return key
75+
76+
6777
########### Basic Tests for Building Transactions ###########
6878

6979

@@ -311,40 +321,14 @@ def test_sign_transaction(mock_account_ids, mock_client):
311321
private_key.sign.return_value = b"signature"
312322
private_key.public_key().to_bytes_raw.return_value = b"public_key"
313323

314-
private_key_admin = MagicMock(spec=PrivateKey)
315-
private_key_admin.sign.return_value = b"admin_signature"
316-
private_key_admin.public_key().to_bytes_raw.return_value = b"admin_public_key"
317-
private_key_admin.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"admin_public_key")
318-
319-
private_key_supply = MagicMock(spec=PrivateKey)
320-
private_key_supply.sign.return_value = b"supply_signature"
321-
private_key_supply.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"supply_public_key")
322-
323-
private_key_freeze = MagicMock(spec=PrivateKey)
324-
private_key_freeze.sign.return_value = b"freeze_signature"
325-
private_key_freeze.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"freeze_public_key")
326-
327-
private_key_wipe = MagicMock(spec=PrivateKey)
328-
private_key_wipe.sign.return_value = b"wipe_signature"
329-
private_key_wipe.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"wipe_public_key")
330-
331-
private_key_metadata = MagicMock(spec=PrivateKey)
332-
private_key_metadata.sign.return_value = b"metadata_signature"
333-
private_key_metadata.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"metadata_public_key")
334-
335-
private_key_pause = MagicMock(spec=PrivateKey)
336-
private_key_pause.sign.return_value = b"pause_signature"
337-
private_key_pause.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"pause_public_key")
338-
339-
private_key_kyc = MagicMock(spec=PrivateKey)
340-
private_key_kyc.sign.return_value = b"kyc_signature"
341-
private_key_kyc.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"kyc_public_key")
342-
343-
private_key_fee_schedule = MagicMock(spec=PrivateKey)
344-
private_key_fee_schedule.sign.return_value = b"fee_schedule_signature"
345-
private_key_fee_schedule.public_key()._to_proto.return_value = basic_types_pb2.Key(
346-
ed25519=b"fee_schedule_public_key"
347-
)
324+
private_key_admin = _mock_private_key(b"admin_public_key", b"admin_signature")
325+
private_key_supply = _mock_private_key(b"supply_public_key", b"supply_signature")
326+
private_key_freeze = _mock_private_key(b"freeze_public_key", b"freeze_signature")
327+
private_key_wipe = _mock_private_key(b"wipe_public_key", b"wipe_signature")
328+
private_key_metadata = _mock_private_key(b"metadata_public_key", b"metadata_signature")
329+
private_key_pause = _mock_private_key(b"pause_public_key", b"pause_signature")
330+
private_key_kyc = _mock_private_key(b"kyc_public_key", b"kyc_signature")
331+
private_key_fee_schedule = _mock_private_key(b"fee_schedule_public_key", b"fee_schedule_signature")
348332

349333
token_tx = TokenCreateTransaction()
350334
token_tx.set_token_name("MyToken")
@@ -778,40 +762,14 @@ def test_build_and_sign_nft_transaction_to_proto(mock_account_ids, mock_client):
778762
private_key_private.sign.return_value = b"private_signature"
779763
private_key_private.public_key().to_bytes_raw.return_value = b"private_public_key"
780764

781-
private_key_admin = MagicMock(spec=PrivateKey)
782-
private_key_admin.sign.return_value = b"admin_signature"
783-
private_key_admin.public_key().to_bytes_raw.return_value = b"admin_public_key"
784-
private_key_admin.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"admin_public_key")
785-
786-
private_key_supply = MagicMock(spec=PrivateKey)
787-
private_key_supply.sign.return_value = b"supply_signature"
788-
private_key_supply.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"supply_public_key")
789-
790-
private_key_freeze = MagicMock(spec=PrivateKey)
791-
private_key_freeze.sign.return_value = b"freeze_signature"
792-
private_key_freeze.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"freeze_public_key")
793-
794-
private_key_wipe = MagicMock(spec=PrivateKey)
795-
private_key_wipe.sign.return_value = b"wipe_signature"
796-
private_key_wipe.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"wipe_public_key")
797-
798-
private_key_metadata = MagicMock(spec=PrivateKey)
799-
private_key_metadata.sign.return_value = b"metadata_signature"
800-
private_key_metadata.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"metadata_public_key")
801-
802-
private_key_pause = MagicMock(spec=PrivateKey)
803-
private_key_pause.sign.return_value = b"pause_signature"
804-
private_key_pause.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"pause_public_key")
805-
806-
private_key_kyc = MagicMock(spec=PrivateKey)
807-
private_key_kyc.sign.return_value = b"kyc_signature"
808-
private_key_kyc.public_key()._to_proto.return_value = basic_types_pb2.Key(ed25519=b"kyc_public_key")
809-
810-
private_key_fee_schedule = MagicMock(spec=PrivateKey)
811-
private_key_fee_schedule.sign.return_value = b"fee_schedule_signature"
812-
private_key_fee_schedule.public_key()._to_proto.return_value = basic_types_pb2.Key(
813-
ed25519=b"fee_schedule_public_key"
814-
)
765+
private_key_admin = _mock_private_key(b"admin_public_key", b"admin_signature")
766+
private_key_supply = _mock_private_key(b"supply_public_key", b"supply_signature")
767+
private_key_freeze = _mock_private_key(b"freeze_public_key", b"freeze_signature")
768+
private_key_wipe = _mock_private_key(b"wipe_public_key", b"wipe_signature")
769+
private_key_metadata = _mock_private_key(b"metadata_public_key", b"metadata_signature")
770+
private_key_pause = _mock_private_key(b"pause_public_key", b"pause_signature")
771+
private_key_kyc = _mock_private_key(b"kyc_public_key", b"kyc_signature")
772+
private_key_fee_schedule = _mock_private_key(b"fee_schedule_public_key", b"fee_schedule_signature")
815773

816774
# Build the transaction
817775
token_tx = TokenCreateTransaction()

0 commit comments

Comments
 (0)