type · Browser

# Vault

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

Import:

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

## Declaration

type:

```ts
type Vault = ReturnType<typeof createVault>;
```

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

[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/Vault/. An index of the documentation for AI assistants is at https://toip.tools/llms.txt.
