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SAAS,EAAE,CAAC;YACnC,QAAQ,KAAR,QAAQ,GACN,YAAY,CAAC,QAAQ,CAAC,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC;gBAC/C,QAAQ,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAC,CAAC,EAAC;QACxE,CAAC;QACD,IAAI,QAAQ,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YAClC,QAAQ,KAAR,QAAQ,GACN,YAAY,CAAC,QAAQ,CAAC,MAAM,EAAE,OAAO,CAAC,MAAM,CAAC;gBAC7C,QAAQ,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE;oBAC7B,OAAO;oBACL,mDAAmD;oBACnD,iBAAiB,CAAC,KAAK,EAAE,OAAO,CAAC,MAAM,CAAC,CACzC,CAAC;gBACJ,CAAC,CAAC,EAAC;QACP,CAAC;QAED,OAAO,QAAQ,CAAC;IAClB,CAAC,CAAC,CAAC;AACL,CAAC","sourcesContent":["import type { KeyringAccount } from '@metamask/keyring-api';\nimport { isScopeEqualToAny } from '@metamask/keyring-utils';\n\nimport { areBothEmpty } from './internal';\n\n/**\n * Selector to query a specific account based on some criteria.\n */\nexport type AccountSelector<Account extends KeyringAccount> = {\n /**\n * Query by account ID.\n */\n id?: Account['id'];\n\n /**\n * Query by account address.\n */\n address?: Account['address'];\n\n /**\n * Query by account type.\n */\n type?: Account['type'];\n\n /**\n * Query by account methods.\n */\n methods?: Account['methods'];\n\n /**\n * Query by account scopes.\n */\n scopes?: Account['scopes'];\n};\n\n/**\n * Query an account matching the selector.\n *\n * @param accounts - List of accounts to select from.\n * @param selector - Query selector.\n * @returns The account matching the selector or undefined if not matching.\n * @throws If multiple accounts match the selector.\n */\nexport function selectOne<Account extends KeyringAccount>(\n accounts: Account[],\n selector: AccountSelector<Account>,\n): Account | undefined {\n const matched = select(accounts, selector);\n\n if (matched.length > 1) {\n throw new Error(\n `Too many account candidates, expected 1, got: ${matched.length}`,\n );\n }\n\n if (matched.length === 0) {\n return undefined;\n }\n\n return matched[0]; // This is safe, see checks above.\n}\n\n/**\n * Query accounts matching the selector.\n *\n * @param accounts - List of accounts to select from.\n * @param selector - Query selector.\n * @returns The accounts matching the selector.\n */\nexport function select<Account extends KeyringAccount>(\n accounts: Account[],\n selector: AccountSelector<Account>,\n): Account[] {\n return accounts.filter((account) => {\n let selected = true;\n\n if (selector.id) {\n selected &&= account.id === selector.id;\n }\n if (selector.address) {\n selected &&= account.address === selector.address;\n }\n if (selector.type) {\n selected &&= account.type === selector.type;\n }\n if (selector.methods !== undefined) {\n selected &&=\n areBothEmpty(selector.methods, account.methods) ||\n selector.methods.some((method) => account.methods.includes(method));\n }\n if (selector.scopes !== undefined) {\n selected &&=\n areBothEmpty(selector.scopes, account.scopes) ||\n selector.scopes.some((scope) => {\n return (\n // This will cover specific EVM EOA scopes as well.\n isScopeEqualToAny(scope, account.scopes)\n );\n });\n }\n\n return selected;\n });\n}\n"]}