-
-
Notifications
You must be signed in to change notification settings - Fork 632
/
layout.py
249 lines (207 loc) · 6.8 KB
/
layout.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
from ubinascii import hexlify
from trezor import ui
from trezor.enums import ButtonRequestType, EthereumDataType
from trezor.messages import EthereumFieldType, EthereumStructMember
from trezor.strings import format_amount
from trezor.ui.components.tt.text import Text
from trezor.ui.layouts import (
confirm_address,
confirm_amount,
confirm_blob,
confirm_output,
confirm_text,
)
from trezor.ui.layouts.tt.altcoin import confirm_total_ethereum
from apps.common.confirm import confirm
from . import networks, tokens
from .helpers import address_from_bytes, decode_typed_data, get_type_name
if False:
from typing import Awaitable, Iterable, Optional
from trezor.wire import Context
def require_confirm_tx(
ctx: Context,
to_bytes: bytes,
value: int,
chain_id: int,
token: tokens.TokenInfo | None = None,
) -> Awaitable[None]:
if to_bytes:
to_str = address_from_bytes(to_bytes, networks.by_chain_id(chain_id))
else:
to_str = "new contract?"
return confirm_output(
ctx,
address=to_str,
amount=format_ethereum_amount(value, token, chain_id),
font_amount=ui.BOLD,
color_to=ui.GREY,
br_code=ButtonRequestType.SignTx,
)
def require_confirm_fee(
ctx: Context,
spending: int,
gas_price: int,
gas_limit: int,
chain_id: int,
token: tokens.TokenInfo | None = None,
) -> Awaitable[None]:
return confirm_total_ethereum(
ctx,
format_ethereum_amount(spending, token, chain_id),
format_ethereum_amount(gas_price, None, chain_id),
format_ethereum_amount(gas_price * gas_limit, None, chain_id),
)
async def require_confirm_eip1559_fee(
ctx: Context, max_priority_fee: int, max_gas_fee: int, gas_limit: int, chain_id: int
) -> None:
await confirm_amount(
ctx,
title="Confirm fee",
description="Maximum fee per gas",
amount=format_ethereum_amount(max_gas_fee, None, chain_id),
)
await confirm_amount(
ctx,
title="Confirm fee",
description="Priority fee per gas",
amount=format_ethereum_amount(max_priority_fee, None, chain_id),
)
await confirm_amount(
ctx,
title="Confirm fee",
description="Maximum fee",
amount=format_ethereum_amount(max_gas_fee * gas_limit, None, chain_id),
)
def require_confirm_unknown_token(
ctx: Context, address_bytes: bytes
) -> Awaitable[None]:
contract_address_hex = "0x" + hexlify(address_bytes).decode()
return confirm_address(
ctx,
"Unknown token",
contract_address_hex,
description="Contract:",
br_type="unknown_token",
icon_color=ui.ORANGE,
br_code=ButtonRequestType.SignTx,
)
def require_confirm_data(ctx: Context, data: bytes, data_total: int) -> Awaitable[None]:
return confirm_blob(
ctx,
"confirm_data",
title="Confirm data",
description=f"Size: {data_total} bytes",
data=data,
br_code=ButtonRequestType.SignTx,
)
async def confirm_hash(ctx: Context, primary_type: str, typed_data_hash: bytes) -> None:
data = "0x" + hexlify(typed_data_hash).decode()
await confirm_blob(
ctx,
"confirm_resulting_hash",
title="Sign typed data?",
description=f"Hashed {primary_type}:",
data=data,
hold=True,
icon=ui.ICON_CONFIG,
icon_color=ui.GREEN,
)
async def should_show_domain(ctx: Context, name: bytes, version: bytes) -> bool:
page = Text("Typed Data", ui.ICON_SEND, icon_color=ui.GREEN)
domain_name = decode_typed_data(name, "string")
domain_version = decode_typed_data(version, "string")
page.bold(f"Name: {domain_name}")
page.normal(f"Version: {domain_version}")
page.br()
page.mono("View EIP712Domain?")
return await confirm(ctx, page, ButtonRequestType.Other)
async def should_show_struct(
ctx: Context,
primary_type: str,
parent_objects: Iterable[str],
data_members: list[EthereumStructMember],
) -> bool:
title = f"{'.'.join(parent_objects)} - {primary_type}"
page = Text(title, ui.ICON_SEND, icon_color=ui.GREEN)
# We have limited screen space, so showing only a preview when having lot of fields
MAX_FIELDS_TO_SHOW = 3
fields_amount = len(data_members)
if fields_amount > MAX_FIELDS_TO_SHOW:
for field in data_members[:MAX_FIELDS_TO_SHOW]:
page.bold(limit_str(field.name))
page.mono(f"...and {fields_amount - MAX_FIELDS_TO_SHOW} more.")
else:
for field in data_members:
page.bold(limit_str(field.name))
page.mono("View full struct?")
return await confirm(ctx, page, ButtonRequestType.Other)
async def should_show_array(
ctx: Context,
name: str,
parent_objects: Iterable[str],
data_type: str,
size: int,
) -> bool:
title = f"{'.'.join(parent_objects)} - {name}"
page = Text(title, ui.ICON_SEND, icon_color=ui.GREEN)
page.bold(limit_str(f"Type: {data_type}"))
page.bold(limit_str(f"Size: {size}"))
page.br()
page.mono("View full array?")
return await confirm(ctx, page, ButtonRequestType.Other)
async def confirm_typed_value(
ctx: Context,
name: str,
value: bytes,
parent_objects: Iterable[str],
primary_type: str,
field: EthereumFieldType,
array_index: Optional[int] = None,
) -> None:
type_name = get_type_name(field)
if parent_objects:
title = f"{'.'.join(parent_objects)} - {primary_type}"
else:
title = primary_type
if array_index is not None:
array_str = f"[{array_index}]"
else:
array_str = ""
description = f"{name}{array_str} ({type_name})"
data = decode_typed_data(value, type_name)
if field.data_type in (EthereumDataType.ADDRESS, EthereumDataType.BYTES):
await confirm_blob(
ctx,
"show_data",
title=title,
data=data,
description=description,
br_code=ButtonRequestType.Other,
)
else:
await confirm_text(
ctx,
"show_data",
title=title,
data=data,
description=description,
br_code=ButtonRequestType.Other,
)
def format_ethereum_amount(
value: int, token: tokens.TokenInfo | None, chain_id: int
) -> str:
if token:
suffix = token.symbol
decimals = token.decimals
else:
suffix = networks.shortcut_by_chain_id(chain_id)
decimals = 18
# Don't want to display wei values for tokens with small decimal numbers
if decimals > 9 and value < 10 ** (decimals - 9):
suffix = "Wei " + suffix
decimals = 0
return f"{format_amount(value, decimals)} {suffix}"
def limit_str(s: str, limit: int = 16) -> str:
if len(s) <= limit + 2:
return s
return s[:limit] + ".."