diff --git a/core/src/apps/common/address_mac.py b/core/src/apps/common/address_mac.py index 496395365..962249dd8 100644 --- a/core/src/apps/common/address_mac.py +++ b/core/src/apps/common/address_mac.py @@ -1,9 +1,6 @@ from typing import TYPE_CHECKING -from trezor import utils, wire -from trezor.crypto import hashlib, hmac - -from .writers import write_bytes_unchecked, write_compact_size, write_uint32_le +from trezor import utils if TYPE_CHECKING: from apps.common.keychain import Keychain @@ -14,12 +11,18 @@ _ADDRESS_MAC_KEY_PATH = [b"SLIP-0024", b"Address MAC key"] def check_address_mac( address: str, mac: bytes, slip44: int, keychain: Keychain ) -> None: + from trezor import wire + from trezor.crypto import hashlib + expected_mac = get_address_mac(address, slip44, keychain) if len(mac) != hashlib.sha256.digest_size or not utils.consteq(expected_mac, mac): raise wire.DataError("Invalid address MAC.") def get_address_mac(address: str, slip44: int, keychain: Keychain) -> bytes: + from trezor.crypto import hmac + from .writers import write_bytes_unchecked, write_compact_size, write_uint32_le + # k = Key(m/"SLIP-0024"/"Address MAC key") node = keychain.derive_slip21(_ADDRESS_MAC_KEY_PATH) diff --git a/core/src/apps/common/authorization.py b/core/src/apps/common/authorization.py index 5e084099e..17bc86b53 100644 --- a/core/src/apps/common/authorization.py +++ b/core/src/apps/common/authorization.py @@ -1,45 +1,53 @@ from typing import Iterable -import storage.cache +import storage.cache as storage_cache from trezor import protobuf from trezor.enums import MessageType -from trezor.utils import ensure WIRE_TYPES: dict[int, tuple[int, ...]] = { MessageType.AuthorizeCoinJoin: (MessageType.SignTx, MessageType.GetOwnershipProof), } +APP_COMMON_AUTHORIZATION_DATA = ( + storage_cache.APP_COMMON_AUTHORIZATION_DATA +) # global_import_cache +APP_COMMON_AUTHORIZATION_TYPE = ( + storage_cache.APP_COMMON_AUTHORIZATION_TYPE +) # global_import_cache + def is_set() -> bool: - return bool(storage.cache.get(storage.cache.APP_COMMON_AUTHORIZATION_TYPE)) + return bool(storage_cache.get(APP_COMMON_AUTHORIZATION_TYPE)) def set(auth_message: protobuf.MessageType) -> None: + from trezor.utils import ensure + buffer = protobuf.dump_message_buffer(auth_message) # only wire-level messages can be stored as authorization # (because only wire-level messages have wire_type, which we use as identifier) ensure(auth_message.MESSAGE_WIRE_TYPE is not None) assert auth_message.MESSAGE_WIRE_TYPE is not None # so that typechecker knows too - storage.cache.set( - storage.cache.APP_COMMON_AUTHORIZATION_TYPE, + storage_cache.set( + APP_COMMON_AUTHORIZATION_TYPE, auth_message.MESSAGE_WIRE_TYPE.to_bytes(2, "big"), ) - storage.cache.set(storage.cache.APP_COMMON_AUTHORIZATION_DATA, buffer) + storage_cache.set(APP_COMMON_AUTHORIZATION_DATA, buffer) def get() -> protobuf.MessageType | None: - stored_auth_type = storage.cache.get(storage.cache.APP_COMMON_AUTHORIZATION_TYPE) + stored_auth_type = storage_cache.get(APP_COMMON_AUTHORIZATION_TYPE) if not stored_auth_type: return None msg_wire_type = int.from_bytes(stored_auth_type, "big") - buffer = storage.cache.get(storage.cache.APP_COMMON_AUTHORIZATION_DATA, b"") + buffer = storage_cache.get(APP_COMMON_AUTHORIZATION_DATA, b"") return protobuf.load_message_buffer(buffer, msg_wire_type) def get_wire_types() -> Iterable[int]: - stored_auth_type = storage.cache.get(storage.cache.APP_COMMON_AUTHORIZATION_TYPE) + stored_auth_type = storage_cache.get(APP_COMMON_AUTHORIZATION_TYPE) if stored_auth_type is None: return () @@ -48,5 +56,5 @@ def get_wire_types() -> Iterable[int]: def clear() -> None: - storage.cache.delete(storage.cache.APP_COMMON_AUTHORIZATION_TYPE) - storage.cache.delete(storage.cache.APP_COMMON_AUTHORIZATION_DATA) + storage_cache.delete(APP_COMMON_AUTHORIZATION_TYPE) + storage_cache.delete(APP_COMMON_AUTHORIZATION_DATA) diff --git a/core/src/apps/common/cbor.py b/core/src/apps/common/cbor.py index 9ba9b2a41..3441c844f 100644 --- a/core/src/apps/common/cbor.py +++ b/core/src/apps/common/cbor.py @@ -2,16 +2,14 @@ Minimalistic CBOR implementation, supports only what we need in cardano. """ -import ustruct as struct from micropython import const from typing import TYPE_CHECKING -from trezor import log, utils - -from . import readers +from trezor import log if TYPE_CHECKING: from typing import Any, Generic, Iterator, TypeVar + from trezor.utils import BufferReader K = TypeVar("K") V = TypeVar("V") @@ -48,16 +46,18 @@ _CBOR_RAW_TAG = const(0x18) def _header(typ: int, l: int) -> bytes: + from ustruct import pack + if l < 24: - return struct.pack(">B", typ + l) + return pack(">B", typ + l) elif l < 2**8: - return struct.pack(">BB", typ + 24, l) + return pack(">BB", typ + 24, l) elif l < 2**16: - return struct.pack(">BH", typ + 25, l) + return pack(">BH", typ + 25, l) elif l < 2**32: - return struct.pack(">BI", typ + 26, l) + return pack(">BI", typ + 26, l) elif l < 2**64: - return struct.pack(">BQ", typ + 27, l) + return pack(">BQ", typ + 27, l) else: raise NotImplementedError # Length not supported @@ -117,7 +117,9 @@ def _cbor_encode(value: Value) -> Iterator[bytes]: raise NotImplementedError -def _read_length(r: utils.BufferReader, aux: int) -> int: +def _read_length(r: BufferReader, aux: int) -> int: + from . import readers + if aux < _CBOR_UINT8_FOLLOWS: return aux elif aux == _CBOR_UINT8_FOLLOWS: @@ -132,7 +134,7 @@ def _read_length(r: utils.BufferReader, aux: int) -> int: raise NotImplementedError # Length not supported -def _cbor_decode(r: utils.BufferReader) -> Value: +def _cbor_decode(r: BufferReader) -> Value: fb = r.get() fb_type = fb & _CBOR_TYPE_MASK fb_aux = fb & _CBOR_INFO_BITS @@ -220,6 +222,7 @@ class Tagged: ) +# TODO: this seems to be unused - is checked against, but is never created??? class Raw: def __init__(self, value: Value): self.value = value @@ -272,7 +275,9 @@ def encode_streamed(value: Value) -> Iterator[bytes]: def decode(cbor: bytes, offset: int = 0) -> Value: - r = utils.BufferReader(cbor) + from trezor.utils import BufferReader + + r = BufferReader(cbor) r.seek(offset) res = _cbor_decode(r) if r.remaining_count(): diff --git a/core/src/apps/common/coininfo.py b/core/src/apps/common/coininfo.py index 9156e823e..c8892c213 100644 --- a/core/src/apps/common/coininfo.py +++ b/core/src/apps/common/coininfo.py @@ -1,6 +1,9 @@ # generated from coininfo.py.mako # (by running `make templates` in `core`) # do not edit manually! + +# NOTE: using positional arguments saves 4500 bytes of flash size + from typing import Any from trezor import utils @@ -93,1598 +96,1598 @@ class CoinInfo: def by_name(name: str) -> CoinInfo: if name == "Bitcoin": return CoinInfo( - coin_name=name, - coin_shortcut="BTC", - decimals=8, - address_type=0, - address_type_p2sh=5, - maxfee_kb=2000000, - signed_message_header="Bitcoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0295b43f, - xpub_magic_multisig_segwit_native=0x02aa7ed3, - bech32_prefix="bc", - cashaddr_prefix=None, - slip44=0, - segwit=True, - taproot=True, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "BTC", # coin_shortcut + 8, # decimals + 0, # address_type + 5, # address_type_p2sh + 2000000, # maxfee_kb + "Bitcoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0295b43f, # xpub_magic_multisig_segwit_p2sh + 0x02aa7ed3, # xpub_magic_multisig_segwit_native + "bc", # bech32_prefix + None, # cashaddr_prefix + 0, # slip44 + True, # segwit + True, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Regtest": return CoinInfo( - coin_name=name, - coin_shortcut="REGTEST", - decimals=8, - address_type=111, - address_type_p2sh=196, - maxfee_kb=10000000, - signed_message_header="Bitcoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=0x044a5262, - xpub_magic_segwit_native=0x045f1cf6, - xpub_magic_multisig_segwit_p2sh=0x024289ef, - xpub_magic_multisig_segwit_native=0x02575483, - bech32_prefix="bcrt", - cashaddr_prefix=None, - slip44=1, - segwit=True, - taproot=True, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "REGTEST", # coin_shortcut + 8, # decimals + 111, # address_type + 196, # address_type_p2sh + 10000000, # maxfee_kb + "Bitcoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + 0x044a5262, # xpub_magic_segwit_p2sh + 0x045f1cf6, # xpub_magic_segwit_native + 0x024289ef, # xpub_magic_multisig_segwit_p2sh + 0x02575483, # xpub_magic_multisig_segwit_native + "bcrt", # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + True, # segwit + True, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="TEST", - decimals=8, - address_type=111, - address_type_p2sh=196, - maxfee_kb=10000000, - signed_message_header="Bitcoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=0x044a5262, - xpub_magic_segwit_native=0x045f1cf6, - xpub_magic_multisig_segwit_p2sh=0x024289ef, - xpub_magic_multisig_segwit_native=0x02575483, - bech32_prefix="tb", - cashaddr_prefix=None, - slip44=1, - segwit=True, - taproot=True, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "TEST", # coin_shortcut + 8, # decimals + 111, # address_type + 196, # address_type_p2sh + 10000000, # maxfee_kb + "Bitcoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + 0x044a5262, # xpub_magic_segwit_p2sh + 0x045f1cf6, # xpub_magic_segwit_native + 0x024289ef, # xpub_magic_multisig_segwit_p2sh + 0x02575483, # xpub_magic_multisig_segwit_native + "tb", # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + True, # segwit + True, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if not utils.BITCOIN_ONLY: if name == "Actinium": return CoinInfo( - coin_name=name, - coin_shortcut="ACM", - decimals=8, - address_type=53, - address_type_p2sh=55, - maxfee_kb=320000000000, - signed_message_header="Actinium Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="acm", - cashaddr_prefix=None, - slip44=228, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "ACM", # coin_shortcut + 8, # decimals + 53, # address_type + 55, # address_type_p2sh + 320000000000, # maxfee_kb + "Actinium Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "acm", # bech32_prefix + None, # cashaddr_prefix + 228, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Axe": return CoinInfo( - coin_name=name, - coin_shortcut="AXE", - decimals=8, - address_type=55, - address_type_p2sh=16, - maxfee_kb=21000000000, - signed_message_header="DarkCoin Signed Message:\n", - xpub_magic=0x02fe52cc, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=4242, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "AXE", # coin_shortcut + 8, # decimals + 55, # address_type + 16, # address_type_p2sh + 21000000000, # maxfee_kb + "DarkCoin Signed Message:\n", # signed_message_header + 0x02fe52cc, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 4242, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Bcash": return CoinInfo( - coin_name=name, - coin_shortcut="BCH", - decimals=8, - address_type=0, - address_type_p2sh=5, - maxfee_kb=14000000, - signed_message_header="Bitcoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix="bitcoincash", - slip44=145, - segwit=False, - taproot=False, - fork_id=0, - force_bip143=True, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "BCH", # coin_shortcut + 8, # decimals + 0, # address_type + 5, # address_type_p2sh + 14000000, # maxfee_kb + "Bitcoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + "bitcoincash", # cashaddr_prefix + 145, # slip44 + False, # segwit + False, # taproot + 0, # fork_id + True, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Bcash Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="TBCH", - decimals=8, - address_type=111, - address_type_p2sh=196, - maxfee_kb=10000000, - signed_message_header="Bitcoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix="bchtest", - slip44=1, - segwit=False, - taproot=False, - fork_id=0, - force_bip143=True, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "TBCH", # coin_shortcut + 8, # decimals + 111, # address_type + 196, # address_type_p2sh + 10000000, # maxfee_kb + "Bitcoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + "bchtest", # cashaddr_prefix + 1, # slip44 + False, # segwit + False, # taproot + 0, # fork_id + True, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Bgold": return CoinInfo( - coin_name=name, - coin_shortcut="BTG", - decimals=8, - address_type=38, - address_type_p2sh=23, - maxfee_kb=380000000, - signed_message_header="Bitcoin Gold Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="btg", - cashaddr_prefix=None, - slip44=156, - segwit=True, - taproot=False, - fork_id=79, - force_bip143=True, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "BTG", # coin_shortcut + 8, # decimals + 38, # address_type + 23, # address_type_p2sh + 380000000, # maxfee_kb + "Bitcoin Gold Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "btg", # bech32_prefix + None, # cashaddr_prefix + 156, # slip44 + True, # segwit + False, # taproot + 79, # fork_id + True, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Bgold Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="TBTG", - decimals=8, - address_type=111, - address_type_p2sh=196, - maxfee_kb=500000, - signed_message_header="Bitcoin Gold Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=0x044a5262, - xpub_magic_segwit_native=0x045f1cf6, - xpub_magic_multisig_segwit_p2sh=0x043587cf, - xpub_magic_multisig_segwit_native=0x043587cf, - bech32_prefix="tbtg", - cashaddr_prefix=None, - slip44=1, - segwit=True, - taproot=False, - fork_id=79, - force_bip143=True, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "TBTG", # coin_shortcut + 8, # decimals + 111, # address_type + 196, # address_type_p2sh + 500000, # maxfee_kb + "Bitcoin Gold Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + 0x044a5262, # xpub_magic_segwit_p2sh + 0x045f1cf6, # xpub_magic_segwit_native + 0x043587cf, # xpub_magic_multisig_segwit_p2sh + 0x043587cf, # xpub_magic_multisig_segwit_native + "tbtg", # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + True, # segwit + False, # taproot + 79, # fork_id + True, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Bprivate": return CoinInfo( - coin_name=name, - coin_shortcut="BTCP", - decimals=8, - address_type=4901, - address_type_p2sh=5039, - maxfee_kb=32000000000, - signed_message_header="BitcoinPrivate Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=183, - segwit=False, - taproot=False, - fork_id=42, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "BTCP", # coin_shortcut + 8, # decimals + 4901, # address_type + 5039, # address_type_p2sh + 32000000000, # maxfee_kb + "BitcoinPrivate Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 183, # slip44 + False, # segwit + False, # taproot + 42, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Bitcore": return CoinInfo( - coin_name=name, - coin_shortcut="BTX", - decimals=8, - address_type=3, - address_type_p2sh=125, - maxfee_kb=14000000000, - signed_message_header="BitCore Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="btx", - cashaddr_prefix=None, - slip44=160, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "BTX", # coin_shortcut + 8, # decimals + 3, # address_type + 125, # address_type_p2sh + 14000000000, # maxfee_kb + "BitCore Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "btx", # bech32_prefix + None, # cashaddr_prefix + 160, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "CPUchain": return CoinInfo( - coin_name=name, - coin_shortcut="CPU", - decimals=8, - address_type=28, - address_type_p2sh=30, - maxfee_kb=8700000000000, - signed_message_header="CPUchain Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="cpu", - cashaddr_prefix=None, - slip44=363, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "CPU", # coin_shortcut + 8, # decimals + 28, # address_type + 30, # address_type_p2sh + 8700000000000, # maxfee_kb + "CPUchain Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "cpu", # bech32_prefix + None, # cashaddr_prefix + 363, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Crown": return CoinInfo( - coin_name=name, - coin_shortcut="CRW", - decimals=8, - address_type=95495, - address_type_p2sh=95473, - maxfee_kb=52000000000, - signed_message_header="Crown Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=72, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "CRW", # coin_shortcut + 8, # decimals + 95495, # address_type + 95473, # address_type_p2sh + 52000000000, # maxfee_kb + "Crown Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 72, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Dash": return CoinInfo( - coin_name=name, - coin_shortcut="DASH", - decimals=8, - address_type=76, - address_type_p2sh=16, - maxfee_kb=45000000, - signed_message_header="DarkCoin Signed Message:\n", - xpub_magic=0x02fe52cc, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=5, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "DASH", # coin_shortcut + 8, # decimals + 76, # address_type + 16, # address_type_p2sh + 45000000, # maxfee_kb + "DarkCoin Signed Message:\n", # signed_message_header + 0x02fe52cc, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 5, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Dash Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="tDASH", - decimals=8, - address_type=140, - address_type_p2sh=19, - maxfee_kb=100000, - signed_message_header="DarkCoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=1, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "tDASH", # coin_shortcut + 8, # decimals + 140, # address_type + 19, # address_type_p2sh + 100000, # maxfee_kb + "DarkCoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Decred": return CoinInfo( - coin_name=name, - coin_shortcut="DCR", - decimals=8, - address_type=1855, - address_type_p2sh=1818, - maxfee_kb=220000000, - signed_message_header="Decred Signed Message:\n", - xpub_magic=0x02fda926, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=42, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=True, - negative_fee=False, - curve_name='secp256k1-decred', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "DCR", # coin_shortcut + 8, # decimals + 1855, # address_type + 1818, # address_type_p2sh + 220000000, # maxfee_kb + "Decred Signed Message:\n", # signed_message_header + 0x02fda926, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 42, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + True, # decred + False, # negative_fee + 'secp256k1-decred', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Decred Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="TDCR", - decimals=8, - address_type=3873, - address_type_p2sh=3836, - maxfee_kb=10000000, - signed_message_header="Decred Signed Message:\n", - xpub_magic=0x043587d1, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=1, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=True, - negative_fee=False, - curve_name='secp256k1-decred', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "TDCR", # coin_shortcut + 8, # decimals + 3873, # address_type + 3836, # address_type_p2sh + 10000000, # maxfee_kb + "Decred Signed Message:\n", # signed_message_header + 0x043587d1, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + True, # decred + False, # negative_fee + 'secp256k1-decred', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "DigiByte": return CoinInfo( - coin_name=name, - coin_shortcut="DGB", - decimals=8, - address_type=30, - address_type_p2sh=63, - maxfee_kb=130000000000, - signed_message_header="DigiByte Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="dgb", - cashaddr_prefix=None, - slip44=20, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "DGB", # coin_shortcut + 8, # decimals + 30, # address_type + 63, # address_type_p2sh + 130000000000, # maxfee_kb + "DigiByte Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "dgb", # bech32_prefix + None, # cashaddr_prefix + 20, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Dogecoin": return CoinInfo( - coin_name=name, - coin_shortcut="DOGE", - decimals=8, - address_type=30, - address_type_p2sh=22, - maxfee_kb=1200000000000, - signed_message_header="Dogecoin Signed Message:\n", - xpub_magic=0x02facafd, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=3, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "DOGE", # coin_shortcut + 8, # decimals + 30, # address_type + 22, # address_type_p2sh + 1200000000000, # maxfee_kb + "Dogecoin Signed Message:\n", # signed_message_header + 0x02facafd, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 3, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Elements": return CoinInfo( - coin_name=name, - coin_shortcut="ELEMENTS", - decimals=8, - address_type=235, - address_type_p2sh=75, - maxfee_kb=10000000, - signed_message_header="Bitcoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=0x044a5262, - xpub_magic_segwit_native=0x045f1cf6, - xpub_magic_multisig_segwit_p2sh=0x043587cf, - xpub_magic_multisig_segwit_native=0x043587cf, - bech32_prefix="ert", - cashaddr_prefix=None, - slip44=1, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets={'address_prefix': 4, 'blech32_prefix': 'el'}, + name, # coin_name + "ELEMENTS", # coin_shortcut + 8, # decimals + 235, # address_type + 75, # address_type_p2sh + 10000000, # maxfee_kb + "Bitcoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + 0x044a5262, # xpub_magic_segwit_p2sh + 0x045f1cf6, # xpub_magic_segwit_native + 0x043587cf, # xpub_magic_multisig_segwit_p2sh + 0x043587cf, # xpub_magic_multisig_segwit_native + "ert", # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + {'address_prefix': 4, 'blech32_prefix': 'el'}, # confidential_assets ) if name == "Feathercoin": return CoinInfo( - coin_name=name, - coin_shortcut="FTC", - decimals=8, - address_type=14, - address_type_p2sh=5, - maxfee_kb=390000000000, - signed_message_header="Feathercoin Signed Message:\n", - xpub_magic=0x0488bc26, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488bc26, - xpub_magic_multisig_segwit_native=0x0488bc26, - bech32_prefix="fc", - cashaddr_prefix=None, - slip44=8, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "FTC", # coin_shortcut + 8, # decimals + 14, # address_type + 5, # address_type_p2sh + 390000000000, # maxfee_kb + "Feathercoin Signed Message:\n", # signed_message_header + 0x0488bc26, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488bc26, # xpub_magic_multisig_segwit_p2sh + 0x0488bc26, # xpub_magic_multisig_segwit_native + "fc", # bech32_prefix + None, # cashaddr_prefix + 8, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Firo": return CoinInfo( - coin_name=name, - coin_shortcut="FIRO", - decimals=8, - address_type=82, - address_type_p2sh=7, - maxfee_kb=640000000, - signed_message_header="Zcoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=136, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "FIRO", # coin_shortcut + 8, # decimals + 82, # address_type + 7, # address_type_p2sh + 640000000, # maxfee_kb + "Zcoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 136, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Firo Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="tFIRO", - decimals=8, - address_type=65, - address_type_p2sh=178, - maxfee_kb=1000000, - signed_message_header="Zcoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=1, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "tFIRO", # coin_shortcut + 8, # decimals + 65, # address_type + 178, # address_type_p2sh + 1000000, # maxfee_kb + "Zcoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Florincoin": return CoinInfo( - coin_name=name, - coin_shortcut="FLO", - decimals=8, - address_type=35, - address_type_p2sh=94, - maxfee_kb=78000000000, - signed_message_header="Florincoin Signed Message:\n", - xpub_magic=0x00174921, - xpub_magic_segwit_p2sh=0x01b26ef6, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x00174921, - xpub_magic_multisig_segwit_native=0x00174921, - bech32_prefix="flo", - cashaddr_prefix=None, - slip44=216, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "FLO", # coin_shortcut + 8, # decimals + 35, # address_type + 94, # address_type_p2sh + 78000000000, # maxfee_kb + "Florincoin Signed Message:\n", # signed_message_header + 0x00174921, # xpub_magic + 0x01b26ef6, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x00174921, # xpub_magic_multisig_segwit_p2sh + 0x00174921, # xpub_magic_multisig_segwit_native + "flo", # bech32_prefix + None, # cashaddr_prefix + 216, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Fujicoin": return CoinInfo( - coin_name=name, - coin_shortcut="FJC", - decimals=8, - address_type=36, - address_type_p2sh=16, - maxfee_kb=35000000000000, - signed_message_header="FujiCoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0295b43f, - xpub_magic_multisig_segwit_native=0x02aa7ed3, - bech32_prefix="fc", - cashaddr_prefix=None, - slip44=75, - segwit=True, - taproot=True, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "FJC", # coin_shortcut + 8, # decimals + 36, # address_type + 16, # address_type_p2sh + 35000000000000, # maxfee_kb + "FujiCoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0295b43f, # xpub_magic_multisig_segwit_p2sh + 0x02aa7ed3, # xpub_magic_multisig_segwit_native + "fc", # bech32_prefix + None, # cashaddr_prefix + 75, # slip44 + True, # segwit + True, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Groestlcoin": return CoinInfo( - coin_name=name, - coin_shortcut="GRS", - decimals=8, - address_type=36, - address_type_p2sh=5, - maxfee_kb=16000000000, - signed_message_header="GroestlCoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="grs", - cashaddr_prefix=None, - slip44=17, - segwit=True, - taproot=True, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1-groestl', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "GRS", # coin_shortcut + 8, # decimals + 36, # address_type + 5, # address_type_p2sh + 16000000000, # maxfee_kb + "GroestlCoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "grs", # bech32_prefix + None, # cashaddr_prefix + 17, # slip44 + True, # segwit + True, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1-groestl', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Groestlcoin Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="tGRS", - decimals=8, - address_type=111, - address_type_p2sh=196, - maxfee_kb=100000, - signed_message_header="GroestlCoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=0x044a5262, - xpub_magic_segwit_native=0x045f1cf6, - xpub_magic_multisig_segwit_p2sh=0x043587cf, - xpub_magic_multisig_segwit_native=0x043587cf, - bech32_prefix="tgrs", - cashaddr_prefix=None, - slip44=1, - segwit=True, - taproot=True, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1-groestl', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "tGRS", # coin_shortcut + 8, # decimals + 111, # address_type + 196, # address_type_p2sh + 100000, # maxfee_kb + "GroestlCoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + 0x044a5262, # xpub_magic_segwit_p2sh + 0x045f1cf6, # xpub_magic_segwit_native + 0x043587cf, # xpub_magic_multisig_segwit_p2sh + 0x043587cf, # xpub_magic_multisig_segwit_native + "tgrs", # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + True, # segwit + True, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1-groestl', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Komodo": return CoinInfo( - coin_name=name, - coin_shortcut="KMD", - decimals=8, - address_type=60, - address_type_p2sh=85, - maxfee_kb=4800000000, - signed_message_header="Komodo Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=141, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=True, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=True, - confidential_assets=None, + name, # coin_name + "KMD", # coin_shortcut + 8, # decimals + 60, # address_type + 85, # address_type_p2sh + 4800000000, # maxfee_kb + "Komodo Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 141, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + True, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + True, # overwintered + None, # confidential_assets ) if name == "Koto": return CoinInfo( - coin_name=name, - coin_shortcut="KOTO", - decimals=8, - address_type=6198, - address_type_p2sh=6203, - maxfee_kb=1000000, - signed_message_header="Koto Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=510, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=True, - confidential_assets=None, + name, # coin_name + "KOTO", # coin_shortcut + 8, # decimals + 6198, # address_type + 6203, # address_type_p2sh + 1000000, # maxfee_kb + "Koto Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 510, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + True, # overwintered + None, # confidential_assets ) if name == "Litecoin": return CoinInfo( - coin_name=name, - coin_shortcut="LTC", - decimals=8, - address_type=48, - address_type_p2sh=50, - maxfee_kb=67000000, - signed_message_header="Litecoin Signed Message:\n", - xpub_magic=0x019da462, - xpub_magic_segwit_p2sh=0x01b26ef6, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x019da462, - xpub_magic_multisig_segwit_native=0x019da462, - bech32_prefix="ltc", - cashaddr_prefix=None, - slip44=2, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "LTC", # coin_shortcut + 8, # decimals + 48, # address_type + 50, # address_type_p2sh + 67000000, # maxfee_kb + "Litecoin Signed Message:\n", # signed_message_header + 0x019da462, # xpub_magic + 0x01b26ef6, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x019da462, # xpub_magic_multisig_segwit_p2sh + 0x019da462, # xpub_magic_multisig_segwit_native + "ltc", # bech32_prefix + None, # cashaddr_prefix + 2, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Litecoin Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="tLTC", - decimals=8, - address_type=111, - address_type_p2sh=58, - maxfee_kb=40000000, - signed_message_header="Litecoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=0x044a5262, - xpub_magic_segwit_native=0x045f1cf6, - xpub_magic_multisig_segwit_p2sh=0x043587cf, - xpub_magic_multisig_segwit_native=0x043587cf, - bech32_prefix="tltc", - cashaddr_prefix=None, - slip44=1, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "tLTC", # coin_shortcut + 8, # decimals + 111, # address_type + 58, # address_type_p2sh + 40000000, # maxfee_kb + "Litecoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + 0x044a5262, # xpub_magic_segwit_p2sh + 0x045f1cf6, # xpub_magic_segwit_native + 0x043587cf, # xpub_magic_multisig_segwit_p2sh + 0x043587cf, # xpub_magic_multisig_segwit_native + "tltc", # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Monacoin": return CoinInfo( - coin_name=name, - coin_shortcut="MONA", - decimals=8, - address_type=50, - address_type_p2sh=55, - maxfee_kb=2100000000, - signed_message_header="Monacoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="mona", - cashaddr_prefix=None, - slip44=22, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "MONA", # coin_shortcut + 8, # decimals + 50, # address_type + 55, # address_type_p2sh + 2100000000, # maxfee_kb + "Monacoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "mona", # bech32_prefix + None, # cashaddr_prefix + 22, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "MonetaryUnit": return CoinInfo( - coin_name=name, - coin_shortcut="MUE", - decimals=8, - address_type=16, - address_type_p2sh=76, - maxfee_kb=600000000000, - signed_message_header="MonetaryUnit Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=31, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "MUE", # coin_shortcut + 8, # decimals + 16, # address_type + 76, # address_type_p2sh + 600000000000, # maxfee_kb + "MonetaryUnit Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 31, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Namecoin": return CoinInfo( - coin_name=name, - coin_shortcut="NMC", - decimals=8, - address_type=52, - address_type_p2sh=5, - maxfee_kb=8700000000, - signed_message_header="Namecoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=7, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "NMC", # coin_shortcut + 8, # decimals + 52, # address_type + 5, # address_type_p2sh + 8700000000, # maxfee_kb + "Namecoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 7, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Peercoin": return CoinInfo( - coin_name=name, - coin_shortcut="PPC", - decimals=6, - address_type=55, - address_type_p2sh=117, - maxfee_kb=13000000000, - signed_message_header="Peercoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="pc", - cashaddr_prefix=None, - slip44=6, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=True, - overwintered=False, - confidential_assets=None, + name, # coin_name + "PPC", # coin_shortcut + 6, # decimals + 55, # address_type + 117, # address_type_p2sh + 13000000000, # maxfee_kb + "Peercoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "pc", # bech32_prefix + None, # cashaddr_prefix + 6, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + True, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Peercoin Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="tPPC", - decimals=6, - address_type=111, - address_type_p2sh=196, - maxfee_kb=2000000, - signed_message_header="Peercoin Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=0x044a5262, - xpub_magic_segwit_native=0x045f1cf6, - xpub_magic_multisig_segwit_p2sh=0x043587cf, - xpub_magic_multisig_segwit_native=0x043587cf, - bech32_prefix="tpc", - cashaddr_prefix=None, - slip44=1, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=True, - overwintered=False, - confidential_assets=None, + name, # coin_name + "tPPC", # coin_shortcut + 6, # decimals + 111, # address_type + 196, # address_type_p2sh + 2000000, # maxfee_kb + "Peercoin Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + 0x044a5262, # xpub_magic_segwit_p2sh + 0x045f1cf6, # xpub_magic_segwit_native + 0x043587cf, # xpub_magic_multisig_segwit_p2sh + 0x043587cf, # xpub_magic_multisig_segwit_native + "tpc", # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + True, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Primecoin": return CoinInfo( - coin_name=name, - coin_shortcut="XPM", - decimals=8, - address_type=23, - address_type_p2sh=83, - maxfee_kb=89000000000, - signed_message_header="Primecoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=24, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "XPM", # coin_shortcut + 8, # decimals + 23, # address_type + 83, # address_type_p2sh + 89000000000, # maxfee_kb + "Primecoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 24, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Qtum": return CoinInfo( - coin_name=name, - coin_shortcut="QTUM", - decimals=8, - address_type=58, - address_type_p2sh=50, - maxfee_kb=1000000000, - signed_message_header="Qtum Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="qc", - cashaddr_prefix=None, - slip44=2301, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "QTUM", # coin_shortcut + 8, # decimals + 58, # address_type + 50, # address_type_p2sh + 1000000000, # maxfee_kb + "Qtum Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "qc", # bech32_prefix + None, # cashaddr_prefix + 2301, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Qtum Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="tQTUM", - decimals=8, - address_type=120, - address_type_p2sh=110, - maxfee_kb=40000000, - signed_message_header="Qtum Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=0x044a5262, - xpub_magic_segwit_native=0x045f1cf6, - xpub_magic_multisig_segwit_p2sh=0x043587cf, - xpub_magic_multisig_segwit_native=0x043587cf, - bech32_prefix="tq", - cashaddr_prefix=None, - slip44=1, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "tQTUM", # coin_shortcut + 8, # decimals + 120, # address_type + 110, # address_type_p2sh + 40000000, # maxfee_kb + "Qtum Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + 0x044a5262, # xpub_magic_segwit_p2sh + 0x045f1cf6, # xpub_magic_segwit_native + 0x043587cf, # xpub_magic_multisig_segwit_p2sh + 0x043587cf, # xpub_magic_multisig_segwit_native + "tq", # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Ravencoin": return CoinInfo( - coin_name=name, - coin_shortcut="RVN", - decimals=8, - address_type=60, - address_type_p2sh=122, - maxfee_kb=170000000000, - signed_message_header="Raven Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=175, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "RVN", # coin_shortcut + 8, # decimals + 60, # address_type + 122, # address_type_p2sh + 170000000000, # maxfee_kb + "Raven Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 175, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Ravencoin Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="tRVN", - decimals=8, - address_type=111, - address_type_p2sh=196, - maxfee_kb=170000000000, - signed_message_header="Raven Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=1, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "tRVN", # coin_shortcut + 8, # decimals + 111, # address_type + 196, # address_type_p2sh + 170000000000, # maxfee_kb + "Raven Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Ritocoin": return CoinInfo( - coin_name=name, - coin_shortcut="RITO", - decimals=8, - address_type=25, - address_type_p2sh=105, - maxfee_kb=39000000000000, - signed_message_header="Rito Signed Message:\n", - xpub_magic=0x0534e7ca, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=19169, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "RITO", # coin_shortcut + 8, # decimals + 25, # address_type + 105, # address_type_p2sh + 39000000000000, # maxfee_kb + "Rito Signed Message:\n", # signed_message_header + 0x0534e7ca, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 19169, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "SmartCash": return CoinInfo( - coin_name=name, - coin_shortcut="SMART", - decimals=8, - address_type=63, - address_type_p2sh=18, - maxfee_kb=780000000000, - signed_message_header="SmartCash Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=224, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1-smart', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "SMART", # coin_shortcut + 8, # decimals + 63, # address_type + 18, # address_type_p2sh + 780000000000, # maxfee_kb + "SmartCash Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 224, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1-smart', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "SmartCash Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="tSMART", - decimals=8, - address_type=65, - address_type_p2sh=21, - maxfee_kb=1000000, - signed_message_header="SmartCash Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=1, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1-smart', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "tSMART", # coin_shortcut + 8, # decimals + 65, # address_type + 21, # address_type_p2sh + 1000000, # maxfee_kb + "SmartCash Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1-smart', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Stakenet": return CoinInfo( - coin_name=name, - coin_shortcut="XSN", - decimals=8, - address_type=76, - address_type_p2sh=16, - maxfee_kb=11000000000, - signed_message_header="DarkCoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="xc", - cashaddr_prefix=None, - slip44=199, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "XSN", # coin_shortcut + 8, # decimals + 76, # address_type + 16, # address_type_p2sh + 11000000000, # maxfee_kb + "DarkCoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "xc", # bech32_prefix + None, # cashaddr_prefix + 199, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Syscoin": return CoinInfo( - coin_name=name, - coin_shortcut="SYS", - decimals=8, - address_type=63, - address_type_p2sh=5, - maxfee_kb=42000000000, - signed_message_header="Syscoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="sys", - cashaddr_prefix=None, - slip44=57, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "SYS", # coin_shortcut + 8, # decimals + 63, # address_type + 5, # address_type_p2sh + 42000000000, # maxfee_kb + "Syscoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "sys", # bech32_prefix + None, # cashaddr_prefix + 57, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Unobtanium": return CoinInfo( - coin_name=name, - coin_shortcut="UNO", - decimals=8, - address_type=130, - address_type_p2sh=30, - maxfee_kb=53000000, - signed_message_header="Unobtanium Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=92, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "UNO", # coin_shortcut + 8, # decimals + 130, # address_type + 30, # address_type_p2sh + 53000000, # maxfee_kb + "Unobtanium Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 92, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "VIPSTARCOIN": return CoinInfo( - coin_name=name, - coin_shortcut="VIPS", - decimals=8, - address_type=70, - address_type_p2sh=50, - maxfee_kb=140000000000000, - signed_message_header="VIPSTARCOIN Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="vips", - cashaddr_prefix=None, - slip44=1919, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "VIPS", # coin_shortcut + 8, # decimals + 70, # address_type + 50, # address_type_p2sh + 140000000000000, # maxfee_kb + "VIPSTARCOIN Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "vips", # bech32_prefix + None, # cashaddr_prefix + 1919, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Verge": return CoinInfo( - coin_name=name, - coin_shortcut="XVG", - decimals=6, - address_type=30, - address_type_p2sh=33, - maxfee_kb=550000000000, - signed_message_header="Name: Dogecoin Dark\n", - xpub_magic=0x022d2533, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=77, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=True, - overwintered=False, - confidential_assets=None, + name, # coin_name + "XVG", # coin_shortcut + 6, # decimals + 30, # address_type + 33, # address_type_p2sh + 550000000000, # maxfee_kb + "Name: Dogecoin Dark\n", # signed_message_header + 0x022d2533, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 77, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + True, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Vertcoin": return CoinInfo( - coin_name=name, - coin_shortcut="VTC", - decimals=8, - address_type=71, - address_type_p2sh=5, - maxfee_kb=13000000000, - signed_message_header="Vertcoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="vtc", - cashaddr_prefix=None, - slip44=28, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "VTC", # coin_shortcut + 8, # decimals + 71, # address_type + 5, # address_type_p2sh + 13000000000, # maxfee_kb + "Vertcoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "vtc", # bech32_prefix + None, # cashaddr_prefix + 28, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Viacoin": return CoinInfo( - coin_name=name, - coin_shortcut="VIA", - decimals=8, - address_type=71, - address_type_p2sh=33, - maxfee_kb=14000000000, - signed_message_header="Viacoin Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=0x049d7cb2, - xpub_magic_segwit_native=0x04b24746, - xpub_magic_multisig_segwit_p2sh=0x0488b21e, - xpub_magic_multisig_segwit_native=0x0488b21e, - bech32_prefix="via", - cashaddr_prefix=None, - slip44=14, - segwit=True, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "VIA", # coin_shortcut + 8, # decimals + 71, # address_type + 33, # address_type_p2sh + 14000000000, # maxfee_kb + "Viacoin Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + 0x049d7cb2, # xpub_magic_segwit_p2sh + 0x04b24746, # xpub_magic_segwit_native + 0x0488b21e, # xpub_magic_multisig_segwit_p2sh + 0x0488b21e, # xpub_magic_multisig_segwit_native + "via", # bech32_prefix + None, # cashaddr_prefix + 14, # slip44 + True, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "ZCore": return CoinInfo( - coin_name=name, - coin_shortcut="ZCR", - decimals=8, - address_type=142, - address_type_p2sh=145, - maxfee_kb=170000000000, - signed_message_header="DarkNet Signed Message:\n", - xpub_magic=0x04b24746, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=428, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "ZCR", # coin_shortcut + 8, # decimals + 142, # address_type + 145, # address_type_p2sh + 170000000000, # maxfee_kb + "DarkNet Signed Message:\n", # signed_message_header + 0x04b24746, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 428, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) if name == "Zcash": return CoinInfo( - coin_name=name, - coin_shortcut="ZEC", - decimals=8, - address_type=7352, - address_type_p2sh=7357, - maxfee_kb=51000000, - signed_message_header="Zcash Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=133, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=True, - confidential_assets=None, + name, # coin_name + "ZEC", # coin_shortcut + 8, # decimals + 7352, # address_type + 7357, # address_type_p2sh + 51000000, # maxfee_kb + "Zcash Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 133, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + True, # overwintered + None, # confidential_assets ) if name == "Zcash Testnet": return CoinInfo( - coin_name=name, - coin_shortcut="TAZ", - decimals=8, - address_type=7461, - address_type_p2sh=7354, - maxfee_kb=10000000, - signed_message_header="Zcash Signed Message:\n", - xpub_magic=0x043587cf, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=1, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=True, - timestamp=False, - overwintered=True, - confidential_assets=None, + name, # coin_name + "TAZ", # coin_shortcut + 8, # decimals + 7461, # address_type + 7354, # address_type_p2sh + 10000000, # maxfee_kb + "Zcash Signed Message:\n", # signed_message_header + 0x043587cf, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 1, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + True, # extra_data + False, # timestamp + True, # overwintered + None, # confidential_assets ) if name == "Brhodium": return CoinInfo( - coin_name=name, - coin_shortcut="XRC", - decimals=8, - address_type=61, - address_type_p2sh=123, - maxfee_kb=1000000000, - signed_message_header="BitCoin Rhodium Signed Message:\n", - xpub_magic=0x0488b21e, - xpub_magic_segwit_p2sh=None, - xpub_magic_segwit_native=None, - xpub_magic_multisig_segwit_p2sh=None, - xpub_magic_multisig_segwit_native=None, - bech32_prefix=None, - cashaddr_prefix=None, - slip44=10291, - segwit=False, - taproot=False, - fork_id=None, - force_bip143=False, - decred=False, - negative_fee=False, - curve_name='secp256k1', - extra_data=False, - timestamp=False, - overwintered=False, - confidential_assets=None, + name, # coin_name + "XRC", # coin_shortcut + 8, # decimals + 61, # address_type + 123, # address_type_p2sh + 1000000000, # maxfee_kb + "BitCoin Rhodium Signed Message:\n", # signed_message_header + 0x0488b21e, # xpub_magic + None, # xpub_magic_segwit_p2sh + None, # xpub_magic_segwit_native + None, # xpub_magic_multisig_segwit_p2sh + None, # xpub_magic_multisig_segwit_native + None, # bech32_prefix + None, # cashaddr_prefix + 10291, # slip44 + False, # segwit + False, # taproot + None, # fork_id + False, # force_bip143 + False, # decred + False, # negative_fee + 'secp256k1', # curve_name + False, # extra_data + False, # timestamp + False, # overwintered + None, # confidential_assets ) raise ValueError # Unknown coin name diff --git a/core/src/apps/common/coininfo.py.mako b/core/src/apps/common/coininfo.py.mako index f2716a12f..fba7ef0ac 100644 --- a/core/src/apps/common/coininfo.py.mako +++ b/core/src/apps/common/coininfo.py.mako @@ -1,6 +1,9 @@ # generated from coininfo.py.mako # (by running `make templates` in `core`) # do not edit manually! + +# NOTE: using positional arguments saves 4500 bytes of flash size + from typing import Any from trezor import utils @@ -142,7 +145,7 @@ def by_name(name: str) -> CoinInfo: if name == ${black_repr(coin["coin_name"])}: return CoinInfo( % for attr, func in ATTRIBUTES: - ${attr}=${func(coin[attr])}, + ${func(coin[attr])}, # ${attr} % endfor ) % endfor @@ -151,7 +154,7 @@ def by_name(name: str) -> CoinInfo: if name == ${black_repr(coin["coin_name"])}: return CoinInfo( % for attr, func in ATTRIBUTES: - ${attr}=${func(coin[attr])}, + ${func(coin[attr])}, # ${attr} % endfor ) % endfor diff --git a/core/src/apps/common/keychain.py b/core/src/apps/common/keychain.py index 22ae6f815..746c80fc1 100644 --- a/core/src/apps/common/keychain.py +++ b/core/src/apps/common/keychain.py @@ -1,11 +1,9 @@ -import sys from typing import TYPE_CHECKING -from trezor import wire from trezor.crypto import bip32 +from trezor.wire import DataError from . import paths, safety_checks -from .seed import Slip21Node, get_seed if TYPE_CHECKING: from typing import ( @@ -18,6 +16,8 @@ if TYPE_CHECKING: from typing_extensions import Protocol from trezor.protobuf import MessageType + from trezor.wire import Context + from .seed import Slip21Node T = TypeVar("T") @@ -36,15 +36,15 @@ if TYPE_CHECKING: MsgIn = TypeVar("MsgIn", bound=MessageType) MsgOut = TypeVar("MsgOut", bound=MessageType) - Handler = Callable[[wire.Context, MsgIn], Awaitable[MsgOut]] - HandlerWithKeychain = Callable[[wire.Context, MsgIn, "Keychain"], Awaitable[MsgOut]] + Handler = Callable[[Context, MsgIn], Awaitable[MsgOut]] + HandlerWithKeychain = Callable[[Context, MsgIn, "Keychain"], Awaitable[MsgOut]] class Deletable(Protocol): def __del__(self) -> None: ... -FORBIDDEN_KEY_PATH = wire.DataError("Forbidden key path") +FORBIDDEN_KEY_PATH = DataError("Forbidden key path") class LRUCache: @@ -54,13 +54,15 @@ class LRUCache: self.cache: dict[Any, Deletable] = {} def insert(self, key: Any, value: Deletable) -> None: - if key in self.cache_keys: - self.cache_keys.remove(key) - self.cache_keys.insert(0, key) + cache_keys = self.cache_keys # local_cache_attribute + + if key in cache_keys: + cache_keys.remove(key) + cache_keys.insert(0, key) self.cache[key] = value - if len(self.cache_keys) > self.size: - dropped_key = self.cache_keys.pop() + if len(cache_keys) > self.size: + dropped_key = cache_keys.pop() self.cache[dropped_key].__del__() del self.cache[dropped_key] @@ -103,7 +105,7 @@ class Keychain: def verify_path(self, path: paths.Bip32Path) -> None: if "ed25519" in self.curve and not paths.path_is_hardened(path): - raise wire.DataError("Non-hardened paths unsupported on Ed25519") + raise DataError("Non-hardened paths unsupported on Ed25519") if not safety_checks.is_strict(): return @@ -137,8 +139,8 @@ class Keychain: if self._root_fingerprint is None: # derive m/0' to obtain root_fingerprint n = self._derive_with_cache( - prefix_len=0, - path=[0 | paths.HARDENED], + 0, + [0 | paths.HARDENED], new_root=lambda: bip32.from_seed(self.seed, self.curve), ) self._root_fingerprint = n.fingerprint() @@ -147,20 +149,22 @@ class Keychain: def derive(self, path: paths.Bip32Path) -> bip32.HDNode: self.verify_path(path) return self._derive_with_cache( - prefix_len=3, - path=path, + 3, + path, new_root=lambda: bip32.from_seed(self.seed, self.curve), ) def derive_slip21(self, path: paths.Slip21Path) -> Slip21Node: + from .seed import Slip21Node + if safety_checks.is_strict() and not any( ns == path[: len(ns)] for ns in self.slip21_namespaces ): raise FORBIDDEN_KEY_PATH return self._derive_with_cache( - prefix_len=1, - path=path, + 1, + path, new_root=lambda: Slip21Node(seed=self.seed), ) @@ -172,11 +176,13 @@ class Keychain: async def get_keychain( - ctx: wire.Context, + ctx: Context, curve: str, schemas: Iterable[paths.PathSchemaType], slip21_namespaces: Iterable[paths.Slip21Path] = (), ) -> Keychain: + from .seed import get_seed + seed = await get_seed(ctx) keychain = Keychain(seed, curve, schemas, slip21_namespaces) return keychain @@ -191,18 +197,15 @@ def with_slip44_keychain( if not patterns: raise ValueError # specify a pattern - if allow_testnet: - slip44_ids: int | tuple[int, int] = (slip44_id, 1) - else: - slip44_ids = slip44_id + slip_44_ids = (slip44_id, 1) if allow_testnet else slip44_id schemas = [] for pattern in patterns: - schemas.append(paths.PathSchema.parse(pattern=pattern, slip44_id=slip44_ids)) + schemas.append(paths.PathSchema.parse(pattern, slip_44_ids)) schemas = [s.copy() for s in schemas] def decorator(func: HandlerWithKeychain[MsgIn, MsgOut]) -> Handler[MsgIn, MsgOut]: - async def wrapper(ctx: wire.Context, msg: MsgIn) -> MsgOut: + async def wrapper(ctx: Context, msg: MsgIn) -> MsgOut: keychain = await get_keychain(ctx, curve, schemas) with keychain: return await func(ctx, msg, keychain) @@ -215,6 +218,8 @@ def with_slip44_keychain( def auto_keychain( modname: str, allow_testnet: bool = True ) -> Callable[[HandlerWithKeychain[MsgIn, MsgOut]], Handler[MsgIn, MsgOut]]: + import sys + rdot = modname.rfind(".") parent_modname = modname[:rdot] parent_module = sys.modules[parent_modname] diff --git a/core/src/apps/common/mnemonic.py b/core/src/apps/common/mnemonic.py index 3dc917ff7..7fcb439a2 100644 --- a/core/src/apps/common/mnemonic.py +++ b/core/src/apps/common/mnemonic.py @@ -1,6 +1,10 @@ -import storage.device -from trezor import ui, utils, workflow -from trezor.enums import BackupType +from typing import TYPE_CHECKING + +import storage.device as storage_device +from trezor import utils + +if TYPE_CHECKING: + from trezor.enums import BackupType def get() -> tuple[bytes | None, BackupType]: @@ -8,11 +12,11 @@ def get() -> tuple[bytes | None, BackupType]: def get_secret() -> bytes | None: - return storage.device.get_mnemonic_secret() + return storage_device.get_mnemonic_secret() def get_type() -> BackupType: - return storage.device.get_backup_type() + return storage_device.get_backup_type() def is_bip39() -> bool: @@ -20,6 +24,8 @@ def is_bip39() -> bool: If False then SLIP-39 (either Basic or Advanced). Other invalid values are checked directly in storage. """ + from trezor.enums import BackupType + return get_type() == BackupType.Bip39 @@ -41,8 +47,8 @@ def get_seed(passphrase: str = "", progress_bar: bool = True) -> bytes: else: # SLIP-39 from trezor.crypto import slip39 - identifier = storage.device.get_slip39_identifier() - iteration_exponent = storage.device.get_slip39_iteration_exponent() + identifier = storage_device.get_slip39_identifier() + iteration_exponent = storage_device.get_slip39_iteration_exponent() if identifier is None or iteration_exponent is None: # Identifier or exponent expected but not found raise RuntimeError @@ -84,6 +90,7 @@ if not utils.BITCOIN_ONLY: def _start_progress() -> None: + from trezor import workflow from trezor.ui.layouts import draw_simple_text # Because we are drawing to the screen manually, without a layout, we @@ -93,6 +100,8 @@ def _start_progress() -> None: def _render_progress(progress: int, total: int) -> None: + from trezor import ui + p = 1000 * progress // total ui.display.loader(p, False, 18, ui.WHITE, ui.BG) ui.refresh() diff --git a/core/src/apps/common/passphrase.py b/core/src/apps/common/passphrase.py index a00199b7c..ec4f0c9a5 100644 --- a/core/src/apps/common/passphrase.py +++ b/core/src/apps/common/passphrase.py @@ -1,66 +1,72 @@ from micropython import const +from typing import TYPE_CHECKING -import storage.device -from trezor import wire, workflow +import storage.device as storage_device +from trezor.wire import DataError + +if TYPE_CHECKING: + from trezor.wire import Context _MAX_PASSPHRASE_LEN = const(50) def is_enabled() -> bool: - return storage.device.is_passphrase_enabled() + return storage_device.is_passphrase_enabled() -async def get(ctx: wire.Context) -> str: - if is_enabled(): - return await _request_from_user(ctx) - else: +async def get(ctx: Context) -> str: + from trezor import workflow + + if not is_enabled(): return "" - - -async def _request_from_user(ctx: wire.Context) -> str: - workflow.close_others() # request exclusive UI access - if storage.device.get_passphrase_always_on_device(): - from trezor.ui.layouts import request_passphrase_on_device - - passphrase = await request_passphrase_on_device(ctx, _MAX_PASSPHRASE_LEN) else: - passphrase = await _request_on_host(ctx) - if len(passphrase.encode()) > _MAX_PASSPHRASE_LEN: - raise wire.DataError( - f"Maximum passphrase length is {_MAX_PASSPHRASE_LEN} bytes" - ) + workflow.close_others() # request exclusive UI access + if storage_device.get_passphrase_always_on_device(): + from trezor.ui.layouts import request_passphrase_on_device - return passphrase + passphrase = await request_passphrase_on_device(ctx, _MAX_PASSPHRASE_LEN) + else: + passphrase = await _request_on_host(ctx) + if len(passphrase.encode()) > _MAX_PASSPHRASE_LEN: + raise DataError(f"Maximum passphrase length is {_MAX_PASSPHRASE_LEN} bytes") + + return passphrase -async def _request_on_host(ctx: wire.Context) -> str: +async def _request_on_host(ctx: Context) -> str: from trezor.messages import PassphraseAck, PassphraseRequest + from trezor.ui.layouts import draw_simple_text - _entry_dialog() + # _entry_dialog + draw_simple_text( + "Passphrase entry", "Please type your\npassphrase on the\nconnected host." + ) request = PassphraseRequest() ack = await ctx.call(request, PassphraseAck) + passphrase = ack.passphrase # local_cache_attribute + if ack.on_device: from trezor.ui.layouts import request_passphrase_on_device - if ack.passphrase is not None: - raise wire.DataError("Passphrase provided when it should not be") + if passphrase is not None: + raise DataError("Passphrase provided when it should not be") return await request_passphrase_on_device(ctx, _MAX_PASSPHRASE_LEN) - if ack.passphrase is None: - raise wire.DataError( + if passphrase is None: + raise DataError( "Passphrase not provided and on_device is False. Use empty string to set an empty passphrase." ) # non-empty passphrase - if ack.passphrase: + if passphrase: from trezor import ui from trezor.ui.layouts import confirm_action, confirm_blob await confirm_action( ctx, "passphrase_host1", - title="Hidden wallet", + "Hidden wallet", description="Access hidden wallet?\n\nNext screen will show\nthe passphrase!", icon=ui.ICON_CONFIG, ) @@ -68,19 +74,11 @@ async def _request_on_host(ctx: wire.Context) -> str: await confirm_blob( ctx, "passphrase_host2", - title="Hidden wallet", - description="Use this passphrase?\n", - data=ack.passphrase, + "Hidden wallet", + passphrase, + "Use this passphrase?\n", icon=ui.ICON_CONFIG, icon_color=ui.ORANGE_ICON, ) - return ack.passphrase - - -def _entry_dialog() -> None: - from trezor.ui.layouts import draw_simple_text - - draw_simple_text( - "Passphrase entry", "Please type your\npassphrase on the\nconnected host." - ) + return passphrase diff --git a/core/src/apps/common/paths.py b/core/src/apps/common/paths.py index fcc76c2d2..c7b59911b 100644 --- a/core/src/apps/common/paths.py +++ b/core/src/apps/common/paths.py @@ -197,23 +197,24 @@ class PathSchema: # optionally replace a keyword component = cls.REPLACEMENTS.get(component, component) + append = schema.append # local_cache_attribute if "-" in component: # parse as a range a, b = [parse(s) for s in component.split("-", 1)] - schema.append(Interval(a, b)) + append(Interval(a, b)) elif "," in component: # parse as a list of values - schema.append(set(parse(s) for s in component.split(","))) + append(set(parse(s) for s in component.split(","))) elif component == "coin_type": # substitute SLIP-44 ids - schema.append(set(parse(s) for s in slip44_id)) + append(set(parse(s) for s in slip44_id)) else: # plain constant - schema.append((parse(component),)) + append((parse(component),)) return cls(schema, trailing_components, compact=True) @@ -258,18 +259,19 @@ class PathSchema: path. If the restriction results in a never-matching schema, then False is returned. """ + schema = self.schema # local_cache_attribute for i, value in enumerate(path): - if i < len(self.schema): + if i < len(schema): # Ensure that the path is a prefix of the schema. - if value not in self.schema[i]: + if value not in schema[i]: self.set_never_matching() return False # Restrict the schema component if there are multiple choices. - component = self.schema[i] + component = schema[i] if not isinstance(component, tuple) or len(component) != 1: - self.schema[i] = (value,) + schema[i] = (value,) else: # The path is longer than the schema. We need to restrict the # trailing components. @@ -278,7 +280,7 @@ class PathSchema: self.set_never_matching() return False - self.schema.append((value,)) + schema.append((value,)) return True @@ -286,6 +288,7 @@ class PathSchema: def __repr__(self) -> str: components = ["m"] + append = components.append # local_cache_attribute def unharden(item: int) -> int: return item ^ (item & HARDENED) @@ -294,7 +297,7 @@ class PathSchema: if isinstance(component, Interval): a, b = component.min, component.max prime = "'" if a & HARDENED else "" - components.append(f"[{unharden(a)}-{unharden(b)}]{prime}") + append(f"[{unharden(a)}-{unharden(b)}]{prime}") else: # typechecker thinks component is a Contanier but we're using it # as a Collection. @@ -307,15 +310,15 @@ class PathSchema: component_str = "[" + component_str + "]" if next(iter(collection)) & HARDENED: component_str += "'" - components.append(component_str) + append(component_str) if self.trailing_components: for key, val in self.WILDCARD_RANGES.items(): if self.trailing_components is val: - components.append(key) + append(key) break else: - components.append("???") + append("???") return "" @@ -362,7 +365,7 @@ def path_is_hardened(address_n: Bip32Path) -> bool: def address_n_to_str(address_n: Iterable[int]) -> str: - def path_item(i: int) -> str: + def _path_item(i: int) -> str: if i & HARDENED: return str(i ^ HARDENED) + "'" else: @@ -371,4 +374,4 @@ def address_n_to_str(address_n: Iterable[int]) -> str: if not address_n: return "m" - return "m/" + "/".join(path_item(i) for i in address_n) + return "m/" + "/".join(_path_item(i) for i in address_n) diff --git a/core/src/apps/common/readers.py b/core/src/apps/common/readers.py index 8c84042e6..e0cecb60e 100644 --- a/core/src/apps/common/readers.py +++ b/core/src/apps/common/readers.py @@ -1,27 +1,32 @@ -from trezor.utils import BufferReader +from typing import TYPE_CHECKING + +if TYPE_CHECKING: + from trezor.utils import BufferReader def read_compact_size(r: BufferReader) -> int: - prefix = r.get() + get = r.get # local_cache_attribute + + prefix = get() if prefix < 253: n = prefix elif prefix == 253: - n = r.get() - n += r.get() << 8 + n = get() + n += get() << 8 elif prefix == 254: - n = r.get() - n += r.get() << 8 - n += r.get() << 16 - n += r.get() << 24 + n = get() + n += get() << 8 + n += get() << 16 + n += get() << 24 elif prefix == 255: - n = r.get() - n += r.get() << 8 - n += r.get() << 16 - n += r.get() << 24 - n += r.get() << 32 - n += r.get() << 40 - n += r.get() << 48 - n += r.get() << 56 + n = get() + n += get() << 8 + n += get() << 16 + n += get() << 24 + n += get() << 32 + n += get() << 40 + n += get() << 48 + n += get() << 56 else: raise ValueError return n diff --git a/core/src/apps/common/request_pin.py b/core/src/apps/common/request_pin.py index 3788eb3a7..c4bc7d24f 100644 --- a/core/src/apps/common/request_pin.py +++ b/core/src/apps/common/request_pin.py @@ -1,20 +1,25 @@ import utime -from typing import Any, NoReturn +from typing import TYPE_CHECKING -import storage.cache -import storage.sd_salt +import storage.cache as storage_cache from trezor import config, wire -from .sdcard import SdCardUnavailable, request_sd_salt +from .sdcard import request_sd_salt + +if TYPE_CHECKING: + from typing import Any, NoReturn + from trezor.wire import Context, GenericContext def can_lock_device() -> bool: """Return True if the device has a PIN set or SD-protect enabled.""" + import storage.sd_salt + return config.has_pin() or storage.sd_salt.is_enabled() async def request_pin( - ctx: wire.GenericContext, + ctx: GenericContext, prompt: str = "Enter your PIN", attempts_remaining: int | None = None, allow_cancel: bool = True, @@ -24,26 +29,26 @@ async def request_pin( return await request_pin_on_device(ctx, prompt, attempts_remaining, allow_cancel) -async def request_pin_confirm(ctx: wire.Context, *args: Any, **kwargs: Any) -> str: +async def request_pin_confirm(ctx: Context, *args: Any, **kwargs: Any) -> str: while True: pin1 = await request_pin(ctx, "Enter new PIN", *args, **kwargs) pin2 = await request_pin(ctx, "Re-enter new PIN", *args, **kwargs) if pin1 == pin2: return pin1 - await pin_mismatch() + await _pin_mismatch() -async def pin_mismatch() -> None: +async def _pin_mismatch() -> None: from trezor.ui.layouts import show_popup await show_popup( - title="PIN mismatch", - description="The PINs you entered\ndo not match.\n\nPlease try again.", + "PIN mismatch", + "The PINs you entered\ndo not match.\n\nPlease try again.", ) async def request_pin_and_sd_salt( - ctx: wire.Context, prompt: str = "Enter your PIN", allow_cancel: bool = True + ctx: Context, prompt: str = "Enter your PIN", allow_cancel: bool = True ) -> tuple[str, bytearray | None]: if config.has_pin(): pin = await request_pin(ctx, prompt, config.get_pin_rem(), allow_cancel) @@ -58,21 +63,23 @@ async def request_pin_and_sd_salt( def _set_last_unlock_time() -> None: now = utime.ticks_ms() - storage.cache.set_int(storage.cache.APP_COMMON_REQUEST_PIN_LAST_UNLOCK, now) - - -def _get_last_unlock_time() -> int: - return storage.cache.get_int(storage.cache.APP_COMMON_REQUEST_PIN_LAST_UNLOCK) or 0 + storage_cache.set_int(storage_cache.APP_COMMON_REQUEST_PIN_LAST_UNLOCK, now) async def verify_user_pin( - ctx: wire.GenericContext = wire.DUMMY_CONTEXT, + ctx: GenericContext = wire.DUMMY_CONTEXT, prompt: str = "Enter your PIN", allow_cancel: bool = True, retry: bool = True, cache_time_ms: int = 0, ) -> None: - last_unlock = _get_last_unlock_time() + from .sdcard import SdCardUnavailable + + # _get_last_unlock_time + last_unlock = int.from_bytes( + storage_cache.get(storage_cache.APP_COMMON_REQUEST_PIN_LAST_UNLOCK, b""), "big" + ) + if ( cache_time_ms and last_unlock @@ -112,28 +119,28 @@ async def verify_user_pin( raise wire.PinInvalid -async def error_pin_invalid(ctx: wire.Context) -> NoReturn: +async def error_pin_invalid(ctx: Context) -> NoReturn: from trezor.ui.layouts import show_error_and_raise await show_error_and_raise( ctx, "warning_wrong_pin", - header="Wrong PIN", - content="The PIN you entered is invalid.", + "The PIN you entered is invalid.", + "Wrong PIN", # header red=True, exc=wire.PinInvalid, ) assert False -async def error_pin_matches_wipe_code(ctx: wire.Context) -> NoReturn: +async def error_pin_matches_wipe_code(ctx: Context) -> NoReturn: from trezor.ui.layouts import show_error_and_raise await show_error_and_raise( ctx, "warning_invalid_new_pin", - header="Invalid PIN", - content="The new PIN must be different from your\nwipe code.", + "The new PIN must be different from your\nwipe code.", + "Invalid PIN", # header red=True, exc=wire.PinInvalid, ) diff --git a/core/src/apps/common/safety_checks.py b/core/src/apps/common/safety_checks.py index bff8d54ca..dbdff4463 100644 --- a/core/src/apps/common/safety_checks.py +++ b/core/src/apps/common/safety_checks.py @@ -1,5 +1,5 @@ -import storage.cache -import storage.device +import storage.cache as storage_cache +import storage.device as storage_device from storage.cache import APP_COMMON_SAFETY_CHECKS_TEMPORARY from storage.device import SAFETY_CHECK_LEVEL_PROMPT, SAFETY_CHECK_LEVEL_STRICT from trezor.enums import SafetyCheckLevel @@ -9,11 +9,11 @@ def read_setting() -> SafetyCheckLevel: """ Returns the effective safety check level. """ - temporary_safety_check_level = storage.cache.get(APP_COMMON_SAFETY_CHECKS_TEMPORARY) + temporary_safety_check_level = storage_cache.get(APP_COMMON_SAFETY_CHECKS_TEMPORARY) if temporary_safety_check_level: return int.from_bytes(temporary_safety_check_level, "big") # type: ignore [int-into-enum] else: - stored = storage.device.safety_check_level() + stored = storage_device.safety_check_level() if stored == SAFETY_CHECK_LEVEL_STRICT: return SafetyCheckLevel.Strict elif stored == SAFETY_CHECK_LEVEL_PROMPT: @@ -27,14 +27,14 @@ def apply_setting(level: SafetyCheckLevel) -> None: Changes the safety level settings. """ if level == SafetyCheckLevel.Strict: - storage.cache.delete(APP_COMMON_SAFETY_CHECKS_TEMPORARY) - storage.device.set_safety_check_level(SAFETY_CHECK_LEVEL_STRICT) + storage_cache.delete(APP_COMMON_SAFETY_CHECKS_TEMPORARY) + storage_device.set_safety_check_level(SAFETY_CHECK_LEVEL_STRICT) elif level == SafetyCheckLevel.PromptAlways: - storage.cache.delete(APP_COMMON_SAFETY_CHECKS_TEMPORARY) - storage.device.set_safety_check_level(SAFETY_CHECK_LEVEL_PROMPT) + storage_cache.delete(APP_COMMON_SAFETY_CHECKS_TEMPORARY) + storage_device.set_safety_check_level(SAFETY_CHECK_LEVEL_PROMPT) elif level == SafetyCheckLevel.PromptTemporarily: - storage.device.set_safety_check_level(SAFETY_CHECK_LEVEL_STRICT) - storage.cache.set(APP_COMMON_SAFETY_CHECKS_TEMPORARY, level.to_bytes(1, "big")) + storage_device.set_safety_check_level(SAFETY_CHECK_LEVEL_STRICT) + storage_cache.set(APP_COMMON_SAFETY_CHECKS_TEMPORARY, level.to_bytes(1, "big")) else: raise ValueError("Unknown SafetyCheckLevel") diff --git a/core/src/apps/common/sdcard.py b/core/src/apps/common/sdcard.py index 27b51ad15..6e3fe62f3 100644 --- a/core/src/apps/common/sdcard.py +++ b/core/src/apps/common/sdcard.py @@ -1,6 +1,5 @@ -import storage.sd_salt from storage.sd_salt import SD_CARD_HOT_SWAPPABLE -from trezor import io, sdcard, ui, wire +from trezor import io, ui, wire from trezor.ui.layouts import confirm_action, show_error_and_raise @@ -14,8 +13,8 @@ async def _confirm_retry_wrong_card(ctx: wire.GenericContext) -> None: ctx, "warning_wrong_sd", "SD card protection", - action="Wrong SD card.", - description="Please insert the correct SD card for this device.", + "Wrong SD card.", + "Please insert the correct SD card for this device.", verb="Retry", verb_cancel="Abort", icon=ui.ICON_WRONG, @@ -26,9 +25,9 @@ async def _confirm_retry_wrong_card(ctx: wire.GenericContext) -> None: await show_error_and_raise( ctx, "warning_wrong_sd", - header="SD card protection", - subheader="Wrong SD card.", - content="Please unplug the\ndevice and insert the correct SD card.", + "Please unplug the\ndevice and insert the correct SD card.", + "SD card protection", + "Wrong SD card.", exc=SdCardUnavailable("Wrong SD card."), ) @@ -39,8 +38,8 @@ async def _confirm_retry_insert_card(ctx: wire.GenericContext) -> None: ctx, "warning_no_sd", "SD card protection", - action="SD card required.", - description="Please insert your SD card.", + "SD card required.", + "Please insert your SD card.", verb="Retry", verb_cancel="Abort", icon=ui.ICON_WRONG, @@ -51,9 +50,9 @@ async def _confirm_retry_insert_card(ctx: wire.GenericContext) -> None: await show_error_and_raise( ctx, "warning_no_sd", - header="SD card protection", - subheader="SD card required.", - content="Please unplug the\ndevice and insert your SD card.", + "Please unplug the\ndevice and insert your SD card.", + "SD card protection", + "SD card required.", exc=SdCardUnavailable("SD card required."), ) @@ -64,8 +63,8 @@ async def _confirm_format_card(ctx: wire.GenericContext) -> None: ctx, "warning_format_sd", "SD card error", - action="Unknown filesystem.", - description="Use a different card or format the SD card to the FAT32 filesystem.", + "Unknown filesystem.", + "Use a different card or format the SD card to the FAT32 filesystem.", icon=ui.ICON_WRONG, icon_color=ui.RED, verb="Format", @@ -79,8 +78,8 @@ async def _confirm_format_card(ctx: wire.GenericContext) -> None: ctx, "confirm_format_sd", "Format SD card", - action="All data on the SD card will be lost.", - description="Do you really want to format the SD card?", + "All data on the SD card will be lost.", + "Do you really want to format the SD card?", reverse=True, verb="Format SD card", icon=ui.ICON_WIPE, @@ -99,8 +98,8 @@ async def confirm_retry_sd( ctx, "warning_sd_retry", "SD card problem", - action=None, - description="There was a problem accessing the SD card.", + None, + "There was a problem accessing the SD card.", icon=ui.ICON_WRONG, icon_color=ui.RED, verb="Retry", @@ -121,18 +120,20 @@ async def ensure_sdcard( filesystem, and allows the user to format the card if a filesystem cannot be mounted. """ + from trezor import sdcard + while not sdcard.is_present(): await _confirm_retry_insert_card(ctx) if not ensure_filesystem: return - + fatfs = io.fatfs # local_cache_attribute while True: try: try: with sdcard.filesystem(mounted=False): - io.fatfs.mount() - except io.fatfs.NoFilesystem: + fatfs.mount() + except fatfs.NoFilesystem: # card not formatted. proceed out of the except clause pass else: @@ -143,9 +144,9 @@ async def ensure_sdcard( # Proceed to formatting. Failure is caught by the outside OSError handler with sdcard.filesystem(mounted=False): - io.fatfs.mkfs() - io.fatfs.mount() - io.fatfs.setlabel("TREZOR") + fatfs.mkfs() + fatfs.mount() + fatfs.setlabel("TREZOR") # format and mount succeeded return @@ -158,14 +159,16 @@ async def ensure_sdcard( async def request_sd_salt( ctx: wire.GenericContext = wire.DUMMY_CONTEXT, ) -> bytearray | None: - if not storage.sd_salt.is_enabled(): + import storage.sd_salt as storage_sd_salt + + if not storage_sd_salt.is_enabled(): return None while True: await ensure_sdcard(ctx, ensure_filesystem=False) try: - return storage.sd_salt.load_sd_salt() - except (storage.sd_salt.WrongSdCard, io.fatfs.NoFilesystem): + return storage_sd_salt.load_sd_salt() + except (storage_sd_salt.WrongSdCard, io.fatfs.NoFilesystem): await _confirm_retry_wrong_card(ctx) except OSError: # Generic problem with loading the SD salt (hardware problem, or we could diff --git a/core/src/apps/common/seed.py b/core/src/apps/common/seed.py index 0b5bd0819..0f9a0d539 100644 --- a/core/src/apps/common/seed.py +++ b/core/src/apps/common/seed.py @@ -1,14 +1,17 @@ from typing import TYPE_CHECKING -from storage import cache, device -from trezor import utils, wire -from trezor.crypto import bip32, hmac +import storage.cache as storage_cache +import storage.device as storage_device +from trezor import utils +from trezor.crypto import hmac from . import mnemonic from .passphrase import get as get_passphrase if TYPE_CHECKING: from .paths import Bip32Path, Slip21Path + from trezor.wire import Context + from trezor.crypto import bip32 class Slip21Node: @@ -47,14 +50,16 @@ if not utils.BITCOIN_ONLY: # We want to derive both the normal seed and the Cardano seed together, AND # expose a method for Cardano to do the same - async def derive_and_store_roots(ctx: wire.Context) -> None: - if not device.is_initialized(): + async def derive_and_store_roots(ctx: Context) -> None: + from trezor import wire + + if not storage_device.is_initialized(): raise wire.NotInitialized("Device is not initialized") - need_seed = not cache.is_set(cache.APP_COMMON_SEED) - need_cardano_secret = cache.get( - cache.APP_COMMON_DERIVE_CARDANO - ) and not cache.is_set(cache.APP_CARDANO_ICARUS_SECRET) + need_seed = not storage_cache.is_set(storage_cache.APP_COMMON_SEED) + need_cardano_secret = storage_cache.get( + storage_cache.APP_COMMON_DERIVE_CARDANO + ) and not storage_cache.is_set(storage_cache.APP_CARDANO_ICARUS_SECRET) if not need_seed and not need_cardano_secret: return @@ -63,17 +68,17 @@ if not utils.BITCOIN_ONLY: if need_seed: common_seed = mnemonic.get_seed(passphrase) - cache.set(cache.APP_COMMON_SEED, common_seed) + storage_cache.set(storage_cache.APP_COMMON_SEED, common_seed) if need_cardano_secret: from apps.cardano.seed import derive_and_store_secrets derive_and_store_secrets(passphrase) - @cache.stored_async(cache.APP_COMMON_SEED) - async def get_seed(ctx: wire.Context) -> bytes: + @storage_cache.stored_async(storage_cache.APP_COMMON_SEED) + async def get_seed(ctx: Context) -> bytes: await derive_and_store_roots(ctx) - common_seed = cache.get(cache.APP_COMMON_SEED) + common_seed = storage_cache.get(storage_cache.APP_COMMON_SEED) assert common_seed is not None return common_seed @@ -81,15 +86,15 @@ else: # === Bitcoin-only variant === # We use the simple version of `get_seed` that never needs to derive anything else. - @cache.stored_async(cache.APP_COMMON_SEED) - async def get_seed(ctx: wire.Context) -> bytes: + @storage_cache.stored_async(storage_cache.APP_COMMON_SEED) + async def get_seed(ctx: Context) -> bytes: passphrase = await get_passphrase(ctx) return mnemonic.get_seed(passphrase) -@cache.stored(cache.APP_COMMON_SEED_WITHOUT_PASSPHRASE) +@storage_cache.stored(storage_cache.APP_COMMON_SEED_WITHOUT_PASSPHRASE) def _get_seed_without_passphrase() -> bytes: - if not device.is_initialized(): + if not storage_device.is_initialized(): raise Exception("Device is not initialized") return mnemonic.get_seed(progress_bar=False) @@ -97,6 +102,8 @@ def _get_seed_without_passphrase() -> bytes: def derive_node_without_passphrase( path: Bip32Path, curve_name: str = "secp256k1" ) -> bip32.HDNode: + from trezor.crypto import bip32 + seed = _get_seed_without_passphrase() node = bip32.from_seed(seed, curve_name) node.derive_path(path) diff --git a/core/src/apps/common/signverify.py b/core/src/apps/common/signverify.py index 8c4057fda..9fbc982d7 100644 --- a/core/src/apps/common/signverify.py +++ b/core/src/apps/common/signverify.py @@ -1,16 +1,15 @@ from typing import TYPE_CHECKING -from ubinascii import hexlify - -from trezor import utils, wire -from trezor.crypto.hashlib import blake256, sha256 - -from apps.common.writers import write_compact_size if TYPE_CHECKING: from apps.common.coininfo import CoinInfo def message_digest(coin: CoinInfo, message: bytes) -> bytes: + from trezor import utils, wire + from trezor.crypto.hashlib import blake256, sha256 + + from apps.common.writers import write_compact_size + if not utils.BITCOIN_ONLY and coin.decred: h = utils.HashWriter(blake256()) else: @@ -28,6 +27,8 @@ def message_digest(coin: CoinInfo, message: bytes) -> bytes: def decode_message(message: bytes) -> str: + from ubinascii import hexlify + try: return bytes(message).decode() except UnicodeError: diff --git a/core/src/apps/common/writers.py b/core/src/apps/common/writers.py index 9a65c74ba..8d2a53ba9 100644 --- a/core/src/apps/common/writers.py +++ b/core/src/apps/common/writers.py @@ -6,61 +6,38 @@ if TYPE_CHECKING: from trezor.utils import Writer +def _write_uint(w: Writer, n: int, bits: int, bigendian: bool) -> int: + ensure(0 <= n <= 2**bits - 1, "overflow") + shifts = range(0, bits, 8) + if bigendian: + shifts = reversed(shifts) + for num in shifts: + w.append((n >> num) & 0xFF) + return bits // 8 + + def write_uint8(w: Writer, n: int) -> int: - ensure(0 <= n <= 0xFF) - w.append(n) - return 1 + return _write_uint(w, n, 8, False) def write_uint16_le(w: Writer, n: int) -> int: - ensure(0 <= n <= 0xFFFF) - w.append(n & 0xFF) - w.append((n >> 8) & 0xFF) - return 2 + return _write_uint(w, n, 16, False) def write_uint32_le(w: Writer, n: int) -> int: - ensure(0 <= n <= 0xFFFF_FFFF) - w.append(n & 0xFF) - w.append((n >> 8) & 0xFF) - w.append((n >> 16) & 0xFF) - w.append((n >> 24) & 0xFF) - return 4 + return _write_uint(w, n, 32, False) def write_uint32_be(w: Writer, n: int) -> int: - ensure(0 <= n <= 0xFFFF_FFFF) - w.append((n >> 24) & 0xFF) - w.append((n >> 16) & 0xFF) - w.append((n >> 8) & 0xFF) - w.append(n & 0xFF) - return 4 + return _write_uint(w, n, 32, True) def write_uint64_le(w: Writer, n: int) -> int: - ensure(0 <= n <= 0xFFFF_FFFF_FFFF_FFFF) - w.append(n & 0xFF) - w.append((n >> 8) & 0xFF) - w.append((n >> 16) & 0xFF) - w.append((n >> 24) & 0xFF) - w.append((n >> 32) & 0xFF) - w.append((n >> 40) & 0xFF) - w.append((n >> 48) & 0xFF) - w.append((n >> 56) & 0xFF) - return 8 + return _write_uint(w, n, 64, False) def write_uint64_be(w: Writer, n: int) -> int: - ensure(0 <= n <= 0xFFFF_FFFF_FFFF_FFFF) - w.append((n >> 56) & 0xFF) - w.append((n >> 48) & 0xFF) - w.append((n >> 40) & 0xFF) - w.append((n >> 32) & 0xFF) - w.append((n >> 24) & 0xFF) - w.append((n >> 16) & 0xFF) - w.append((n >> 8) & 0xFF) - w.append(n & 0xFF) - return 8 + return _write_uint(w, n, 64, True) def write_bytes_unchecked(w: Writer, b: bytes | memoryview) -> int: @@ -82,16 +59,18 @@ def write_bytes_reversed(w: Writer, b: bytes, length: int) -> int: def write_compact_size(w: Writer, n: int) -> None: ensure(0 <= n <= 0xFFFF_FFFF) + append = w.append # local_cache_attribute + if n < 253: - w.append(n & 0xFF) + append(n & 0xFF) elif n < 0x1_0000: - w.append(253) + append(253) write_uint16_le(w, n) elif n < 0x1_0000_0000: - w.append(254) + append(254) write_uint32_le(w, n) else: - w.append(255) + append(255) write_uint64_le(w, n) diff --git a/core/tests/test_apps.common.cbor.py b/core/tests/test_apps.common.cbor.py index 7ce79139b..2a53edd15 100644 --- a/core/tests/test_apps.common.cbor.py +++ b/core/tests/test_apps.common.cbor.py @@ -1,5 +1,3 @@ -import math - from common import * from apps.common.cbor import ( @@ -12,43 +10,8 @@ from apps.common.cbor import ( decode, encode, encode_streamed, - utils ) -# NOTE: moved into tests not to occupy flash space -# in firmware binary, when it is not used in production -def encode_chunked(value: "Value", max_chunk_size: int) -> "Iterator[bytes]": - """ - Returns the encoded value as an iterable of chunks of a given size, - removing the need to reserve a continuous chunk of memory for the - full serialized representation of the value. - """ - if max_chunk_size <= 0: - raise ValueError - - chunks = encode_streamed(value) - - chunk_buffer = utils.empty_bytearray(max_chunk_size) - try: - current_chunk_view = utils.BufferReader(next(chunks)) - while True: - num_bytes_to_write = min( - current_chunk_view.remaining_count(), - max_chunk_size - len(chunk_buffer), - ) - chunk_buffer.extend(current_chunk_view.read(num_bytes_to_write)) - - if len(chunk_buffer) >= max_chunk_size: - yield chunk_buffer - chunk_buffer[:] = bytes() - - if current_chunk_view.remaining_count() == 0: - current_chunk_view = utils.BufferReader(next(chunks)) - except StopIteration: - if len(chunk_buffer) > 0: - yield chunk_buffer - - class TestCardanoCbor(unittest.TestCase): def test_create_array_header(self): @@ -211,43 +174,6 @@ class TestCardanoCbor(unittest.TestCase): self.assertEqual(b''.join(encoded_streamed), encoded) - def test_encode_chunked(self): - large_dict = {i: i for i in range(100)} - encoded = encode(large_dict) - - encoded_len = len(encoded) - assert encoded_len == 354 - - arbitrary_encoded_len_factor = 59 - arbitrary_power_of_two = 64 - larger_than_encoded_len = encoded_len + 1 - - for max_chunk_size in [ - 1, - 10, - arbitrary_encoded_len_factor, - arbitrary_power_of_two, - encoded_len, - larger_than_encoded_len - ]: - encoded_chunks = [ - bytes(chunk) for chunk in encode_chunked(large_dict, max_chunk_size) - ] - - expected_number_of_chunks = math.ceil(len(encoded) / max_chunk_size) - self.assertEqual(len(encoded_chunks), expected_number_of_chunks) - - # all chunks except the last should be of chunk_size - for i in range(len(encoded_chunks) - 1): - self.assertEqual(len(encoded_chunks[i]), max_chunk_size) - - # last chunk should contain the remaining bytes or the whole chunk - remaining_bytes = len(encoded) % max_chunk_size - expected_last_chunk_size = remaining_bytes if remaining_bytes > 0 else max_chunk_size - self.assertEqual(len(encoded_chunks[-1]), expected_last_chunk_size) - - self.assertEqual(b''.join(encoded_chunks), encoded) - if __name__ == '__main__': unittest.main() diff --git a/core/tests/test_apps.common.writers.py b/core/tests/test_apps.common.writers.py new file mode 100644 index 000000000..39b023088 --- /dev/null +++ b/core/tests/test_apps.common.writers.py @@ -0,0 +1,44 @@ +from common import * + +import apps.common.writers as writers + + +class TestSeed(unittest.TestCase): + def test_write_uint8(self): + buf = bytearray() + writers.write_uint8(buf, 0x12) + self.assertEqual(buf, b"\x12") + + def test_write_uint16_le(self): + buf = bytearray() + writers.write_uint16_le(buf, 0x1234) + self.assertEqual(buf, b"\x34\x12") + + def test_write_uint16_le_overflow(self): + buf = bytearray() + with self.assertRaises(AssertionError): + writers.write_uint16_le(buf, 0x12345678) + + def test_write_uint32_le(self): + buf = bytearray() + writers.write_uint32_le(buf, 0x12345678) + self.assertEqual(buf, b"\x78\x56\x34\x12") + + def test_write_uint64_le(self): + buf = bytearray() + writers.write_uint64_le(buf, 0x1234567890abcdef) + self.assertEqual(buf, b"\xef\xcd\xab\x90\x78\x56\x34\x12") + + def test_write_uint32_be(self): + buf = bytearray() + writers.write_uint32_be(buf, 0x12345678) + self.assertEqual(buf, b"\x12\x34\x56\x78") + + def test_write_uint64_be(self): + buf = bytearray() + writers.write_uint64_be(buf, 0x1234567890abcdef) + self.assertEqual(buf, b"\x12\x34\x56\x78\x90\xab\xcd\xef") + + +if __name__ == "__main__": + unittest.main()