src/vault/store.ts defines VaultStore, the one object that owns vault state on the device. src/vault/sources.ts keeps a single instance for the life of the data module and exposes it through three sources.
What is stored where
Location | Content |
|---|---|
File | The envelope: |
Keychain secret | A JSON bundle of 32-byte keys in standard base64. Key |
Keychain secret | The signing key map owned by Identity |
Memory only | The 64-byte data key (DEK), the decrypted state, and any pending connection token |
The remote secret's bundle key is the string ${vaultUrl}|${userId}|${credentialId}. The wrapping key for the DEK is that remote credential secret while connected, and local otherwise (wrappingKey). persist() re-seals the state under the current DEK and rewrites the file, tolerating EEXIST from mkdir.
Sources and the queue
Source | Arguments | Runs |
|---|---|---|
|
|
|
|
|
|
|
|
|
All but cancel go through run(fn), a promise chain: "Each operation waits for its predecessor; a refusal does not poison the queue." cancel bypasses the queue, which lets it interrupt a poll in flight (inferred from the code and the cancellation tests). Time is always an argument ("clocks are arguments").
Every source answers VaultStatus, which "never includes a seed, KEK, DEK, or remote credential secret": mode (Local or Remote), locked, error, connection (disconnected, pending, connected), url, connectUrl, expiresAt, pollWarning, connectError, syncError, localVersion, remoteVersion, syncedVersion, lastSync (formatted in UTC, or Never) and ids.
Loading and failing closed
load(now) runs once per instance:
File present: parse the envelope, unwrap the DEK, open the state, reconcile(now, false), then publish keys.
File absent (ENOENT): create the local KEK if needed, seal an empty state, reconcile, persist.
Any failure: zero and drop the DEK, set error to Failed to open the vault: ... and rethrow. Nothing is written, so a wrong or missing Keychain key never overwrites ciphertext. A missing key reports Vault key is missing from this device’s Keychain.
Bridging the Keychain map
Three methods keep the vault state and the seed-keys map consistent.
publishKeys() writes every vault account into the map (name empty when it equals the account id), skipping accounts with a pending removal marker, yielding after each.
reconcile(now, removeMissing = true) folds Me's edits into the state, "retaining delegations and extras":
For each vault account: if its key is absent and a removal marker exists and removeMissing is true, write a tombstone deletedAccounts[id] = max(now, createTime, existing) and drop the account.
If its key is absent with no marker, keep the account and flag it as unpublished.
If its key is present with a different name, adopt the name.
Keys in the map that the vault lacks are added (after checking the seed derives that id), and any tombstone for them is deleted.
If anything changed, bump remote.localVersion when connected and persist.
Only then clear the markers: they "are dropped only after the tombstones are on disk".
Republish unpublished accounts.
rebaseKeys(before, now) replays edits Me made while a network request was in flight, by diffing the map before and after, "so publishing remote keys never overwrites a newer local action".
The removal rule
An identity is tombstoned only for an explicit {removed: true} marker. A key that is merely absent is not a deletion. The doc comment states why: the map "is a copy written after the vault state, so it is behind whenever publishing was interrupted", and reading that lag as a removal "once tombstoned a whole remote vault". rebaseKeys applies the same test: "Gone without a removal marker means the map was rewritten, not that a person removed the identity."
Other operations
read(now): load, reconcile, and sync at once when something changed.
start, poll, cancel: see Connect ceremony.
sync(now): see API and sync.
disconnect(clear): no server call. Drops remote and credentials, reduces the Keychain bundle to {local}, and with clear also empties the state and takes a fresh DEK.
lock(now): reconcile, persist, zero the DEK, empty the in-memory state and clear the signing map, so no feature can sign until unlock.
unlock(now): reload with the Keychain key. The comment notes that "no password is used by this vault format".
Turn budget
publishKeys, reconcile and rebaseKeys call yieldCryptoTurn() after each account, because each iteration may derive an account id. See The 100 ms turn budget.
Tests
In vault.test.ts (27 tests in total) the store-level cases include: local ciphertext under a Keychain KEK that "never contains a private seed"; a wrong Keychain key failing closed without overwriting; rename and deletion syncing with a tombstone; lock, unlock, switch to local and logout; Me edits made during a remote fetch surviving; and the three regression tests for the removal rule (lines 408 to 466): a connection cut short before identities reach the Keychain, a map that lost its entries, and a marker spent once recorded.
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime