2020-04-20 09:53:33 +00:00
|
|
|
from trezor import wire
|
|
|
|
|
2020-08-14 13:39:42 +00:00
|
|
|
from apps.common import HARDENED, paths
|
2020-07-21 12:59:54 +00:00
|
|
|
from apps.common.keychain import get_keychain
|
2020-04-20 09:53:33 +00:00
|
|
|
|
2020-05-18 11:17:34 +00:00
|
|
|
from . import CURVE, networks
|
|
|
|
|
2020-04-20 09:53:33 +00:00
|
|
|
if False:
|
2020-09-23 10:24:03 +00:00
|
|
|
from typing import Callable, Iterable
|
2020-04-20 09:53:33 +00:00
|
|
|
from typing_extensions import Protocol
|
|
|
|
|
|
|
|
from protobuf import MessageType
|
|
|
|
|
|
|
|
from trezor.messages.EthereumSignTx import EthereumSignTx
|
|
|
|
|
2020-07-21 12:59:54 +00:00
|
|
|
from apps.common.keychain import MsgOut, Handler, HandlerWithKeychain
|
2020-04-20 09:53:33 +00:00
|
|
|
|
|
|
|
class MsgWithAddressN(MessageType, Protocol):
|
2020-10-16 14:20:44 +00:00
|
|
|
address_n: paths.Bip32Path
|
2020-04-20 09:53:33 +00:00
|
|
|
|
|
|
|
|
2020-08-14 13:39:42 +00:00
|
|
|
# We believe Ethereum should use 44'/60'/a' for everything, because it is
|
|
|
|
# account-based, rather than UTXO-based. Unfortunately, lot of Ethereum
|
|
|
|
# tools (MEW, Metamask) do not use such scheme and set a = 0 and then
|
2020-09-23 10:24:03 +00:00
|
|
|
# iterate the address index i. For compatibility, we allow this scheme as well.
|
2020-08-14 13:39:42 +00:00
|
|
|
|
2020-09-23 10:24:03 +00:00
|
|
|
PATTERNS_ADDRESS = (paths.PATTERN_BIP44, paths.PATTERN_SEP5)
|
2020-08-14 13:39:42 +00:00
|
|
|
|
|
|
|
|
2020-09-23 10:24:03 +00:00
|
|
|
def _schemas_from_address_n(
|
|
|
|
patterns: Iterable[str], address_n: paths.Bip32Path
|
|
|
|
) -> Iterable[paths.PathSchema]:
|
2020-04-20 09:53:33 +00:00
|
|
|
if len(address_n) < 2:
|
2020-09-23 10:24:03 +00:00
|
|
|
return ()
|
2020-08-14 13:39:42 +00:00
|
|
|
|
2020-04-20 09:53:33 +00:00
|
|
|
slip44_hardened = address_n[1]
|
|
|
|
if slip44_hardened not in networks.all_slip44_ids_hardened():
|
2020-09-23 10:24:03 +00:00
|
|
|
return ()
|
2020-08-14 13:39:42 +00:00
|
|
|
|
|
|
|
if not slip44_hardened & HARDENED:
|
2020-09-23 10:24:03 +00:00
|
|
|
return ()
|
2020-08-14 13:39:42 +00:00
|
|
|
|
|
|
|
slip44_id = slip44_hardened - HARDENED
|
2020-09-23 10:24:03 +00:00
|
|
|
return (paths.PathSchema(pattern, slip44_id) for pattern in patterns)
|
2020-04-20 09:53:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def with_keychain_from_path(
|
2020-09-23 10:24:03 +00:00
|
|
|
*patterns: str,
|
2020-08-14 13:39:42 +00:00
|
|
|
) -> Callable[
|
|
|
|
[HandlerWithKeychain[MsgWithAddressN, MsgOut]], Handler[MsgWithAddressN, MsgOut]
|
|
|
|
]:
|
|
|
|
def decorator(
|
|
|
|
func: HandlerWithKeychain[MsgWithAddressN, MsgOut]
|
|
|
|
) -> Handler[MsgWithAddressN, MsgOut]:
|
|
|
|
async def wrapper(ctx: wire.Context, msg: MsgWithAddressN) -> MsgOut:
|
2020-09-23 10:24:03 +00:00
|
|
|
schemas = _schemas_from_address_n(patterns, msg.address_n)
|
|
|
|
keychain = await get_keychain(ctx, CURVE, schemas)
|
2020-08-14 13:39:42 +00:00
|
|
|
with keychain:
|
|
|
|
return await func(ctx, msg, keychain)
|
2020-04-20 09:53:33 +00:00
|
|
|
|
2020-08-14 13:39:42 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
return decorator
|
|
|
|
|
|
|
|
|
2020-09-23 10:24:03 +00:00
|
|
|
def _schemas_from_chain_id(msg: EthereumSignTx) -> Iterable[paths.PathSchema]:
|
2020-08-14 13:39:42 +00:00
|
|
|
if msg.chain_id is None:
|
2020-09-23 10:24:03 +00:00
|
|
|
return _schemas_from_address_n(PATTERNS_ADDRESS, msg.address_n)
|
2020-08-14 13:39:42 +00:00
|
|
|
|
|
|
|
info = networks.by_chain_id(msg.chain_id)
|
|
|
|
if info is None:
|
2020-11-06 13:55:27 +00:00
|
|
|
# allow Ethereum or testnet paths for unknown networks
|
|
|
|
slip44_id = (60, 1)
|
|
|
|
elif networks.is_wanchain(msg.chain_id, msg.tx_type):
|
2020-09-23 10:24:03 +00:00
|
|
|
slip44_id = (networks.SLIP44_WANCHAIN,)
|
|
|
|
elif info.slip44 != 60 and info.slip44 != 1:
|
|
|
|
# allow cross-signing with Ethereum unless it's testnet
|
|
|
|
slip44_id = (info.slip44, 60)
|
|
|
|
else:
|
|
|
|
slip44_id = (info.slip44,)
|
|
|
|
|
|
|
|
return (paths.PathSchema(pattern, slip44_id) for pattern in PATTERNS_ADDRESS)
|
2020-04-20 09:53:33 +00:00
|
|
|
|
|
|
|
|
|
|
|
def with_keychain_from_chain_id(
|
|
|
|
func: HandlerWithKeychain[EthereumSignTx, MsgOut]
|
|
|
|
) -> Handler[EthereumSignTx, MsgOut]:
|
2020-08-14 13:39:42 +00:00
|
|
|
# this is only for SignTx, and only PATTERN_ADDRESS is allowed
|
2020-04-20 09:53:33 +00:00
|
|
|
async def wrapper(ctx: wire.Context, msg: EthereumSignTx) -> MsgOut:
|
2020-09-23 10:24:03 +00:00
|
|
|
schemas = _schemas_from_chain_id(msg)
|
|
|
|
keychain = await get_keychain(ctx, CURVE, schemas)
|
2020-04-20 09:53:33 +00:00
|
|
|
with keychain:
|
|
|
|
return await func(ctx, msg, keychain)
|
|
|
|
|
|
|
|
return wrapper
|