toip.tools

SDK library · Browser

web-vault

Seal browser account seeds and named blobs using a passkey's PRF output or a passphrase-derived AES-GCM key. Unlocked seeds and keys are available to application code in memory; storage encryption does not make the running tab a protected execution environment.

@toip-tools/web-vaultv0.1.0Source

Setup

Install the package from npm. It needs Node 22.13 or later, or a bundler for browser entry points.

ShellInstall
npm install @toip-tools/web-vault

Runtime: Browsers. Tests run in Node with stand-ins for storage and the authenticator.

TypeScriptImport
import { createVault, PasskeyUnsupported } from "@toip-tools/web-vault";

Usage

Create, unlock, and back up an account

Create one vault per app prefix with createVault({ prefix, rpName }). Call createAccount(name, { kind: 'passkey' }); handle PasskeyUnsupported with an explicit passphrase flow. After reload, select an entry from accounts() and call unlock(account, passphrase?). Store backupCode(unlocked) securely; restore by checking seedFromBackup(code) and passing the resulting seed to createAccount with new protection.

Persist account data and non-extractable keys

Use the vault's writeJson/readJson for structured data and writeBlob/readBlob for bytes. Blob names use lowercase letters, digits, and dashes; seed, accounts, and keys are reserved. Use putCryptoKey/getCryptoKey for browser CryptoKey objects in IndexedDB. For a legacy account with a clear seed, call protect before relying on sealed writes.

Example

In a browser: keep an account's identity seed sealed by a passkey, with a passphrase fallback.

Typechecked, not run: it needs a browser.

TypeScriptsite/examples/snippets/web-vault.ts
import { PasskeyUnsupported, createVault } from "@toip-tools/web-vault";
import { identityFromSeed } from "@toip-tools/trust-core";

const vault = createVault({ prefix: "garden", rpName: "Community Garden" });

export async function signUp(name: string, passphrase: string) {
  try {
    return await vault.createAccount(name, { kind: "passkey" }); // Touch ID, Windows Hello, a security key
  } catch (e) {
    if (!(e instanceof PasskeyUnsupported)) throw e;
    return await vault.createAccount(name, { kind: "passphrase", passphrase });
  }
}

export async function signInAgain() {
  const [account] = vault.accounts();
  const unlocked = await vault.unlock(account); // the passkey prompt
  const me = identityFromSeed(unlocked.seed);
  await vault.writeJson(unlocked, "credentials", []); // sealed under the same key, bound to this account
  return { me, backup: vault.backupCode(unlocked) }; // show once; it restores the account elsewhere
}

Lifecycle and persistence

  • The vault keeps accounts unlocked until lockAll() or reload. lockAll() clears its internal map; it does not erase seeds or keys retained in application variables. Release those references when locking the UI.

  • Account records and blobs use localStorage; CryptoKeys use the IndexedDB database <prefix>.keys. Choose a distinct prefix per app on an origin. The vault closes its own IndexedDB connections after transactions.

  • Await removeAccount(id) for attempted CryptoKey cleanup as well as immediate localStorage removal. IndexedDB cleanup failures are swallowed by this method. Removing local records does not delete the authenticator's passkey. A seed backup restores the identity, not saved blobs or separate CryptoKeys.

Error handling

PasskeyUnsupported

Passkey creation or unlocking throws when the authenticator provides no usable PRF secret. passkeysAvailable() checks basic browser/domain support, not PRF capability.

Offer passphrase protection when creating an account. Distinguish unsupported PRF from user cancellation or other WebAuthn failures.

Unlock or blob-open exception

Wrong protection produces an unlock error. readBlob and sealed readJson throw when data is altered, belongs to another account/name, or cannot decrypt; a missing blob returns null.

Preserve the stored record for recovery and request valid protection. Do not silently overwrite unreadable data. readState deliberately returns null for both missing and unreadable state.

Validation / storage exception

Invalid prefixes or blob names, a passphrase shorter than eight characters, and writing byte blobs before protection throw. Browser storage and IndexedDB operations can also fail.

Validate user input, protect legacy accounts, and report persistence failures. A null result from seedFromBackup means the code has the wrong prefix or does not decode to a 32-byte seed.

Limitations

  • Signing with the passkey itself. The seed is unsealed in memory while the account is open.

API reference

Choose an entry point for the complete export list. Each export has a reference page with its signature, parameter types, return type, fields, and public methods where applicable.