Allow enableRecovery to derive the key from a passphrase

enableRecovery now takes an optional passphrase and returns the recovery
key as the suspend result (like resetRecoveryKey), avoiding the
flow/return-value race. For the passphrase path the SDK-derived base58
key is kept out of the session-scoped progress flow so it is never
surfaced to the user; the caller receives it as the return value and is
responsible for scrubbing it.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This commit is contained in:
Jenna Vassar
2026-06-02 13:52:08 -07:00
parent 62ac6a3d2f
commit 0cb9d2911d
3 changed files with 27 additions and 11 deletions
@@ -131,19 +131,28 @@ class RustEncryptionService(
override suspend fun enableRecovery(
waitForBackupsToUpload: Boolean,
): Result<Unit> = withContext(dispatchers.io) {
passphrase: String?,
): Result<String> = withContext(dispatchers.io) {
runCatchingExceptions {
service.enableRecovery(
// The key arrives as the suspend return value (like resetRecoveryKey), avoiding a
// flow/return-value race; the listener only feeds sub-progress.
enableRecoveryProgressStateFlow.value = EnableRecoveryProgress.Starting
val key = service.enableRecovery(
waitForBackupsToUpload = waitForBackupsToUpload,
progressListener = object : EnableRecoveryProgressListener {
override fun onUpdate(status: RustEnableRecoveryProgress) {
enableRecoveryProgressStateFlow.value = enableRecoveryProgressMapper.map(status)
}
},
passphrase = null,
passphrase = passphrase,
)
// enableRecovery returns the encryption key, but we read it from the state flow
.let { }
// Pin Done explicitly so observers get a coherent terminal value. For the passphrase
// path the user never sees the SDK base58 key, so keep it out of this session-scoped
// (in-memory) flow entirely; the caller still receives it as the return value and is
// responsible for scrubbing it. The auto-generated path must retain the key here since
// that is how it is surfaced to the user.
enableRecoveryProgressStateFlow.value = EnableRecoveryProgress.Done(if (passphrase != null) "" else key)
key
}.mapFailure {
it.mapRecoveryException()
}