From 5578c5046ffa2c09784da4c03f4bc69c63bc5172 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20Palmer?= Date: Tue, 22 Oct 2024 09:50:24 +0200 Subject: [PATCH] [tests] use the base58 format for signature --- test/utils/account.py | 32 +++++++++++++++----------------- test/utils/client.py | 4 ++-- test/utils/navigator.py | 6 +++--- 3 files changed, 20 insertions(+), 22 deletions(-) diff --git a/test/utils/account.py b/test/utils/account.py index 09dd96ee..eef82e3b 100644 --- a/test/utils/account.py +++ b/test/utils/account.py @@ -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 @@ -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""" @@ -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()}" diff --git a/test/utils/client.py b/test/utils/client.py index 97def54c..ab77e7ac 100644 --- a/test/utils/client.py +++ b/test/utils/client.py @@ -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( @@ -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( diff --git a/test/utils/navigator.py b/test/utils/navigator.py index ded96d4c..e69f94e8 100644 --- a/test/utils/navigator.py +++ b/test/utils/navigator.py @@ -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') @@ -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 @@ -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