Skip to content

Commit

Permalink
Tests: add tests for sign
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmer25 committed Jan 19, 2024
1 parent 6f66e3c commit 0d3c7fa
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 1 deletion.
37 changes: 37 additions & 0 deletions test/python/test_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
send_and_navigate,
get_current_commit
)
from utils.message import RawMessage

TESTS_ROOT_DIR = Path(__file__).parent

Expand Down Expand Up @@ -302,3 +303,39 @@ def test_get_all_hwm(

assert received_test_hwm == test_hwm, \
f"Expected test hmw {test_hwm} but got {received_test_hwm}"


@pytest.mark.parametrize("account", [DEFAULT_ACCOUNT])
@pytest.mark.parametrize("with_hash", [False, True])
def test_sign_message(
account: Account,
with_hash: bool,
client: TezosClient,
firmware: Firmware,
navigator: Navigator) -> None:
"""Test the SIGN(_WITH_HASH) instruction."""

main_chain_id: int = 0
main_hwm = Hwm(0)
test_hwm = Hwm(0)

instructions = get_setup_app_context_instructions(firmware)

send_and_navigate(
send=lambda: client.setup_app_context(
account,
main_chain_id,
main_hwm,
test_hwm),
navigate=lambda: navigator.navigate(instructions))

tx = RawMessage("0270000007000000000000000000000000000000000000000000000000000000000000000014000000000003000000010000000000000000000000000000000000000000000000000000000000000000")

if with_hash:
signature = client.sign_message(account, tx)
account.check_signature(signature, bytes(tx))
else:
tx_hash, signature = client.sign_message_with_hash(account, tx)
assert tx_hash == tx.hash, \
f"Expected hash {tx.hash.hex()} but got {tx_hash.hex()}"
account.check_signature(signature, bytes(tx))
33 changes: 32 additions & 1 deletion test/python/utils/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,38 @@ def sign_message(self,
sig_scheme=account.sig_scheme,
payload=bytes(account.path))

return self._exchange(
signature = self._exchange(
ins=Ins.SIGN,
index=Index.LAST,
payload=bytes(message))

assert len(signature) == 64, \
f"Signatures should be {64} bytes long " \
f"but {signature.hex()} is {len(signature)} bytes long"

return signature

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

self._exchange(
ins=Ins.SIGN_WITH_HASH,
sig_scheme=account.sig_scheme,
payload=bytes(account.path))

data = self._exchange(
ins=Ins.SIGN_WITH_HASH,
index=Index.LAST,
payload=bytes(message))

assert len(data) == Message.HASH_SIZE + 64, \
f"The data is expected to be a hash followed by a signature.\n" \
f"Combined, it should be {Message.HASH_SIZE + 64} bytes long " \
f"but {data.hex()} is {len(data)} bytes long"

return (
data[:Message.HASH_SIZE],
data[Message.HASH_SIZE:]
)

0 comments on commit 0d3c7fa

Please sign in to comment.