Skip to content

Commit

Permalink
Python client now handle missing token information for EIP-712 amount…
Browse files Browse the repository at this point in the history
…-joins

Client now sends token information for EIP 712 amount-joins only when needed instead of all at the beginning of the flow.
  • Loading branch information
apaillier-ledger committed Sep 6, 2024
1 parent a9705cf commit 1c08933
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
43 changes: 31 additions & 12 deletions client/src/ledger_app_clients/ethereum/eip712/InputData.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
# global variables
app_client: EthAppClient = None
filtering_paths: dict = {}
filtering_tokens: list[dict] = list()
current_path: list[str] = list()
sig_ctx: dict[str, Any] = {}

Expand Down Expand Up @@ -194,6 +195,18 @@ def encode_bytes_dyn(value: str, typesize: int) -> bytes:
encoding_functions[EIP712FieldType.DYN_BYTES] = encode_bytes_dyn


def send_filtering_token(token_idx: int):
assert token_idx < len(filtering_tokens)
if len(filtering_tokens[token_idx]) > 0:
token = filtering_tokens[token_idx]
if not token["sent"]:
app_client.provide_token_metadata(token["ticker"],
bytes.fromhex(token["addr"][2:]),
token["decimals"],
token["chain_id"])
token["sent"] = True


def send_struct_impl_field(value, field):
# Something wrong happened if this triggers
if isinstance(value, list) or (field["enum"] == EIP712FieldType.CUSTOM):
Expand All @@ -204,16 +217,19 @@ def send_struct_impl_field(value, field):
if filtering_paths:
path = ".".join(current_path)
if path in filtering_paths.keys():
if filtering_paths[path]["type"] == "amount_join_token":
send_filtering_amount_join_token(filtering_paths[path]["token"])
elif filtering_paths[path]["type"] == "amount_join_value":
if filtering_paths[path]["type"].startswith("amount_join_"):
if "token" in filtering_paths[path].keys():
token = filtering_paths[path]["token"]
token_idx = filtering_paths[path]["token"]
send_filtering_token(token_idx)
else:
# Permit (ERC-2612)
token = 0xff
send_filtering_amount_join_value(token,
filtering_paths[path]["name"])
send_filtering_token(0)
token_idx = 0xff
if filtering_paths[path]["type"].endswith("_token"):
send_filtering_amount_join_token(token_idx)
elif filtering_paths[path]["type"].endswith("_value"):
send_filtering_amount_join_value(token_idx,
filtering_paths[path]["name"])
elif filtering_paths[path]["type"] == "datetime":
send_filtering_datetime(filtering_paths[path]["name"])
elif filtering_paths[path]["type"] == "raw":
Expand Down Expand Up @@ -351,17 +367,20 @@ def send_filtering_raw(display_name):

def prepare_filtering(filtr_data, message):
global filtering_paths
global filtering_tokens

if "fields" in filtr_data:
filtering_paths = filtr_data["fields"]
else:
filtering_paths = {}

if "tokens" in filtr_data:
for token in filtr_data["tokens"]:
app_client.provide_token_metadata(token["ticker"],
bytes.fromhex(token["addr"][2:]),
token["decimals"],
token["chain_id"])
filtering_tokens = filtr_data["tokens"]
for token in filtering_tokens:
if len(token) > 0:
token["sent"] = False
else:
filtering_tokens = []


def handle_optional_domain_values(domain):
Expand Down
16 changes: 8 additions & 8 deletions tests/ragger/test_eip712.py
Original file line number Diff line number Diff line change
Expand Up @@ -242,14 +242,14 @@ def __init__(self, data: dict, filters: dict, suffix: str = ""):
"name": "Advanced Filtering",
"tokens": [
{
"addr": "0x6b175474e89094c44da98b954eedeac495271d0f",
"ticker": "DAI",
"addr": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"ticker": "WETH",
"decimals": 18,
"chain_id": 1,
},
{
"addr": "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2",
"ticker": "WETH",
"addr": "0x6b175474e89094c44da98b954eedeac495271d0f",
"ticker": "DAI",
"decimals": 18,
"chain_id": 1,
},
Expand All @@ -258,20 +258,20 @@ def __init__(self, data: dict, filters: dict, suffix: str = ""):
"value_send": {
"type": "amount_join_value",
"name": "Send",
"token": 0,
"token": 1,
},
"token_send": {
"type": "amount_join_token",
"token": 0,
"token": 1,
},
"value_recv": {
"type": "amount_join_value",
"name": "Receive",
"token": 1,
"token": 0,
},
"token_recv": {
"type": "amount_join_token",
"token": 1,
"token": 0,
},
"with": {
"type": "raw",
Expand Down

0 comments on commit 1c08933

Please sign in to comment.