Skip to content

Commit 480b663

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 35aa267 commit 480b663

3 files changed

Lines changed: 8 additions & 22 deletions

File tree

src/hiero_sdk_python/crypto/public_key.py

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -452,12 +452,7 @@ def _encode_der_oid(cls, oid: str) -> bytes:
452452
"""
453453
parts = [int(part) for part in oid.split(".")]
454454

455-
is_valid = (
456-
len(parts) >= 2 and
457-
parts[0] in (0, 1, 2) and
458-
parts[1] >= 0 and
459-
(parts[0] == 2 or parts[1] < 40)
460-
)
455+
is_valid = len(parts) >= 2 and parts[0] in (0, 1, 2) and parts[1] >= 0 and (parts[0] == 2 or parts[1] < 40)
461456
if not is_valid:
462457
raise ValueError(f"Invalid OID structure for '{oid}'")
463458

@@ -495,8 +490,7 @@ def to_bytes_der_ecdsa_compressed(self) -> bytes:
495490

496491
# id-ecPublicKey + secp256k1 OID algorithm identifier
497492
algorithm_id = self._encode_der_sequence(
498-
self._encode_der_oid("1.2.840.10045.2.1")
499-
+ self._encode_der_oid("1.3.132.0.10")
493+
self._encode_der_oid("1.2.840.10045.2.1") + self._encode_der_oid("1.3.132.0.10")
500494
)
501495
compressed_point = self.to_bytes_ecdsa(compressed=True)
502496
subject_public_key = self._encode_der_bit_string(compressed_point)
@@ -529,7 +523,7 @@ def to_string_der(self) -> str:
529523
Hex-encoded DER form of the public key.
530524
"""
531525
return self.to_bytes_der().hex()
532-
526+
533527
def to_string_der_ecdsa_compressed(self) -> str:
534528
"""
535529
Returns DER SPKI hex for ECDSA secp256k1 using a compressed SEC1 point.

src/hiero_sdk_python/utils/key_utils.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,14 @@
77

88
from __future__ import annotations
99

10-
from typing import Union
11-
1210
from hiero_sdk_python.crypto.key import Key as SdkKey
1311
from hiero_sdk_python.crypto.private_key import PrivateKey
1412
from hiero_sdk_python.crypto.public_key import PublicKey
1513
from hiero_sdk_python.hapi.services import basic_types_pb2
1614

1715

1816
# Type alias for keys that can be either PrivateKey or PublicKey
19-
Key = Union[PrivateKey, PublicKey, SdkKey]
17+
Key = PrivateKey | PublicKey | SdkKey
2018

2119

2220
def key_to_proto(key: Key | None) -> basic_types_pb2.Key | None:

tests/unit/keys_public_test.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,9 @@ def test_encode_der_length_negative_raises():
344344

345345
def test_encode_der_oid_known_values():
346346
# id-ecPublicKey
347-
assert PublicKey._encode_der_oid("1.2.840.10045.2.1") == bytes.fromhex(
348-
"06072a8648ce3d0201"
349-
)
347+
assert PublicKey._encode_der_oid("1.2.840.10045.2.1") == bytes.fromhex("06072a8648ce3d0201")
350348
# secp256k1
351-
assert PublicKey._encode_der_oid("1.3.132.0.10") == bytes.fromhex(
352-
"06052b8104000a"
353-
)
349+
assert PublicKey._encode_der_oid("1.3.132.0.10") == bytes.fromhex("06052b8104000a")
354350

355351

356352
def test_encode_der_oid_combined_root_multibyte():
@@ -390,11 +386,9 @@ def test_to_bytes_der_ecdsa_compressed_structure_and_roundtrip(ecdsa_keypair):
390386
compressed_point = public_key.to_bytes_ecdsa(compressed=True)
391387

392388
# Fixed SPKI prefix for secp256k1 compressed-point encoding.
393-
expected_prefix = bytes.fromhex(
394-
"3036301006072a8648ce3d020106052b8104000a032200"
395-
)
389+
expected_prefix = bytes.fromhex("3036301006072a8648ce3d020106052b8104000a032200")
396390
assert der.startswith(expected_prefix)
397-
assert der[len(expected_prefix):] == compressed_point
391+
assert der[len(expected_prefix) :] == compressed_point
398392

399393
# Ensure produced DER is parseable and preserves the same public key bytes.
400394
loaded = PublicKey.from_der(der)

0 commit comments

Comments
 (0)