forked from trezor/trezor-firmware
-
Notifications
You must be signed in to change notification settings - Fork 0
/
coin_info.py
executable file
·702 lines (552 loc) · 20.3 KB
/
coin_info.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
#!/usr/bin/env python3
from __future__ import annotations
import json
import logging
import re
from collections import OrderedDict, defaultdict
from pathlib import Path
from typing import Dict # for python38 support, must be used in type aliases
from typing import List # for python38 support, must be used in type aliases
from typing import Any, Callable, Iterable, Iterator, cast
from typing_extensions import ( # for python37 support, is not present in typing there
Literal,
TypedDict,
)
try:
import requests
except ImportError:
requests = None
log = logging.getLogger(__name__)
ROOT = Path(__file__).resolve().parent.parent
DEFS_DIR = ROOT / "defs"
class SupportItemBool(TypedDict):
supported: dict[str, bool]
unsupported: dict[str, bool]
class SupportItemVersion(TypedDict):
supported: dict[str, str]
unsupported: dict[str, str]
class SupportData(TypedDict):
connect: SupportItemBool
suite: SupportItemBool
t1b1: SupportItemVersion
t2t1: SupportItemVersion
t2b1: SupportItemVersion
class SupportInfoItem(TypedDict):
connect: bool
suite: bool
t1b1: Literal[False] | str
t2t1: Literal[False] | str
t2b1: Literal[False] | str
SupportInfo = Dict[str, SupportInfoItem]
class Coin(TypedDict):
# Necessary fields for BTC - from BTC_CHECKS
coin_name: str
coin_shortcut: str
coin_label: str
website: str
github: str
maintainer: str
curve_name: str
address_type: int
address_type_p2sh: int
maxfee_kb: int
minfee_kb: int
hash_genesis_block: str
xprv_magic: int
xpub_magic: int
xpub_magic_segwit_p2sh: int
xpub_magic_segwit_native: int
slip44: int
segwit: bool
decred: bool
fork_id: int
force_bip143: bool
default_fee_b: dict[str, int]
dust_limit: int
blocktime_seconds: int
signed_message_header: str
uri_prefix: str
min_address_length: int
max_address_length: int
bech32_prefix: str
cashaddr_prefix: str
# Other fields optionally coming from JSON
links: dict[str, str]
curve: str
decimals: int
# Mandatory fields added later in coin.update()
name: str
shortcut: str
key: str
icon: str
# Special ETH fields
coingecko_id: str
chain: str
chain_id: int
url: str
# Special erc20 fields
symbol: str
address: str
address_bytes: bytes
dup_key_nontoken: bool
# Special NEM fields
ticker: str
# Fields that are being created
unsupported: bool
duplicate: bool
support: SupportInfoItem
is_testnet: bool
# Backend-oriented fields
blockchain_link: dict[str, Any]
blockbook: list[str]
bitcore: list[str]
Coins = List[Coin]
CoinBuckets = Dict[str, Coins]
class FidoApp(TypedDict):
name: str
webauthn: list[str]
u2f: list[dict[str, str]]
use_sign_count: bool
use_self_attestation: bool
use_compact: bool
no_icon: bool
key: str
icon: str
FidoApps = List[FidoApp]
def load_json(*path: str | Path) -> Any:
"""Convenience function to load a JSON file from DEFS_DIR."""
if len(path) == 1 and isinstance(path[0], Path):
file = path[0]
else:
file = Path(DEFS_DIR, *path)
return json.loads(file.read_text(), object_pairs_hook=OrderedDict)
def get_btc_testnet_status(name: str) -> bool:
return any((mark in name.lower()) for mark in ("testnet", "regtest"))
# ====== CoinsInfo ======
class CoinsInfo(Dict[str, Coins]):
"""Collection of information about all known kinds of coins.
It contains the following lists:
`bitcoin` for btc-like coins,
`eth` for ethereum networks,
`erc20` for ERC20 tokens,
`nem` for NEM mosaics,
`misc` for other networks.
Accessible as a dict or by attribute: `info["misc"] == info.misc`
"""
def as_list(self) -> Coins:
return sum(self.values(), [])
def as_dict(self) -> dict[str, Coin]:
return {coin["key"]: coin for coin in self.as_list()}
def __getattr__(self, attr: str) -> Coins:
if attr in self:
return self[attr]
else:
raise AttributeError(attr)
# ====== coin validation ======
def check_type(
val: Any,
types: type | tuple[type, ...],
nullable: bool = False,
empty: bool = False,
regex: str | None = None,
choice: list[str] | None = None,
) -> None:
# check nullable
if val is None:
if nullable:
return
else:
raise ValueError("Missing required value")
# check type
if not isinstance(val, types):
raise TypeError(f"Wrong type (expected: {types})")
# check empty
if isinstance(val, (list, dict)) and not empty and not val:
raise ValueError("Empty collection")
if isinstance(val, str) and not empty and not val:
raise ValueError("Empty string")
# check regex
if regex is not None:
if types is not str:
raise TypeError("Wrong type for regex check")
assert isinstance(val, str)
if not re.search(regex, val):
raise ValueError(f"Value does not match regex {regex}")
# check choice
if choice is not None and val not in choice:
choice_str = ", ".join(choice)
raise ValueError(f"Value not allowed, use one of: {choice_str}")
def check_key(
key: str, types: type | tuple[type, ...], optional: bool = False, **kwargs: Any
) -> Callable[[Coin], None]:
def do_check(coin: Coin) -> None:
if key not in coin:
if optional:
return
else:
raise KeyError(f"{key}: Missing key")
try:
check_type(coin[key], types, **kwargs)
except Exception as e:
raise ValueError(f"{key}: {e}") from e
return do_check
BTC_CHECKS = [
check_key("coin_name", str, regex=r"^[A-Z]"),
check_key("coin_shortcut", str, regex=r"^t?[A-Z]{3,}$"),
check_key("coin_label", str, regex=r"^x?[A-Z]"),
check_key("website", str, regex=r"^https://.*[^/]$"),
check_key("github", str, regex=r"^https://git(hu|la)b.com/.*[^/]$"),
check_key("maintainer", str),
check_key(
"curve_name",
str,
choice=[
"secp256k1",
"secp256k1_decred",
"secp256k1_groestl",
"secp256k1_smart",
],
),
check_key("address_type", int),
check_key("address_type_p2sh", int),
check_key("maxfee_kb", int),
check_key("minfee_kb", int),
check_key("hash_genesis_block", str, regex=r"^[0-9a-f]{64}$"),
check_key("xprv_magic", int),
check_key("xpub_magic", int),
check_key("xpub_magic_segwit_p2sh", int, nullable=True),
check_key("xpub_magic_segwit_native", int, nullable=True),
check_key("slip44", int),
check_key("segwit", bool),
check_key("decred", bool),
check_key("fork_id", int, nullable=True),
check_key("force_bip143", bool),
check_key("default_fee_b", dict),
check_key("dust_limit", int),
check_key("blocktime_seconds", int),
check_key("signed_message_header", str),
check_key("uri_prefix", str, regex=r"^[a-z-\.\+]+$"),
check_key("min_address_length", int),
check_key("max_address_length", int),
check_key("bech32_prefix", str, regex=r"^[a-z-\.\+]+$", nullable=True),
check_key("cashaddr_prefix", str, regex=r"^[a-z-\.\+]+$", nullable=True),
]
def validate_btc(coin: Coin) -> list[str]:
errors: list[str] = []
for check in BTC_CHECKS:
try:
check(coin)
except Exception as e:
errors.append(str(e))
magics: list[int] = [
coin[k]
for k in (
"xprv_magic",
"xpub_magic",
"xpub_magic_segwit_p2sh",
"xpub_magic_segwit_native",
)
if coin[k] is not None
]
# each of those must be unique
# therefore length of list == length of set of unique values
if len(magics) != len(set(magics)):
errors.append("XPUB/XPRV magic numbers must be unique")
if coin["address_type"] == coin["address_type_p2sh"]:
errors.append("address_type must be distinct from address_type_p2sh")
if not coin["maxfee_kb"] >= coin["minfee_kb"]:
errors.append("max fee must not be smaller than min fee")
if not coin["max_address_length"] >= coin["min_address_length"]:
errors.append("max address length must not be smaller than min address length")
if coin["is_testnet"] and coin["slip44"] != 1:
errors.append("testnet coins must use slip44 coin type 1")
if coin["segwit"]:
if coin["bech32_prefix"] is None:
errors.append("bech32_prefix must be defined for segwit-enabled coin")
if coin["xpub_magic_segwit_p2sh"] is None:
errors.append(
"xpub_magic_segwit_p2sh must be defined for segwit-enabled coin"
)
else:
if coin["bech32_prefix"] is not None:
errors.append("bech32_prefix must not be defined for segwit-disabled coin")
if coin["xpub_magic_segwit_p2sh"] is not None:
errors.append(
"xpub_magic_segwit_p2sh must not be defined for segwit-disabled coin"
)
return errors
# ======= Coin json loaders =======
def _load_btc_coins() -> Coins:
"""Load btc-like coins from `bitcoin/*.json`"""
coins: Coins = []
for file in DEFS_DIR.glob("bitcoin/*.json"):
coin: Coin = load_json(file)
coin.update(
name=coin["coin_label"],
shortcut=coin["coin_shortcut"],
key=f"bitcoin:{coin['coin_shortcut']}",
icon=str(file.with_suffix(".png")),
is_testnet=get_btc_testnet_status(coin["coin_label"]),
)
coins.append(coin)
return coins
def _load_builtin_ethereum_networks() -> Coins:
"""Load ethereum networks from `ethereum/networks.json`"""
chains_data = load_json("ethereum", "networks.json")
networks: Coins = []
for chain_data in chains_data:
chain_data["key"] = f"eth:{chain_data['shortcut']}:{chain_data['chain_id']}"
# is_testnet is present in the JSON
networks.append(cast(Coin, chain_data))
return networks
def _load_builtin_erc20_tokens() -> Coins:
"""Load ERC20 tokens from `ethereum/tokens.json`."""
tokens_data = load_json("ethereum", "tokens.json")
all_tokens: Coins = []
for chain_id_and_chain, tokens in tokens_data.items():
chain_id, chain = chain_id_and_chain.split(";", maxsplit=1)
for token in tokens:
token.update(
chain=chain,
chain_id=int(chain_id),
address=token["address"].lower(),
address_bytes=bytes.fromhex(token["address"][2:]),
symbol=token["shortcut"],
key=f"erc20:{chain}:{token['shortcut']}",
is_testnet=False,
)
all_tokens.append(cast(Coin, token))
return all_tokens
def _load_nem_mosaics() -> Coins:
"""Loads NEM mosaics from `nem/nem_mosaics.json`"""
mosaics: Coins = load_json("nem/nem_mosaics.json")
for mosaic in mosaics:
shortcut = mosaic["ticker"].strip()
mosaic.update(
shortcut=shortcut,
key=f"nem:{shortcut}",
is_testnet=False,
)
return mosaics
def _load_misc() -> Coins:
"""Loads miscellaneous networks from `misc/misc.json`"""
others: Coins = load_json("misc/misc.json")
for other in others:
other.update(
key=f"misc:{other['shortcut']}",
is_testnet=False,
)
return others
def _load_fido_apps() -> FidoApps:
"""Load FIDO apps from `fido/*.json`"""
apps: FidoApps = []
for file in sorted(DEFS_DIR.glob("fido/*.json")):
app_name = file.stem.lower()
app = load_json(file)
app.setdefault("use_sign_count", None)
app.setdefault("use_self_attestation", None)
app.setdefault("use_compact", None)
app.setdefault("u2f", [])
app.setdefault("webauthn", [])
icon_file = file.with_suffix(".png")
if not icon_file.exists():
icon_path = None
else:
icon_path = str(icon_file)
app.update(key=app_name, icon=icon_path)
apps.append(app)
return apps
# ====== support info ======
RELEASES_URL = "https://data.trezor.io/firmware/{}/releases.json"
MISSING_SUPPORT_MEANS_NO = ("connect", "suite")
VERSIONED_SUPPORT_INFO = ("T1B1", "T2T1", "T2B1")
def get_support_data() -> SupportData:
"""Get raw support data from `support.json`."""
return load_json("support.json")
def latest_releases() -> dict[str, Any]:
"""Get latest released firmware versions for all models"""
if not requests:
raise RuntimeError("requests library is required for getting release info")
latest: dict[str, Any] = {}
for model in VERSIONED_SUPPORT_INFO:
# TODO: support new UPPERCASE model names in RELEASES_URL
url_model = model.lower() # need to be e.g. t1b1 for now
releases = requests.get(RELEASES_URL.format(url_model)).json()
latest[model] = max(tuple(r["version"]) for r in releases)
return latest
def support_info_single(support_data: SupportData, coin: Coin) -> SupportInfoItem:
"""Extract a support dict from `support.json` data.
Returns a dict of support values for each "device", i.e., `support.json`
top-level key.
The support value for each device is determined in order of priority:
* if the coin has an entry in `unsupported`, its support is `False`
* if the coin has an entry in `supported` its support is that entry
(usually a version string, or `True` for connect/suite)
* if the coin doesn't have an entry, its support status is `None`
"""
support_info_item = {}
key = coin["key"]
for device, values in support_data.items():
assert isinstance(values, dict)
if key in values["unsupported"]:
support_value: Any = False
elif key in values["supported"]:
support_value = values["supported"][key]
elif device in MISSING_SUPPORT_MEANS_NO:
support_value = False
else:
support_value = None
support_info_item[device] = support_value
return cast(SupportInfoItem, support_info_item)
def support_info(coins: Iterable[Coin] | CoinsInfo | dict[str, Coin]) -> SupportInfo:
"""Generate Trezor support information.
Takes a collection of coins and generates a support-info entry for each.
The support-info is a dict with keys based on `support.json` keys.
These are usually: "T1B1", "T2T1", "T2B1", "connect" and "suite".
The `coins` argument can be a `CoinsInfo` object, a list or a dict of
coin items.
Support information is taken from `support.json`.
"""
if isinstance(coins, CoinsInfo):
coins = coins.as_list()
elif isinstance(coins, dict):
coins = coins.values()
support_data = get_support_data()
support: SupportInfo = {}
for coin in coins:
support[coin["key"]] = support_info_single(support_data, coin)
return support
# ====== data cleanup functions ======
def _ensure_mandatory_values(coins: Coins) -> None:
"""Checks that every coin has the mandatory fields: name, shortcut, key"""
for coin in coins:
if not all(coin.get(k) for k in ("name", "shortcut", "key")):
raise ValueError(coin)
def symbol_from_shortcut(shortcut: str) -> tuple[str, str]:
symsplit = shortcut.split(" ", maxsplit=1)
return symsplit[0], symsplit[1] if len(symsplit) > 1 else ""
def mark_duplicate_shortcuts(coins: Coins) -> CoinBuckets:
"""Finds coins with identical symbols and sets their `duplicate` field.
"Symbol" here means the first part of `shortcut` (separated by space),
so, e.g., "BTL (Battle)" and "BTL (Bitlle)" have the same symbol "BTL".
The result of this function is a dictionary of _buckets_, each of which is
indexed by the duplicated symbol, or `_override`. The `_override` bucket will
contain all coins that are set to `true` in `duplicity_overrides.json`.
Each coin in every bucket will have its "duplicate" property set to True, unless
it's explicitly marked as `false` in `duplicity_overrides.json`.
"""
dup_symbols: CoinBuckets = defaultdict(list)
for coin in coins:
symbol, _ = symbol_from_shortcut(coin["shortcut"].lower())
dup_symbols[symbol].append(coin)
dup_symbols = {k: v for k, v in dup_symbols.items() if len(v) > 1}
# mark duplicate symbols
for values in dup_symbols.values():
for coin in values:
coin["duplicate"] = True
return dup_symbols
def apply_duplicity_overrides(coins: Coins) -> Coins:
overrides = load_json("duplicity_overrides.json")
override_bucket: Coins = []
for coin in coins:
override_value = overrides.get(coin["key"])
if override_value is True:
override_bucket.append(coin)
if override_value is not None:
coin["duplicate"] = override_value
return override_bucket
def find_duplicate_keys(all_coins: Coins) -> None:
dups: CoinBuckets = defaultdict(list)
for coin in all_coins:
dups[coin["key"]].append(coin)
for coins in dups.values():
if len(coins) <= 1:
continue
coin = coins[0]
raise ValueError(f"Duplicate key {coin['key']}")
def fill_blockchain_links(all_coins: CoinsInfo) -> None:
blockchain_links = load_json("blockchain_link.json")
for coins in all_coins.values():
for coin in coins:
link = blockchain_links.get(coin["key"])
coin["blockchain_link"] = link
if link and link["type"] == "blockbook":
coin["blockbook"] = link["url"]
else:
coin["blockbook"] = []
def _btc_sort_key(coin: Coin) -> str:
if coin["name"] in ("Bitcoin", "Testnet", "Regtest"):
return "000000" + coin["name"]
else:
return coin["name"]
def collect_coin_info() -> CoinsInfo:
"""Returns all definition as dict organized by coin type.
`coins` for btc-like coins,
`eth` for ethereum networks,
`erc20` for ERC20 tokens,
`nem` for NEM mosaics,
`misc` for other networks.
"""
all_coins = CoinsInfo(
bitcoin=_load_btc_coins(),
eth=_load_builtin_ethereum_networks(),
erc20=_load_builtin_erc20_tokens(),
nem=_load_nem_mosaics(),
misc=_load_misc(),
)
for coins in all_coins.values():
_ensure_mandatory_values(coins)
fill_blockchain_links(all_coins)
return all_coins
def sort_coin_infos(all_coins: CoinsInfo) -> None:
for k, coins in all_coins.items():
if k == "bitcoin":
coins.sort(key=_btc_sort_key)
elif k == "nem":
# do not sort nem
pass
elif k == "eth":
# sort ethereum networks by chain_id
coins.sort(key=lambda c: c["chain_id"])
else:
coins.sort(key=lambda c: c["key"].upper())
def coin_info_with_duplicates() -> tuple[CoinsInfo, CoinBuckets]:
"""Collects coin info, detects duplicates but does not remove them.
Returns the CoinsInfo object and duplicate buckets.
"""
all_coins = collect_coin_info()
coin_list = all_coins.as_list()
# generate duplicity buckets based on shortcuts
buckets = mark_duplicate_shortcuts(all_coins.as_list())
# ensure the whole list has unique keys
find_duplicate_keys(coin_list)
# apply duplicity overrides
buckets["_override"] = apply_duplicity_overrides(coin_list)
sort_coin_infos(all_coins)
return all_coins, buckets
def coin_info() -> CoinsInfo:
"""Collects coin info, fills out support info and returns the result.
Does not auto-delete duplicates. This should now be based on support info.
"""
all_coins, _ = coin_info_with_duplicates()
return all_coins
def fido_info() -> FidoApps:
"""Returns info about known FIDO/U2F apps."""
return _load_fido_apps()
def search(coins: CoinsInfo | Coins, keyword: str) -> Iterator[Any]:
kwl = keyword.lower()
if isinstance(coins, CoinsInfo):
coins = coins.as_list()
for coin in coins:
key = coin["key"].lower()
name = coin["name"].lower()
shortcut = coin["shortcut"].lower()
symbol, suffix = symbol_from_shortcut(shortcut)
if (
kwl == key
or kwl in name
or kwl == shortcut
or kwl == symbol
or kwl in suffix
):
yield coin