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-vault` · v0.1.0 · [Source](https://github.com/yourbuddyconner/toip-tools/blob/main/packages/web-vault/src/index.ts)

## Setup

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

Install:

```bash
npm install @toip-tools/web-vault
```

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

Import:

```ts
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.

[`createVault`](https://toip.tools/toolkit/web-vault/api/createVault/) · [`VaultConfig`](https://toip.tools/toolkit/web-vault/api/VaultConfig/) · [`Unlocked`](https://toip.tools/toolkit/web-vault/api/Unlocked/) · [`PasskeyUnsupported`](https://toip.tools/toolkit/web-vault/api/PasskeyUnsupported/)

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

[`Vault`](https://toip.tools/toolkit/web-vault/api/Vault/) · [`Account`](https://toip.tools/toolkit/web-vault/api/Account/) · [`Protection`](https://toip.tools/toolkit/web-vault/api/Protection/)

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

site/examples/snippets/web-vault.ts:

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

- [`@toip-tools/web-vault`](https://toip.tools/toolkit/web-vault/api/)

  Browser · 10 exports

## Related

### Built on

- [`@toip-tools/trust-core`](https://toip.tools/toolkit/trust-core/): Identities, signed and encrypted Trust Tasks over TSP, credentials, status lists and local policy.

### Used by these examples

- [Settle](https://toip.tools/examples/#settle): Conditional payments with shared control of escrow.
- [Tipline](https://toip.tools/examples/#tipline): A drop box that lets sources prove membership without revealing their identity.
- [Agora](https://toip.tools/examples/#agora): Anonymous forums with verified membership.
- [Capture](https://toip.tools/examples/#capture): Verifiable origins for photos and sensor readings.

### Concepts

[Concepts](https://toip.tools/concepts/) explains the protocols behind web-vault; the [FAQ](https://toip.tools/faq/) covers install, runtimes and support.

---

This is the Markdown version of https://toip.tools/toolkit/web-vault/. An index of the documentation for AI assistants is at https://toip.tools/llms.txt.
