UNPKG

@metamask/account-api

Version:
59 lines 2.21 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.selectOne = selectOne; exports.select = select; const keyring_utils_1 = require("@metamask/keyring-utils"); const internal_1 = require("./internal/index.cjs"); /** * 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. */ 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. */ 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 = (0, internal_1.areBothEmpty)(selector.methods, account.methods) || selector.methods.some((method) => account.methods.includes(method))); } if (selector.scopes !== undefined) { selected && (selected = (0, internal_1.areBothEmpty)(selector.scopes, account.scopes) || selector.scopes.some((scope) => { return ( // This will cover specific EVM EOA scopes as well. (0, keyring_utils_1.isScopeEqualToAny)(scope, account.scopes)); })); } return selected; }); } //# sourceMappingURL=selector.cjs.map