Keystore
Web adapter
SQLite in IndexedDB through sql.js for browsers and extensions, plus small KV adapters.
Overview
In browsers and Chrome extensions, use the SQLite web backend: sql.js (SQLite compiled to WebAssembly) with the database persisted to IndexedDB. Your app serves the sql-wasm.wasm file; it is never loaded from a CDN. Treat XSS as full secret compromise on any web backend.
Install
bash
npm install @gray3/keystore sql.jsSignature
js
await GrayKeystore.create({
sqlite: { web: { name, wasmUrl } }, // or { name, locateFile }
password: pin,
})Parameters
| Field | Required | Description |
|---|---|---|
| sqlite.web.name | Yes | `string` — IndexedDB record name for this database |
| sqlite.web.wasmUrl | No | `string` — URL of sql-wasm.wasm served by your app. Required unless locateFile is set. |
| sqlite.web.locateFile | No | `(file: string) => string` — sql.js-style resolver, instead of wasmUrl |
| password | No | `string` — when encryption enabled |
Serving sql-wasm.wasm
- Vite:
import wasmUrl from "sql.js/dist/sql-wasm.wasm?url" - Next.js: copy
node_modules/sql.js/dist/sql-wasm.wasmtopublic/and passwasmUrl: "/sql-wasm.wasm" - Chrome extension: ship the file and pass
chrome.runtime.getURL("sql-wasm.wasm"); the CSP needs'wasm-unsafe-eval'
Usage
js
import wasmUrl from "sql.js/dist/sql-wasm.wasm?url";
import { GrayKeystore } from "@gray3/keystore";
import { GrayClient } from "@gray3/client";
const keystore = await GrayKeystore.create({
sqlite: { web: { name: "gray-wallets", wasmUrl } },
password: pin,
});
const client = await GrayClient.create({ storage: keystore });
// On sign-out: lock and flush pending writes to IndexedDB
await keystore.close();Key-value adapters
For small wallet counts you can use a key-value adapter instead. localStorageAdapter() works on pages (not in extension service workers), memoryAdapter() keeps everything in memory, and asyncKvAdapter(store) wraps any async store such as chrome.storage.local.
js
import { GrayKeystore, localStorageAdapter, memoryAdapter } from "@gray3/keystore";
await GrayKeystore.create({ adapter: localStorageAdapter(), password: pin, prefix: "acme-gray" });
await GrayKeystore.create({ adapter: memoryAdapter(), password: pin });ts
// KeystoreAdapter shape
{
getItem(key: string): Promise<string | null>;
setItem(key: string, value: string): Promise<void>;
removeItem(key: string): Promise<void>;
getAllKeys?(): Promise<string[]>;
}Throws
| Field | Required | Description |
|---|---|---|
| invalid_argument | No | Missing name, or neither wasmUrl nor locateFile; both sqlite and adapter. |
| unsupported_capability | No | sqlite: { web } in a React Native build, or IndexedDB unavailable. |
| unauthorized | No | Wrong PIN. |
Full setup per framework: React with Vite, Next.js, and Chrome extensions. See Threat model for XSS and device loss.