Skip to content

fix: correct ECDSA signature format and transaction signing logic #223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ dependencies = [
"grpcio==1.68.1",
"cryptography==44.0.0",
"python-dotenv==1.0.1",
"requests==2.32.3"
"requests==2.32.3",
"pycryptodome==3.23.0",
]
classifiers = [
"Development Status :: 2 - Pre-Alpha",
Expand Down
10 changes: 8 additions & 2 deletions src/hiero_sdk_python/crypto/private_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.asymmetric import ed25519, ec
from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
from hiero_sdk_python.crypto.public_key import PublicKey
from hiero_sdk_python.utils.crypto_utils import keccak256

class PrivateKey:
"""
Expand Down Expand Up @@ -273,8 +275,12 @@ def sign(self, data: bytes) -> bytes:
if isinstance(self._private_key, ed25519.Ed25519PrivateKey):
# Ed25519 automatically handles the hashing internally
return self._private_key.sign(data)
# ECDSA requires specifying a hash algorithm
return self._private_key.sign(data, ec.ECDSA(hashes.SHA256()))

data_hash = keccak256(data)
signature_der = self._private_key.sign(data_hash, ec.ECDSA(asym_utils.Prehashed(hashes.SHA256())))
r, s = asym_utils.decode_dss_signature(signature_der)
signature = r.to_bytes(32, "big") + s.to_bytes(32, "big")
return signature

def public_key(self) -> PublicKey:
"""
Expand Down
16 changes: 14 additions & 2 deletions src/hiero_sdk_python/crypto/public_key.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@

from cryptography.hazmat.primitives.asymmetric import ed25519, ec
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
from hiero_sdk_python.hapi.services.basic_types_pb2 import Key
from hiero_sdk_python.hapi.services import basic_types_pb2
from hiero_sdk_python.utils.crypto_utils import keccak256

def _warn_ed25519_ambiguity(caller_name: str) -> None:
warnings.warn(
Expand Down Expand Up @@ -485,15 +487,25 @@ def verify_ecdsa(self, signature: bytes, data: bytes) -> None:
Verify an ECDSA (secp256k1) signature using SHA-256.

Args:
signature: The DER-encoded signature bytes.
signature: DER-encoded signature bytes, or raw 64-byte signature (r + s concatenated, 32 bytes each)
data: The original message bytes.

Raises:
InvalidSignature: If the signature does not match.
"""
if not isinstance(self._public_key, ec.EllipticCurvePublicKey):
raise TypeError("Not an ECDSA key")
self._public_key.verify(signature, data, ec.ECDSA(hashes.SHA256()))

# Convert raw 64-byte signature to DER format
if len(signature) == 64:
r = int.from_bytes(signature[:32], "big")
s = int.from_bytes(signature[32:], "big")
signature_der = asym_utils.encode_dss_signature(r, s)
else:
signature_der = signature

data_hash = keccak256(data)
self._public_key.verify(signature_der, data_hash, ec.ECDSA(asym_utils.Prehashed(hashes.SHA256())))

def __repr__(self) -> str:
"""
Expand Down
14 changes: 10 additions & 4 deletions src/hiero_sdk_python/transaction/transaction.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,10 +164,16 @@ def sign(self, private_key):

public_key_bytes = private_key.public_key().to_bytes_raw()

sig_pair = basic_types_pb2.SignaturePair(
pubKeyPrefix=public_key_bytes,
ed25519=signature
)
if private_key.is_ed25519():
sig_pair = basic_types_pb2.SignaturePair(
pubKeyPrefix=public_key_bytes,
ed25519=signature
)
else:
sig_pair = basic_types_pb2.SignaturePair(
pubKeyPrefix=public_key_bytes,
ECDSA_secp256k1=signature
)

# We initialize the signature map for this body_bytes if it doesn't exist yet
self._signature_map.setdefault(body_bytes, basic_types_pb2.SignatureMap())
Expand Down
5 changes: 4 additions & 1 deletion tests/unit/test_keys_public.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@
import warnings
from cryptography.hazmat.primitives.asymmetric import ec, ed25519
from cryptography.hazmat.primitives import serialization, hashes
from cryptography.hazmat.primitives.asymmetric import utils as asym_utils
from cryptography.exceptions import InvalidSignature
from hiero_sdk_python.hapi.services.basic_types_pb2 import Key
from hiero_sdk_python.crypto.public_key import PublicKey
from hiero_sdk_python.utils.crypto_utils import keccak256

pytestmark = pytest.mark.unit

Expand Down Expand Up @@ -529,7 +531,8 @@ def test_verify_ecdsa_success(ecdsa_keypair):
pk = PublicKey(pub)

msg = b"some message"
signature = priv.sign(msg, ec.ECDSA(hashes.SHA256()))
msg_hash = keccak256(msg)
signature = priv.sign(msg_hash, ec.ECDSA(asym_utils.Prehashed(hashes.SHA256())))
# If the signature is correct, verify() returns None and raises no error.
pk.verify(signature, msg)

Expand Down