Three files define how vault data looks at rest and on the wire. They are byte-compatible with the Go daemon's vault (backend/storage/vault), because the phone, the desktop app and the web vault all read the same remote state. The logic that uses them is in Vault.
Layers
flowchart LR
S[State object] -->|"CBOR encode, then gzip with mtime 0"| Z[compressed bytes]
Z -->|"XChaCha20-Poly1305 under the DEK"| E[encryptedData]
D["DEK, 64 bytes"] -->|"XChaCha20-Poly1305 under a 32-byte wrapping key"| W[wrappedDEK]
E --> J["envelope JSON"]
W --> JThe data encryption key (DEK) is 64 bytes, of which the cipher uses the first 32. The wrapping key is 32 bytes: the local key-encryption key (KEK) from the Keychain, or the remote credential secret (Vault: the store and on-device format).
state-codec.ts
state-codec.ts is the "Go-parity codec"; the client's older name-deduplicating codec is deliberately excluded.
Type | Fields |
|---|---|
|
|
|
|
|
|
|
|
VAULT_VERSION is 2. createEmpty() returns {version: 2, accounts: []}.
serializeState(state): client CBOR encode, then gzipSync with mtime: 0 so output is deterministic.
deserializeState(bytes): gunzip, decode, and throw Vault schema version mismatch unless version is 2.
Neither direction dedupes or renames accounts. The only normalization is Go's fill-empty-name rule: a whitespace-empty name becomes the account id.
notificationServerUrl and deletedAccounts are written only when non-empty.
getAccountName(account): the trimmed name if it matches ^[A-Za-z0-9_-]+$, else the account id.
Unknown fields
Fields this version does not know are kept, so a newer client's data survives a round trip through the phone. Decoding stores the leftover keys of the state, each account, each delegation and each capability under private symbols. getUnknownFields(target) and setUnknownFields(target, extras) read and replace them, and encoding spreads them back before the known fields. The merge uses these accessors.
Account id cache
accountIdFromSeed(seed) derives an account id and remembers it. The comment gives the reason: deriving an id is an Ed25519 public key derivation, "milliseconds each in the app's JS engine", merging derives every identity several times in one synchronous turn, and Exact refuses a turn over 100 ms, "so a vault of a few dozen identities could never finish connecting on a phone".
The cache is a Map keyed by the SHA-256 of the seed ("never the seed itself"), capped at 4096 entries and cleared when full.
warmAccountIds(...states) derives the id of every 32-byte seed not yet cached and calls yieldCryptoTurn() after each one, so each derivation gets its own turn.
vault.ts
vault.ts combines the state codec with the AEAD from Primitives.
Function | Behaviour |
|---|---|
| Returns |
| Decrypts |
| Unwraps the DEK, decrypts |
A wrong wrapping key makes the AEAD throw, which is what lets the store fail closed.
envelope.ts
envelope.ts is the persisted file, compatible with the Go daemon's vault.json. The app keeps its own codec because the shared client's parser "drops the remote sync-version fields", which the device role needs.
Field | Encoding |
|---|---|
| standard base64 with padding |
|
|
|
|
Numeric and string remote fields follow Go's omitempty: zero and empty values are not written.
remote.lastSyncTime is Unix seconds, not milliseconds.
serializeEnvelope writes two-space indented JSON in Go field order. parseEnvelope validates like Go: both byte fields must be non-empty, and a remote needs a vaultUrl and a userId.
createRemoteSyncState(init) fills the counters with zero values.
Tests
"vault crypto and Go envelope/state parity" in crypto.test.ts has 9 tests: literals for base64, SHA-256, HKDF and the connect id; exact AEAD ciphertext; exact envelope JSON for a local and a remote envelope; a Go fixture plus eight invalid envelopes; two original gzipped states that must re-encode to identical CBOR with unknown fields intact; the schema mismatch; and a full seal, open and re-wrap. The fixtures come from Parity vectors.
Do you like what you are reading? Subscribe to receive updates.
Unsubscribe anytime