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AmBF;AAzFD,kDAyFC;6HA/EG,SAA+C,GAAG,EAAE,CAAC,IAAI;IAEzD,MAAM,QAAQ,GAAmC,EAAE,CAAC;IAEpD,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI;IACvC,2EAA2E;IAC3E,yEAAyE;IACzE,kDAAkD;IAClD,2CAA2C,CAC5C,EAAE;QACD,IACE,IAAA,4BAAc,EAAC,OAAO,CAAC;YACvB,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,EACf;YACA,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;SACxB;KACF;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import {\n  isBip44Account,\n  type AccountProvider,\n  type Bip44Account,\n} from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type {\n  KeyringMetadata,\n  KeyringSelector,\n} from '@metamask/keyring-controller';\n\nimport type { MultichainAccountServiceMessenger } from '../types';\n\n/**\n * Asserts a keyring account is BIP-44 compatible.\n *\n * @param account - Keyring account to check.\n * @throws If the keyring account is not compatible.\n */\nexport function assertIsBip44Account(\n  account: KeyringAccount,\n): asserts account is Bip44Account<KeyringAccount> {\n  if (!isBip44Account(account)) {\n    throw new Error('Created account is not BIP-44 compatible');\n  }\n}\n\n/**\n * Asserts that a list of keyring accounts are all BIP-44 compatible.\n *\n * @param accounts - Keyring accounts to check.\n * @throws If any of the keyring account is not compatible.\n */\nexport function assertAreBip44Accounts(\n  accounts: KeyringAccount[],\n): asserts accounts is Bip44Account<KeyringAccount>[] {\n  accounts.forEach(assertIsBip44Account);\n}\n\nexport abstract class BaseAccountProvider\n  implements AccountProvider<Bip44Account<KeyringAccount>>\n{\n  protected readonly messenger: MultichainAccountServiceMessenger;\n\n  constructor(messenger: MultichainAccountServiceMessenger) {\n    this.messenger = messenger;\n  }\n\n  #getAccounts(\n    filter: (account: KeyringAccount) => boolean = () => true,\n  ): Bip44Account<KeyringAccount>[] {\n    const accounts: Bip44Account<KeyringAccount>[] = [];\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        isBip44Account(account) &&\n        this.isAccountCompatible(account) &&\n        filter(account)\n      ) {\n        accounts.push(account);\n      }\n    }\n\n    return accounts;\n  }\n\n  getAccounts(): Bip44Account<KeyringAccount>[] {\n    return this.#getAccounts();\n  }\n\n  getAccount(\n    id: Bip44Account<KeyringAccount>['id'],\n  ): Bip44Account<KeyringAccount> {\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  protected async withKeyring<SelectedKeyring, CallbackResult = void>(\n    selector: KeyringSelector,\n    operation: ({\n      keyring,\n      metadata,\n    }: {\n      keyring: SelectedKeyring;\n      metadata: KeyringMetadata;\n    }) => Promise<CallbackResult>,\n  ): Promise<CallbackResult> {\n    const result = await this.messenger.call(\n      'KeyringController:withKeyring',\n      selector,\n      ({ keyring, metadata }) =>\n        operation({\n          keyring: keyring as SelectedKeyring,\n          metadata,\n        }),\n    );\n\n    return result as CallbackResult;\n  }\n\n  abstract isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;\n\n  abstract createAccounts({\n    entropySource,\n    groupIndex,\n  }: {\n    entropySource: EntropySourceId;\n    groupIndex: number;\n  }): Promise<Bip44Account<KeyringAccount>[]>;\n\n  abstract discoverAndCreateAccounts({\n    entropySource,\n    groupIndex,\n  }: {\n    entropySource: EntropySourceId;\n    groupIndex: number;\n  }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n"]}