Vault
यह कंटेंट अभी तक आपकी भाषा में उपलब्ध नहीं है।
What your app saves, only the user can read. Each value is encrypted on the user’s device before it is saved. Your database, your server and Geometer see scrambled text.
What this does not protect
- Your app’s own code. The code that encrypts runs inside your app. If you ship changed code, it can read the text before it is encrypted.
- A lost key. If the user loses their passkey and their recovery code, nobody can open their data. Not you. Not Geometer.
- What is not encrypted. Only the fields you pass through the Vault are encrypted. Ids, dates and other plain columns stay readable.
- A
searchKey. It is a fingerprint. Anyone who copies your database can see which rows have the same value.The full list: limits.
Vault code runs in the browser: in Next.js put it in a client component (a file that starts with "use client";), and call it from a user action such as a button, not while the page renders. For a first try with no account, use browserKeyStore() (development only; see the quickstart). If a passkey is not available (no PRF, or a preview frame from Lovable or Bolt), use unlock: { passphrase }: at least 15 characters, not common, no app name or user id, no run of 6 identical or consecutive characters.
How it works, in four steps
Section titled “How it works, in four steps”| Step | What happens | Where |
|---|---|---|
| 1 | The user unlocks with a passkey or a passphrase. | Device |
| 2 | You call vault.encrypt(text, { table, id, field }). |
Device |
| 3 | You save the returned string in any text column. | Your database |
| 4 | Later you load it and call vault.decrypt(...). |
Device |
The table, id and field are part of the encryption. A saved value cannot be moved to another table, row or column and still open.
One full example: a notes app on Supabase
Section titled “One full example: a notes app on Supabase”Table (the key table trust_keys is in the quickstart):
create table if not exists public.notes ( id uuid primary key, user_id uuid not null default auth.uid() references auth.users (id), title text not null, -- encrypted string title_key text not null, -- searchKey of the title (for exact-match lookups) body text not null, -- encrypted string created_at timestamptz not null default now());
alter table public.notes enable row level security;create policy "own notes" on public.notes for all using (auth.uid() = user_id) with check (auth.uid() = user_id);Code, in src/notes.ts:
import { createClient } from "@supabase/supabase-js";import { openVault, supabaseKeyStore, type Vault } from "@geometer/trust/vault";
const supabase = createClient( import.meta.env.VITE_SUPABASE_URL, import.meta.env.VITE_SUPABASE_ANON_KEY,);
let vault: Vault | null = null;
// Call once after sign-in. Show the code to the user when it arrives.export async function unlock(onRecoveryCode: (info: { code: string }) => void) { const { data } = await supabase.auth.getUser(); if (!data.user) throw new Error("Sign in first."); vault = await openVault({ app: "my-notes", user: data.user.id, keyStore: supabaseKeyStore(supabase, { app: "my-notes", user: data.user.id }), unlock: "passkey", onRecoveryCode, });}
export async function addNote(title: string, body: string) { if (!vault) throw new Error("Call unlock() first."); const id = vault.newId(); // make the id first: it is part of the encryption const { error } = await supabase.from("notes").insert({ id, title: await vault.encrypt(title, { table: "notes", id, field: "title" }), title_key: await vault.searchKey(title.trim().toLowerCase(), { table: "notes", field: "title" }), body: await vault.encrypt(body, { table: "notes", id, field: "body" }), }); if (error) throw error; return id;}
export async function listNotes() { if (!vault) throw new Error("Call unlock() first."); const { data, error } = await supabase.from("notes").select("id, title, body"); if (error) throw error; return Promise.all( data.map(async (row) => ({ id: row.id as string, title: await vault!.decrypt(row.title, { table: "notes", id: row.id, field: "title" }), body: await vault!.decrypt(row.body, { table: "notes", id: row.id, field: "body" }), })), );}
// Exact-match lookup: no decrypting on the server, no LIKE.export async function findByTitle(title: string) { if (!vault) throw new Error("Call unlock() first."); const key = await vault.searchKey(title.trim().toLowerCase(), { table: "notes", field: "title" }); const { data, error } = await supabase.from("notes").select("id, title").eq("title_key", key); if (error) throw error; // Decrypt each hit and compare it: someone who can write to your database can copy a search key onto another row. const hits = []; for (const row of data) { const text = await vault.decrypt(row.title, { table: "notes", id: row.id, field: "title" }); if (text.trim().toLowerCase() === title.trim().toLowerCase()) hits.push(row.id as string); } return hits;}table, id and field together say where a value lives. Use the same three when you decrypt. table is the name of your database table or collection.
Recovery
Section titled “Recovery”onRecoveryCode is called with { code, replacesPrevious, reason }. Show the code and ask the user to write it down. Do not store it. reason says why you are showing it, so you can choose your words:
reason |
When | Say |
|---|---|---|
"setup" |
First setup | “Write down your recovery code.” |
"recovery", "requested" |
After recoverVault, or vault.newRecoveryCode() |
“Here is your new recovery code. Your old one stops working once you confirm this one.” |
"rotation" |
After vault.rotateKey() |
“Your key was changed. Your old recovery code no longer works. Write down this new one and destroy the old one.” |
"unconfirmed" |
A later visit shows a code the user never confirmed | “You have not confirmed your recovery code yet.” |
First setup fails with vault/recovery-code-handler-missing when there is no handler. Pass the handler on every openVault call.
If the user loses their passkey, they enter the code:
import { recoverVault, supabaseKeyStore } from "@geometer/trust/vault";
const vault = await recoverVault({ app: "my-notes", user: user.id, keyStore: supabaseKeyStore(supabase, { app: "my-notes", user: user.id }), recoveryCode: codeTypedByUser, onRecoveryCode: ({ code }) => showRecoveryCodeToUser(code), // recovery makes a new code at once});
await vault.addPasskey(); // set up a new passkey// or: await vault.setPassphrase("a long phrase the user chooses");Offer a Recovery Kit
Section titled “Offer a Recovery Kit”Next to the code, offer “Download” and “Print” buttons. recoveryKit makes a plain-text file from the code: the code in 8 groups, what it is, where to keep it, that your app will never ask for it by message, and the date. It runs in the page, makes no network call and never logs the code.
import { recoveryKit } from "@geometer/trust/vault";
onRecoveryCode: ({ code }) => { const kit = recoveryKit({ code, app: "my-notes", user: user.id, appName: "My Notes" }); // { text, filename, blob } showRecoveryScreen({ code, onDownload: () => { const a = document.createElement("a"); a.href = URL.createObjectURL(kit.blob); a.download = kit.filename; // my-notes-recovery-kit-2026-11-27.txt a.click(); URL.revokeObjectURL(a.href); }, onPrint: () => printText(kit.text), // for example: open a window with <pre>{kit.text}</pre> and call print() });},The file starts like this:
My Notes: recovery kit
YOUR RECOVERY CODE
7QK4-9M2B-XH5T-0RZ1-G8VD-3NCW-6EYF-A2JP
WHAT THIS ISThis code opens your private data in My Notes if you lose the passkey orpassphrase you normally use. ...Keep the code on screen until the user has saved or written it, then let them confirm.
When passkeys cannot work: the fallback passphrase
Section titled “When passkeys cannot work: the fallback passphrase”A passkey cannot work in a preview frame from another site (Lovable, Bolt), in a browser without the PRF extension, or without WebAuthn. Do not make users type a passphrase every time; ask only when needed:
const vault = await openVault({ app: "my-notes", user: user.id, keyStore, unlock: { passkey: true, fallbackPassphrase: () => askUserForPassphrase() }, // returns a Promise<string> onRecoveryCode,});Vault tries the passkey. Only when it fails with vault/passkey-unsupported does it call fallbackPassphrase() (once) and carry on with the passphrase: at first setup it sets one with the same rules (15 characters, not common, no app or user name), and later it unlocks with it. A cancelled passkey prompt, a wrong passkey and a damaged key file never reach the fallback. If the key file has no passkey at all, the passphrase is asked straight away. After recoverVault, use await vault.addUnlock({ passkey: true, fallbackPassphrase }): it adds a passkey, or a passphrase where passkeys cannot work, and says which ("passkey" or "passphrase").
Remember this device (optional)
Section titled “Remember this device (optional)”Off unless you ask for it. Show an unchecked checkbox “Remember this device for 30 days” and, only when it is checked, pass rememberDevice: { days: 30 } (a whole number, 1 to 90) to openVault, recoverVault or acceptStartOverVault. The next openVault on that device then opens without a prompt until the days are over. Say in plain words that anyone who can use this browser on this device can open the Vault until then.
const vault = await openVault({ ..., rememberDevice: checked ? { days: 30 } : undefined });vault.rememberedUntil; // a Date, or null (not remembered, or this browser cannot do it)vault.rememberedSession; // true when this Vault was opened without a prompt, from the remembered deviceawait vault.forgetDevice(); // "Forget this device": the next open asks againvault.lock({ forget: true }); // lock and forget in one step
// Sign-out: works with NO open Vault (after a page reload, say). Wait for it before the sign-out finishes.import { forgetDevice } from "@geometer/trust/vault";await forgetDevice({ app: "my-notes", user: user.id }); // true when gone; false if the browser could not delete itWire your sign-out to forgetDevice({ app, user }). Without it, signing out leaves the device remembered.
Vault keeps, in its own IndexedDB database, only keys that the browser can use but nobody can read out or copy (extractable: false): the keys that read and write values, and the one that checks the key file. It never stores the Vault key itself. It still runs every key-file and device check first. A key rotation, a retired key, a start-over, a replaced or older key file, or the end date all end it, and the user is asked normally; a retire or rotation done on another device ends it here only when Vault next runs here and sees the change. A script on your own page can use the keys while the entry exists (to read and write the user’s values), but cannot copy them away, and loses them when the entry ends. Where the browser cannot do it (some private windows), the option does nothing and rememberedUntil is null.
A remembered session cannot change keys. It reads and writes values (encrypt, decrypt, encryptJSON, decryptJSON, reencrypt, searchKey) but addPasskey, addUnlock, setPassphrase, removePasskey, removePassphrase, newRecoveryCode, rotateKey and retireKey throw vault/unlock-needed (“Unlock with your passkey or passphrase to change keys”). Show the unlock prompt, then call openVault({ ..., unlock: "passkey", skipRemembered: true }) (add rememberDevice to remember again) and make the change on that Vault. Nothing is remembered while a recovery code is waiting to be confirmed.
When the key file looks wrong
Section titled “When the key file looks wrong”| Error | What it means | What to do |
|---|---|---|
vault/key-file-rolled-back |
Someone put back an older key file. | Do not keep writing. Restore the newest key file from a backup. |
vault/key-file-behind |
A value uses a key this key file does not know, on a device that never saw a newer file (a new device is in this state until it sees one). | Do not write. Restore the newest key file. If you have checked and the value is not a problem, call vault.resolveKeyFileBehind({ confirm }): the user unlocks again with their own secret and you confirm; that key is then ignored. |
vault/key-file-replaced |
This device opened a different Vault for this user before. | If the user started over on another device, ask them, then call acceptStartOverVault; their own secrets must open the new Vault, and the old one is then left behind on this device. Otherwise restore the key file from a database backup. |
vault/key-store-failed during a check |
Your database could not be asked. | Try again. The Vault stays open. |
Vault also writes a signed receipt for each key event (created, recovered, rotated and so on; never for encrypt or decrypt). Pass receiptStore, onReceipt or onReceiptError to openVault if you want them; a failing receipt never blocks the Vault.
Other things a Vault can do
Section titled “Other things a Vault can do”| Method | Use |
|---|---|
vault.addPasskey(), vault.setPassphrase(p) |
Add an unlock method. A passphrase needs at least 15 characters and is refused if common. |
vault.removePasskey(slotId), vault.removePassphrase(), vault.listUnlockMethods() |
Remove or list unlock methods. The last one cannot be removed. |
vault.newRecoveryCode() |
Make a new recovery code. The handler shows it. |
vault.rotateKey() |
Start a new key (and a new passkey or passphrase, and a new recovery code). Re-encrypt old values (needsReencrypt, reencrypt), then retireKey(kid); listKeys() shows the ids. Rotation is never blocked; vault.previousKeyWarning turns true from 8 old keys on, and above 32 the oldest are retired automatically. After a rotation tell users to destroy their old recovery code and never to type an old passphrase: an old secret the user kept can still open an older copy of the key file. |
startOverVault({ ..., archive }) |
Only for a user who truly lost the Vault: a new, empty Vault. Never call it automatically. archive(oldKeyFile) must save the old file first. |
vault.lock(), vault.lock({ forget: true }) |
Forget the key in memory; with forget, also forget this device. |
vault.addUnlock({ passkey: true, fallbackPassphrase }) |
Add a passkey, or a passphrase where passkeys cannot work. |
vault.forgetDevice(), forgetDevice({ app, user }), vault.rememberedUntil, vault.rememberedSession |
See “Remember this device”. |
vault.deviceHistoryNotUpdated |
true when a key change was saved but this device’s history note could not be updated (“saved; device history not updated”). The change worked; the note catches up on the next change. |
recoveryKit({ code, app, user, appName? }) |
The Recovery Kit file for the “Download” and “Print” buttons. |
sanitizePasskeyOptions, sanitizePasskeyResponse |
If your own sign-in uses passkeys, pass your server’s options and results through these so Vault’s secret never reaches your server. |
Values other than text
Section titled “Values other than text”const saved = await vault.encryptJSON({ done: false, tags: ["home"] }, { table: "notes", id, field: "meta" });const meta = await vault.decryptJSON(saved, { table: "notes", id, field: "meta" });Store saved in a text column.
Common mistakes
Section titled “Common mistakes”| Mistake | What goes wrong | Do this instead |
|---|---|---|
| Not showing the recovery code, or not asking the user to write it down | A lost passkey means lost data, for good. | Show it in onRecoveryCode. Make the user confirm. |
Searching encrypted columns with LIKE, ILIKE or full-text search |
It finds nothing. The column holds scrambled text. | Store vault.searchKey(...) in a plain column and use .eq(...), then decrypt and compare each hit. This finds exact matches only. |
| Decrypting on the server | The server then sees the plain text. This defeats Vault. | Encrypt and decrypt only in the browser. |
Using a different table, id or field when you decrypt |
Decrypt fails with vault/wrong-context. |
Use the exact table, id and field from the encrypt call. |
Changing the app name later |
The keys no longer match. Old data does not open. | Pick the name once. |
Using browserKeyStore() in production |
The key file sits only in one browser. | Use supabaseKeyStore or firebaseKeyStore. browserKeyStore and memoryKeyStore are for development and tests. |
| Using the Vault for AI chats | A Vault protects saved data. It does not protect a chat with an AI. | Use Sealed AI or Masked AI for chats. |
See also: API reference, errors, Supabase on Next.js, Firebase.