Skip to content

Commit

Permalink
Merge pull request #31 from blooo-io/feat/LDG-530--nano-app-implement…
Browse files Browse the repository at this point in the history
…-signtransferwithscheduleand

Feat/LDG-530--nano-app-implement-signtransferwithscheduleand
  • Loading branch information
n4l5u0r authored Dec 9, 2024
2 parents 5124c01 + 01dd25e commit 2d8a950
Show file tree
Hide file tree
Showing 46 changed files with 142 additions and 6 deletions.
49 changes: 47 additions & 2 deletions tests/application_client/boilerplate_command_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class P1(IntEnum):
P1_NO_CONFIRM = 0x01
# Parameter 1 for the scheduled amounts
P1_SCHEDULED_AMOUNTS = 0x01
# Parameter 1 for scheduled transfer with memo
P1_MEMO_SCHEDULED_TRANSFER = 0x03
P1_INITIAL_SCHEDULED_TRANSFER_WITH_MEMO = 0x02
# Basic P1 for all instructions
P1_NONE = 0x00

Expand Down Expand Up @@ -179,8 +182,8 @@ def sign_simple_transfer_with_memo(
data=data,
)
index += 1
memo_chunk = split_message(memo, MAX_APDU_LEN)
for chunk in memo_chunk:
memo_chunks = split_message(memo, MAX_APDU_LEN)
for chunk in memo_chunks:
self.backend.exchange(
cla=CLA,
ins=InsType.SIGN_TRANSFER_WITH_MEMO,
Expand Down Expand Up @@ -227,6 +230,48 @@ def sign_tx_with_schedule_part_2(self, data: bytes) -> Generator[None, None, Non
) as response:
yield response

def sign_tx_with_schedule_and_memo_part_1(
self, path: str, header_and_to_address: bytes, num_pairs: int, memo_length: int
) -> RAPDU:
data = pack_derivation_path(path)
data += header_and_to_address
data += num_pairs.to_bytes(1, byteorder="big")
memo_length_bytes = memo_length.to_bytes(2, byteorder="big")
data += memo_length_bytes
return self.backend.exchange(
cla=CLA,
ins=InsType.SIGN_TRANSFER_WITH_SCHEDULE_AND_MEMO,
p1=P1.P1_INITIAL_SCHEDULED_TRANSFER_WITH_MEMO,
p2=P2.P2_NONE,
data=data,
)

@contextmanager
def sign_tx_with_schedule_and_memo_part_2(
self, memo_chunk: bytes
) -> Generator[None, None, None]:
with self.backend.exchange_async(
cla=CLA,
ins=InsType.SIGN_TRANSFER_WITH_SCHEDULE_AND_MEMO,
p1=P1.P1_MEMO_SCHEDULED_TRANSFER,
p2=P2.P2_NONE,
data=memo_chunk,
) as response:
yield response

@contextmanager
def sign_tx_with_schedule_and_memo_part_3(
self, data: bytes
) -> Generator[None, None, None]:
with self.backend.exchange_async(
cla=CLA,
ins=InsType.SIGN_TRANSFER_WITH_SCHEDULE_AND_MEMO,
p1=P1.P1_SCHEDULED_AMOUNTS,
p2=P2.P2_NONE,
data=data,
) as response:
yield response

@contextmanager
def sign_configure_delegation(
self, path: str, transaction: bytes
Expand Down
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.
99 changes: 95 additions & 4 deletions tests/test_sign_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from utils import navigate_until_text_and_compare, instructions_builder, split_message

MAX_SCHEDULE_PAIRS_IN_ONE_APDU: int = (250 // 16) * 16
MAX_APDU_LEN: int = 255


# In this test we send to the device a transaction to sign and validate it on screen
Expand Down Expand Up @@ -91,8 +92,7 @@ def test_sign_tx_simple_transfer_new_path(
# In this test we send to the device a transaction to sign and validate it on screen
# The transaction is short and will be sent in one chunk
# We will ensure that the displayed information is correct by using screenshots comparison
# TODO: fix this test
# @pytest.mark.active_test_scope
@pytest.mark.active_test_scope
def test_sign_tx_simple_transfer_with_memo_legacy_path(
backend, firmware, navigator, default_screenshot_path, test_name
):
Expand Down Expand Up @@ -135,8 +135,7 @@ def test_sign_tx_simple_transfer_with_memo_legacy_path(
# assert check_signature_validity(public_key, der_sig, transaction)


# TODO: fix this test
# @pytest.mark.active_test_scope
@pytest.mark.active_test_scope
def test_sign_tx_transfer_with_schedule_legacy_path(
backend, firmware, navigator, default_screenshot_path, test_name
):
Expand Down Expand Up @@ -214,3 +213,95 @@ def test_sign_tx_transfer_with_schedule_legacy_path(
response_hex
== "e22fa38f78a79db71e84376c4eec2382166cdc412994207e7631b0ba3828f069b17b6f30351a64c50e5efacec3fe25161e9f7131e0235cd740739b24e0b06308"
)


@pytest.mark.active_test_scope
def test_sign_tx_transfer_with_schedule_and_memo_legacy_path(
backend, firmware, navigator, default_screenshot_path, test_name
):
# Initialize the command sender client
client = BoilerplateCommandSender(backend)
# Define the path for the transaction
path = "m/1105/0/0/0/0/2/0/0"

# Create the transaction that will be sent to the device for signing
header_and_to_address = "20a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7000000000000000a0000000000000064000000290000000063de5da71820a845815bd43a1999e90fbf971537a70392eb38f89e6bd32b3dd70e1a9551d7"
header_and_to_address = bytes.fromhex(header_and_to_address)

# Define the memo
memo = "6474657374" # "test" in hex
memo = bytes.fromhex(memo)

memo_chunks = split_message(memo, MAX_APDU_LEN)

# Define the schedule pairs
pairs = [
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
"0000017a396883d90000000005f5e100",
]
joined_pairs = bytes.fromhex("".join(pairs))

# Ensure pairs are a multiple of 16 bytes
if len(joined_pairs) % 16 != 0:
raise ValueError("Pairs must be a multiple of 16 bytes")

# Split the pairs into chunks for APDU transmission
pairs_chunk = split_message(joined_pairs, MAX_SCHEDULE_PAIRS_IN_ONE_APDU)

# Send the first part of the transaction signing request
response = client.sign_tx_with_schedule_and_memo_part_1(
path=path,
header_and_to_address=header_and_to_address,
num_pairs=len(pairs),
memo_length=len(memo),
)
print("km------------response", response)
assert response.status == 0x9000
# Send the part with the memo
for chunk in memo_chunks:
with client.sign_tx_with_schedule_and_memo_part_2(memo_chunk=chunk):
# Navigate and compare screenshots for validation
navigator.navigate_and_compare(
default_screenshot_path,
test_name,
instructions_builder(6, backend),
10,
True,
False,
)

# Process each chunk of pairs
screenshots_so_far = 7
for chunk in pairs_chunk:
number_of_pairs_in_chunk = len(chunk) // 16
# Create the instructions to validate each pair
instructions = []
for _ in range(number_of_pairs_in_chunk):
instructions.extend(instructions_builder(2, backend))

# Send the second part of the transaction signing request
with client.sign_tx_with_schedule_part_2(data=chunk):
# Navigate and compare screenshots for validation
navigator.navigate_and_compare(
default_screenshot_path,
test_name,
instructions,
10,
False,
False,
screenshots_so_far,
)
screenshots_so_far += number_of_pairs_in_chunk * 3

# The device as yielded the result, parse it and ensure that the signature is correct
response = client.get_async_response().data
response_hex = response.hex()
print("km------------response", response_hex)
# TODO: verify the signature
assert (
response_hex
== "9056db36dfa7b0ba722660b2becb227ed490dcaff9e332a7fba4c6d534ff0ff3368b21da8e7ebb62891be561261abd7c0435dfb46e596b1116c9996269d2a70b"
)

0 comments on commit 2d8a950

Please sign in to comment.