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AM,CAAC,OAAO,CAAC,EACf;YACA,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;SACxB;KACF;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import type { AccountProvider } from '@metamask/account-api';\nimport type { AccountId } from '@metamask/accounts-controller';\nimport type {\n  KeyringAccount,\n  KeyringAccountEntropyMnemonicOptions,\n} from '@metamask/keyring-api';\nimport { KeyringAccountEntropyTypeOption } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport type Bip44Account<Account extends KeyringAccount> = Account & {\n  options: {\n    entropy: KeyringAccountEntropyMnemonicOptions;\n  };\n};\n\n/**\n * Checks if an account is BIP-44 compatible.\n *\n * @param account - The account to be tested.\n * @returns True if the account is BIP-44 compatible.\n */\nexport function isBip44Account<Account extends KeyringAccount>(\n  account: Account,\n): account is Bip44Account<Account> {\n  if (\n    !account.options.entropy ||\n    account.options.entropy.type !== KeyringAccountEntropyTypeOption.Mnemonic\n  ) {\n    console.warn(\n      \"! Found an HD account with invalid entropy options: account won't be associated to its wallet.\",\n    );\n    return false;\n  }\n\n  return true;\n}\n\nexport abstract class BaseAccountProvider\n  implements AccountProvider<InternalAccount>\n{\n  protected readonly messenger: MultichainAccountServiceMessenger;\n\n  constructor(messenger: MultichainAccountServiceMessenger) {\n    this.messenger = messenger;\n  }\n\n  #getAccounts(\n    filter: (account: InternalAccount) => boolean = () => true,\n  ): Bip44Account<InternalAccount>[] {\n    const accounts: Bip44Account<InternalAccount>[] = [];\n\n    for (const account of this.messenger.call(\n      // NOTE: Even though the name is misleading, this only fetches all internal\n      // accounts, including EVM and non-EVM. We might wanna change this action\n      // name once we fully support multichain accounts.\n      'AccountsController:listMultichainAccounts',\n    )) {\n      if (\n        this.isAccountCompatible(account) &&\n        isBip44Account(account) &&\n        filter(account)\n      ) {\n        accounts.push(account);\n      }\n    }\n\n    return accounts;\n  }\n\n  getAccounts(): InternalAccount[] {\n    return this.#getAccounts();\n  }\n\n  getAccount(id: AccountId): InternalAccount {\n    // TODO: Maybe just use a proper find for faster lookup?\n    const [found] = this.#getAccounts((account) => account.id === id);\n\n    if (!found) {\n      throw new Error(`Unable to find account: ${id}`);\n    }\n\n    return found;\n  }\n\n  abstract isAccountCompatible(account: InternalAccount): boolean;\n}\n"]}