Skip to content

Commit

Permalink
[tests] use the base58 format for signature
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmer25 committed Oct 22, 2024
1 parent 127d754 commit 5578c50
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 22 deletions.
32 changes: 15 additions & 17 deletions test/utils/account.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,17 +82,8 @@ def from_bytes(cls, raw_path: bytes) -> 'BipPath':
class Signature:
"""Class representing signature."""

GENERIC_SIGNATURE_PREFIX = bytes.fromhex("04822b") # sig(96)

def __init__(self, value: bytes):
value = Signature.GENERIC_SIGNATURE_PREFIX + value
self.value: bytes = base58.b58encode_check(value)

def __repr__(self) -> str:
return self.value.hex()

@classmethod
def from_tlv(cls, tlv: Union[bytes, bytearray]) -> 'Signature':
@staticmethod
def from_secp256_tlv(tlv: Union[bytes, bytearray]) -> bytes:
"""Get the signature encapsulated in a TLV."""
# See:
# https://developers.ledger.com/docs/embedded-app/crypto-api/lcx__ecdsa_8h/#cx_ecdsa_sign
Expand Down Expand Up @@ -125,14 +116,21 @@ def from_tlv(cls, tlv: Union[bytes, bytearray]) -> 'Signature':
# A size adjustment is required here.
def adjust_size(data, size):
return data[-size:].rjust(size, b'\x00')
return Signature(adjust_size(r, 32) + adjust_size(s, 32))
return adjust_size(r, 32) + adjust_size(s, 32)

@classmethod
def from_bytes(cls, data: bytes, sig_scheme: SigScheme) -> 'Signature':
def from_bytes(cls, data: bytes, sig_scheme: SigScheme) -> str:
"""Get the signature according to the SigScheme."""
if sig_scheme in { SigScheme.ED25519, SigScheme.BIP32_ED25519 }:
return Signature(data)
return Signature.from_tlv(data)
prefix = bytes([9, 245, 205, 134, 18])
elif sig_scheme in { SigScheme.SECP256K1, SigScheme.SECP256R1 }:
prefix = bytes([13, 115, 101, 19, 63]) if sig_scheme == SigScheme.SECP256K1 \
else bytes([54, 240, 44, 52])
data = Signature.from_secp256_tlv(data)
else:
assert False, f"Wrong signature type: {sig_scheme}"

return base58.b58encode_check(prefix + data).decode()

class PublicKey:
"""Set of functions over public key management"""
Expand Down Expand Up @@ -250,14 +248,14 @@ def sign_prehashed_message(self, prehashed_message: bytes) -> bytes:
raise ValueError(f"Account do not have a right signature type: {self.sig_scheme}")

def check_signature(self,
signature: Union[bytes, Signature],
signature: Union[str, bytes],
message: Union[str, bytes]):
"""Check that the signature is the signature of the message by the account."""
if isinstance(message, str):
message = bytes.fromhex(message)
if isinstance(signature, bytes):
signature = Signature.from_bytes(signature, self.sig_scheme)
assert self.key.verify(signature.value, message), \
assert self.key.verify(signature.encode(), message), \
f"Fail to verify signature {signature}, \n\
with account {self} \n\
and message {message.hex()}"
4 changes: 2 additions & 2 deletions test/utils/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def get_all_hwm(self) -> Tuple[str, Hwm, Hwm]:

def sign_message(self,
account: Account,
message: Message) -> Signature:
message: Message) -> str:
"""Send the SIGN instruction."""

self._exchange(
Expand All @@ -359,7 +359,7 @@ def sign_message(self,

def sign_message_with_hash(self,
account: Account,
message: Message) -> Tuple[bytes, Signature]:
message: Message) -> Tuple[bytes, str]:
"""Send the SIGN_WITH_HASH instruction."""

self._exchange(
Expand Down
6 changes: 3 additions & 3 deletions test/utils/navigator.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

from common import TESTS_ROOT_DIR, EMPTY_PATH
from utils.client import TezosClient, Hwm
from utils.account import Account, Signature
from utils.account import Account
from utils.message import Delegation

RESPONSE = TypeVar('RESPONSE')
Expand Down Expand Up @@ -511,7 +511,7 @@ def sign_delegation(self,
account: Account,
delegation: Delegation,
navigate: Optional[Callable] = None,
**kwargs) -> Signature:
**kwargs) -> str:
"""Send a sign request on delegation and navigate until accept"""
if navigate is None:
navigate = self.accept_sign_navigate
Expand All @@ -527,7 +527,7 @@ def sign_delegation_with_hash(self,
account: Account,
delegation: Delegation,
navigate: Optional[Callable] = None,
**kwargs) -> Tuple[bytes, Signature]:
**kwargs) -> Tuple[bytes, str]:
"""Send a sign and get hash request on delegation and navigate until accept"""
if navigate is None:
navigate = self.accept_sign_navigate
Expand Down

0 comments on commit 5578c50

Please sign in to comment.