toip.tools

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-vaultView source
TypeScriptImport
import { createVault } from "@toip-tools/web-vault";

Declaration

TypeScriptSignature
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

configrequired
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
prefixrequired
string

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

rpNameoptional
string | undefined

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

hkdfSaltoptional
string | undefined

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

backupPrefixoptional
string | undefined

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

pbkdf2Roundsoptional
number | undefined

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

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

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

storageoptional
Storage | undefined

Default: localStorage.

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

Default: navigator.credentials.

rpIdoptional
string | undefined

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

indexedDBoptional
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

prefixrequired
string

Methods

accounts

TypeScriptSignature
accounts(): Account[];

No parameters.

Returns

Account[]

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

removeAccount

TypeScriptSignature
removeAccount(id: string): Promise<void>;

Parameters

idrequired
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

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

Parameters

namerequired
string

Display name, trimmed and limited to 60 characters.

howrequired
Protection

Passkey protection or a passphrase of at least eight characters.

seedoptional
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

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

Parameters

arequired
Account

Saved account metadata from accounts().

passphraseoptional
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

TypeScriptSignature
unlockedAccount(id: string): Unlocked | undefined;

Parameters

idrequired
string

Account ID to look up.

Returns

Unlocked | undefined

protect

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

Parameters

urequired
Unlocked

Unlocked account whose seed will be protected.

howrequired
Protection

New passkey or passphrase protection.

Returns

Promise<Unlocked>

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

lockAll

TypeScriptSignature
lockAll(): void;

No parameters.

Returns

void

passkeysAvailable

TypeScriptSignature
passkeysAvailable(): boolean;

No parameters.

Returns

boolean

readBlob

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

Parameters

urequired
Unlocked

Protected, unlocked account whose key decrypts the blob.

namerequired
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

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

Parameters

urequired
Unlocked

Protected, unlocked account whose key seals the blob.

namerequired
string

Valid nonreserved blob name, as required by readBlob.

bytesrequired
Uint8Array<ArrayBufferLike>

Plaintext bytes to save.

Returns

Promise<void>

Completion of encryption and storage.

readJson

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

Parameters

urequired
Unlocked

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

namerequired
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

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

Parameters

urequired
Unlocked

Unlocked account; call protect first when encryption is required.

namerequired
string

Valid nonreserved blob name.

valuerequired
unknown

JSON-serializable value to save.

Returns

Promise<void>

Completion of serialization, optional encryption, and storage.

removeBlob

TypeScriptSignature
removeBlob(a: Account, name: string): void;

Parameters

arequired
Account
namerequired
string

Returns

void

readState

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

Parameters

urequired
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

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

Parameters

urequired
Unlocked
valuerequired
unknown

Returns

Promise<void>

putCryptoKey

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

Parameters

arequired
Account

Account that owns the key.

namerequired
string

Valid nonreserved key name.

keyrequired
CryptoKey

Browser CryptoKey to persist.

Returns

Promise<void>

Completion of the IndexedDB write transaction.

getCryptoKey

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

Parameters

arequired
Account

Account that owns the key.

namerequired
string

Valid nonreserved key name.

Returns

Promise<CryptoKey | null>

The stored key, or null when absent.

backupCode

TypeScriptSignature
backupCode(u: Unlocked): string;

Parameters

urequired
Unlocked

Unlocked account to back up.

Returns

string

seedFromBackup

TypeScriptSignature
seedFromBackup(code: string): Uint8Array | null;

Parameters

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

TypeScriptSignature
storageKey(name: string, did: string): string;

Parameters

namerequired
string
didrequired
string

Returns

string

Throws

If the storage prefix contains unsupported characters or is empty.