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 sourceimport { createVault } from "@toip-tools/web-vault";
Declaration
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
configrequiredVaultConfigApp-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.
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
prefixrequiredstring
Methods
accounts
accounts(): Account[];
No parameters.
Returns
Account[]Stored accounts, or an empty list when the account record cannot be read or decoded.
removeAccount
removeAccount(id: string): Promise<void>;
Parameters
idrequiredstringSaved 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
createAccount(name: string, how: Protection, seed?: Uint8Array): Promise<Unlocked>;
Parameters
namerequiredstringDisplay name, trimmed and limited to 60 characters.
howrequiredProtectionPasskey protection or a passphrase of at least eight characters.
seedoptionalUint8Array<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
unlock(a: Account, passphrase?: string): Promise<Unlocked>;
Parameters
arequiredAccountSaved account metadata from
accounts().passphraseoptionalstring | undefinedRequired 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
unlockedAccount(id: string): Unlocked | undefined;
Parameters
idrequiredstringAccount ID to look up.
Returns
Unlocked | undefinedprotect
protect(u: Unlocked, how: Protection): Promise<Unlocked>;
Parameters
urequiredUnlockedUnlocked account whose seed will be protected.
howrequiredProtectionNew passkey or passphrase protection.
Returns
Promise<Unlocked>An updated saved account and cached unlocked handle with its sealing key.
lockAll
lockAll(): void;
No parameters.
Returns
voidpasskeysAvailable
passkeysAvailable(): boolean;
No parameters.
Returns
booleanreadBlob
readBlob(u: Unlocked, name: string): Promise<Uint8Array | null>;
Parameters
urequiredUnlockedProtected, unlocked account whose key decrypts the blob.
namerequiredstringLowercase letters, digits, and dashes;
seed,accounts, andkeysare reserved.
Returns
Promise<Uint8Array<ArrayBufferLike> | null>Decrypted bytes, or null when the blob is absent.
writeBlob
writeBlob(u: Unlocked, name: string, bytes: Uint8Array): Promise<void>;
Parameters
urequiredUnlockedProtected, unlocked account whose key seals the blob.
namerequiredstringValid nonreserved blob name, as required by
readBlob.bytesrequiredUint8Array<ArrayBufferLike>Plaintext bytes to save.
Returns
Promise<void>Completion of encryption and storage.
readJson
readJson<T>(u: Unlocked, name: string): Promise<T | null>;
Parameters
urequiredUnlockedUnlocked account; a sealing key is required when the stored value is encrypted.
namerequiredstringValid nonreserved blob name.
Returns
Promise<T | null>The parsed value, or null when absent. The generic type does not validate stored data.
writeJson
writeJson(u: Unlocked, name: string, value: unknown): Promise<void>;
Parameters
urequiredUnlockedUnlocked account; call
protectfirst when encryption is required.namerequiredstringValid nonreserved blob name.
valuerequiredunknownJSON-serializable value to save.
Returns
Promise<void>Completion of serialization, optional encryption, and storage.
removeBlob
removeBlob(a: Account, name: string): void;
Parameters
arequiredAccountnamerequiredstring
Returns
voidreadState
readState<T>(u: Unlocked): Promise<T | null>;
Parameters
urequiredUnlockedUnlocked account whose state is requested.
Returns
Promise<T | null>Parsed state, or null when missing, unreadable, or unavailable without a sealing key.
writeState
writeState(u: Unlocked, value: unknown): Promise<void>;
Parameters
urequiredUnlockedvaluerequiredunknown
Returns
Promise<void>putCryptoKey
putCryptoKey(a: Account, name: string, key: CryptoKey): Promise<void>;
Parameters
arequiredAccountAccount that owns the key.
namerequiredstringValid nonreserved key name.
keyrequiredCryptoKeyBrowser CryptoKey to persist.
Returns
Promise<void>Completion of the IndexedDB write transaction.
getCryptoKey
getCryptoKey(a: Account, name: string): Promise<CryptoKey | null>;
Parameters
arequiredAccountAccount that owns the key.
namerequiredstringValid nonreserved key name.
Returns
Promise<CryptoKey | null>The stored key, or null when absent.
backupCode
backupCode(u: Unlocked): string;
Parameters
urequiredUnlockedUnlocked account to back up.
Returns
stringseedFromBackup
seedFromBackup(code: string): Uint8Array | null;
Parameters
coderequiredstringBackup text, with optional surrounding whitespace.
Returns
Uint8Array<ArrayBufferLike> | nullA 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.
storageKey(name: string, did: string): string;
Parameters
namerequiredstringdidrequiredstring
Returns
stringThrows
If the storage prefix contains unsupported characters or is empty.