Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test now verifies the signature against the wallet's address #140

Merged
merged 1 commit into from
Feb 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import pytest

from ragger.conftest import configuration
from .utils import WalletAddr


###########################
### CONFIGURATION START ###
Expand All @@ -20,3 +24,7 @@

# Pull all features from the base ragger conftest using the overridden configuration
pytest_plugins = ("ragger.conftest.base_conftest", )

@pytest.fixture
def wallet_addr(backend):
return WalletAddr(backend)
36 changes: 21 additions & 15 deletions tests/test_swap.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
from pathlib import Path
import json
import os

import datetime

from web3 import Web3
from eth_typing import ChainId

from ledger_app_clients.ethereum.client import EthAppClient, StatusWord
from ledger_app_clients.ethereum.utils import get_selector_from_data
from ledger_app_clients.ethereum.client import EthAppClient
import ledger_app_clients.ethereum.response_parser as ResponseParser
from ledger_app_clients.ethereum.utils import get_selector_from_data, recover_transaction
from ragger.navigator import NavInsID

from .utils import get_appname_from_makefile
from .utils import get_appname_from_makefile, DERIVATION_PATH


ROOT_SCREENSHOT_PATH = Path(__file__).parent
ABIS_FOLDER = "%s/abis" % (os.path.dirname(__file__))

PLUGIN_NAME = get_appname_from_makefile()


with open("%s/0x000102030405060708090a0b0c0d0e0f10111213.abi.json" % (ABIS_FOLDER)) as file:
contract = Web3().eth.contract(
abi=json.load(file),
Expand All @@ -28,7 +29,7 @@


# EDIT THIS: build your own test
def test_swap_exact_eth_for_token(backend, firmware, navigator, test_name):
def test_swap_exact_eth_for_token(backend, firmware, navigator, test_name, wallet_addr):
apaillier-ledger marked this conversation as resolved.
Show resolved Hide resolved
client = EthAppClient(backend)

data = contract.encodeABI("swapExactETHForTokens", [
Expand All @@ -48,17 +49,18 @@ def test_swap_exact_eth_for_token(backend, firmware, navigator, test_name):
get_selector_from_data(data)):
pass

tx_params = {
"nonce": 20,
"maxFeePerGas": Web3.to_wei(145, "gwei"),
"maxPriorityFeePerGas": Web3.to_wei(1.5, "gwei"),
"gas": 173290,
"to": contract.address,
"value": Web3.to_wei(0.1, "ether"),
"chainId": ChainId.ETH,
"data": data
}
# send the transaction
with client.sign("m/44'/60'/1'/0/0", {
"nonce": 20,
"maxFeePerGas": Web3.to_wei(145, "gwei"),
"maxPriorityFeePerGas": Web3.to_wei(1.5, "gwei"),
"gas": 173290,
"to": contract.address,
"value": Web3.to_wei(0.1, "ether"),
"chainId": ChainId.ETH,
"data": data
}):
with client.sign(DERIVATION_PATH, tx_params):
# Validate the on-screen request by performing the navigation appropriate for this device
if firmware.device.startswith("nano"):
navigator.navigate_until_text_and_compare(NavInsID.RIGHT_CLICK,
Expand All @@ -73,3 +75,7 @@ def test_swap_exact_eth_for_token(backend, firmware, navigator, test_name):
"Hold to sign",
ROOT_SCREENSHOT_PATH,
test_name)
# verify signature
vrs = ResponseParser.signature(client.response().data)
addr = recover_transaction(tx_params, vrs)
assert addr == wallet_addr.get()
18 changes: 17 additions & 1 deletion tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
import os
import re

from pathlib import Path
from typing import Optional

from ledger_app_clients.ethereum.client import EthAppClient
import ledger_app_clients.ethereum.response_parser as ResponseParser

DERIVATION_PATH = "m/44'/60'/0'/0/0"
makefile_relative_path = "../Makefile"

makefile_path = (Path(os.path.dirname(os.path.realpath(__file__))) / Path(makefile_relative_path)).resolve()
Expand All @@ -12,6 +15,7 @@

default_strip_parameter = " \t\n\r\x0b\x0c"


def get_appname_from_makefile() -> str:
'''
Parse the app Makefile to automatically get the APPNAME value
Expand All @@ -27,3 +31,15 @@ def get_appname_from_makefile() -> str:
raise AssertionError("Unable to find APPNAME in the Makefile")

return APPNAME


class WalletAddr:
client: EthAppClient

def __init__(self, backend):
self.client = EthAppClient(backend)

def get(self, path=DERIVATION_PATH) -> bytes:
with self.client.get_public_addr(display=False, bip32_path=path):
pass
return ResponseParser.pk_addr(self.client.response().data)[1]
Loading