Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.
/**
* Typed wrapper around the AliasVault Rust core (shared with iOS and Android).
*
* Algorithms (URL matching, credential filtering, domain extraction, the vault codec) live in `core/rust`.
*/
import { resolveDefaultLanguage } from '@aliasvault/i18n/languages';
import { FieldKey } from '@aliasvault/models/vault';
import { getPlatform } from '../platform/ClientPlatform';
import { deviceLanguage } from '../platform/DeviceLanguage';
import { AutofillMatchingMode } from './RustCoreTypes';
import type { IRustCore } from './RustCoreBinding';
import type { AccountKeyHierarchy, FaviconTarget, IdentityNameInput, IdentityRequest, KeyChainOpenResult, ParsedEmail, PinFailure, ReencryptedAccountKey } from './RustCoreTypes';
import type { Identity } from '@aliasvault/models/identity';
import type { Item, PasswordSettings } from '@aliasvault/models/vault';
export { AutofillMatchingMode } from './RustCoreTypes';
export type { FaviconTarget, IdentityNameInput, IdentityRequest, ParsedEmail, ParsedEmailAttachment, PinFailure } from './RustCoreTypes';
/**
* The host's Rust core binding.
*/
export function rustCore(): IRustCore {
return getPlatform().rustCore;
}
/**
* Extract the host (subdomain + domain) from a URL.
* Example: `https://www.example.com/path` > `example.com`.
* Returns empty string for inputs the Rust extractor rejects, e.g.
* reversed-TLD app bundle identifiers like `com.example.app`.
*/
export async function extractDomain(url: string): Promise<string> {
return rustCore().extractDomain(url);
}
/**
* Extract the root domain.
* Example: `sub.example.co.uk` > `example.co.uk`.
*/
export async function extractRootDomain(domain: string): Promise<string> {
return rustCore().extractRootDomain(domain);
}
/**
* Whether a page on `host` may use `rpId` as its WebAuthn RP ID (the host or a parent that is not a public suffix).
*/
export async function isRpIdAllowedForHost(rpId: string, host: string): Promise<boolean> {
return rustCore().isRpIdAllowedForHost(rpId, host);
}
/**
* Whether `callerOrigin` is listed in `origins` from an RP's `/.well-known/webauthn` file.
*/
export async function isRelatedOriginAllowed(callerOrigin: string, origins: string[]): Promise<boolean> {
return rustCore().isRelatedOriginAllowed(callerOrigin, origins);
}
/**
* Read a URL field value as an ordered list, accepting the single-string and multi-value
* shapes a field can hold.
*/
export function toUrlList(urlValue: string | string[] | undefined | null): string[] {
if (!urlValue) {
return [];
}
const urlList = Array.isArray(urlValue) ? urlValue : [urlValue];
return urlList.filter((url): url is string => Boolean(url?.trim()));
}
/**
* Pick which of an item's URLs a favicon comes from, and the key to store it under.
* Returns null when no URL qualifies.
*/
export async function selectFaviconTarget(urls: string[]): Promise<FaviconTarget | null> {
return rustCore().selectFaviconTarget(urls);
}
/**
* Generate a password or passphrase from the given settings.
*/
export async function generatePassword(settings: PasswordSettings, seed?: string): Promise<string> {
const effective = await applyEffectiveDicewareLanguage(settings);
const payload = seed ? { ...effective, Seed: seed } : effective;
return rustCore().generatePassword(JSON.stringify(payload));
}
/**
* Resolve the effective Diceware passphrase language when none is explicitly chosen.
*/
async function applyEffectiveDicewareLanguage(settings: PasswordSettings): Promise<PasswordSettings> {
if (settings.Type !== 'diceware' || (settings.Language && settings.Language.trim().length > 0)) {
return settings;
}
const codes = await getDicewareLanguages();
return { ...settings, Language: resolveDefaultLanguage(deviceLanguage(), codes) };
}
/**
* Get the list of bundled Diceware wordlist language ISO codes (first is the default, 'en').
*/
export async function getDicewareLanguages(): Promise<string[]> {
const languages = await rustCore().getDicewareLanguages();
return languages.length > 0 ? languages : ['en'];
}
/**
* Generate a random identity (alias persona) in the Rust core.
* Returns the identity with a yyyy-MM-dd birth date string.
*/
export async function generateIdentity(request: IdentityRequest): Promise<Identity> {
return JSON.parse(await rustCore().generateIdentity(JSON.stringify(request))) as Identity;
}
/**
* Generate a username from persona name fields (alphanumeric, 6-20 characters).
*/
export async function generateIdentityUsername(input: IdentityNameInput): Promise<string> {
return rustCore().generateIdentityUsername(JSON.stringify(input));
}
/**
* Generate an email prefix from persona name fields (6-20 characters).
*/
export async function generateIdentityEmailPrefix(input: IdentityNameInput): Promise<string> {
return rustCore().generateIdentityEmailPrefix(JSON.stringify(input));
}
/**
* Generate a random alphanumeric email prefix that is not based on any identity.
* Used for login-type credentials where no persona fields are available.
*/
export async function generateRandomEmailPrefix(length: number = 14): Promise<string> {
return rustCore().generateRandomEmailPrefix(length);
}
/**
* Get the list of bundled identity dictionary language ISO codes.
* The set is owned by the Rust core; unknown codes fall back to English during generation.
*/
export async function getIdentityLanguages(): Promise<string[]> {
const languages = await rustCore().getIdentityLanguages();
return languages.length > 0 ? languages : ['en'];
}
/**
* Get the list of identity age range option values ('random' plus 5-year ranges).
*/
export async function getIdentityAgeRanges(): Promise<string[]> {
return rustCore().getIdentityAgeRanges();
}
/**
* Parse a raw RFC 822 email source into its html/plain bodies and attachment metadata.
*/
export async function parseEmailSource(source: Uint8Array): Promise<ParsedEmail> {
return rustCore().parseEmailSource(source);
}
/**
* Turn a stored email source into the raw RFC 822 message bytes for showing the message source without parsing it.
*/
export async function decodeEmailSource(source: Uint8Array): Promise<Uint8Array> {
return rustCore().decodeEmailSource(source);
}
/**
* Extract the decoded bytes of one attachment, identified by its index in the parsed attachment list.
*/
export async function extractEmailAttachment(source: Uint8Array, index: number, detachedBody?: Uint8Array): Promise<Uint8Array> {
return rustCore().extractEmailAttachment(source, index, detachedBody);
}
/**
* A fresh random salt for a new PIN wrap.
*/
export async function pinGenerateSalt(): Promise<Uint8Array> {
return rustCore().pinGenerateSalt();
}
/**
* The salt the browser extension derives its PIN key with, from the stored random salt and the extension id.
*/
export async function pinExtensionSalt(salt: Uint8Array, extensionId: string): Promise<Uint8Array> {
return rustCore().pinExtensionSalt(salt, extensionId);
}
/**
* Encrypt `secret` with a key derived from the PIN and salt, as `IV | ciphertext | tag`.
*/
export async function pinEncrypt(pin: string, salt: Uint8Array, secret: Uint8Array): Promise<Uint8Array> {
return rustCore().pinEncrypt(pin, salt, secret);
}
/**
* Decrypt a PIN wrap. Rejects for a wrong PIN.
*/
export async function pinDecrypt(pin: string, salt: Uint8Array, encrypted: Uint8Array): Promise<Uint8Array> {
return rustCore().pinDecrypt(pin, salt, encrypted);
}
/**
* Whether a stored failed-attempt count means the PIN is locked.
*/
export async function pinIsLocked(failedAttempts: number): Promise<boolean> {
return rustCore().pinIsLocked(failedAttempts);
}
/**
* The counter state after one more failed PIN attempt on top of the stored count.
*/
export async function pinRegisterFailure(failedAttempts: number): Promise<PinFailure> {
return rustCore().pinRegisterFailure(failedAttempts);
}
/**
* Derive a 32-byte key from a password using Argon2id.
*/
export async function argon2DeriveKey(password: string, salt: string, encryptionSettings: string): Promise<Uint8Array> {
return rustCore().argon2DeriveKey(password, salt, encryptionSettings);
}
/**
* Open a key chain with the unlock key (base64), the password's Argon2id output. Never accepts the Account Key.
*/
export async function openKeyChainWithUnlockKey(unlockKey: string, encryptedAccountKey: string, encryptedVek: string, encryptedAccountPrivateKey: string | null): Promise<KeyChainOpenResult> {
return rustCore().openKeyChainWithUnlockKey(unlockKey, encryptedAccountKey, encryptedVek, encryptedAccountPrivateKey);
}
/**
* Open a key chain with the Account Key (base64). Never accepts an unlock key.
*/
export async function openKeyChainWithAccountKey(accountKey: string, encryptedVek: string, encryptedAccountPrivateKey: string | null): Promise<KeyChainOpenResult> {
return rustCore().openKeyChainWithAccountKey(accountKey, encryptedVek, encryptedAccountPrivateKey);
}
/**
* Create a new account key hierarchy for the unlock key (base64).
*/
export async function createAccountKeyHierarchy(unlockKeyBase64: string): Promise<AccountKeyHierarchy> {
return rustCore().createAccountKeyHierarchy(unlockKeyBase64);
}
/**
* Re-encrypt the Account Key from the old to the new unlock key (base64); null when the old unlock key does not open it.
*/
export async function reencryptAccountKey(encryptedAccountKey: string, oldUnlockKeyBase64: string, newUnlockKeyBase64: string): Promise<ReencryptedAccountKey | null> {
return rustCore().reencryptAccountKey(encryptedAccountKey, oldUnlockKeyBase64, newUnlockKeyBase64);
}
/**
* Generate a random 32-byte seed as a 64-character hex string, suitable for the
* `seed` argument of {@link generatePassword}.
*/
export function generateSeed(): string {
const bytes = new Uint8Array(32);
crypto.getRandomValues(bytes);
return Array.from(bytes, (b) => b.toString(16).padStart(2, '0')).join('');
}
/**
* Filter items by URL/title for autofill. Returns at most 3 matches.
*/
export async function filterItems(items: Item[], currentUrl: string, pageTitle: string, matchingMode: AutofillMatchingMode = AutofillMatchingMode.DEFAULT): Promise<Item[]> {
const credentials = items.map(item => ({
id: item.Id,
itemName: item.Name ?? '',
itemUrls: getFieldValues(item, FieldKey.LoginUrl)
}));
const result = await rustCore().filterCredentials({
credentials,
currentUrl,
pageTitle,
matchingMode
});
return result.matchedIds
.map(id => items.find(item => item.Id === id))
.filter((item): item is Item => item !== undefined);
}
/**
* True if `newUrl` is already represented in `existingUrls` under
* host-only comparison (scheme, `www.`, path, query, fragment, trailing
* slash all ignored). Falls back to lowercased exact-match when the Rust
* extractor returns no domain (app bundle identifiers).
*/
export async function isUrlAlreadyLinked(existingUrls: string[], newUrl: string): Promise<boolean> {
const newKey = await urlComparisonKey(newUrl);
if (!newKey) {
return false;
}
const existingKeys = await Promise.all(existingUrls.map(existing => urlComparisonKey(existing)));
return existingKeys.some(key => key === newKey);
}
/**
* Host-only comparison key of a URL: its domain, or the lowercased URL when the extractor yields none.
*/
async function urlComparisonKey(url: string): Promise<string> {
const trimmed = url.trim().toLowerCase();
if (!trimmed) {
return trimmed;
}
const domain = await rustCore().extractDomain(trimmed);
return domain.length > 0 ? domain : trimmed;
}
/*
* Vault codec (manifest-v1 storage format).
*/
/**
* The `Logos.Id` to use for the logo `(kind, source)` inside the manifest with id `manifestId`.
*
* Logo identity is derived: two devices that fetch the same favicon independently produce
* the same row and merge by LWW. The same domain in two different manifests deliberately yields
* two different ids, so a shared manifest's logo and the user's own logo for that domain never
* overwrite each other.
*/
export async function vaultCodecLogoIdFor(manifestId: string, kind: string, source: string): Promise<string> {
return rustCore().vaultCodecLogoIdFor(manifestId, kind, source);
}
/**
* The SHA-256 (lowercase hex) of an uploaded logo's bytes: the `Source` a `custom` logo row is stored
* under, which is what makes picking the same image again reuse the row that already holds it.
*/
export async function vaultCodecLogoContentHash(bytes: Uint8Array): Promise<string> {
return rustCore().vaultCodecLogoContentHash(bytes);
}
/**
* Read all non-empty values for a field key from an item, returning them as
* a string array (single-value fields are wrapped to a 1-element array).
*/
function getFieldValues(item: Item, fieldKey: string): string[] {
const field = item.Fields?.find(f => f.FieldKey === fieldKey);
if (!field) {
return [];
}
if (Array.isArray(field.Value)) {
return field.Value.filter(v => v && v.length > 0);
}
return field.Value ? [field.Value] : [];
}