Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
20 changes: 14 additions & 6 deletions sdk/examples/remote-signer/app/signer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import hashlib
import ecdsa
from ecdsa import SECP256k1

from cryptography.hazmat.primitives import hashes
from cryptography.hazmat.primitives.asymmetric import ec
from cryptography.hazmat.primitives.asymmetric.utils import (
Prehashed,
decode_dss_signature,
)

# secp256k1 curve order half (for low-S check)
_HALF_ORDER = 0x3FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DBFE92F1A9F2A0746
Expand Down Expand Up @@ -29,14 +34,17 @@ def _bitcoin_message_magic(message: bytes) -> bytes:

def sign_bytes(message: bytes, private_key_bytes: bytes) -> str:
"""Sign message bytes using the Bitcoin signed message format."""
privkey = ecdsa.SigningKey.from_string(private_key_bytes, curve=SECP256k1)
privkey = ec.derive_private_key(
int.from_bytes(private_key_bytes, "big"), ec.SECP256K1()
)

prefixed = _bitcoin_message_magic(message)
digest = hashlib.sha256(hashlib.sha256(prefixed).digest()).digest()
raw = privkey.sign_digest(digest, sigencode=ecdsa.util.sigencode_string)
der_sig = privkey.sign(digest, ec.ECDSA(Prehashed(hashes.SHA256())))
r, s = decode_dss_signature(der_sig)
raw = r.to_bytes(32, "big") + s.to_bytes(32, "big")

s_int = int.from_bytes(raw[32:], "big")
rec_id = 27 if s_int <= _HALF_ORDER else 28
rec_id = 27 if s <= _HALF_ORDER else 28

return bytes([rec_id]).hex() + raw.hex()

Expand Down
8 changes: 4 additions & 4 deletions sdk/examples/remote-signer/requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
fastapi==0.115.6
uvicorn==0.34.0
ecdsa==0.19.1
python-dotenv==1.0.1
pytest==8.3.4
pytest-asyncio==0.25.2
cryptography==44.0.0
python-dotenv==1.2.2
pytest==9.0.3
pytest-asyncio==1.4.0
httpx==0.28.1
Loading