1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-11-17 21:22:10 +00:00

core/cardano: use caching decorators and new Keychain API for Cardano as well

This commit is contained in:
matejcik 2020-04-20 11:37:47 +02:00 committed by matejcik
parent 7541d529a3
commit fd9e945308
6 changed files with 52 additions and 45 deletions

View File

@ -7,9 +7,8 @@ from apps.common import paths
from apps.common.layout import address_n_to_str, show_address, show_qr
async def get_address(ctx, msg):
keychain = await seed.get_keychain(ctx)
@seed.with_keychain
async def get_address(ctx, msg, keychain: seed.Keychain):
await paths.validate_path(ctx, validate_full_path, keychain, msg.address_n, CURVE)
try:

View File

@ -10,9 +10,8 @@ from apps.common import layout, paths
from apps.common.seed import remove_ed25519_prefix
async def get_public_key(ctx, msg):
keychain = await seed.get_keychain(ctx)
@seed.with_keychain
async def get_public_key(ctx, msg, keychain: seed.Keychain):
await paths.validate_path(
ctx,
paths.validate_path_for_get_public_key,

View File

@ -3,54 +3,64 @@ from storage import cache
from trezor import wire
from trezor.crypto import bip32
from apps.cardano import CURVE, SEED_NAMESPACE
from apps.cardano import SEED_NAMESPACE
from apps.common import mnemonic
from apps.common.passphrase import get as get_passphrase
if False:
from typing import Tuple
from apps.common.seed import Bip32Path, MsgIn, MsgOut, Handler, HandlerWithKeychain
class Keychain:
def __init__(self, path: list, root: bip32.HDNode):
self.path = path
"""Cardano keychain hard-coded to SEED_NAMESPACE."""
def __init__(self, root: bip32.HDNode) -> None:
self.root = root
def validate_path(self, checked_path: list, checked_curve: str):
if checked_curve != CURVE or checked_path[:2] != SEED_NAMESPACE:
def match_path(self, path: Bip32Path) -> Tuple[int, Bip32Path]:
if path[: len(SEED_NAMESPACE)] != SEED_NAMESPACE:
raise wire.DataError("Forbidden key path")
return 0, path[len(SEED_NAMESPACE) :]
def derive(self, node_path: list) -> bip32.HDNode:
# check we are in the cardano namespace
prefix = node_path[: len(self.path)]
suffix = node_path[len(self.path) :]
if prefix != self.path:
raise wire.DataError("Forbidden key path")
def derive(self, node_path: Bip32Path) -> bip32.HDNode:
_, suffix = self.match_path(node_path)
# derive child node from the root
node = self.root.clone()
for i in suffix:
node.derive_cardano(i)
return node
# XXX the root node remains in session cache so we should not delete it
# def __del__(self) -> None:
# self.root.__del__()
@cache.stored_async(cache.APP_CARDANO_ROOT)
async def get_keychain(ctx: wire.Context) -> Keychain:
root = cache.get(cache.APP_CARDANO_ROOT)
if not storage.is_initialized():
raise wire.NotInitialized("Device is not initialized")
if root is None:
passphrase = await get_passphrase(ctx)
if mnemonic.is_bip39():
# derive the root node from mnemonic and passphrase
root = bip32.from_mnemonic_cardano(
mnemonic.get_secret().decode(), passphrase
)
else:
seed = mnemonic.get_seed(passphrase)
root = bip32.from_seed(seed, "ed25519 cardano seed")
passphrase = await get_passphrase(ctx)
if mnemonic.is_bip39():
# derive the root node from mnemonic and passphrase
root = bip32.from_mnemonic_cardano(mnemonic.get_secret().decode(), passphrase)
else:
seed = mnemonic.get_seed(passphrase)
root = bip32.from_seed(seed, "ed25519 cardano seed")
# derive the namespaced root node
for i in SEED_NAMESPACE:
root.derive_cardano(i)
storage.cache.set(cache.APP_CARDANO_ROOT, root)
# derive the namespaced root node
for i in SEED_NAMESPACE:
root.derive_cardano(i)
keychain = Keychain(SEED_NAMESPACE, root)
keychain = Keychain(root)
return keychain
def with_keychain(func: HandlerWithKeychain[MsgIn, MsgOut]) -> Handler[MsgIn, MsgOut]:
async def wrapper(ctx: wire.Context, msg: MsgIn) -> MsgOut:
keychain = await get_keychain(ctx)
return await func(ctx, msg, keychain)
return wrapper

View File

@ -63,9 +63,8 @@ async def request_transaction(ctx, tx_req: CardanoTxRequest, index: int):
return await ctx.call(tx_req, CardanoTxAck)
async def sign_tx(ctx, msg):
keychain = await seed.get_keychain(ctx)
@seed.with_keychain
async def sign_tx(ctx, msg, keychain: seed.Keychain):
progress.init(msg.transactions_count, "Loading data")
try:

View File

@ -21,7 +21,7 @@ class TestCardanoAddress(unittest.TestCase):
node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
node.derive_cardano(0x80000000 | 44)
node.derive_cardano(0x80000000 | 1815)
keychain = Keychain([0x80000000 | 44, 0x80000000 | 1815], node)
keychain = Keychain(node)
addresses = [
"Ae2tdPwUPEZ98eHFwxSsPBDz73amioKpr58Vw85mP1tMkzq8siaftiejJ3j",
@ -68,7 +68,7 @@ class TestCardanoAddress(unittest.TestCase):
node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
node.derive_cardano(0x80000000 | 44)
node.derive_cardano(0x80000000 | 1815)
keychain = Keychain([0x80000000 | 44, 0x80000000 | 1815], node)
keychain = Keychain(node)
addresses = [
"Ae2tdPwUPEZ5YUb8sM3eS8JqKgrRLzhiu71crfuH2MFtqaYr5ACNRdsswsZ",
@ -116,7 +116,7 @@ class TestCardanoAddress(unittest.TestCase):
node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
node.derive_cardano(0x80000000 | 44)
node.derive_cardano(0x80000000 | 1815)
keychain = Keychain([0x80000000 | 44, 0x80000000 | 1815], node)
keychain = Keychain(node)
# 44'/1815'
address, _ = derive_address_and_node(keychain, [0x80000000 | 44, 0x80000000 | 1815])
@ -207,7 +207,7 @@ class TestCardanoAddress(unittest.TestCase):
# Check derived nodes and addresses.
node.derive_cardano(0x80000000 | 44)
node.derive_cardano(0x80000000 | 1815)
keychain = Keychain([0x80000000 | 44, 0x80000000 | 1815], node)
keychain = Keychain(node)
nodes = [
(
@ -271,7 +271,7 @@ class TestCardanoAddress(unittest.TestCase):
# Check derived nodes and addresses.
node.derive_cardano(0x80000000 | 44)
node.derive_cardano(0x80000000 | 1815)
keychain = Keychain([0x80000000 | 44, 0x80000000 | 1815], node)
keychain = Keychain(node)
nodes = [
(

View File

@ -13,7 +13,7 @@ class TestCardanoGetPublicKey(unittest.TestCase):
node = bip32.from_mnemonic_cardano(mnemonic, passphrase)
node.derive_cardano(0x80000000 | 44)
node.derive_cardano(0x80000000 | 1815)
keychain = Keychain([0x80000000 | 44, 0x80000000 | 1815], node)
keychain = Keychain(node)
derivation_paths = [
[0x80000000 | 44, 0x80000000 | 1815, 0x80000000, 0, 0x80000000],
@ -67,7 +67,7 @@ class TestCardanoGetPublicKey(unittest.TestCase):
node.derive_cardano(0x80000000 | 44)
node.derive_cardano(0x80000000 | 1815)
keychain = Keychain([0x80000000 | 44, 0x80000000 | 1815], node)
keychain = Keychain(node)
# 44'/1815'/0'/0/i
derivation_paths = [
@ -118,7 +118,7 @@ class TestCardanoGetPublicKey(unittest.TestCase):
node.derive_cardano(0x80000000 | 44)
node.derive_cardano(0x80000000 | 1815)
keychain = Keychain([0x80000000 | 44, 0x80000000 | 1815], node)
keychain = Keychain(node)
# 44'/1815'/0'/0/i
derivation_paths = [