async def cardano_sign_message(ctx, msg): mnemonic = storage.get_mnemonic() root_node = bip32.from_mnemonic_cardano(mnemonic) try: signature = _sign_message(root_node, msg.message, msg.address_n) except ValueError as e: if __debug__: log.exception(__name__, e) raise wire.ProcessError("Signing failed") mnemonic = None root_node = None if not await show_swipable_with_confirmation( ctx, msg.message, "Signing message", ui.ICON_RECEIVE, ui.GREEN ): raise wire.ActionCancelled("Signing cancelled") if not await show_swipable_with_confirmation( ctx, _break_address_n_to_lines(msg.address_n), "With address", ui.ICON_RECEIVE, ui.GREEN, ): raise wire.ActionCancelled("Signing cancelled") return signature
async def dispatch_DebugLinkGetState(ctx, msg): m = DebugLinkState() m.mnemonic = storage.get_mnemonic() m.passphrase_protection = storage.has_passphrase() m.reset_entropy = reset_device.internal_entropy m.reset_word = reset_device.current_word return m
async def get_address(ctx, msg): await paths.validate_path(ctx, validate_full_path, path=msg.address_n) mnemonic = storage.get_mnemonic() passphrase = await seed._get_cached_passphrase(ctx) root_node = bip32.from_mnemonic_cardano(mnemonic, passphrase) try: address, _ = derive_address_and_node(root_node, msg.address_n) except ValueError as e: if __debug__: log.exception(__name__, e) raise wire.ProcessError("Deriving address failed") mnemonic = None root_node = None if msg.show_display: if not await confirm_with_pagination(ctx, address, "Export address", icon=ui.ICON_SEND, icon_color=ui.GREEN): raise wire.ActionCancelled("Exporting cancelled") return CardanoAddress(address=address)
async def recovery_device(ctx, msg): """ Recover BIP39 seed into empty device. 1. Ask for the number of words in recovered seed. 2. Let user type in the mnemonic words one by one. 3. Optionally check the seed validity. 4. Optionally ask for the PIN, with confirmation. 5. Save into storage. """ if not msg.dry_run and storage.is_initialized(): raise wire.UnexpectedMessage("Already initialized") text = Text("Device recovery", ui.ICON_RECOVERY) text.normal("Do you really want to", "recover the device?", "") await require_confirm(ctx, text, code=ProtectCall) if msg.dry_run and config.has_pin(): curpin = await request_pin_ack(ctx, "Enter PIN", config.get_pin_rem()) if not config.check_pin(pin_to_int(curpin)): raise wire.PinInvalid("PIN invalid") # ask for the number of words wordcount = await request_wordcount(ctx) # ask for mnemonic words one by one mnemonic = await request_mnemonic(ctx, wordcount) # check mnemonic validity if msg.enforce_wordlist or msg.dry_run: if not bip39.check(mnemonic): raise wire.ProcessError("Mnemonic is not valid") # ask for pin repeatedly if msg.pin_protection: newpin = await request_pin_confirm(ctx, cancellable=False) # dry run if msg.dry_run: digest_input = sha256(mnemonic).digest() digest_stored = sha256(storage.get_mnemonic()).digest() if consteq(digest_stored, digest_input): return Success( message="The seed is valid and matches the one in the device") else: raise wire.ProcessError( "The seed is valid but does not match the one in the device") # save into storage if msg.pin_protection: config.change_pin(pin_to_int(""), pin_to_int(newpin)) storage.set_u2f_counter(msg.u2f_counter) storage.load_settings(label=msg.label, use_passphrase=msg.passphrase_protection) storage.load_mnemonic(mnemonic=mnemonic, needs_backup=False, no_backup=False) return Success(message="Device recovered")
async def cardano_get_address(ctx, msg): mnemonic = storage.get_mnemonic() root_node = bip32.from_mnemonic_cardano(mnemonic) try: address, _ = derive_address_and_node(root_node, msg.address_n) except ValueError as e: if __debug__: log.exception(__name__, e) raise wire.ProcessError("Deriving address failed") mnemonic = None root_node = None if msg.show_display: if not await show_swipable_with_confirmation(ctx, address, "Export address", icon=ui.ICON_SEND, icon_color=ui.GREEN): raise wire.ActionCancelled("Exporting cancelled") else: lines = _break_address_n_to_lines(msg.address_n) if not await show_swipable_with_confirmation(ctx, lines, "For BIP32 path", icon=ui.ICON_SEND, icon_color=ui.GREEN): raise wire.ActionCancelled("Exporting cancelled") return CardanoAddress(address=address)
async def _get_cached_seed(ctx: wire.Context) -> bytes: if not storage.is_initialized(): raise wire.ProcessError('Device is not initialized') if cache.get_seed() is None: passphrase = await _get_cached_passphrase(ctx) seed = bip39.seed(storage.get_mnemonic(), passphrase) cache.set_seed(seed) return cache.get_seed()
def derive_node_without_passphrase(path, curve_name=_DEFAULT_CURVE): if not storage.is_initialized(): raise Exception('Device is not initialized') seed = bip39.seed(storage.get_mnemonic(), '') node = bip32.from_seed(seed, curve_name) node.derive_path(path) return node
async def _compute_seed(ctx: wire.Context) -> bytes: passphrase = cache.get_passphrase() if passphrase is None: passphrase = await protect_by_passphrase(ctx) cache.set_passphrase(passphrase) _start_bip39_progress() seed = bip39.seed(storage.get_mnemonic(), passphrase, _render_bip39_progress) cache.set_seed(seed) return seed
async def dispatch_DebugLinkGetState(ctx, msg): m = DebugLinkState() m.mnemonic = storage.get_mnemonic() m.passphrase_protection = storage.has_passphrase() m.reset_word_pos = reset_word_index m.reset_entropy = reset_internal_entropy if reset_current_words: m.reset_word = " ".join(reset_current_words) return m
def derive_node_without_passphrase(path: list, curve_name: str = "secp256k1" ) -> bip32.HDNode: if not storage.is_initialized(): raise Exception("Device is not initialized") seed = bip39.seed(storage.get_mnemonic(), "") node = bip32.from_seed(seed, curve_name) node.derive_path(path) return node
async def dispatch_DebugLinkGetState(ctx, msg): from trezor.messages.DebugLinkState import DebugLinkState from apps.common import storage from apps.management import reset_device m = DebugLinkState() m.mnemonic = storage.get_mnemonic() m.passphrase_protection = storage.has_passphrase() m.reset_entropy = reset_device.internal_entropy m.reset_word = reset_device.current_word return m
async def cardano_get_public_key(ctx, msg): mnemonic = storage.get_mnemonic() root_node = bip32.from_mnemonic_cardano(mnemonic) try: key = _get_public_key(root_node, msg.address_n) except ValueError as e: if __debug__: log.exception(__name__, e) raise wire.ProcessError("Deriving public key failed") mnemonic = None root_node = None return key
async def get_keychain(ctx: wire.Context, namespaces: list) -> Keychain: if not storage.is_initialized(): raise wire.ProcessError("Device is not initialized") seed = cache.get_seed() if seed is None: # derive seed from mnemonic and passphrase passphrase = cache.get_passphrase() if passphrase is None: passphrase = await protect_by_passphrase(ctx) cache.set_passphrase(passphrase) seed = bip39.seed(storage.get_mnemonic(), passphrase) cache.set_seed(seed) keychain = Keychain(seed, namespaces) return keychain
async def sign_tx(ctx, msg): mnemonic = storage.get_mnemonic() passphrase = await seed._get_cached_passphrase(ctx) root_node = bip32.from_mnemonic_cardano(mnemonic, passphrase) progress.init(msg.transactions_count, "Loading data") try: # request transactions transactions = [] tx_req = CardanoTxRequest() for index in range(msg.transactions_count): progress.advance() tx_ack = await request_transaction(ctx, tx_req, index) transactions.append(tx_ack.transaction) # clear progress bar display_homescreen() for i in msg.inputs: await validate_path(ctx, validate_full_path, path=i.address_n) # sign the transaction bundle and prepare the result transaction = Transaction(msg.inputs, msg.outputs, transactions, root_node, msg.network) tx_body, tx_hash = transaction.serialise_tx() tx = CardanoSignedTx(tx_body=tx_body, tx_hash=tx_hash) except ValueError as e: if __debug__: log.exception(__name__, e) raise wire.ProcessError("Signing failed") # display the transaction in UI if not await show_tx( ctx, transaction.output_addresses, transaction.outgoing_coins, transaction.change_derivation_paths, transaction.change_coins, transaction.fee, len(tx_body), transaction.network_name, ): raise wire.ActionCancelled("Signing cancelled") return tx
async def get_keychain(ctx: wire.Context) -> Keychain: if not storage.is_initialized(): raise wire.ProcessError("Device is not initialized") # derive the root node from mnemonic and passphrase passphrase = cache.get_passphrase() if passphrase is None: passphrase = await protect_by_passphrase(ctx) cache.set_passphrase(passphrase) root = bip32.from_mnemonic_cardano(storage.get_mnemonic(), passphrase) # derive the namespaced root node for i in SEED_NAMESPACE[0]: root.derive_cardano(i) keychain = Keychain(SEED_NAMESPACE[0], root) return keychain
async def get_public_key(ctx, msg): mnemonic = storage.get_mnemonic() passphrase = await seed._get_cached_passphrase(ctx) root_node = bip32.from_mnemonic_cardano(mnemonic, passphrase) try: key = _get_public_key(root_node, msg.address_n) except ValueError as e: if __debug__: log.exception(__name__, e) raise wire.ProcessError("Deriving public key failed") mnemonic = None root_node = None if msg.show_display: await layout.show_pubkey(ctx, key.node.public_key) return key
async def recovery_device(ctx, msg): """ Recover BIP39 seed into empty device. 1. Ask for the number of words in recovered seed. 2. Let user type in the mnemonic words one by one. 3. Optionally check the seed validity. 4. Optionally ask for the PIN, with confirmation. 5. Save into storage. """ if not msg.dry_run and storage.is_initialized(): raise wire.UnexpectedMessage("Already initialized") # ask for the number of words wordcount = await request_wordcount(ctx) # ask for mnemonic words one by one mnemonic = await request_mnemonic(ctx, wordcount) # check mnemonic validity if msg.enforce_wordlist or msg.dry_run: if not bip39.check(mnemonic): raise wire.ProcessError("Mnemonic is not valid") # ask for pin repeatedly if msg.pin_protection: newpin = await request_pin_confirm(ctx, cancellable=False) # save into storage if not msg.dry_run: if msg.pin_protection: config.change_pin(pin_to_int(""), pin_to_int(newpin), None) storage.load_settings(label=msg.label, use_passphrase=msg.passphrase_protection) storage.load_mnemonic(mnemonic=mnemonic, needs_backup=False, no_backup=False) return Success(message="Device recovered") else: if storage.get_mnemonic() == mnemonic: return Success( message="The seed is valid and matches the one in the device") else: raise wire.ProcessError( "The seed is valid but does not match the one in the device")
async def backup_device(ctx, msg): if not storage.is_initialized(): raise wire.FailureError(ProcessError, 'Device is not initialized') if not storage.needs_backup(): raise wire.FailureError(ProcessError, 'Seed already backed up') mnemonic = storage.get_mnemonic() storage.set_backed_up() # warn user about mnemonic safety await show_warning(ctx) while True: # show mnemonic and require confirmation of a random word await show_mnemonic(ctx, mnemonic) if await check_mnemonic(ctx, mnemonic): break await show_wrong_entry(ctx) return Success(message='Seed successfully backed up')
async def cardano_get_public_key(ctx, msg): mnemonic = storage.get_mnemonic() root_node = bip32.from_mnemonic_cardano(mnemonic) try: key = _get_public_key(root_node, msg.address_n) except ValueError as e: if __debug__: log.exception(__name__, e) raise wire.ProcessError("Deriving public key failed") mnemonic = None root_node = None lines = ["For BIP32 path: ", ""] lines.extend(_break_address_n_to_lines(msg.address_n)) if not await show_swipable_with_confirmation(ctx, lines, "Export xpub key"): raise wire.ActionCancelled("Exporting cancelled") return key
async def _compute_seed(ctx: wire.Context) -> (bytes, str): if not storage.is_initialized(): raise wire.FailureError(ProcessError, 'Device is not initialized') passphrase = await protect_by_passphrase(ctx) return bip39.seed(storage.get_mnemonic(), passphrase), passphrase