UNPKG

@metamask/account-api

Version:
55 lines 2.04 kB
import { isScopeEqualToAny } from "@metamask/keyring-utils"; import { areBothEmpty } from "./internal/index.mjs"; /** * Query an account matching the selector. * * @param accounts - List of accounts to select from. * @param selector - Query selector. * @returns The account matching the selector or undefined if not matching. * @throws If multiple accounts match the selector. */ export function selectOne(accounts, selector) { const matched = select(accounts, selector); if (matched.length > 1) { throw new Error(`Too many account candidates, expected 1, got: ${matched.length}`); } if (matched.length === 0) { return undefined; } return matched[0]; // This is safe, see checks above. } /** * Query accounts matching the selector. * * @param accounts - List of accounts to select from. * @param selector - Query selector. * @returns The accounts matching the selector. */ export function select(accounts, selector) { return accounts.filter((account) => { let selected = true; if (selector.id) { selected && (selected = account.id === selector.id); } if (selector.address) { selected && (selected = account.address === selector.address); } if (selector.type) { selected && (selected = account.type === selector.type); } if (selector.methods !== undefined) { selected && (selected = areBothEmpty(selector.methods, account.methods) || selector.methods.some((method) => account.methods.includes(method))); } if (selector.scopes !== undefined) { selected && (selected = areBothEmpty(selector.scopes, account.scopes) || selector.scopes.some((scope) => { return ( // This will cover specific EVM EOA scopes as well. isScopeEqualToAny(scope, account.scopes)); })); } return selected; }); } //# sourceMappingURL=selector.mjs.map