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
await GrayKeystore.create(options)Parameters
Pass either sqlite or adapter (not both).
| Field | Required | Description |
|---|---|---|
| sqlite | No | `{ 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. |
| adapter | No | `KeystoreAdapter` — KV (localStorage / AsyncStorage / memory) |
| encryption | No | `boolean` — default `true`. `false` = plaintext secrets, no PIN |
| password | No | `string` — scrypt PIN when encryption on (any non-empty string) |
| encryptionKey | No | `Uint8Array` — raw 32-byte key (not with password). Wallet ops need no PIN. After `lock()`, reopen with `unlockWithKey(key)`. |
| masterKeyAdapter | No | `KeystoreAdapter` — separate store (Keychain / secure storage) for the generated master key. Must not be the same object as `adapter`. |
| insecure | No | `boolean` — allow the generated master key to sit next to the secrets when no password, encryptionKey, or separate masterKeyAdapter is given. Development only. |
| prefix | No | `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.
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 backendUsage
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.
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" } | …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
| Field | Required | Description |
|---|---|---|
| invalid_argument | No | Both password + encryptionKey; empty password; key ≠ 32 bytes; both sqlite + adapter; no password, encryptionKey, or separate masterKeyAdapter without insecure: true. |
| unsupported_capability | No | The sqlite backend is not available in this build (for example path in a browser). |
| unauthorized | No | Locked; bad unlock / wrong PIN on client ops or `reset`. |
| internal | No | Corrupt master key. |