feat: a new profile from Landing is backed by a recovery code (recovery-code-v3, Phase 7)
recoveryCodeProfileWriter replaces seedProfileWriter, and CreateProfileRoute.withWallet becomes withBackup. Nothing in the app makes words from random bytes any more; wallet-audit fails on MnemonicCode.toMnemonics outside RecoveryCodeKeys. A code, or its exported words, restores the same npub and id on an empty device. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
5c6082d7d2
commit
f367294cb1
7 files changed
+82
-41
No files matched your search
@@ -10,8 +10,9 @@ data class NewProfile(val nostrPublicKey: HexKey, val id: WalletId)
|
||||
* Makes the secret a new profile signs with and puts it on disk.
|
||||
*
|
||||
* Two live in the app, and the create screen does not know which it was given. One
|
||||
* generates twelve words and writes a seed -- for Landing, where the first profile on a
|
||||
* device is the one the wallet will belong to. The other generates a bare key and
|
||||
* generates a nine-block recovery code and writes it with its 24 words -- for Landing, where
|
||||
* the first profile on a device is the one with a backup to write down
|
||||
* (docs/recovery-code-v3.md, Phase 7). The other generates a bare key and
|
||||
* writes a credential -- for the switcher, where a user who wants another profile has
|
||||
* not asked for another Lightning node, another set of channels, or a second phrase
|
||||
* with funds behind it. Under docs/multiple-profiles.md the two differ by exactly one
|
||||
|
||||
+9
-8
@@ -135,7 +135,7 @@ import mobi.giza.compose.ui.composable.ProfilesScreen
|
||||
import mobi.giza.compose.ui.view.model.ProfilesViewModel
|
||||
import mobi.giza.compose.ui.view.model.getAvailableWalletsMeta
|
||||
import mobi.giza.compose.ui.view.model.bareKeyProfileWriter
|
||||
import mobi.giza.compose.ui.view.model.seedProfileWriter
|
||||
import mobi.giza.compose.ui.view.model.recoveryCodeProfileWriter
|
||||
import mobi.giza.compose.ui.composable.navigation.routes.SearchMemberToAddToChatRoomRoute
|
||||
import mobi.giza.compose.ui.composable.navigation.routes.SearchResultRoute
|
||||
import mobi.giza.compose.ui.composable.navigation.routes.SearchRoute
|
||||
@@ -475,7 +475,7 @@ fun GizaNavHost(
|
||||
navController.navigate(route = SignInRoute)
|
||||
},
|
||||
onNavigateToCreateProfile = {
|
||||
navController.navigate(route = CreateProfileRoute(withWallet = false))
|
||||
navController.navigate(route = CreateProfileRoute(withBackup = false))
|
||||
},
|
||||
forceWalletId = null,
|
||||
)
|
||||
@@ -501,10 +501,10 @@ fun GizaNavHost(
|
||||
route = SignInRoute
|
||||
)
|
||||
},
|
||||
// The first profile on a device is the one the wallet will belong to.
|
||||
// The first profile on a device is the one with a backup to write down.
|
||||
onNavigateToCreateProfile = {
|
||||
navController.navigate(
|
||||
route = CreateProfileRoute(withWallet = true)
|
||||
route = CreateProfileRoute(withBackup = true)
|
||||
)
|
||||
}
|
||||
)
|
||||
@@ -517,9 +517,10 @@ fun GizaNavHost(
|
||||
},
|
||||
nostrRepository = databaseNostrRepository,
|
||||
marmotRepository = databaseMarmotRepository,
|
||||
// A seed from Landing, a bare key from inside: the screen does not know
|
||||
// which. See docs/multiple-profiles.md, Phase 5.
|
||||
newProfile = if (route.withWallet) sovereignWalletViewModel.seedProfileWriter() else sovereignWalletViewModel.bareKeyProfileWriter(),
|
||||
// A recovery code from Landing, a bare key from inside: the screen does not
|
||||
// know which. See docs/multiple-profiles.md, Phase 5, and
|
||||
// docs/recovery-code-v3.md, Phase 7.
|
||||
newProfile = if (route.withBackup) sovereignWalletViewModel.recoveryCodeProfileWriter() else sovereignWalletViewModel.bareKeyProfileWriter(),
|
||||
// The sign-in tail, with the popUpTo(0) it always had there: back from the
|
||||
// new profile's startup must not return to a form whose secret is already
|
||||
// on disk. The account rows are in the database before this runs, so the
|
||||
@@ -928,7 +929,7 @@ fun GizaNavHost(
|
||||
navController.navigate(route = SignInRoute)
|
||||
},
|
||||
onCreateProfile = {
|
||||
navController.navigate(route = CreateProfileRoute(withWallet = false))
|
||||
navController.navigate(route = CreateProfileRoute(withBackup = false))
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
+6
-5
@@ -3,12 +3,13 @@ package mobi.giza.compose.ui.composable.navigation.routes
|
||||
import kotlinx.serialization.Serializable
|
||||
|
||||
/**
|
||||
* @param withWallet whether the profile is made of a seed, with a wallet attached, or of
|
||||
* a bare key. True from Landing, where the first profile on a device is the one the
|
||||
* wallet will belong to; false from the switcher, where wanting another profile is not
|
||||
* wanting another wallet. See docs/multiple-profiles.md, Phase 5.
|
||||
* @param withBackup whether the profile is made of a recovery code -- a backup to write
|
||||
* down, attached to the profile -- or of a bare key. True from Landing, where the first
|
||||
* profile on a device is the one with a backup to write down; false from the switcher,
|
||||
* where wanting another profile is not wanting another thing to write down. See
|
||||
* docs/multiple-profiles.md, Phase 5, and docs/recovery-code-v3.md, Phase 7.
|
||||
*/
|
||||
@Serializable
|
||||
data class CreateProfileRoute(
|
||||
val withWallet: Boolean,
|
||||
val withBackup: Boolean,
|
||||
): Route()
|
||||
+14
-17
@@ -2,32 +2,29 @@ package mobi.giza.compose.ui.view.model
|
||||
|
||||
import com.vitorpamplona.crypto.random.RandomInstance
|
||||
|
||||
import fr.acinq.bitcoin.MnemonicCode
|
||||
import fr.acinq.bitcoin.PrivateKey
|
||||
import fr.acinq.phoenix.managers.nostrPublicKeyHex
|
||||
import fr.acinq.phoenix.utils.MnemonicLanguage
|
||||
import kotlinx.coroutines.CompletableDeferred
|
||||
import mobi.giza.compose.identity.IdentityWriter
|
||||
import mobi.giza.compose.identity.NewProfile
|
||||
import mobi.giza.compose.identity.NewProfileWriter
|
||||
import mobi.giza.compose.identity.StoredIdentity
|
||||
import mobi.giza.compose.identity.recoverycode.RecoveryCode
|
||||
import mobi.giza.compose.identity.recoverycode.RecoveryCodeKeys
|
||||
|
||||
/**
|
||||
* A profile with a wallet attached: twelve fresh words through the seed writer, which
|
||||
* since docs/multiple-profiles.md writes the derived key to the credentials file as
|
||||
* well. Callback-shaped underneath, awaited here: exactly one of the two callbacks
|
||||
* fires.
|
||||
* A profile with a backup to write down: a fresh nine-block recovery code, written with its
|
||||
* 24 words and its key by [IdentityWriter.writeRecoveryCode]. What Landing makes, since
|
||||
* docs/recovery-code-v3.md, Phase 7; before that it was twelve random words, and nothing in
|
||||
* the app makes words from random bytes any more.
|
||||
*/
|
||||
fun SovereignWalletViewModel.seedProfileWriter(): NewProfileWriter = NewProfileWriter {
|
||||
val words = MnemonicCode.toMnemonics(RandomInstance.bytes(16), MnemonicLanguage.English.wordlist())
|
||||
val outcome = CompletableDeferred<Result<NewProfile>>()
|
||||
writeSeed(
|
||||
mnemonics = words,
|
||||
isRestoringWallet = false,
|
||||
onSeedWritten = { id -> outcome.complete(Result.success(NewProfile(StoredIdentity.nostrPublicKeyOf(words), id))) },
|
||||
onSeedWriteError = { error -> outcome.complete(Result.failure(IllegalStateException("seed write failed: $error"))) },
|
||||
)
|
||||
outcome.await()
|
||||
fun SovereignWalletViewModel.recoveryCodeProfileWriter(): NewProfileWriter = NewProfileWriter {
|
||||
val code = RecoveryCode.generate()
|
||||
when (val result = writeRecoveryCode(code)) {
|
||||
is IdentityWriter.WriteRecoveryCodeResult.Written ->
|
||||
Result.success(NewProfile(StoredIdentity.nostrPublicKeyOf(RecoveryCodeKeys.words(code)), result.id))
|
||||
is IdentityWriter.WriteRecoveryCodeResult.AlreadyExists -> Result.failure(IllegalStateException("a fresh code collided with one already here"))
|
||||
is IdentityWriter.WriteRecoveryCodeResult.CannotLoadKeys -> Result.failure(IllegalStateException("the key files could not be read"))
|
||||
}
|
||||
}
|
||||
|
||||
/** A profile that is a key: thirty-two random bytes, written the way a pasted nsec is. */
|
||||
|
||||
@@ -467,4 +467,37 @@ class IdentityWriterJvmTest {
|
||||
val phrase = assertIs<StoredIdentity.Mnemonic>(withoutCodes[coded.walletId])
|
||||
assertEquals(coded.nostrPublicKey, phrase.nostrPublicKey)
|
||||
}
|
||||
|
||||
/**
|
||||
* The round trip a lost phone is: a code made here, every key file gone, and the code
|
||||
* typed back in -- or its 24 words, from the export -- lands on the same npub and id.
|
||||
*/
|
||||
@Test
|
||||
fun `a code, or its words, restores the same profile on an empty device`() = runBlocking<Unit> {
|
||||
val coded = Coded()
|
||||
val made = assertIs<IdentityWriter.WriteRecoveryCodeResult.Written>(writeCode(coded))
|
||||
val npub = credentials()!!.keys.single()
|
||||
|
||||
fun wipe() = listOf("seed.dat", "nostr-credentials.dat", "recovery-codes.dat").forEach {
|
||||
FileSystem.SYSTEM.delete(SeedManager.getDatadir(phoenixGlobal.ctx).resolve(it), mustExist = false)
|
||||
}
|
||||
|
||||
wipe()
|
||||
val restored = assertIs<IdentityWriter.WriteRecoveryCodeResult.Written>(writeCode(coded))
|
||||
assertEquals(made.id, restored.id)
|
||||
assertEquals(setOf(npub), credentials()!!.keys)
|
||||
assertIs<StoredIdentity.RecoveryCode>(StoredIdentity.merge(seeds()!!, credentials()!!, StoredIdentity.parseCodes(codes()!!))[made.id])
|
||||
|
||||
wipe()
|
||||
val outcome = CompletableDeferred<Result<WalletId>>()
|
||||
IdentityWriter.writeMnemonic(
|
||||
log = log, phoenixGlobal = phoenixGlobal, globalPrefs = globalPrefs,
|
||||
writingState = WritingSeedState.Init, viewModelScope = scope, mnemonics = coded.words,
|
||||
onWritingSeedError = { outcome.complete(Result.failure(IllegalStateException(it.toString()))) },
|
||||
onWritingSeedStateWriting = {}, isRestoringWallet = true, isTorEnabled = false, customElectrumServer = null,
|
||||
onSeedWritten = { outcome.complete(Result.success(it)) },
|
||||
)
|
||||
assertEquals(made.id, withTimeout(15_000) { outcome.await() }.getOrThrow())
|
||||
assertEquals(setOf(npub), credentials()!!.keys)
|
||||
}
|
||||
}
|
||||
+9
-7
@@ -47,7 +47,7 @@ import mobi.giza.compose.ui.view.model.SignInToProfileViewModel
|
||||
import mobi.giza.compose.ui.view.model.SovereignWalletViewModel
|
||||
import mobi.giza.compose.ui.view.model.WritingSeedState
|
||||
import mobi.giza.compose.ui.view.model.bareKeyProfileWriter
|
||||
import mobi.giza.compose.ui.view.model.seedProfileWriter
|
||||
import mobi.giza.compose.ui.view.model.recoveryCodeProfileWriter
|
||||
import mobi.giza.compose.ui.view.state.CreateProfileUIState
|
||||
import mobi.giza.compose.ui.view.state.NavigationUIState
|
||||
import mobi.giza.compose.ui.view.state.StartupChoice
|
||||
@@ -57,8 +57,9 @@ import mobi.giza.compose.ui.view.state.StartupChoice
|
||||
* seams of Phases 1 through 7 in a row, through the real view models on an in-memory
|
||||
* database with a real key store.
|
||||
*
|
||||
* A is made from a phrase, the way Landing makes the first profile: both files written,
|
||||
* listed once with the phrase attached. No profile has a node behind it since
|
||||
* A is made from a recovery code, the way Landing makes the first profile since
|
||||
* docs/recovery-code-v3.md: its key, its 24 words and the code written, listed once with
|
||||
* the code attached. No profile has a node behind it since
|
||||
* docs/giza-wallet-removal.md, Phase 6. B's npub is signed in from inside; B is open
|
||||
* and read-only, and the machine routes it from its placeholder. A kind 1 queued for A
|
||||
* while B is open waits, and goes out when A is back. C is created from inside as a bare
|
||||
@@ -194,9 +195,10 @@ class MultipleProfilesRoundTripJvmTest {
|
||||
writeRecoveryCode = { mobi.giza.compose.identity.IdentityWriter.WriteRecoveryCodeResult.CannotLoadKeys },
|
||||
)
|
||||
|
||||
// A, from a phrase: both files written, listed once with a wallet attached.
|
||||
val idA = create(sovereign.seedProfileWriter(), "Ada")
|
||||
val storedA = assertIs<StoredIdentity.Mnemonic>(listed(sovereign, 1)[idA], "a profile with a wallet attached, under the wallet's id")
|
||||
// A, from a recovery code, the way Landing makes it: the key, the 24 words and the
|
||||
// code written, listed once with the code attached (docs/recovery-code-v3.md, Phase 7).
|
||||
val idA = create(sovereign.recoveryCodeProfileWriter(), "Ada")
|
||||
val storedA = assertIs<StoredIdentity.RecoveryCode>(listed(sovereign, 1)[idA], "a profile with a code attached, under its words' id")
|
||||
val credentials = NostrCredentialManager.loadAndDecryptOrNull(phoenixGlobal)
|
||||
assertEquals(mapOf(storedA.nostrPublicKey to NostrCredential.Secret(storedA.privateKey)), credentials, "the seed's key is a credential")
|
||||
sovereign.setActiveIdentity(activated(storedA))
|
||||
@@ -265,7 +267,7 @@ class MultipleProfilesRoundTripJvmTest {
|
||||
val booted = SovereignWalletViewModel(phoenixGlobal)
|
||||
val identities = withTimeout(15_000) { booted.availableIdentities.first { it.isNotEmpty() } }
|
||||
assertEquals(setOf(idA), identities.keys)
|
||||
assertEquals(storedA.privateKey, assertIs<StoredIdentity.Mnemonic>(identities[idA]).privateKey, "listed from the credential, not derived")
|
||||
assertEquals(storedA.privateKey, assertIs<StoredIdentity.RecoveryCode>(identities[idA]).privateKey, "listed from the credential, not derived")
|
||||
val opens = StartupChoice.resolve(
|
||||
force = null,
|
||||
desired = null,
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
# docs/scripts/wallet-audit.sh lists the lines that break a rule
|
||||
# docs/scripts/wallet-audit.sh --check and exits 1 if there are any
|
||||
#
|
||||
# Giza keeps no wallet, and Bennu, which it takes fixes from, keeps several. Four rules
|
||||
# Giza keeps no wallet, and Bennu, which it takes fixes from, keeps several. Five rules
|
||||
# catch one coming back:
|
||||
#
|
||||
# 1. a retired kind's number (30331-30337, 30350-30366) in the app's own code. They
|
||||
@@ -18,7 +18,10 @@
|
||||
# transactions, Electrum. Named rather than every *Wallet*: the profile pickers under
|
||||
# widgets/wallet/ and the SovereignWallet* startup kept Phoenix-era names, and the
|
||||
# files that decline a wallet (WalletRefusal, RemoveWalletsMigration) are about one;
|
||||
# 4. an amount in sats in a string a person reads.
|
||||
# 4. an amount in sats in a string a person reads;
|
||||
# 5. a phrase made from random bytes (`MnemonicCode.toMnemonics`) in the app's code. A new
|
||||
# profile is backed by a recovery code (docs/recovery-code-v3.md), whose 24 words are
|
||||
# made in RecoveryCodeKeys.kt and nowhere else. Phrases are restored, never made.
|
||||
#
|
||||
# Needs only bash and git, like the other two audits, so the CI budgets job can run it.
|
||||
|
||||
@@ -45,6 +48,8 @@ files=$(
|
||||
|
||||
sats=$(git grep -niE '>[^<]*\b(sats?|satoshis?)\b' -- composeApp/src/commonMain/composeResources)
|
||||
|
||||
phrases=$(git grep -nE 'MnemonicCode\.toMnemonics' -- "${MAIN[@]}" ':!*/identity/recoverycode/RecoveryCodeKeys.kt')
|
||||
|
||||
report() {
|
||||
local title=$1 lines=$2 n=0
|
||||
[[ -n "$lines" ]] && n=$(printf '%s\n' "$lines" | wc -l)
|
||||
@@ -58,6 +63,7 @@ report "Retired wallet kinds in the app's code" "$kinds"
|
||||
report "lightning-kmp imports" "$lightning"
|
||||
report "Files named for a wallet's pieces" "$files"
|
||||
report "Amounts in sats in the strings" "$sats"
|
||||
report "Phrases made from random bytes" "$phrases"
|
||||
|
||||
if (( CHECK && total > 0 )); then
|
||||
echo "Giza keeps no wallet. See docs/giza-wallet-removal.md before bringing one back." >&2
|
||||
|
||||
Reference in new issue
Block a user