function · Browser

# createVault

Creates a browser account vault with its own storage namespace and unlocked-account cache. Seeds and blobs are sealed with an AES-GCM key derived from a passkey's PRF output or a passphrase. Unlocked seeds and keys remain accessible to application code in memory.

`@toip-tools/web-vault` · [View source](https://github.com/yourbuddyconner/toip-tools/blob/main/packages/web-vault/src/vault.ts#L113)

Import:

```ts
import { createVault } from "@toip-tools/web-vault";
```

## Declaration

Signature:

```ts
createVault(config: VaultConfig): {
    prefix: string;
    accounts: () => Account[];
    removeAccount: (id: string) => Promise<void>;
    createAccount: (name: string, how: Protection, seed?: Uint8Array) => Promise<Unlocked>;
    unlock: (a: Account, passphrase?: string) => Promise<Unlocked>;
    unlockedAccount: (id: string) => Unlocked | undefined;
    protect: (u: Unlocked, how: Protection) => Promise<Unlocked>;
    lockAll: () => void;
    passkeysAvailable: () => boolean;
    readBlob: (u: Unlocked, name: string) => Promise<Uint8Array | null>;
    writeBlob: (u: Unlocked, name: string, bytes: Uint8Array) => Promise<void>;
    readJson: <T>(u: Unlocked, name: string) => Promise<T | null>;
    writeJson: (u: Unlocked, name: string, value: unknown) => Promise<void>;
    removeBlob: (a: Account, name: string) => void;
    readState: <T>(u: Unlocked) => Promise<T | null>;
    writeState: (u: Unlocked, value: unknown) => Promise<void>;
    putCryptoKey: (a: Account, name: string, key: CryptoKey) => Promise<void>;
    getCryptoKey: (a: Account, name: string) => Promise<CryptoKey | null>;
    backupCode: (u: Unlocked) => string;
    seedFromBackup: (code: string) => Uint8Array | null;
    storageKey: (name: string, did: string) => string;
};
```

### Parameters

**`config` (required)**

`VaultConfig`

App-specific prefix and optional protection, browser API, storage, and identity settings. The prefix must contain only letters, digits, and dashes; use a distinct prefix per app on an origin.

**Fields of `config`**

**`prefix` (required)**

`string`

Storage key prefix, one per app on an origin: letters, digits and dashes.

**`rpName` (optional)**

`string | undefined`

The passkey's relying-party name, shown in the passkey prompt. Default: the prefix.

**`hkdfSalt` (optional)**

`string | undefined`

HKDF salt for the passkey-derived key. Default `<prefix>/account-key/v1`.

**`backupPrefix` (optional)**

`string | undefined`

Backup code prefix. Default `<prefix>-key-1-`.

**`pbkdf2Rounds` (optional)**

`number | undefined`

PBKDF2 rounds for new passphrase locks. Default 600,000.

**`didOf` (optional)**

`((seed: Uint8Array) => string) | undefined`

The account's DID from its seed. Default: the trust core's did:key.

**`storage` (optional)**

`Storage | undefined`

Default: `localStorage`.

**`credentials` (optional)**

`Pick<CredentialsContainer, "create" | "get"> | undefined`

Default: `navigator.credentials`.

**`rpId` (optional)**

`string | undefined`

The relying-party id. Default: `location.hostname`.

**`indexedDB` (optional)**

`IDBFactory | undefined`

Where CryptoKeys are kept. Default: `indexedDB`.

### Returns

`{ prefix: string; accounts: () => Account[]; removeAccount: (id: string) => Promise<void>; createAccount: (name: string, how: Protection, seed?: Uint8Array) => Promise<Unlocked>; unlock: (a: Account, passphrase?: string) => Promise<Unlocked>; unlockedAccount: (id: string) => Unlocked | undefined; protect: (u: Unlocked, how: Protection) => Promise<Unlocked>; lockAll: () => void; passkeysAvailable: () => boolean; readBlob: (u: Unlocked, name: string) => Promise<Uint8Array | null>; writeBlob: (u: Unlocked, name: string, bytes: Uint8Array) => Promise<void>; readJson: <T>(u: Unlocked, name: string) => Promise<T | null>; writeJson: (u: Unlocked, name: string, value: unknown) => Promise<void>; removeBlob: (a: Account, name: string) => void; readState: <T>(u: Unlocked) => Promise<T | null>; writeState: (u: Unlocked, value: unknown) => Promise<void>; putCryptoKey: (a: Account, name: string, key: CryptoKey) => Promise<void>; getCryptoKey: (a: Account, name: string) => Promise<CryptoKey | null>; backupCode: (u: Unlocked) => string; seedFromBackup: (code: string) => Uint8Array | null; storageKey: (name: string, did: string) => string; }`

Account, sealed-blob, backup, and IndexedDB CryptoKey methods. Call `lockAll()` when locking the UI.

## Returned object

The returned object exposes these properties and methods.

## Properties

- **`prefix` (required)**: `string`

## Methods

### `accounts`

Signature:

```ts
accounts(): Account[];
```

No parameters.

#### Returns

`Account[]`

Stored accounts, or an empty list when the account record cannot be read or decoded.

### `removeAccount`

Signature:

```ts
removeAccount(id: string): Promise<void>;
```

#### Parameters

**`id` (required)**

`string`

Saved account ID to remove; an unknown ID is a no-op.

#### Returns

`Promise<void>`

Completion of attempted CryptoKey deletion. IndexedDB cleanup failures are swallowed.

### `createAccount`

Signature:

```ts
createAccount(name: string, how: Protection, seed?: Uint8Array): Promise<Unlocked>;
```

#### Parameters

**`name` (required)**

`string`

Display name, trimmed and limited to 60 characters.

**`how` (required)**

`Protection`

Passkey protection or a passphrase of at least eight characters.

**`seed` (optional)**

`Uint8Array<ArrayBufferLike>`

Identity seed to restore, normally decoded with `seedFromBackup`; defaults to 32 random bytes.

Default: `random(32)`

#### Returns

`Promise<Unlocked>`

The account metadata, plaintext seed, and derived sealing key.

### `unlock`

Signature:

```ts
unlock(a: Account, passphrase?: string): Promise<Unlocked>;
```

#### Parameters

**`a` (required)**

`Account`

Saved account metadata from `accounts()`.

**`passphrase` (optional)**

`string | undefined`

Required to decrypt a passphrase-protected account; unused for passkey protection.

#### Returns

`Promise<Unlocked>`

The account's plaintext seed and sealing key, also retained in the unlocked cache.

### `unlockedAccount`

Signature:

```ts
unlockedAccount(id: string): Unlocked | undefined;
```

#### Parameters

**`id` (required)**

`string`

Account ID to look up.

#### Returns

`Unlocked | undefined`

### `protect`

Signature:

```ts
protect(u: Unlocked, how: Protection): Promise<Unlocked>;
```

#### Parameters

**`u` (required)**

`Unlocked`

Unlocked account whose seed will be protected.

**`how` (required)**

`Protection`

New passkey or passphrase protection.

#### Returns

`Promise<Unlocked>`

An updated saved account and cached unlocked handle with its sealing key.

### `lockAll`

Signature:

```ts
lockAll(): void;
```

No parameters.

#### Returns

`void`

### `passkeysAvailable`

Signature:

```ts
passkeysAvailable(): boolean;
```

No parameters.

#### Returns

`boolean`

### `readBlob`

Signature:

```ts
readBlob(u: Unlocked, name: string): Promise<Uint8Array | null>;
```

#### Parameters

**`u` (required)**

`Unlocked`

Protected, unlocked account whose key decrypts the blob.

**`name` (required)**

`string`

Lowercase letters, digits, and dashes; `seed`, `accounts`, and `keys` are reserved.

#### Returns

`Promise<Uint8Array<ArrayBufferLike> | null>`

Decrypted bytes, or `null` when the blob is absent.

### `writeBlob`

Signature:

```ts
writeBlob(u: Unlocked, name: string, bytes: Uint8Array): Promise<void>;
```

#### Parameters

**`u` (required)**

`Unlocked`

Protected, unlocked account whose key seals the blob.

**`name` (required)**

`string`

Valid nonreserved blob name, as required by `readBlob`.

**`bytes` (required)**

`Uint8Array<ArrayBufferLike>`

Plaintext bytes to save.

#### Returns

`Promise<void>`

Completion of encryption and storage.

### `readJson`

Signature:

```ts
readJson<T>(u: Unlocked, name: string): Promise<T | null>;
```

#### Parameters

**`u` (required)**

`Unlocked`

Unlocked account; a sealing key is required when the stored value is encrypted.

**`name` (required)**

`string`

Valid nonreserved blob name.

#### Returns

`Promise<T | null>`

The parsed value, or `null` when absent. The generic type does not validate stored data.

### `writeJson`

Signature:

```ts
writeJson(u: Unlocked, name: string, value: unknown): Promise<void>;
```

#### Parameters

**`u` (required)**

`Unlocked`

Unlocked account; call `protect` first when encryption is required.

**`name` (required)**

`string`

Valid nonreserved blob name.

**`value` (required)**

`unknown`

JSON-serializable value to save.

#### Returns

`Promise<void>`

Completion of serialization, optional encryption, and storage.

### `removeBlob`

Signature:

```ts
removeBlob(a: Account, name: string): void;
```

#### Parameters

- **`a` (required)**: `Account`
- **`name` (required)**: `string`

#### Returns

`void`

### `readState`

Signature:

```ts
readState<T>(u: Unlocked): Promise<T | null>;
```

#### Parameters

**`u` (required)**

`Unlocked`

Unlocked account whose state is requested.

#### Returns

`Promise<T | null>`

Parsed state, or `null` when missing, unreadable, or unavailable without a sealing key.

### `writeState`

Signature:

```ts
writeState(u: Unlocked, value: unknown): Promise<void>;
```

#### Parameters

- **`u` (required)**: `Unlocked`
- **`value` (required)**: `unknown`

#### Returns

`Promise<void>`

### `putCryptoKey`

Signature:

```ts
putCryptoKey(a: Account, name: string, key: CryptoKey): Promise<void>;
```

#### Parameters

**`a` (required)**

`Account`

Account that owns the key.

**`name` (required)**

`string`

Valid nonreserved key name.

**`key` (required)**

`CryptoKey`

Browser CryptoKey to persist.

#### Returns

`Promise<void>`

Completion of the IndexedDB write transaction.

### `getCryptoKey`

Signature:

```ts
getCryptoKey(a: Account, name: string): Promise<CryptoKey | null>;
```

#### Parameters

**`a` (required)**

`Account`

Account that owns the key.

**`name` (required)**

`string`

Valid nonreserved key name.

#### Returns

`Promise<CryptoKey | null>`

The stored key, or `null` when absent.

### `backupCode`

Signature:

```ts
backupCode(u: Unlocked): string;
```

#### Parameters

**`u` (required)**

`Unlocked`

Unlocked account to back up.

#### Returns

`string`

### `seedFromBackup`

Signature:

```ts
seedFromBackup(code: string): Uint8Array | null;
```

#### Parameters

**`code` (required)**

`string`

Backup text, with optional surrounding whitespace.

#### Returns

`Uint8Array<ArrayBufferLike> | null`

A 32-byte seed, or `null` for another prefix or invalid seed encoding/length.

### `storageKey`

The storage key of a blob, for data an app keeps outside the vault but wants removed with the account.

Signature:

```ts
storageKey(name: string, did: string): string;
```

#### Parameters

- **`name` (required)**: `string`
- **`did` (required)**: `string`

#### Returns

`string`

## Throws

If the storage prefix contains unsupported characters or is empty.

[Usage guide](https://toip.tools/toolkit/web-vault/#usage) · [Error handling](https://toip.tools/toolkit/web-vault/#errors) · [All exports](https://toip.tools/toolkit/web-vault/api/)

---

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