UNPKG

@metamask/multichain-account-service

Version:
1 lines 8.98 kB
{"version":3,"file":"AccountProviderWrapper.mjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAUA,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AAEtE;;;GAGG;AACH,MAAM,OAAO,sBAAuB,SAAQ,wBAAwB;IAKlE,YACE,SAA4C,EAC5C,QAAkC;QAElC,KAAK,CAAC,SAAS,CAAC,CAAC;QARX,cAAS,GAAY,IAAI,CAAC;QAShC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAEQ,OAAO;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;IACjC,CAAC;IAED,IAAI,YAAY;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC,YAAY,CAAC;IACpC,CAAC;IAED;;;;;OAKG;IACM,IAAI,CAAC,QAA8C;QAC1D,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;IAED;;;;;;;;;;;OAWG;IACH,MAAM;QACJ,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,OAAgB;QACzB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,UAAU;QACR,OAAO,CAAC,IAAI,CAAC,SAAS,CAAC;IACzB,CAAC;IAED;;;;OAIG;IACM,KAAK,CAAC,cAAc,CAC3B,QAAyC;QAEzC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,OAAO;QACT,CAAC;QACD,MAAM,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,QAAQ,CAAC,CAAC;IAC/C,CAAC;IAED;;;;OAIG;IACM,WAAW;QAClB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,OAA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;GAKG;AACH,MAAM,UAAU,wBAAwB,CACtC,QAAiB;IAEjB,OAAO,QAAQ,YAAY,sBAAsB,CAAC;AACpD,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport type {\n CreateAccountOptions,\n EntropySourceId,\n KeyringAccount,\n} from '@metamask/keyring-api';\nimport type { KeyringCapabilities } from '@metamask/keyring-api/v2';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { MultichainAccountServiceMessenger } from '../types';\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\n\n/**\n * A simple wrapper that adds disable functionality to any BaseBip44AccountProvider.\n * When disabled, the provider will not create new accounts and return empty results.\n */\nexport class AccountProviderWrapper extends BaseBip44AccountProvider {\n private isEnabled: boolean = true;\n\n private readonly provider: BaseBip44AccountProvider;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n provider: BaseBip44AccountProvider,\n ) {\n super(messenger);\n this.provider = provider;\n }\n\n override getName(): string {\n return this.provider.getName();\n }\n\n get capabilities(): KeyringCapabilities {\n return this.provider.capabilities;\n }\n\n /**\n * Forward initialization to the wrapped provider to ensure both\n * instances share the same visible account IDs.\n *\n * @param accounts - Account IDs to initialize with.\n */\n override init(accounts: Bip44Account<KeyringAccount>['id'][]): void {\n this.provider.init(accounts);\n }\n\n /**\n * Returns the underlying (unwrapped) provider.\n *\n * Most callers should go through the wrapper's public surface so that the\n * `disabled` gating is respected. This escape hatch is reserved for\n * cleanup flows (e.g. wallet removal) that must see the wrapped\n * provider's accounts regardless of enabled state, so that snap-backed\n * accounts created while the provider was enabled can still be deleted\n * after it has been disabled.\n *\n * @returns The wrapped provider instance.\n */\n unwrap(): BaseBip44AccountProvider {\n return this.provider;\n }\n\n /**\n * Set the enabled state for this provider.\n *\n * @param enabled - Whether the provider should be enabled.\n */\n setEnabled(enabled: boolean): void {\n this.isEnabled = enabled;\n }\n\n /**\n * Check if the provider is disabled.\n *\n * @returns True if the provider is disabled, false otherwise.\n */\n isDisabled(): boolean {\n return !this.isEnabled;\n }\n\n /**\n * Override resyncAccounts to not execute it when disabled.\n *\n * @param accounts - List of local accounts.\n */\n override async resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void> {\n if (!this.isEnabled) {\n return;\n }\n await this.provider.resyncAccounts(accounts);\n }\n\n /**\n * Override getAccounts to return empty array when disabled.\n *\n * @returns Array of accounts, or empty array if disabled.\n */\n override getAccounts(): Bip44Account<KeyringAccount>[] {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.getAccounts();\n }\n\n /**\n * Override getAccount to throw when disabled.\n *\n * @param id - The account ID to retrieve.\n * @returns The account with the specified ID.\n * @throws When disabled or account not found.\n */\n override getAccount(\n id: Bip44Account<KeyringAccount>['id'],\n ): Bip44Account<KeyringAccount> {\n if (!this.isEnabled) {\n throw new Error('Provider is disabled');\n }\n return this.provider.getAccount(id);\n }\n\n /**\n * Returns true immediately when disabled (a disabled provider is considered\n * aligned by definition). Delegates to the wrapped provider otherwise.\n *\n * @param context - The entropy source and group index to check.\n * @param context.entropySource - The entropy source to check against.\n * @param context.groupIndex - The group index to check against.\n * @param accountIds - Account IDs pre-filtered by the caller.\n * @returns Whether the provider is aligned for the given context.\n */\n override isAligned(\n context: { entropySource: EntropySourceId; groupIndex: number },\n accountIds: Bip44Account<KeyringAccount>['id'][],\n ): boolean {\n if (!this.isEnabled) {\n return true;\n }\n return this.provider.isAligned(context, accountIds);\n }\n\n /**\n * Implement abstract method: Check if account is compatible.\n * Delegates directly to wrapped provider - no runtime checks needed!\n *\n * @param account - The account to check.\n * @returns True if the account is compatible.\n */\n isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean {\n return this.provider.isAccountCompatible(account);\n }\n\n /**\n * Implement abstract method: Create accounts, returns empty array when disabled.\n *\n * @param options - Account creation options.\n * @returns Promise resolving to created accounts, or empty array if disabled.\n */\n async createAccounts(\n options: CreateAccountOptions,\n ): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.createAccounts(options);\n }\n\n /**\n * Forwards to the wrapped provider unconditionally, because deletion must run even\n * when the wrapper is disabled, so that wallet-removal flows can clean up\n * snap-backed accounts that were created while the provider was previously\n * enabled.\n *\n * @param id - The id of the account to delete.\n * @returns A promise that resolves when the account is deleted.\n */\n async deleteAccount(id: Bip44Account<KeyringAccount>['id']): Promise<void> {\n return this.provider.deleteAccount(id);\n }\n\n /**\n * Implement abstract method: Discover and create accounts, returns empty array when disabled.\n *\n * @param options - Account discovery options.\n * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to discovered accounts, or empty array if disabled.\n */\n async discoverAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.discoverAccounts(options);\n }\n}\n\n/**\n * Simple type guard to check if a provider is wrapped.\n *\n * @param provider - The provider to check.\n * @returns True if the provider is an AccountProviderWrapper.\n */\nexport function isAccountProviderWrapper(\n provider: unknown,\n): provider is AccountProviderWrapper {\n return provider instanceof AccountProviderWrapper;\n}\n"]}