Files

255 lines
7.9 KiB
Python
Raw Permalink Normal View History

2024-03-26 09:43:26 +01:00
from typing import Callable, Optional, Any
import json
2023-08-23 11:49:35 +02:00
import pytest
2024-03-26 09:43:26 +01:00
from web3 import Web3
2023-08-23 11:49:35 +02:00
from ragger.error import ExceptionRAPDU
from ragger.firmware import Firmware
from ragger.backend import BackendInterface
2024-04-16 23:48:16 +02:00
from ragger.navigator.navigation_scenario import NavigateWithScenario
2024-03-26 09:43:26 +01:00
from constants import ABIS_FOLDER
2023-08-23 11:49:35 +02:00
2024-04-16 23:48:16 +02:00
from client.client import EthAppClient, StatusWord
import client.response_parser as ResponseParser
from client.utils import get_selector_from_data, recover_transaction
2023-08-23 11:49:35 +02:00
BIP32_PATH = "m/44'/60'/0'/0/0"
NONCE = 21
GAS_PRICE = 13
GAS_LIMIT = 21000
FROM = bytes.fromhex("1122334455667788990011223344556677889900")
TO = bytes.fromhex("0099887766554433221100998877665544332211")
2023-11-21 11:24:05 +01:00
NFTS = [(1, 3), (5, 2), (7, 4)] # tuples of (token_id, amount)
2023-08-23 11:49:35 +02:00
DATA = "Some data".encode()
DEVICE_ADDR: Optional[bytes] = None
2023-08-23 11:49:35 +02:00
2023-11-21 11:24:05 +01:00
class NFTCollection:
2023-08-23 11:49:35 +02:00
addr: bytes
name: str
chain_id: int
2023-11-21 11:24:05 +01:00
def __init__(self, addr: bytes, name: str, chain_id: int, contract):
2023-08-23 11:49:35 +02:00
self.addr = addr
self.name = name
self.chain_id = chain_id
self.contract = contract
2023-08-23 11:49:35 +02:00
2023-11-21 11:24:05 +01:00
class Action:
fn_name: str
fn_args: list[Any]
2023-11-21 11:24:05 +01:00
2024-04-16 23:48:16 +02:00
def __init__(self, fn_name: str, fn_args: list[Any]):
self.fn_name = fn_name
self.fn_args = fn_args
2023-08-23 11:49:35 +02:00
2023-11-21 11:24:05 +01:00
2024-03-26 09:43:26 +01:00
def common_test_nft(firmware: Firmware,
backend: BackendInterface,
2024-04-16 23:48:16 +02:00
scenario_navigator: NavigateWithScenario,
2023-08-23 11:49:35 +02:00
collec: NFTCollection,
action: Action,
reject: bool,
plugin_name: str):
global DEVICE_ADDR
2024-03-26 09:43:26 +01:00
app_client = EthAppClient(backend)
2023-08-23 11:49:35 +02:00
if firmware == Firmware.NANOS:
2023-08-23 11:49:35 +02:00
pytest.skip("Not supported on LNS")
if DEVICE_ADDR is None: # to only have to request it once
with app_client.get_public_addr(display=False):
pass
_, DEVICE_ADDR, _ = ResponseParser.pk_addr(app_client.response().data)
2024-07-30 14:35:56 +02:00
data = collec.contract.encode_abi(action.fn_name, action.fn_args)
app_client.set_plugin(plugin_name,
collec.addr,
get_selector_from_data(data),
collec.chain_id)
2024-06-11 10:59:14 +02:00
app_client.provide_nft_metadata(collec.name, collec.addr, collec.chain_id)
tx_params = {
"nonce": NONCE,
"gasPrice": Web3.to_wei(GAS_PRICE, "gwei"),
"gas": GAS_LIMIT,
"to": collec.addr,
"value": 0,
"chainId": collec.chain_id,
"data": data,
}
with app_client.sign(BIP32_PATH, tx_params):
2024-04-16 23:48:16 +02:00
test_name = f"{plugin_name.lower()}_{action.fn_name}_{str(collec.chain_id)}"
if reject:
test_name += "-rejected"
scenario_navigator.review_reject(test_name=test_name)
2024-04-16 23:48:16 +02:00
else:
if firmware.is_nano:
2024-04-16 23:48:16 +02:00
end_text = "Accept"
else:
end_text = "Sign"
scenario_navigator.review_approve(test_name=test_name, custom_screen_text=end_text)
2024-04-16 23:48:16 +02:00
# verify signature
vrs = ResponseParser.signature(app_client.response().data)
addr = recover_transaction(tx_params, vrs)
assert addr == DEVICE_ADDR
2023-08-23 11:49:35 +02:00
def common_test_nft_reject(test_fn: Callable,
2024-03-26 09:43:26 +01:00
firmware: Firmware,
backend: BackendInterface,
2024-04-16 23:48:16 +02:00
scenario_navigator: NavigateWithScenario,
2023-08-23 11:49:35 +02:00
collec: NFTCollection,
action: Action):
with pytest.raises(ExceptionRAPDU) as e:
test_fn(firmware, backend, scenario_navigator, collec, action, True)
assert e.value.status == StatusWord.CONDITION_NOT_SATISFIED
2023-08-23 11:49:35 +02:00
# ERC-721
2023-11-21 11:24:05 +01:00
2023-08-23 11:49:35 +02:00
ERC721_PLUGIN = "ERC721"
2024-03-26 09:43:26 +01:00
with open(f"{ABIS_FOLDER}/erc721.json", encoding="utf-8") as file:
contract_erc721 = Web3().eth.contract(
abi=json.load(file),
address=bytes(20)
2023-08-23 11:49:35 +02:00
)
collecs_721 = [
NFTCollection(bytes.fromhex("bc4ca0eda7647a8ab7c2061c2e118a18a936f13d"),
2023-11-21 11:24:05 +01:00
"Bored Ape Yacht Club",
1,
contract_erc721),
2023-08-23 11:49:35 +02:00
NFTCollection(bytes.fromhex("670fd103b1a08628e9557cd66b87ded841115190"),
2023-11-21 11:24:05 +01:00
"y00ts",
137,
contract_erc721),
2023-08-23 11:49:35 +02:00
NFTCollection(bytes.fromhex("2909cf13e458a576cdd9aab6bd6617051a92dacf"),
2023-11-21 11:24:05 +01:00
"goerlirocks",
5,
contract_erc721),
2023-08-23 11:49:35 +02:00
]
actions_721 = [
2024-04-16 23:48:16 +02:00
Action("safeTransferFrom", [FROM, TO, NFTS[0][0], DATA]),
Action("safeTransferFrom", [FROM, TO, NFTS[0][0]]),
Action("transferFrom", [FROM, TO, NFTS[0][0]]),
Action("approve", [TO, NFTS[0][0]]),
Action("setApprovalForAll", [TO, False]),
2023-08-23 11:49:35 +02:00
]
2024-03-26 09:43:26 +01:00
@pytest.fixture(name="collec_721", params=collecs_721)
def collec_721_fixture(request) -> NFTCollection:
2023-08-23 11:49:35 +02:00
return request.param
2023-11-21 11:24:05 +01:00
2024-03-26 09:43:26 +01:00
@pytest.fixture(name="action_721", params=actions_721)
def action_721_fixture(request) -> Action:
2023-08-23 11:49:35 +02:00
return request.param
2023-11-21 11:24:05 +01:00
2023-08-23 11:49:35 +02:00
def test_erc721(firmware: Firmware,
2023-11-21 11:24:05 +01:00
backend: BackendInterface,
2024-04-16 23:48:16 +02:00
scenario_navigator: NavigateWithScenario,
2023-11-21 11:24:05 +01:00
collec_721: NFTCollection,
action_721: Action,
reject: bool = False):
2023-08-23 11:49:35 +02:00
common_test_nft(firmware,
backend,
2024-04-16 23:48:16 +02:00
scenario_navigator,
2023-08-23 11:49:35 +02:00
collec_721,
action_721,
reject,
ERC721_PLUGIN)
2023-11-21 11:24:05 +01:00
2023-08-23 11:49:35 +02:00
def test_erc721_reject(firmware: Firmware,
2023-11-21 11:24:05 +01:00
backend: BackendInterface,
scenario_navigator: NavigateWithScenario):
2023-08-23 11:49:35 +02:00
common_test_nft_reject(test_erc721,
firmware,
backend,
2024-04-16 23:48:16 +02:00
scenario_navigator,
2023-08-23 11:49:35 +02:00
collecs_721[0],
actions_721[0])
2023-11-21 11:24:05 +01:00
2023-08-23 11:49:35 +02:00
# ERC-1155
ERC1155_PLUGIN = "ERC1155"
2024-03-26 09:43:26 +01:00
with open(f"{ABIS_FOLDER}/erc1155.json", encoding="utf-8") as file:
contract_erc1155 = Web3().eth.contract(
abi=json.load(file),
address=bytes(20)
2023-08-23 11:49:35 +02:00
)
2023-08-23 11:49:35 +02:00
collecs_1155 = [
NFTCollection(bytes.fromhex("495f947276749ce646f68ac8c248420045cb7b5e"),
2023-11-21 11:24:05 +01:00
"OpenSea Shared Storefront",
1,
contract_erc1155),
2023-08-23 11:49:35 +02:00
NFTCollection(bytes.fromhex("2953399124f0cbb46d2cbacd8a89cf0599974963"),
2023-11-21 11:24:05 +01:00
"OpenSea Collections",
137,
contract_erc1155),
2023-08-23 11:49:35 +02:00
NFTCollection(bytes.fromhex("f4910c763ed4e47a585e2d34baa9a4b611ae448c"),
2023-11-21 11:24:05 +01:00
"OpenSea Collections",
5,
contract_erc1155),
2023-08-23 11:49:35 +02:00
]
actions_1155 = [
2024-04-16 23:48:16 +02:00
Action("safeTransferFrom", [FROM, TO, NFTS[0][0], NFTS[0][1], DATA]),
Action("safeBatchTransferFrom",
[
FROM,
TO,
list(map(lambda nft: nft[0], NFTS)),
list(map(lambda nft: nft[1], NFTS)),
DATA
2024-04-16 23:48:16 +02:00
]),
Action("setApprovalForAll", [TO, False]),
2023-08-23 11:49:35 +02:00
]
2023-11-21 11:24:05 +01:00
2024-03-26 09:43:26 +01:00
@pytest.fixture(name="collec_1155", params=collecs_1155)
def collec_1155_fixture(request) -> bool:
2023-08-23 11:49:35 +02:00
return request.param
2023-11-21 11:24:05 +01:00
2024-03-26 09:43:26 +01:00
@pytest.fixture(name="action_1155", params=actions_1155)
def action_1155_fixture(request) -> Action:
2023-08-23 11:49:35 +02:00
return request.param
2023-11-21 11:24:05 +01:00
2023-08-23 11:49:35 +02:00
def test_erc1155(firmware: Firmware,
backend: BackendInterface,
2024-04-16 23:48:16 +02:00
scenario_navigator: NavigateWithScenario,
2023-08-23 11:49:35 +02:00
collec_1155: NFTCollection,
action_1155: Action,
reject: bool = False):
common_test_nft(firmware,
backend,
2024-04-16 23:48:16 +02:00
scenario_navigator,
2023-08-23 11:49:35 +02:00
collec_1155,
action_1155,
reject,
ERC1155_PLUGIN)
2023-11-21 11:24:05 +01:00
2023-08-23 11:49:35 +02:00
def test_erc1155_reject(firmware: Firmware,
backend: BackendInterface,
scenario_navigator: NavigateWithScenario):
2023-08-23 11:49:35 +02:00
common_test_nft_reject(test_erc1155,
firmware,
backend,
2024-04-16 23:48:16 +02:00
scenario_navigator,
2023-08-23 11:49:35 +02:00
collecs_1155[0],
actions_1155[0])