Skip to content
Docs

Keystore

Encrypt with a password

scrypt PIN or raw key seals secrets; pass the same PIN on client ops.

Overview

Default encryption: true seals mnemonic/PK with scrypt + AEAD. Pass password or encryptionKey (32 bytes). Without either, the keystore generates a master key, which must live in a separate masterKeyAdapter (for example secure storage). Keeping it next to the sealed secrets requires insecure: true and is for development only.

Signature

js
await GrayKeystore.create(options)

Parameters

Pass either sqlite or adapter (not both).

FieldRequiredDescription
sqliteNo`{ path } | { web: { name, wasmUrl | locateFile } } | { expo: SQLiteDatabase } | { driver: SqliteDriver }` — recommended backend. path needs better-sqlite3 (Node.js), web needs sql.js, expo needs expo-sqlite.
adapterNo`KeystoreAdapter` — KV (localStorage / AsyncStorage / memory)
encryptionNo`boolean` — default `true`. `false` = plaintext secrets, no PIN
passwordNo`string` — scrypt PIN when encryption on (any non-empty string)
encryptionKeyNo`Uint8Array` — raw 32-byte key (not with password). Wallet ops need no PIN. After `lock()`, reopen with `unlockWithKey(key)`.
masterKeyAdapterNo`KeystoreAdapter` — separate store (Keychain / secure storage) for the generated master key. Must not be the same object as `adapter`.
insecureNo`boolean` — allow the generated master key to sit next to the secrets when no password, encryptionKey, or separate masterKeyAdapter is given. Development only.
prefixNo`string` — KV key prefix (default `"gray-keystore"`)

Response

Promise<GrayKeystore> — starts locked when encryption is on. Apps should not call unlock—pass the same PIN to GrayClient wallet ops.

js
ks.isLocked            // true after create when encryption on
ks.encryptionEnabled   // false when create({ encryption: false })
ks.lock()              // drop key material from memory
await ks.unlockWithKey(key) // key mode only: reopen after lock()
await ks.close()       // lock, flush, and close the backend

Usage

js
const keystore = await GrayKeystore.create({
  sqlite: { path: "./gray-wallets.db" },
  password: pin,
});
const client = await GrayClient.create({ storage: keystore });
await client.wallets.create({ label: "Main", password: pin });
await client.wallets.open(id, { password: pin });

// Key mode: a 32-byte key from secure storage instead of a PIN
const keyed = await GrayKeystore.create({
  sqlite: { path: "./gray-wallets.db" },
  encryptionKey: keyFromSecureStorage,
});

// Dev / plaintext
const plain = await GrayKeystore.create({
  sqlite: { path: "./gray-wallets.db" },
  encryption: false,
});
await (await GrayClient.create({ storage: plain })).wallets.create({ label: "Main" });

Crypto helpers

Low-level helpers, exported for advanced use. Apps using GrayClient do not need them.

js
randomKey(bytes?)                         // default 32 → Uint8Array
toHex(bytes) / fromHex(hex)
deriveKeyFromPassword(password, salt, params?)
sealSecret(plaintextJson, mode, { walletId, kind })   // → SecretEnvelopeV2
openSecret(envelope, mode, context?)                   // → string (context required for v2)

// mode: { mode: "password", password } | { mode: "raw", key } | { mode: "none" } | …
ts
type SecretEnvelopeV2 = {
  v: 2;              // wallet id and kind are bound into the AEAD tag
  kdf: "scrypt" | "raw" | "none";
  salt: string;
  nonce: string;
  ciphertext: string;
  N?: number; r?: number; p?: number;
};

Version 2 envelopes bind the wallet ID and secret kind as associated data, so a sealed secret cannot be moved onto another wallet. Version 1 envelopes written by earlier releases still open.

Throws

FieldRequiredDescription
invalid_argumentNoBoth password + encryptionKey; empty password; key ≠ 32 bytes; both sqlite + adapter; no password, encryptionKey, or separate masterKeyAdapter without insecure: true.
unsupported_capabilityNoThe sqlite backend is not available in this build (for example path in a browser).
unauthorizedNoLocked; bad unlock / wrong PIN on client ops or `reset`.
internalNoCorrupt master key.