Skip to content

Commit

Permalink
fix(core): update structure of common seed so it works on BTC_ONLY
Browse files Browse the repository at this point in the history
[no changelog]
  • Loading branch information
M1nd3r committed Nov 27, 2024
1 parent f589062 commit 5b21b90
Showing 1 changed file with 56 additions and 44 deletions.
100 changes: 56 additions & 44 deletions core/src/apps/common/seed.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,66 +68,78 @@ async def get_seed() -> bytes: # type: ignore [Function declaration "get_seed"
assert common_seed is not None
return common_seed

else:

@cache.stored_async(APP_COMMON_SEED)
async def get_seed() -> bytes:
await derive_and_store_roots_legacy()
common_seed = context.cache_get(APP_COMMON_SEED)
assert common_seed is not None
return common_seed
if utils.BITCOIN_ONLY:
# === Bitcoin_only variant ===
# We want to derive the normal seed ONLY

async def derive_and_store_roots(
ctx: Context, msg: ThpCreateNewSession
) -> None:

if utils.BITCOIN_ONLY:
# === Bitcoin_only variant ===
# We want to derive the normal seed ONLY
if msg.passphrase is not None and msg.on_device:
raise DataError("Passphrase provided when it shouldn't be!")

async def derive_and_store_roots(ctx: Context, msg: ThpCreateNewSession) -> None:
if ctx.cache.is_set(APP_COMMON_SEED):
raise Exception("Seed is already set!")

if msg.passphrase is not None and msg.on_device:
raise DataError("Passphrase provided when it shouldn't be!")

if ctx.cache.is_set(APP_COMMON_SEED):
raise Exception("Seed is already set!")

from trezor import wire
from trezor import wire

if not storage_device.is_initialized():
raise wire.NotInitialized("Device is not initialized")
if not storage_device.is_initialized():
raise wire.NotInitialized("Device is not initialized")

passphrase = await get_passphrase(msg)
common_seed = mnemonic.get_seed(passphrase)
ctx.cache.set(APP_COMMON_SEED, common_seed)
passphrase = await get_passphrase(msg)
common_seed = mnemonic.get_seed(passphrase)
ctx.cache.set(APP_COMMON_SEED, common_seed)

else:
# === Cardano variant ===
# We want to derive both the normal seed and the Cardano seed together, AND
# expose a method for Cardano to do the same
else:
# === Cardano variant ===
# We want to derive both the normal seed and the Cardano seed together
async def derive_and_store_roots(
ctx: Context, msg: ThpCreateNewSession
) -> None:

async def derive_and_store_roots(ctx: Context, msg: ThpCreateNewSession) -> None:
if msg.passphrase is not None and msg.on_device:
raise DataError("Passphrase provided when it shouldn't be!")

if msg.passphrase is not None and msg.on_device:
raise DataError("Passphrase provided when it shouldn't be!")
from trezor import wire

from trezor import wire
if not storage_device.is_initialized():
raise wire.NotInitialized("Device is not initialized")

if not storage_device.is_initialized():
raise wire.NotInitialized("Device is not initialized")
if ctx.cache.is_set(APP_CARDANO_ICARUS_SECRET):
raise Exception("Cardano icarus secret is already set!")

if ctx.cache.is_set(APP_CARDANO_ICARUS_SECRET):
raise Exception("Cardano icarus secret is already set!")
passphrase = await get_passphrase(msg)
common_seed = mnemonic.get_seed(passphrase)
ctx.cache.set(APP_COMMON_SEED, common_seed)

passphrase = await get_passphrase(msg)
common_seed = mnemonic.get_seed(passphrase)
ctx.cache.set(APP_COMMON_SEED, common_seed)
if msg.derive_cardano:
from apps.cardano.seed import derive_and_store_secrets

if msg.derive_cardano:
from apps.cardano.seed import derive_and_store_secrets
ctx.cache.set_bool(APP_COMMON_DERIVE_CARDANO, True)
derive_and_store_secrets(ctx, passphrase)

ctx.cache.set_bool(APP_COMMON_DERIVE_CARDANO, True)
derive_and_store_secrets(ctx, passphrase)
else:
if utils.BITCOIN_ONLY:
# === Bitcoin-only variant ===
# We use the simple version of `get_seed` that never needs to derive anything else.

if not utils.USE_THP:
@cache.stored_async(APP_COMMON_SEED)
async def get_seed() -> bytes:
passphrase = await get_passphrase_legacy()
return mnemonic.get_seed(passphrase=passphrase)

else:
# === Cardano variant ===
# We want to derive both the normal seed and the Cardano seed together, AND
# expose a method for Cardano to do the same

@cache.stored_async(APP_COMMON_SEED)
async def get_seed() -> bytes:
await derive_and_store_roots_legacy()
common_seed = context.cache_get(APP_COMMON_SEED)
assert common_seed is not None
return common_seed

async def derive_and_store_roots_legacy() -> None:
from trezor import wire
Expand Down

0 comments on commit 5b21b90

Please sign in to comment.