|
4 | 4 |
|
5 | 5 | from cryptography.hazmat.primitives.asymmetric import ed25519, ec
|
6 | 6 | from cryptography.hazmat.primitives import serialization, hashes
|
| 7 | +from cryptography.hazmat.primitives.asymmetric import utils as asym_utils |
7 | 8 | from hiero_sdk_python.hapi.services.basic_types_pb2 import Key
|
8 | 9 | from hiero_sdk_python.hapi.services import basic_types_pb2
|
| 10 | +from hiero_sdk_python.utils.crypto_utils import keccak256 |
9 | 11 |
|
10 | 12 | def _warn_ed25519_ambiguity(caller_name: str) -> None:
|
11 | 13 | warnings.warn(
|
@@ -485,15 +487,25 @@ def verify_ecdsa(self, signature: bytes, data: bytes) -> None:
|
485 | 487 | Verify an ECDSA (secp256k1) signature using SHA-256.
|
486 | 488 |
|
487 | 489 | 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) |
489 | 491 | data: The original message bytes.
|
490 | 492 |
|
491 | 493 | Raises:
|
492 | 494 | InvalidSignature: If the signature does not match.
|
493 | 495 | """
|
494 | 496 | if not isinstance(self._public_key, ec.EllipticCurvePublicKey):
|
495 | 497 | 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()))) |
497 | 509 |
|
498 | 510 | def __repr__(self) -> str:
|
499 | 511 | """
|
|
0 commit comments