mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-11-21 23:18:13 +00:00
src/apps/ethereum: refactor networks module
This commit is contained in:
parent
6c2c99688b
commit
ad1e0e81b2
@ -60,7 +60,7 @@ def format_ethereum_amount(value: int, token, chain_id: int, tx_type=None):
|
||||
suffix = token[2]
|
||||
decimals = token[3]
|
||||
else:
|
||||
suffix = networks.suffix_by_chain_id(chain_id, tx_type)
|
||||
suffix = networks.shortcut_by_chain_id(chain_id, tx_type)
|
||||
decimals = 18
|
||||
|
||||
if value <= 1e9:
|
||||
|
@ -1,19 +1,148 @@
|
||||
suffixes = {
|
||||
1: 'ETH', # Ethereum Mainnet
|
||||
2: 'EXP', # Expanse
|
||||
3: 'tETH', # Ethereum Testnet: Ropsten
|
||||
4: 'tETH', # Ethereum Testnet: Rinkeby
|
||||
8: 'UBQ', # UBIQ
|
||||
30: 'RSK', # Rootstock Mainnet
|
||||
31: 'tRSK', # Rootstock Testnet
|
||||
42: 'tETH', # Ethereum Testnet: Kovan
|
||||
61: 'ETC', # Ethereum Classic Mainnet
|
||||
62: 'tETC', # Ethereum Classic Testnet
|
||||
}
|
||||
class NetworkInfo:
|
||||
|
||||
def __init__(
|
||||
self,
|
||||
chain_id: int,
|
||||
slip44: int,
|
||||
shortcut: str,
|
||||
name: str,
|
||||
rskip60: bool
|
||||
):
|
||||
self.chain_id = chain_id
|
||||
self.slip44 = slip44
|
||||
self.shortcut = shortcut
|
||||
self.name = name
|
||||
self.rskip60 = rskip60
|
||||
|
||||
|
||||
def suffix_by_chain_id(chain_id, tx_type=None):
|
||||
# the following list is generated using tools/codegen/gen_eth_networks.py
|
||||
# do not edit manually!
|
||||
NETWORKS = [
|
||||
NetworkInfo(
|
||||
chain_id=1,
|
||||
slip44=60,
|
||||
shortcut='ETH',
|
||||
name='Ethereum',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=2,
|
||||
slip44=40,
|
||||
shortcut='EXP',
|
||||
name='Expanse',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=3,
|
||||
slip44=1,
|
||||
shortcut='tETH',
|
||||
name='Ethereum Testnet Ropsten',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=4,
|
||||
slip44=1,
|
||||
shortcut='tETH',
|
||||
name='Ethereum Testnet Rinkeby',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=8,
|
||||
slip44=108,
|
||||
shortcut='UBQ',
|
||||
name='UBIQ',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=20,
|
||||
slip44=2018,
|
||||
shortcut='EOSC',
|
||||
name='EOS Classic',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=28,
|
||||
slip44=1128,
|
||||
shortcut='ETSC',
|
||||
name='Ethereum Social',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=30,
|
||||
slip44=137,
|
||||
shortcut='RSK',
|
||||
name='RSK',
|
||||
rskip60=True,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=31,
|
||||
slip44=1,
|
||||
shortcut='tRSK',
|
||||
name='RSK Testnet',
|
||||
rskip60=True,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=42,
|
||||
slip44=1,
|
||||
shortcut='tETH',
|
||||
name='Ethereum Testnet Kovan',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=61,
|
||||
slip44=61,
|
||||
shortcut='ETC',
|
||||
name='Ethereum Classic',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=62,
|
||||
slip44=1,
|
||||
shortcut='tETC',
|
||||
name='Ethereum Classic Testnet',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=64,
|
||||
slip44=163,
|
||||
shortcut='ELLA',
|
||||
name='Ellaism',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=820,
|
||||
slip44=820,
|
||||
shortcut='CLO',
|
||||
name='Callisto',
|
||||
rskip60=False,
|
||||
),
|
||||
NetworkInfo(
|
||||
chain_id=1987,
|
||||
slip44=1987,
|
||||
shortcut='EGEM',
|
||||
name='EtherGem',
|
||||
rskip60=False,
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
def shortcut_by_chain_id(chain_id, tx_type=None):
|
||||
if tx_type in [1, 6] and chain_id in [1, 3]:
|
||||
return 'WAN'
|
||||
else:
|
||||
return suffixes.get(chain_id, 'UNKN')
|
||||
n = by_chain_id(chain_id)
|
||||
return n.shortcut if n is not None else 'UNKN'
|
||||
|
||||
|
||||
def by_chain_id(chain_id):
|
||||
for n in NETWORKS:
|
||||
if n.chain_id == chain_id:
|
||||
return n
|
||||
return None
|
||||
|
||||
|
||||
def by_slip44(slip44):
|
||||
for n in NETWORKS:
|
||||
if n.slip44 == slip44:
|
||||
return n
|
||||
return None
|
||||
|
14
tools/codegen/gen_eth_networks.py
Executable file
14
tools/codegen/gen_eth_networks.py
Executable file
@ -0,0 +1,14 @@
|
||||
#!/usr/bin/env python3
|
||||
import json
|
||||
|
||||
j = json.load(open('../../vendor/trezor-common/defs/ethereum/networks.json', 'r'))
|
||||
|
||||
print('NETWORKS = [')
|
||||
|
||||
for n in j:
|
||||
print(' NetworkInfo(')
|
||||
for f in ['chain_id', 'slip44', 'shortcut', 'name', 'rskip60']:
|
||||
print(' %s=%s,' % (f, repr(n[f])))
|
||||
print(' ),')
|
||||
|
||||
print(']')
|
2
vendor/trezor-common
vendored
2
vendor/trezor-common
vendored
@ -1 +1 @@
|
||||
Subproject commit babc60a48ec95df8de0ddd11b9d7e24b0e7e1d46
|
||||
Subproject commit 57a31feb2e48ac5057761fd07e5c7b280b66b5c6
|
Loading…
Reference in New Issue
Block a user