Vault: API and sync
The vault HTTP client in src/vault/api.ts (endpoints, bearer, timeouts, error classes) and the sync pass in src/vault/sync.ts (fetch with knownVersion, merge, upload decision, 409 retry), plus how VaultStore wraps it.

src/vault/api.ts is the only code that talks to the vault server. src/vault/sync.ts is one sync pass, a port of Go's syncRemote, remoteNeedsUpload and recordRemoteSyncSuccess. Both are driven by VaultStore (the store).

Endpoints

Paths are joined to the normalized vault URL by resolveVaultEndpoint, so with the default https://hyper.media/vault the first row is https://hyper.media/vault/api/vault.

Function

Request

Auth

Result

getVault(vaultUrl, bearer, knownVersion?)

GET api/vault, with ?knownVersion=N when N > 0

bearer

{encryptedData?, version, credentials, unchanged}

saveVault(vaultUrl, bearer, {encryptedData, version})

POST api/vault, JSON body

bearer

resolves when the body reports success; 409 throws VaultWriteConflictError

getVaultConnect(vaultUrl, connectId)

GET api/vault-connect/{connectId}

none

{found, payload?}; 404 is {found: false}

deleteSecretCredential(vaultUrl, bearer, credentialId)

DELETE api/credentials/secret/{credentialId}

bearer

resolves on success

version in saveVault is the expected current server version; the server increments it. The file header states that the app only ever uses bearer auth plus the unauthenticated one-time mailbox read, never session cookies.

Bearer

buildBearerAuth(credentialId, secret) returns <credentialId>:<b64url(authKey)>, where authKey is HKDF-SHA256 of the 32-byte credential secret with the info string seed-hypermedia-vault-secret-authentication (Crypto primitives). It is sent as Authorization: Bearer .... An empty credential id throws.

Transport

vaultFetch sets exactTimeout on every request:

Constant

Value

Applies to

REQUEST_TIMEOUT_MS

5,000 ms

unauthenticated requests (the mailbox)

AUTHENTICATED_TIMEOUT_MS

20,000 ms

requests with a bearer: "the server verifies the bearer with Argon2id and a phone may be on a slow link"

Errors are classified:

Class

When

VaultTransportError

fetch threw with kind Aborted, Timeout or Network, an AbortError or TimeoutError, or a TypeError. Retryable without a new browser approval

VaultApiError

a non-success status; carries status and the server's error text when the body is JSON

VaultWriteConflictError

status 409 from POST api/vault

The error classes set name with Object.defineProperty because "Exact hardens Error.prototype"; assigning this.name would throw on device. A unit test freezes the prototype the same way to prove it.

findSecretCredential(credentials, id) returns the secret credential with that id, and throws if it is absent or has no wrapped DEK.

One sync pass

runRemoteSync(host, now) takes a RemoteSyncHost (callbacks for context, the local snapshot, applying a merged state and recording success).

flowchart TD A[Load sync context] --> B["GET api/vault?knownVersion=remoteVersion"] B --> C{"unchanged?"} C -- yes --> D["Upload needed if localVersion differs from syncedLocalVersion"] C -- no --> E["Unwrap DEK, decrypt and decode remote state"] E --> F["Warm account ids, merge remote into local"] F --> G["Upload needed if remote and local are not semantically equal"] D --> H{"upload?"} G --> H H -- no --> K[Record success] H -- yes --> I["POST api/vault with version = fetched version"] I -- ok --> K I -- "409, attempts left" --> B I -- "409 on third attempt" --> X[Throw]

Rules, as coded:

    At most 3 attempts. Only a 409 with attempts left loops; every other error throws.

    When the server says unchanged, the DEK comes from the wrapped DEK already stored in the envelope; otherwise from the credential in the response.

    A merge-in does not bump localVersion.

    The upload decision is semantic (statesEqual), "never bytes".

    After a successful upload the recorded remoteVersion is the fetched version plus 1.

    lastSyncTime is floor(now / 1000): Unix seconds.

    REMOTE_SYNC_INTERVAL_MS is 30,000 ms.

yieldCryptoTurn() separates decryption, decoding, warming, equality and serialization, for the 100 ms turn budget. The merge itself is in Merge and tombstones.

How the store wraps it

VaultStore.sync(now) does nothing when local or locked. Otherwise:

    reconcile(now) to fold in Me's edits, and snapshot the key map.

    Run the pass against a candidate state, so a failed pass leaves the real state untouched.

    On success: adopt the candidate, rebaseKeys, persist, publishKeys.

    On failure: write lastSyncError and lastSyncTime into the envelope and persist. The screen shows the error and offers "Sync now".

Step 2 is what the test "a failed remote upload does not turn newly downloaded identities into local deletions" protects.

When sync runs

Root tasks trigger it every 30 s while connected, unlocked and visible, once on returning to the foreground, and after any Me edit; see Screens and tasks.

Tests

In vault.test.ts: the read-only Error name; sync that downloads, uploads, retries one 409 and persists counters; three conflicts stopping with remote vault write conflict and recovering on retry; a corrupted wrapped DEK reporting an error while preserving the local vault. The real vault suite adds a real 409 and an idle sync that gets unchanged and uploads nothing.

Do you like what you are reading? Subscribe to receive updates.

Unsubscribe anytime