Skip to content

Commit

Permalink
Add test for Tx Simulation
Browse files Browse the repository at this point in the history
  • Loading branch information
cedelavergne-ledger committed Dec 10, 2024
1 parent 88986a6 commit cb0663d
Show file tree
Hide file tree
Showing 2 changed files with 203 additions and 1 deletion.
9 changes: 8 additions & 1 deletion tests/ragger/test_sign.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ def common(firmware: Firmware,
tx_params: dict,
test_name: str = "",
path: str = BIP32_PATH,
confirm: bool = False):
confirm: bool = False,
with_simu: bool = False):
app_client = EthAppClient(backend)

if tx_params["chainId"] == 3:
Expand Down Expand Up @@ -77,6 +78,12 @@ def common(firmware: Firmware,
else:
end_text = "Sign"

if with_simu:
navigator.navigate_and_compare(default_screenshot_path,
f"{test_name}/confirm",
[NavInsID.USE_CASE_CHOICE_REJECT],
screen_change_after_last_instruction=False)

scenario_navigator.review_approve(custom_screen_text=end_text, do_comparison=test_name!="")

# verify signature
Expand Down
195 changes: 195 additions & 0 deletions tests/ragger/test_tx_simulation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
from pathlib import Path
from web3 import Web3
import rlp
import pytest
from eth_utils import keccak

from ragger.error import ExceptionRAPDU
from ragger.backend import BackendInterface
from ragger.firmware import Firmware
from ragger.navigator import Navigator
from ragger.navigator.navigation_scenario import NavigateWithScenario

from test_sign import common as sign_tx

from client.client import EthAppClient, StatusWord
from client.settings import SettingID, settings_toggle



def test_tx_simulation_disabled(firmware: Firmware, backend: BackendInterface) -> None:
"""Test the TX Simulation APDU with the W3C setting disabled.
It should return an error
"""

if firmware.is_nano:
pytest.skip("Not yet supported on Nano")

app_client = EthAppClient(backend)

tx_hash = bytes.fromhex("deadbeaf"*8)
chain_id = 5
risk = 0x1234
category = 1
message = "This is a test message"
url = "https://www.ledger.com"
try:
app_client.provide_tx_simulation(tx_hash, risk, category, message, url, chain_id, True)
except ExceptionRAPDU as err:
assert err.status == StatusWord.NOT_IMPLEMENTED
else:
assert False # An exception should have been raised


def test_tx_simulation_enabled(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator) -> None:
"""Test the TX Simulation APDU with the W3C setting enabled"""

if firmware.is_nano:
pytest.skip("Not yet supported on Nano")

app_client = EthAppClient(backend)

tx_hash = bytes.fromhex("deadbeaf"*8)
chain_id = 5
risk = 0x1234
category = 1
message = "This is a test message"
url = "https://www.ledger.com"

# Toggle the W3C setting
settings_toggle(firmware, navigator, [SettingID.W3C])

response = app_client.provide_tx_simulation(tx_hash, risk, category, message, url, chain_id, True)
assert response.status == StatusWord.OK


def test_tx_simulation_with_good_tx(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
test_name: str,
default_screenshot_path: Path) -> None:
"""Test the TX Simulation APDU with a simple transaction"""

if firmware.is_nano:
pytest.skip("Not yet supported on Nano")

app_client = EthAppClient(backend)

chain_id = 5
risk = 0x8234
category = 1
message = "This is a test message"
url = "https://www.ledger.com"

tx_params: dict = {
"nonce": 21,
"gasPrice": Web3.to_wei(13, 'gwei'),
"gas": 21000,
"to": bytes.fromhex("5a321744667052affa8386ed49e00ef223cbffc3"),
"value": Web3.to_wei(1.22, "ether"),
"chainId": chain_id
}
# Compute the tx hash
tx = Web3().eth.account.create().sign_transaction(tx_params).rawTransaction
decoded = rlp.decode(tx)[:-3] # remove already computed signature
tx_hash = keccak(rlp.encode(decoded + [int(tx_params["chainId"]), bytes(), bytes()]))

# Toggle the W3C setting
settings_toggle(firmware, navigator, [SettingID.W3C])

response = app_client.provide_tx_simulation(tx_hash, risk, category, message, url, chain_id, False)
assert response.status == StatusWord.OK

sign_tx(firmware,
backend,
navigator,
scenario_navigator,
default_screenshot_path,
tx_params,
test_name,
with_simu=True)


def test_tx_simulation_with_bad_tx(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
test_name: str,
default_screenshot_path: Path) -> None:
"""Test the TX Simulation APDU with a simple transaction
but given a wrong tx hash in TX Simu APDU
"""

if firmware.is_nano:
pytest.skip("Not yet supported on Nano")

app_client = EthAppClient(backend)

chain_id = 5
risk = 0x8234
category = 1
message = "This is a test message"
url = "https://www.ledger.com"

tx_params: dict = {
"nonce": 21,
"gasPrice": Web3.to_wei(13, 'gwei'),
"gas": 21000,
"to": bytes.fromhex("5a321744667052affa8386ed49e00ef223cbffc3"),
"value": Web3.to_wei(1.22, "ether"),
"chainId": chain_id
}
# Wrong tx hash
tx_hash = bytes.fromhex("deadbeaf"*8)

# Toggle the W3C setting
settings_toggle(firmware, navigator, [SettingID.W3C])

response = app_client.provide_tx_simulation(tx_hash, risk, category, message, url, chain_id, False)
assert response.status == StatusWord.OK

sign_tx(firmware,
backend,
navigator,
scenario_navigator,
default_screenshot_path,
tx_params,
test_name,
with_simu=True)


def test_tx_simulation_sign_no_simu(firmware: Firmware,
backend: BackendInterface,
navigator: Navigator,
scenario_navigator: NavigateWithScenario,
test_name: str,
default_screenshot_path: Path) -> None:
"""Test the TX Transaction APDU without TX Simulation APDU
Here, the W3C setting is enabled"""

if firmware.is_nano:
pytest.skip("Not yet supported on Nano")

tx_params: dict = {
"nonce": 21,
"gasPrice": Web3.to_wei(13, 'gwei'),
"gas": 21000,
"to": bytes.fromhex("5a321744667052affa8386ed49e00ef223cbffc3"),
"value": Web3.to_wei(1.22, "ether"),
"chainId": 5
}

# Toggle the W3C setting
settings_toggle(firmware, navigator, [SettingID.W3C])

sign_tx(firmware,
backend,
navigator,
scenario_navigator,
default_screenshot_path,
tx_params,
test_name,
with_simu=True)

0 comments on commit cb0663d

Please sign in to comment.