1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-18 05:28:40 +00:00

apps: unify wire registering, load modules automatically

This commit is contained in:
Jan Pochyla 2018-08-24 17:07:47 +02:00
parent a08a1a5e2a
commit 8421fa4fdb
20 changed files with 79 additions and 339 deletions

View File

@ -1,46 +1,10 @@
from trezor.messages.MessageType import (
CardanoGetAddress,
CardanoGetPublicKey,
CardanoSignMessage,
CardanoSignTx,
CardanoVerifyMessage,
)
from trezor.wire import protobuf_workflow, register
def dispatch_CardanoGetAddress(*args, **kwargs):
from .get_address import cardano_get_address
return cardano_get_address(*args, **kwargs)
def dispatch_CardanoGetPublicKey(*args, **kwargs):
from .get_public_key import cardano_get_public_key
return cardano_get_public_key(*args, **kwargs)
def dispatch_CardanoSignMessage(*args, **kwargs):
from .sign_message import cardano_sign_message
return cardano_sign_message(*args, **kwargs)
def dispatch_CardanoSignTransaction(*args, **kwargs):
from .sign_transaction import cardano_sign_transaction
return cardano_sign_transaction(*args, **kwargs)
def dispatch_CardanoVerifyMessage(*args, **kwargs):
from .verify_message import cardano_verify_message
return cardano_verify_message(*args, **kwargs)
from trezor import wire
from trezor.messages import MessageType
def boot():
register(CardanoGetAddress, protobuf_workflow, dispatch_CardanoGetAddress)
register(CardanoGetPublicKey, protobuf_workflow, dispatch_CardanoGetPublicKey)
register(CardanoSignMessage, protobuf_workflow, dispatch_CardanoSignMessage)
register(CardanoVerifyMessage, protobuf_workflow, dispatch_CardanoVerifyMessage)
register(CardanoSignTx, protobuf_workflow, dispatch_CardanoSignTransaction)
wire.add(MessageType.CardanoGetAddress, __name__, "get_address")
wire.add(MessageType.CardanoGetPublicKey, __name__, "get_public_key")
wire.add(MessageType.CardanoSignMessage, __name__, "sign_message")
wire.add(MessageType.CardanoVerifyMessage, __name__, "sign_transaction")
wire.add(MessageType.CardanoSignTx, __name__, "verify_message")

View File

@ -1,38 +1,9 @@
from trezor.messages.MessageType import (
EthereumGetAddress,
EthereumSignMessage,
EthereumSignTx,
EthereumVerifyMessage,
)
from trezor.wire import protobuf_workflow, register
def dispatch_EthereumGetAddress(*args, **kwargs):
from .get_address import ethereum_get_address
return ethereum_get_address(*args, **kwargs)
def dispatch_EthereumSignTx(*args, **kwargs):
from .sign_tx import ethereum_sign_tx
return ethereum_sign_tx(*args, **kwargs)
def dispatch_EthereumSignMessage(*args, **kwargs):
from .sign_message import ethereum_sign_message
return ethereum_sign_message(*args, **kwargs)
def dispatch_EthereumVerifyMessage(*args, **kwargs):
from .verify_message import ethereum_verify_message
return ethereum_verify_message(*args, **kwargs)
from trezor import wire
from trezor.messages import MessageType
def boot():
register(EthereumGetAddress, protobuf_workflow, dispatch_EthereumGetAddress)
register(EthereumSignTx, protobuf_workflow, dispatch_EthereumSignTx)
register(EthereumSignMessage, protobuf_workflow, dispatch_EthereumSignMessage)
register(EthereumVerifyMessage, protobuf_workflow, dispatch_EthereumVerifyMessage)
wire.add(MessageType.EthereumGetAddress, __name__, "get_address")
wire.add(MessageType.EthereumSignTx, __name__, "sign_tx")
wire.add(MessageType.EthereumSignMessage, __name__, "sign_message")
wire.add(MessageType.EthereumVerifyMessage, __name__, "verify_message")

