Skip to content

Commit

Permalink
Tests: add tests for signing delegation
Browse files Browse the repository at this point in the history
  • Loading branch information
spalmer25 committed Feb 5, 2024
1 parent 4b04f50 commit dbbcf31
Show file tree
Hide file tree
Showing 141 changed files with 137 additions and 2 deletions.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions test/python/test_instructions.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from utils.helper import get_current_commit
from utils.message import (
Message,
Delegation,
Preattestation,
Attestation,
AttestationDal,
Expand Down Expand Up @@ -585,6 +586,50 @@ def test_sign_level_authorized(
with StatusCode.WRONG_VALUES.expected():
client.sign_message(account, message_2)


@pytest.mark.parametrize("account", ACCOUNTS)
@pytest.mark.parametrize("with_hash", [False, True])
def test_sign_delegation(
account: Account,
with_hash: bool,
tezos_navigator: TezosNavigator,
test_name: Path) -> None:
"""Test the SIGN(_WITH_HASH) instruction on delegation."""
snap_path = Path(f"{account}")

tezos_navigator.setup_app_context(
account,
DEFAULT_CHAIN_ID,
main_hwm=Hwm(0),
test_hwm=Hwm(0)
)

delegation = Delegation(
delegate=account.public_key_hash,
source=account.public_key_hash,
)

raw_delegation = delegation.forge()

if with_hash:
signature = tezos_navigator.sign_delegation(
account,
delegation,
snap_path=snap_path
)
account.check_signature(signature, bytes(raw_delegation))
else:
delegation_hash, signature = \
tezos_navigator.sign_delegation_with_hash(
account,
delegation,
snap_path=snap_path
)
assert delegation_hash == raw_delegation.hash, \
f"Expected hash {raw_delegation.hash.hex()} but got {delegation_hash.hex()}"
account.check_signature(signature, bytes(raw_delegation))


def test_sign_not_authorized_key(
client: TezosClient,
tezos_navigator: TezosNavigator) -> None:
Expand Down
32 changes: 32 additions & 0 deletions test/python/utils/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
from enum import IntEnum
from hashlib import blake2b
from typing import Optional, Union
from pytezos import pytezos
from pytezos.operation.group import OperationGroup
from pytezos.block.forge import forge_int_fixed, forge_fitness
from pytezos.michelson import forge

Expand Down Expand Up @@ -101,6 +103,35 @@ class OperationTag(IntEnum):
# Context_hash.zero
DEFAULT_CONTEXT_HASH = "CoUeJrcPBj3T3iJL3PY4jZHnmZa5rRZ87VQPdSBNBcwZRMWJGh9j"

class UnsafeOp:
"""Class representing an unsafe operation."""

operation: OperationGroup

def __init__(self, operation: OperationGroup):
self.operation = operation

def forge(self, branch: str = DEFAULT_BLOCK_HASH) -> Message:
"""Forge the operation."""
watermark = forge_int_fixed(MagicByte.UNSAFE_OP, 1)
self.operation.branch = branch
raw = watermark + bytes.fromhex(self.operation.forge())
return RawMessage(raw)

class Delegation(UnsafeOp):
"""Class representing a delegation."""

def __init__(self,
delegate: str,
source: str,
counter: int = 0,
fee: int = 0,
gas_limit: int = 0,
storage_limit: int = 0):
ctxt = pytezos.using()
delegation = ctxt.delegation(delegate, source, counter, fee, gas_limit, storage_limit)
super().__init__(delegation)

class Preattestation:
"""Class representing a preattestation."""

Expand Down Expand Up @@ -263,6 +294,7 @@ class BlockHeader:
"""Class representing a block header."""

level: int
proto_level: int
predecessor: str
timestamp: str
validation_pass: int
Expand Down
62 changes: 60 additions & 2 deletions test/python/utils/navigator.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Module providing a tezos navigator."""

from pathlib import Path
from typing import TypeVar, Callable, List, Optional, Union
from typing import TypeVar, Callable, Optional, Tuple, Union
import time

from multiprocessing.pool import ThreadPool
Expand All @@ -20,7 +20,11 @@

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

RESPONSE = TypeVar('RESPONSE')

Expand Down Expand Up @@ -324,3 +328,57 @@ def setup_app_context(self,
),
navigate=lambda: navigate(**kwargs)
)

def accept_sign_navigate(self, **kwargs):
"""Navigate until accept signing"""
if self.firmware.is_nano:
self.navigate_and_compare(
navigate_instruction = NavInsID.RIGHT_CLICK,
validation_instructions = [NavInsID.BOTH_CLICK],
text = 'Accept',
**kwargs
)
else:
self.navigate_and_compare(
navigate_instruction = NavInsID.USE_CASE_REVIEW_TAP,
validation_instructions = [
NavInsID.USE_CASE_CHOICE_CONFIRM,
NavInsID.USE_CASE_STATUS_DISMISS
],
text = 'Approve',
**kwargs
)

def sign_delegation(self,
account: Account,
delegation: Delegation,
branch: str = DEFAULT_BLOCK_HASH,
navigate: Optional[Callable] = None,
**kwargs) -> Signature:
"""Send a sign request on delegation and navigate until accept"""
if navigate is None:
navigate = self.accept_sign_navigate
return send_and_navigate(
send=lambda: self.client.sign_message(
account,
delegation.forge(branch)
),
navigate=lambda: navigate(**kwargs)
)

def sign_delegation_with_hash(self,
account: Account,
delegation: Delegation,
branch: str = DEFAULT_BLOCK_HASH,
navigate: Optional[Callable] = None,
**kwargs) -> Tuple[bytes, Signature]:
"""Send a sign and get hash request on delegation and navigate until accept"""
if navigate is None:
navigate = self.accept_sign_navigate
return send_and_navigate(
send=lambda: self.client.sign_message_with_hash(
account,
delegation.forge(branch)
),
navigate=lambda: navigate(**kwargs)
)

0 comments on commit dbbcf31

Please sign in to comment.