Skip to content

Commit 6fb62fe

Browse files
committed
Update ECDSA signature verification to match signing format changes
Signed-off-by: 0xJohnZW <roseiliend@gmail.com>
1 parent f17d6f5 commit 6fb62fe

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

src/hiero_sdk_python/crypto/public_key.py

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44

55
from cryptography.hazmat.primitives.asymmetric import ed25519, ec
66
from cryptography.hazmat.primitives import serialization, hashes
7+
from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
78
from hiero_sdk_python.hapi.services.basic_types_pb2 import Key
89
from hiero_sdk_python.hapi.services import basic_types_pb2
10+
from hiero_sdk_python.utils.crypto_utils import keccak256
911

1012
def _warn_ed25519_ambiguity(caller_name: str) -> None:
1113
warnings.warn(
@@ -485,15 +487,25 @@ def verify_ecdsa(self, signature: bytes, data: bytes) -> None:
485487
Verify an ECDSA (secp256k1) signature using SHA-256.
486488
487489
Args:
488-
signature: The DER-encoded signature bytes.
490+
signature: DER-encoded signature bytes, or raw 64-byte signature (r + s concatenated, 32 bytes each)
489491
data: The original message bytes.
490492
491493
Raises:
492494
InvalidSignature: If the signature does not match.
493495
"""
494496
if not isinstance(self._public_key, ec.EllipticCurvePublicKey):
495497
raise TypeError("Not an ECDSA key")
496-
self._public_key.verify(signature, data, ec.ECDSA(hashes.SHA256()))
498+
499+
# Convert raw 64-byte signature to DER format
500+
if len(signature) == 64:
501+
r = int.from_bytes(signature[:32], "big")
502+
s = int.from_bytes(signature[32:], "big")
503+
signature_der = asym_utils.encode_dss_signature(r, s)
504+
else:
505+
signature_der = signature
506+
507+
data_hash = keccak256(data)
508+
self._public_key.verify(signature_der, data_hash, ec.ECDSA(asym_utils.Prehashed(hashes.SHA256())))
497509

498510
def __repr__(self) -> str:
499511
"""

0 commit comments

Comments
 (0)