View File

@ -2,7 +2,7 @@ from apps.common.show import show_address, show_qr
from apps.ethereum import networks
async def ethereum_get_address(ctx, msg):
async def get_address(ctx, msg):
from trezor.messages.EthereumAddress import EthereumAddress
from trezor.crypto.curve import secp256k1
from trezor.crypto.hashlib import sha3_256

View File

@ -10,18 +10,15 @@ from apps.common.signverify import split_message
def message_digest(message):
h = HashWriter(sha3_256, keccak=True)
signed_message_header = "\x19Ethereum Signed Message:\n"
h.extend(signed_message_header)
h.extend(str(len(message)))
h.extend(message)
return h.get_digest()
async def ethereum_sign_message(ctx, msg):
async def sign_message(ctx, msg):
await require_confirm_sign_message(ctx, msg.message)
address_n = msg.address_n or ()

View File

@ -1,9 +1,13 @@
from trezor import wire
from trezor.crypto import rlp
from trezor.crypto.curve import secp256k1
from trezor.crypto.hashlib import sha3_256
from trezor.messages.EthereumSignTx import EthereumSignTx
from trezor.messages.EthereumTxRequest import EthereumTxRequest
from trezor.messages.MessageType import EthereumTxAck
from trezor.utils import HashWriter
from apps.common import seed
from apps.ethereum import tokens
from apps.ethereum.layout import (
require_confirm_data,
@ -15,9 +19,7 @@ from apps.ethereum.layout import (
MAX_CHAIN_ID = 2147483629
async def ethereum_sign_tx(ctx, msg):
from trezor.crypto.hashlib import sha3_256
async def sign_tx(ctx, msg):
msg = sanitize(msg)
check(msg)
@ -115,8 +117,6 @@ def get_total_length(msg: EthereumSignTx, data_total: int) -> int:
async def send_request_chunk(ctx, data_left: int):
from trezor.messages.MessageType import EthereumTxAck
# TODO: layoutProgress ?
req = EthereumTxRequest()
if data_left <= 1024:
@ -128,9 +128,6 @@ async def send_request_chunk(ctx, data_left: int):
async def send_signature(ctx, msg: EthereumSignTx, digest):
from trezor.crypto.curve import secp256k1
from apps.common import seed
address_n = msg.address_n or ()
node = await seed.derive_node(ctx, address_n)

View File

@ -11,8 +11,7 @@ from apps.common.signverify import split_message
from apps.ethereum.sign_message import message_digest
async def ethereum_verify_message(ctx, msg):
async def verify_message(ctx, msg):
digest = message_digest(msg.message)
sig = bytearray([msg.signature[64]]) + msg.signature[:64]
pubkey = secp256k1.verify_recover(sig, digest)

View File

@ -1,46 +1,10 @@
from trezor.messages.MessageType import (
LiskGetAddress,
LiskGetPublicKey,
LiskSignMessage,
LiskSignTx,
LiskVerifyMessage,
)
from trezor.wire import protobuf_workflow, register
def dispatch_LiskGetAddress(*args, **kwargs):
from .get_address import layout_lisk_get_address
return layout_lisk_get_address(*args, **kwargs)
def dispatch_LiskGetPublicKey(*args, **kwargs):
from .get_public_key import lisk_get_public_key
return lisk_get_public_key(*args, **kwargs)
def dispatch_LiskSignTx(*args, **kwargs):
from .sign_tx import lisk_sign_tx
return lisk_sign_tx(*args, **kwargs)
def dispatch_LiskSignMessage(*args, **kwargs):
from .sign_message import lisk_sign_message
return lisk_sign_message(*args, **kwargs)
def dispatch_LiskVerifyMessage(*args, **kwargs):
from .verify_message import lisk_verify_message
return lisk_verify_message(*args, **kwargs)
from trezor import wire
from trezor.messages import MessageType
def boot():
register(LiskGetPublicKey, protobuf_workflow, dispatch_LiskGetPublicKey)
register(LiskGetAddress, protobuf_workflow, dispatch_LiskGetAddress)
register(LiskSignMessage, protobuf_workflow, dispatch_LiskSignMessage)
register(LiskVerifyMessage, protobuf_workflow, dispatch_LiskVerifyMessage)
register(LiskSignTx, protobuf_workflow, dispatch_LiskSignTx)
wire.add(MessageType.LiskGetPublicKey, __name__, "get_public_key")
wire.add(MessageType.LiskGetAddress, __name__, "get_address")
wire.add(MessageType.LiskSignMessage, __name__, "sign_message")
wire.add(MessageType.LiskVerifyMessage, __name__, "verify_message")
wire.add(MessageType.LiskSignTx, __name__, "sign_tx")

View File

@ -6,7 +6,7 @@ from apps.common import seed
from apps.common.show import show_address, show_qr
async def layout_lisk_get_address(ctx, msg):
async def get_address(ctx, msg):
address_n = msg.address_n or ()
node = await seed.derive_node(ctx, address_n, LISK_CURVE)

View File

@ -5,7 +5,7 @@ from .helpers import LISK_CURVE
from apps.common import seed, show
async def lisk_get_public_key(ctx, msg):
async def get_public_key(ctx, msg):
address_n = msg.address_n or ()
node = await seed.derive_node(ctx, address_n, LISK_CURVE)

View File

@ -22,7 +22,7 @@ def message_digest(message):
return sha256(h.get_digest()).digest()
async def lisk_sign_message(ctx, msg):
async def sign_message(ctx, msg):
message = msg.message
address_n = msg.address_n or ()

View File

@ -13,7 +13,7 @@ from .helpers import LISK_CURVE, get_address_from_public_key
from apps.common import seed
async def lisk_sign_tx(ctx, msg):
async def sign_tx(ctx, msg):
pubkey, seckey = await _get_keys(ctx, msg)
transaction = _update_raw_tx(msg.transaction, pubkey)

View File

@ -8,7 +8,7 @@ from .sign_message import message_digest
from apps.wallet.verify_message import require_confirm_verify_message
async def lisk_verify_message(ctx, msg):
async def verify_message(ctx, msg):
digest = message_digest(msg.message)
verified = ed25519.verify(msg.public_key, msg.signature, digest)
if not verified:

View File

@ -1,80 +1,16 @@
from trezor.messages.MessageType import (
ApplyFlags,
ApplySettings,
BackupDevice,
ChangePin,
LoadDevice,
RecoveryDevice,
ResetDevice,
SetU2FCounter,
WipeDevice,
)
from trezor.wire import protobuf_workflow, register
def dispatch_LoadDevice(*args, **kwargs):
from .load_device import load_device
return load_device(*args, **kwargs)
def dispatch_ResetDevice(*args, **kwargs):
from .reset_device import reset_device
return reset_device(*args, **kwargs)
def dispatch_BackupDevice(*args, **kwargs):
from .backup_device import backup_device
return backup_device(*args, **kwargs)
def dispatch_WipeDevice(*args, **kwargs):
from .wipe_device import wipe_device
return wipe_device(*args, **kwargs)
def dispatch_RecoveryDevice(*args, **kwargs):
from .recovery_device import recovery_device
return recovery_device(*args, **kwargs)
def dispatch_ApplySettings(*args, **kwargs):
from .apply_settings import apply_settings
return apply_settings(*args, **kwargs)
def dispatch_ApplyFlags(*args, **kwargs):
from .apply_flags import apply_flags
return apply_flags(*args, **kwargs)
def dispatch_ChangePin(*args, **kwargs):
from .change_pin import change_pin
return change_pin(*args, **kwargs)
def dispatch_SetU2FCounter(*args, **kwargs):
from .set_u2f_counter import set_u2f_counter
return set_u2f_counter(*args, **kwargs)
from trezor import wire
from trezor.messages import MessageType
def boot():
# only enable LoadDevice in debug builds
if __debug__:
register(LoadDevice, protobuf_workflow, dispatch_LoadDevice)
register(ResetDevice, protobuf_workflow, dispatch_ResetDevice)
register(BackupDevice, protobuf_workflow, dispatch_BackupDevice)
register(WipeDevice, protobuf_workflow, dispatch_WipeDevice)
register(RecoveryDevice, protobuf_workflow, dispatch_RecoveryDevice)
register(ApplySettings, protobuf_workflow, dispatch_ApplySettings)
register(ApplyFlags, protobuf_workflow, dispatch_ApplyFlags)
register(ChangePin, protobuf_workflow, dispatch_ChangePin)
register(SetU2FCounter, protobuf_workflow, dispatch_SetU2FCounter)
wire.add(MessageType.LoadDevice, __name__, "load_device")
wire.add(MessageType.ResetDevice, __name__, "reset_device")
wire.add(MessageType.BackupDevice, __name__, "backup_device")
wire.add(MessageType.WipeDevice, __name__, "wipe_device")
wire.add(MessageType.RecoveryDevice, __name__, "recovery_device")
wire.add(MessageType.ApplySettings, __name__, "apply_settings")
wire.add(MessageType.ApplyFlags, __name__, "apply_flags")
wire.add(MessageType.ChangePin, __name__, "change_pin")
wire.add(MessageType.SetU2FCounter, __name__, "set_u2f_counter")

View File

@ -1,19 +1,7 @@
from trezor.messages.MessageType import NEMGetAddress, NEMSignTx
from trezor.wire import protobuf_workflow, register
def dispatch_NemGetAddress(*args, **kwargs):
from .get_address import get_address
return get_address(*args, **kwargs)
def dispatch_NemSignTx(*args, **kwargs):
from .signing import sign_tx
return sign_tx(*args, **kwargs)
from trezor import wire
from trezor.messages import MessageType
def boot():
register(NEMGetAddress, protobuf_workflow, dispatch_NemGetAddress)
register(NEMSignTx, protobuf_workflow, dispatch_NemSignTx)
wire.add(MessageType.NEMGetAddress, __name__, "get_address")
wire.add(MessageType.NEMSignTx, __name__, "sign_tx")

View File

@ -1,19 +1,7 @@
from trezor.messages.MessageType import RippleGetAddress, RippleSignTx
from trezor.wire import protobuf_workflow, register
def dispatch_RippleGetAddress(*args, **kwargs):
from .get_address import get_address
return get_address(*args, **kwargs)
def dispatch_RippleSignTx(*args, **kwargs):
from .sign_tx import sign_tx
return sign_tx(*args, **kwargs)
from trezor import wire
from trezor.messages import MessageType
def boot():
register(RippleGetAddress, protobuf_workflow, dispatch_RippleGetAddress)
register(RippleSignTx, protobuf_workflow, dispatch_RippleSignTx)
wire.add(MessageType.RippleGetAddress, __name__, "get_address")
wire.add(MessageType.RippleSignTx, __name__, "sign_tx")

View File

@ -1,19 +1,7 @@
from trezor.messages.MessageType import StellarGetAddress, StellarSignTx
from trezor.wire import protobuf_workflow, register
def dispatch_StellarGetAddress(*args, **kwargs):
from .get_address import get_address
return get_address(*args, **kwargs)
def dispatch_StellarSignTx(*args, **kwargs):
from .sign_tx import sign_tx
return sign_tx(*args, **kwargs)
from trezor import wire
from trezor.messages import MessageType
def boot():
register(StellarGetAddress, protobuf_workflow, dispatch_StellarGetAddress)
register(StellarSignTx, protobuf_workflow, dispatch_StellarSignTx)
wire.add(MessageType.StellarGetAddress, __name__, "get_address")
wire.add(MessageType.StellarSignTx, __name__, "sign_tx")

View File

@ -1,78 +1,14 @@
from trezor.messages.MessageType import (
CipherKeyValue,
GetAddress,
GetECDHSessionKey,
GetEntropy,
GetPublicKey,
SignIdentity,
SignMessage,
SignTx,
VerifyMessage,
)
from trezor.wire import protobuf_workflow, register
def dispatch_GetPublicKey(*args, **kwargs):
from .get_public_key import get_public_key
return get_public_key(*args, **kwargs)
def dispatch_GetAddress(*args, **kwargs):
from .get_address import get_address
return get_address(*args, **kwargs)
def dispatch_GetEntropy(*args, **kwargs):
from .get_entropy import get_entropy
return get_entropy(*args, **kwargs)
def dispatch_SignTx(*args, **kwargs):
from .sign_tx import sign_tx
return sign_tx(*args, **kwargs)
def dispatch_SignMessage(*args, **kwargs):
from .sign_message import sign_message
return sign_message(*args, **kwargs)
def dispatch_VerifyMessage(*args, **kwargs):
from .verify_message import verify_message
return verify_message(*args, **kwargs)
def dispatch_SignIdentity(*args, **kwargs):
from .sign_identity import sign_identity
return sign_identity(*args, **kwargs)
def dispatch_GetECDHSessionKey(*args, **kwargs):
from .ecdh import get_ecdh_session_key
return get_ecdh_session_key(*args, **kwargs)
def dispatch_CipherKeyValue(*args, **kwargs):
from .cipher_key_value import cipher_key_value
return cipher_key_value(*args, **kwargs)
from trezor import wire
from trezor.messages import MessageType
def boot():
register(GetPublicKey, protobuf_workflow, dispatch_GetPublicKey)
register(GetAddress, protobuf_workflow, dispatch_GetAddress)
register(GetEntropy, protobuf_workflow, dispatch_GetEntropy)
register(SignTx, protobuf_workflow, dispatch_SignTx)
register(SignMessage, protobuf_workflow, dispatch_SignMessage)
register(VerifyMessage, protobuf_workflow, dispatch_VerifyMessage)
register(SignIdentity, protobuf_workflow, dispatch_SignIdentity)
register(GetECDHSessionKey, protobuf_workflow, dispatch_GetECDHSessionKey)
register(CipherKeyValue, protobuf_workflow, dispatch_CipherKeyValue)
wire.add(MessageType.GetPublicKey, __name__, "get_public_key")
wire.add(MessageType.GetAddress, __name__, "get_address")
wire.add(MessageType.GetEntropy, __name__, "get_entropy")
wire.add(MessageType.SignTx, __name__, "sign_tx")
wire.add(MessageType.SignMessage, __name__, "sign_message")
wire.add(MessageType.VerifyMessage, __name__, "verify_message")
wire.add(MessageType.SignIdentity, __name__, "sign_identity")
wire.add(MessageType.GetECDHSessionKey, __name__, "get_ecdh_session_key")
wire.add(MessageType.CipherKeyValue, __name__, "cipher_key_value")

View File

@ -6,6 +6,11 @@ from trezor.wire.errors import *
workflow_handlers = {}
def add(mtype, pkgname, modname, *args):
"""Shortcut for registering a dynamically-imported Protobuf workflow."""
register(mtype, protobuf_workflow, import_workflow, pkgname, modname, *args)
def register(mtype, handler, *args):
"""Register `handler` to get scheduled after `mtype` message is received."""
if isinstance(mtype, type) and issubclass(mtype, protobuf.MessageType):
@ -160,6 +165,13 @@ async def protobuf_workflow(ctx, reader, handler, *args):
await ctx.write(res)
def import_workflow(ctx, req, pkgname, modname):
modpath = "%s.%s" % (pkgname, modname)
module = __import__(modpath, None, None, (modname,), 0)
handler = getattr(module, modname)
return handler(ctx, req)
async def unexpected_msg(ctx, reader):
from trezor.messages.Failure import Failure