Skip to content
Docs

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.js

Signature

js
await GrayKeystore.create({
  sqlite: { web: { name, wasmUrl } },   // or { name, locateFile }
  password: pin,
})

Parameters

FieldRequiredDescription
sqlite.web.nameYes`string` — IndexedDB record name for this database
sqlite.web.wasmUrlNo`string` — URL of sql-wasm.wasm served by your app. Required unless locateFile is set.
sqlite.web.locateFileNo`(file: string) => string` — sql.js-style resolver, instead of wasmUrl
passwordNo`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.wasm to public/ and pass wasmUrl: "/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

FieldRequiredDescription
invalid_argumentNoMissing name, or neither wasmUrl nor locateFile; both sqlite and adapter.
unsupported_capabilityNosqlite: { web } in a React Native build, or IndexedDB unavailable.
unauthorizedNoWrong PIN.

Full setup per framework: React with Vite, Next.js, and Chrome extensions. See Threat model for XSS and device loss.