@metamask-previews/multichain-account-service 4.1.0-preview-a9886279 → 5.0.0-preview-6a568504

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (118) hide show
  1. package/CHANGELOG.md +36 -1
  2. package/dist/MultichainAccountGroup.cjs +84 -45
  3. package/dist/MultichainAccountGroup.cjs.map +1 -1
  4. package/dist/MultichainAccountGroup.d.cts +17 -4
  5. package/dist/MultichainAccountGroup.d.cts.map +1 -1
  6. package/dist/MultichainAccountGroup.d.mts +17 -4
  7. package/dist/MultichainAccountGroup.d.mts.map +1 -1
  8. package/dist/MultichainAccountGroup.mjs +84 -45
  9. package/dist/MultichainAccountGroup.mjs.map +1 -1
  10. package/dist/MultichainAccountService.cjs +150 -112
  11. package/dist/MultichainAccountService.cjs.map +1 -1
  12. package/dist/MultichainAccountService.d.cts +49 -20
  13. package/dist/MultichainAccountService.d.cts.map +1 -1
  14. package/dist/MultichainAccountService.d.mts +49 -20
  15. package/dist/MultichainAccountService.d.mts.map +1 -1
  16. package/dist/MultichainAccountService.mjs +151 -113
  17. package/dist/MultichainAccountService.mjs.map +1 -1
  18. package/dist/MultichainAccountWallet.cjs +108 -123
  19. package/dist/MultichainAccountWallet.cjs.map +1 -1
  20. package/dist/MultichainAccountWallet.d.cts +5 -4
  21. package/dist/MultichainAccountWallet.d.cts.map +1 -1
  22. package/dist/MultichainAccountWallet.d.mts +5 -4
  23. package/dist/MultichainAccountWallet.d.mts.map +1 -1
  24. package/dist/MultichainAccountWallet.mjs +108 -123
  25. package/dist/MultichainAccountWallet.mjs.map +1 -1
  26. package/dist/providers/AccountProviderWrapper.cjs +18 -0
  27. package/dist/providers/AccountProviderWrapper.cjs.map +1 -1
  28. package/dist/providers/AccountProviderWrapper.d.cts +12 -0
  29. package/dist/providers/AccountProviderWrapper.d.cts.map +1 -1
  30. package/dist/providers/AccountProviderWrapper.d.mts +12 -0
  31. package/dist/providers/AccountProviderWrapper.d.mts.map +1 -1
  32. package/dist/providers/AccountProviderWrapper.mjs +18 -0
  33. package/dist/providers/AccountProviderWrapper.mjs.map +1 -1
  34. package/dist/providers/BaseBip44AccountProvider.cjs +45 -20
  35. package/dist/providers/BaseBip44AccountProvider.cjs.map +1 -1
  36. package/dist/providers/BaseBip44AccountProvider.d.cts +58 -6
  37. package/dist/providers/BaseBip44AccountProvider.d.cts.map +1 -1
  38. package/dist/providers/BaseBip44AccountProvider.d.mts +58 -6
  39. package/dist/providers/BaseBip44AccountProvider.d.mts.map +1 -1
  40. package/dist/providers/BaseBip44AccountProvider.mjs +45 -20
  41. package/dist/providers/BaseBip44AccountProvider.mjs.map +1 -1
  42. package/dist/providers/BtcAccountProvider.cjs +47 -32
  43. package/dist/providers/BtcAccountProvider.cjs.map +1 -1
  44. package/dist/providers/BtcAccountProvider.d.cts +2 -1
  45. package/dist/providers/BtcAccountProvider.d.cts.map +1 -1
  46. package/dist/providers/BtcAccountProvider.d.mts +2 -1
  47. package/dist/providers/BtcAccountProvider.d.mts.map +1 -1
  48. package/dist/providers/BtcAccountProvider.mjs +47 -32
  49. package/dist/providers/BtcAccountProvider.mjs.map +1 -1
  50. package/dist/providers/EvmAccountProvider.cjs +39 -5
  51. package/dist/providers/EvmAccountProvider.cjs.map +1 -1
  52. package/dist/providers/EvmAccountProvider.d.cts +8 -0
  53. package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
  54. package/dist/providers/EvmAccountProvider.d.mts +8 -0
  55. package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
  56. package/dist/providers/EvmAccountProvider.mjs +39 -5
  57. package/dist/providers/EvmAccountProvider.mjs.map +1 -1
  58. package/dist/providers/SnapAccountProvider.cjs +81 -54
  59. package/dist/providers/SnapAccountProvider.cjs.map +1 -1
  60. package/dist/providers/SnapAccountProvider.d.cts +16 -4
  61. package/dist/providers/SnapAccountProvider.d.cts.map +1 -1
  62. package/dist/providers/SnapAccountProvider.d.mts +16 -4
  63. package/dist/providers/SnapAccountProvider.d.mts.map +1 -1
  64. package/dist/providers/SnapAccountProvider.mjs +81 -54
  65. package/dist/providers/SnapAccountProvider.mjs.map +1 -1
  66. package/dist/providers/SolAccountProvider.cjs +40 -31
  67. package/dist/providers/SolAccountProvider.cjs.map +1 -1
  68. package/dist/providers/SolAccountProvider.d.cts.map +1 -1
  69. package/dist/providers/SolAccountProvider.d.mts.map +1 -1
  70. package/dist/providers/SolAccountProvider.mjs +40 -31
  71. package/dist/providers/SolAccountProvider.mjs.map +1 -1
  72. package/dist/providers/TrxAccountProvider.cjs +48 -29
  73. package/dist/providers/TrxAccountProvider.cjs.map +1 -1
  74. package/dist/providers/TrxAccountProvider.d.cts +1 -0
  75. package/dist/providers/TrxAccountProvider.d.cts.map +1 -1
  76. package/dist/providers/TrxAccountProvider.d.mts +1 -0
  77. package/dist/providers/TrxAccountProvider.d.mts.map +1 -1
  78. package/dist/providers/TrxAccountProvider.mjs +48 -29
  79. package/dist/providers/TrxAccountProvider.mjs.map +1 -1
  80. package/dist/snaps/SnapPlatformWatcher.cjs +60 -0
  81. package/dist/snaps/SnapPlatformWatcher.cjs.map +1 -0
  82. package/dist/snaps/SnapPlatformWatcher.d.cts +8 -0
  83. package/dist/snaps/SnapPlatformWatcher.d.cts.map +1 -0
  84. package/dist/snaps/SnapPlatformWatcher.d.mts +8 -0
  85. package/dist/snaps/SnapPlatformWatcher.d.mts.map +1 -0
  86. package/dist/snaps/SnapPlatformWatcher.mjs +57 -0
  87. package/dist/snaps/SnapPlatformWatcher.mjs.map +1 -0
  88. package/dist/tests/messenger.cjs +12 -1
  89. package/dist/tests/messenger.cjs.map +1 -1
  90. package/dist/tests/messenger.d.cts +9 -4
  91. package/dist/tests/messenger.d.cts.map +1 -1
  92. package/dist/tests/messenger.d.mts +9 -4
  93. package/dist/tests/messenger.d.mts.map +1 -1
  94. package/dist/tests/messenger.mjs +12 -1
  95. package/dist/tests/messenger.mjs.map +1 -1
  96. package/dist/tests/providers.cjs +62 -7
  97. package/dist/tests/providers.cjs.map +1 -1
  98. package/dist/tests/providers.d.cts +17 -3
  99. package/dist/tests/providers.d.cts.map +1 -1
  100. package/dist/tests/providers.d.mts +17 -3
  101. package/dist/tests/providers.d.mts.map +1 -1
  102. package/dist/tests/providers.mjs +60 -6
  103. package/dist/tests/providers.mjs.map +1 -1
  104. package/dist/types.cjs.map +1 -1
  105. package/dist/types.d.cts +14 -6
  106. package/dist/types.d.cts.map +1 -1
  107. package/dist/types.d.mts +14 -6
  108. package/dist/types.d.mts.map +1 -1
  109. package/dist/types.mjs.map +1 -1
  110. package/dist/utils.cjs +1 -11
  111. package/dist/utils.cjs.map +1 -1
  112. package/dist/utils.d.cts +0 -1
  113. package/dist/utils.d.cts.map +1 -1
  114. package/dist/utils.d.mts +0 -1
  115. package/dist/utils.d.mts.map +1 -1
  116. package/dist/utils.mjs +0 -9
  117. package/dist/utils.mjs.map +1 -1
  118. package/package.json +3 -2
@@ -12,12 +12,20 @@ export declare class AccountProviderWrapper extends BaseBip44AccountProvider {
12
12
  private readonly provider;
13
13
  constructor(messenger: MultichainAccountServiceMessenger, provider: BaseBip44AccountProvider);
14
14
  getName(): string;
15
+ /**
16
+ * Forward initialization to the wrapped provider to ensure both
17
+ * instances share the same visible account IDs.
18
+ *
19
+ * @param accounts - Account IDs to initialize with.
20
+ */
21
+ init(accounts: Bip44Account<KeyringAccount>['id'][]): void;
15
22
  /**
16
23
  * Set the enabled state for this provider.
17
24
  *
18
25
  * @param enabled - Whether the provider should be enabled.
19
26
  */
20
27
  setEnabled(enabled: boolean): void;
28
+ isDisabled(): boolean;
21
29
  /**
22
30
  * Override resyncAccounts to not execute it when disabled.
23
31
  *
@@ -30,6 +38,10 @@ export declare class AccountProviderWrapper extends BaseBip44AccountProvider {
30
38
  * @returns Array of accounts, or empty array if disabled.
31
39
  */
32
40
  getAccounts(): Bip44Account<KeyringAccount>[];
41
+ alignAccounts(options: {
42
+ entropySource: EntropySourceId;
43
+ groupIndex: number;
44
+ }): Promise<Bip44Account<KeyringAccount>['id'][]>;
33
45
  /**
34
46
  * Override getAccount to throw when disabled.
35
47
  *
@@ -1 +1 @@
1
- {"version":3,"file":"AccountProviderWrapper.d.mts","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAC7E,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,wBAAwB;IAClE,OAAO,CAAC,SAAS,CAAiB;IAElC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA2B;gBAGlD,SAAS,EAAE,iCAAiC,EAC5C,QAAQ,EAAE,wBAAwB;IAM3B,OAAO,IAAI,MAAM;IAI1B;;;;OAIG;IACH,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAIlC;;;;OAIG;IACY,cAAc,CAC3B,QAAQ,EAAE,YAAY,CAAC,eAAe,CAAC,EAAE,GACxC,OAAO,CAAC,IAAI,CAAC;IAOhB;;;;OAIG;IACM,WAAW,IAAI,YAAY,CAAC,cAAc,CAAC,EAAE;IAOtD;;;;;;OAMG;IACM,UAAU,CACjB,EAAE,EAAE,YAAY,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,GACrC,YAAY,CAAC,cAAc,CAAC;IAO/B;;;;;;OAMG;IACH,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,cAAc,CAAC,GAAG,OAAO;IAInE;;;;;;;OAOG;IACG,cAAc,CAAC,OAAO,EAAE;QAC5B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAA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C,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC;IAOjD;;;;;;OAMG;IACM,UAAU,CACjB,EAAE,EAAE,YAAY,CAAC,cAAc,CAAC,CAAC,IAAI,CAAC,GACrC,YAAY,CAAC,cAAc,CAAC;IAO/B;;;;;;OAMG;IACH,mBAAmB,CAAC,OAAO,EAAE,YAAY,CAAC,cAAc,CAAC,GAAG,OAAO;IAInE;;;;;;;OAOG;IACG,cAAc,CAAC,OAAO,EAAE;QAC5B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;IAO3C;;;;;;;OAOG;IACG,gBAAgB,CAAC,OAAO,EAAE;QAC9B,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,YAAY,CAAC,cAAc,CAAC,EAAE,CAAC;CAM5C;AAED;;;;;GAKG;AACH,wBAAgB,wBAAwB,CACtC,QAAQ,EAAE,OAAO,GAChB,QAAQ,IAAI,sBAAsB,CAEpC"}
@@ -12,6 +12,15 @@ export class AccountProviderWrapper extends BaseBip44AccountProvider {
12
12
  getName() {
13
13
  return this.provider.getName();
14
14
  }
15
+ /**
16
+ * Forward initialization to the wrapped provider to ensure both
17
+ * instances share the same visible account IDs.
18
+ *
19
+ * @param accounts - Account IDs to initialize with.
20
+ */
21
+ init(accounts) {
22
+ this.provider.init(accounts);
23
+ }
15
24
  /**
16
25
  * Set the enabled state for this provider.
17
26
  *
@@ -20,6 +29,9 @@ export class AccountProviderWrapper extends BaseBip44AccountProvider {
20
29
  setEnabled(enabled) {
21
30
  this.isEnabled = enabled;
22
31
  }
32
+ isDisabled() {
33
+ return !this.isEnabled;
34
+ }
23
35
  /**
24
36
  * Override resyncAccounts to not execute it when disabled.
25
37
  *
@@ -42,6 +54,12 @@ export class AccountProviderWrapper extends BaseBip44AccountProvider {
42
54
  }
43
55
  return this.provider.getAccounts();
44
56
  }
57
+ async alignAccounts(options) {
58
+ if (this.isDisabled()) {
59
+ return [];
60
+ }
61
+ return await this.provider.alignAccounts(options);
62
+ }
45
63
  /**
46
64
  * Override getAccount to throw when disabled.
47
65
  *
@@ -1 +1 @@
1
- {"version":3,"file":"AccountProviderWrapper.mjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AAGtE;;;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;;;;OAIG;IACH,UAAU,CAAC,OAAgB;QACzB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,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,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;IACrC,CAAC;IAED;;;;;;OAMG;IACM,UAAU,CACjB,EAAsC;QAEtC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;QAC1C,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;OAMG;IACH,mBAAmB,CAAC,OAAqC;QACvD,OAAO,IAAI,CAAC,QAAQ,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,cAAc,CAAC,OAGpB;QACC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,gBAAgB,CAAC,OAGtB;QACC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE,CAAC;YACpB,OAAO,EAAE,CAAC;QACZ,CAAC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,gBAAgB,CAAC,OAAO,CAAC,CAAC;IACjD,CAAC;CACF;AAED;;;;;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 { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport type { MultichainAccountServiceMessenger } from '../types';\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 /**\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 * 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 * 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 * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to created accounts, or empty array if disabled.\n */\n async createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.createAccounts(options);\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"]}
1
+ {"version":3,"file":"AccountProviderWrapper.mjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAIA,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AAGtE;;;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;;;;;OAKG;IACM,IAAI,CAAC,QAA8C;QAC1D,IAAI,CAAC,QAAQ,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;IAC/B,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,OAAgB;QACzB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,CAAC;IAED,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,Q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MAAM,UAAU,wBAAwB,CACtC,QAAiB;IAEjB,OAAO,QAAQ,YAAY,sBAAsB,CAAC;AACpD,CAAC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport type { MultichainAccountServiceMessenger } from '../types';\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 /**\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 * 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 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 override async alignAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>['id'][]> {\n if (this.isDisabled()) {\n return [];\n }\n return await this.provider.alignAccounts(options);\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 * 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 * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to created accounts, or empty array if disabled.\n */\n async createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.createAccounts(options);\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"]}
@@ -4,7 +4,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
4
4
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
5
5
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
6
6
  };
7
- var _BaseBip44AccountProvider_instances, _BaseBip44AccountProvider_getAccounts;
7
+ var _BaseBip44AccountProvider_instances, _BaseBip44AccountProvider_getAccountIds;
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
9
9
  exports.BaseBip44AccountProvider = exports.assertAreBip44Accounts = exports.assertIsBip44Account = void 0;
10
10
  const account_api_1 = require("@metamask/account-api");
@@ -33,18 +33,47 @@ exports.assertAreBip44Accounts = assertAreBip44Accounts;
33
33
  class BaseBip44AccountProvider {
34
34
  constructor(messenger) {
35
35
  _BaseBip44AccountProvider_instances.add(this);
36
+ this.accounts = new Set();
36
37
  this.messenger = messenger;
37
38
  }
39
+ /**
40
+ * Add accounts to the provider.
41
+ *
42
+ * Note: There's an implicit assumption that the accounts are BIP-44 compatible.
43
+ *
44
+ * @param accounts - The accounts to add.
45
+ */
46
+ init(accounts) {
47
+ for (const account of accounts) {
48
+ this.accounts.add(account);
49
+ }
50
+ }
51
+ /**
52
+ * Get the accounts list for the provider from the AccountsController.
53
+ *
54
+ * @returns The accounts list.
55
+ */
38
56
  getAccounts() {
39
- return __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccounts).call(this);
57
+ const accountsIds = __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccountIds).call(this);
58
+ const internalAccounts = this.messenger.call('AccountsController:getAccounts', accountsIds);
59
+ // we cast here because we know that the accounts are BIP-44 compatible
60
+ return internalAccounts;
40
61
  }
62
+ /**
63
+ * Get the account for the provider.
64
+ *
65
+ * @param id - The account ID.
66
+ * @returns The account.
67
+ * @throws If the account is not found.
68
+ */
41
69
  getAccount(id) {
42
- // TODO: Maybe just use a proper find for faster lookup?
43
- const [found] = __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccounts).call(this, (account) => account.id === id);
44
- if (!found) {
70
+ const hasAccount = this.accounts.has(id);
71
+ if (!hasAccount) {
45
72
  throw new Error(`Unable to find account: ${id}`);
46
73
  }
47
- return found;
74
+ // We need to upcast here since InternalAccounts are not always BIP-44 compatible
75
+ // but we know that the account is BIP-44 compatible here so it is safe to do so
76
+ return this.messenger.call('AccountsController:getAccount', id);
48
77
  }
49
78
  async withKeyring(selector, operation) {
50
79
  const result = await this.messenger.call('KeyringController:withKeyring', selector, ({ keyring, metadata }) => operation({
@@ -53,21 +82,17 @@ class BaseBip44AccountProvider {
53
82
  }));
54
83
  return result;
55
84
  }
85
+ async alignAccounts({ entropySource, groupIndex, }) {
86
+ const accounts = await this.createAccounts({
87
+ entropySource,
88
+ groupIndex,
89
+ });
90
+ const accountIds = accounts.map((account) => account.id);
91
+ return accountIds;
92
+ }
56
93
  }
57
94
  exports.BaseBip44AccountProvider = BaseBip44AccountProvider;
58
- _BaseBip44AccountProvider_instances = new WeakSet(), _BaseBip44AccountProvider_getAccounts = function _BaseBip44AccountProvider_getAccounts(filter = () => true) {
59
- const accounts = [];
60
- for (const account of this.messenger.call(
61
- // NOTE: Even though the name is misleading, this only fetches all internal
62
- // accounts, including EVM and non-EVM. We might wanna change this action
63
- // name once we fully support multichain accounts.
64
- 'AccountsController:listMultichainAccounts')) {
65
- if ((0, account_api_1.isBip44Account)(account) &&
66
- this.isAccountCompatible(account) &&
67
- filter(account)) {
68
- accounts.push(account);
69
- }
70
- }
71
- return accounts;
95
+ _BaseBip44AccountProvider_instances = new WeakSet(), _BaseBip44AccountProvider_getAccountIds = function _BaseBip44AccountProvider_getAccountIds() {
96
+ return [...this.accounts];
72
97
  };
73
98
  //# sourceMappingURL=BaseBip44AccountProvider.cjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseBip44AccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names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AE,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,CAAC;QACF,IACE,IAAA,4BAAc,EAAC,OAAO,CAAC;YACvB,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,EACf,CAAC;YACD,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import { isBip44Account } from '@metamask/account-api';\nimport type { AccountProvider, Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\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 type Bip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> = AccountProvider<Account> & {\n getName(): string;\n\n /**\n * Re-synchronize MetaMask accounts and the providers accounts if needed.\n *\n * NOTE: This is mostly required if one of the providers (keyrings or Snaps)\n * have different sets of accounts. This method would ensure that both are\n * in-sync and use the same accounts (and same IDs).\n */\n resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;\n};\n\nexport abstract class BaseBip44AccountProvider implements Bip44AccountProvider {\n protected readonly messenger: MultichainAccountServiceMessenger;\n\n constructor(messenger: MultichainAccountServiceMessenger) {\n this.messenger = messenger;\n }\n\n abstract getName(): string;\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 resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void>;\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 discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n"]}
1
+ {"version":3,"file":"BaseBip44AccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uDAAuD;AAWvD;;;;;GAKG;AACH,SAAgB,oBAAoB,CAClC,OAAuB;IAEvB,IAAI,CAAC,IAAA,4BAAc,EAAC,OAAO,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC;AAND,oDAMC;AAED;;;;;GAKG;AACH,SAAgB,sBAAsB,CACpC,QAA0B;IAE1B,QAAQ,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC;AACzC,CAAC;AAJD,wDAIC;AA8CD,MAAsB,wBAAwB;IAQ5C,YAAY,SAA4C;;QAF9C,aAAQ,GAA4C,IAAI,GAAG,EAAE,CAAC;QAGtE,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED;;;;;;OAMG;IACH,IAAI,CAAC,QAAyB;QAC5B,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC/B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;IAWD;;;;OAIG;IACH,WAAW;QACT,MAAM,WAAW,GAAG,uBAAA,IAAI,oFAAe,MAAnB,IAAI,CAAiB,CAAC;QAC1C,MAAM,gBAAgB,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAC1C,gCAAgC,EAChC,WAAW,CACZ,CAAC;QACF,uEAAuE;QACvE,OAAO,gBAAwC,CAAC;IAClD,CAAC;IAED;;;;;;OAMG;IACH,UAAU,CAAC,EAAiB;QAC1B,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAEzC,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAC;QACnD,CAAC;QAED,iFAAiF;QACjF,gFAAgF;QAChF,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CACxB,+BAA+B,EAC/B,EAAE,CACmB,CAAC;IAC1B,CAAC;IAES,KAAK,CAAC,WAAW,CACzB,QAAyB,EACzB,SAM6B;QAE7B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACtC,+BAA+B,EAC/B,QAAQ,EACR,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,CACxB,SAAS,CAAC;YACR,OAAO,EAAE,OAA0B;YACnC,QAAQ;SACT,CAAC,CACL,CAAC;QAEF,OAAO,MAAwB,CAAC;IAClC,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,EAClB,aAAa,EACb,UAAU,GAIX;QACC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC;YACzC,aAAa;YACb,UAAU;SACX,CAAC,CAAC;QACH,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACzD,OAAO,UAAU,CAAC;IACpB,CAAC;CAyBF;AApID,4DAoIC;;IArGG,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { isBip44Account } from '@metamask/account-api';\nimport type { AccountProvider, Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\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 type Bip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> = AccountProvider<Account> & {\n /**\n * Get the name of the provider.\n *\n * @returns The name of the provider.\n */\n getName(): string;\n /**\n * Initialize the provider with the given accounts.\n *\n * @param accounts - The accounts to initialize the provider with.\n */\n init(accounts: Bip44Account<KeyringAccount>['id'][]): void;\n /**\n * Check if the account is compatible with the provider.\n */\n isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;\n /**\n * Align the accounts with the given entropy source and group index.\n *\n * @param options - The options for aligning the accounts.\n * @param options.entropySource - The entropy source.\n * @param options.groupIndex - The group index.\n * @returns The already and newly aligned accounts IDs.\n */\n alignAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account['id'][]>;\n /**\n * Re-synchronize MetaMask accounts and the providers accounts if needed.\n *\n * NOTE: This is mostly required if one of the providers (keyrings or Snaps)\n * have different sets of accounts. This method would ensure that both are\n * in-sync and use the same accounts (and same IDs).\n */\n resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;\n};\n\nexport abstract class BaseBip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> implements Bip44AccountProvider\n{\n protected readonly messenger: MultichainAccountServiceMessenger;\n\n protected accounts: Set<Bip44Account<KeyringAccount>['id']> = new Set();\n\n constructor(messenger: MultichainAccountServiceMessenger) {\n this.messenger = messenger;\n }\n\n /**\n * Add accounts to the provider.\n *\n * Note: There's an implicit assumption that the accounts are BIP-44 compatible.\n *\n * @param accounts - The accounts to add.\n */\n init(accounts: Account['id'][]): void {\n for (const account of accounts) {\n this.accounts.add(account);\n }\n }\n\n /**\n * Get the accounts list for the provider.\n *\n * @returns The accounts list.\n */\n #getAccountIds(): Account['id'][] {\n return [...this.accounts];\n }\n\n /**\n * Get the accounts list for the provider from the AccountsController.\n *\n * @returns The accounts list.\n */\n getAccounts(): Account[] {\n const accountsIds = this.#getAccountIds();\n const internalAccounts = this.messenger.call(\n 'AccountsController:getAccounts',\n accountsIds,\n );\n // we cast here because we know that the accounts are BIP-44 compatible\n return internalAccounts as unknown as Account[];\n }\n\n /**\n * Get the account for the provider.\n *\n * @param id - The account ID.\n * @returns The account.\n * @throws If the account is not found.\n */\n getAccount(id: Account['id']): Account {\n const hasAccount = this.accounts.has(id);\n\n if (!hasAccount) {\n throw new Error(`Unable to find account: ${id}`);\n }\n\n // We need to upcast here since InternalAccounts are not always BIP-44 compatible\n // but we know that the account is BIP-44 compatible here so it is safe to do so\n return this.messenger.call(\n 'AccountsController:getAccount',\n id,\n ) as unknown as Account;\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 async alignAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account['id'][]> {\n const accounts = await this.createAccounts({\n entropySource,\n groupIndex,\n });\n const accountIds = accounts.map((account) => account.id);\n return accountIds;\n }\n\n abstract getName(): string;\n\n abstract resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void>;\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<Account[]>;\n\n abstract discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account[]>;\n}\n"]}
@@ -18,7 +18,34 @@ export declare function assertIsBip44Account(account: KeyringAccount): asserts a
18
18
  */
19
19
  export declare function assertAreBip44Accounts(accounts: KeyringAccount[]): asserts accounts is Bip44Account<KeyringAccount>[];
20
20
  export type Bip44AccountProvider<Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>> = AccountProvider<Account> & {
21
+ /**
22
+ * Get the name of the provider.
23
+ *
24
+ * @returns The name of the provider.
25
+ */
21
26
  getName(): string;
27
+ /**
28
+ * Initialize the provider with the given accounts.
29
+ *
30
+ * @param accounts - The accounts to initialize the provider with.
31
+ */
32
+ init(accounts: Bip44Account<KeyringAccount>['id'][]): void;
33
+ /**
34
+ * Check if the account is compatible with the provider.
35
+ */
36
+ isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;
37
+ /**
38
+ * Align the accounts with the given entropy source and group index.
39
+ *
40
+ * @param options - The options for aligning the accounts.
41
+ * @param options.entropySource - The entropy source.
42
+ * @param options.groupIndex - The group index.
43
+ * @returns The already and newly aligned accounts IDs.
44
+ */
45
+ alignAccounts({ entropySource, groupIndex, }: {
46
+ entropySource: EntropySourceId;
47
+ groupIndex: number;
48
+ }): Promise<Account['id'][]>;
22
49
  /**
23
50
  * Re-synchronize MetaMask accounts and the providers accounts if needed.
24
51
  *
@@ -28,26 +55,51 @@ export type Bip44AccountProvider<Account extends Bip44Account<KeyringAccount> =
28
55
  */
29
56
  resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;
30
57
  };
31
- export declare abstract class BaseBip44AccountProvider implements Bip44AccountProvider {
58
+ export declare abstract class BaseBip44AccountProvider<Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>> implements Bip44AccountProvider {
32
59
  #private;
33
60
  protected readonly messenger: MultichainAccountServiceMessenger;
61
+ protected accounts: Set<Bip44Account<KeyringAccount>['id']>;
34
62
  constructor(messenger: MultichainAccountServiceMessenger);
35
- abstract getName(): string;
36
- getAccounts(): Bip44Account<KeyringAccount>[];
37
- getAccount(id: Bip44Account<KeyringAccount>['id']): Bip44Account<KeyringAccount>;
63
+ /**
64
+ * Add accounts to the provider.
65
+ *
66
+ * Note: There's an implicit assumption that the accounts are BIP-44 compatible.
67
+ *
68
+ * @param accounts - The accounts to add.
69
+ */
70
+ init(accounts: Account['id'][]): void;
71
+ /**
72
+ * Get the accounts list for the provider from the AccountsController.
73
+ *
74
+ * @returns The accounts list.
75
+ */
76
+ getAccounts(): Account[];
77
+ /**
78
+ * Get the account for the provider.
79
+ *
80
+ * @param id - The account ID.
81
+ * @returns The account.
82
+ * @throws If the account is not found.
83
+ */
84
+ getAccount(id: Account['id']): Account;
38
85
  protected withKeyring<SelectedKeyring, CallbackResult = void>(selector: KeyringSelector, operation: ({ keyring, metadata, }: {
39
86
  keyring: SelectedKeyring;
40
87
  metadata: KeyringMetadata;
41
88
  }) => Promise<CallbackResult>): Promise<CallbackResult>;
89
+ alignAccounts({ entropySource, groupIndex, }: {
90
+ entropySource: EntropySourceId;
91
+ groupIndex: number;
92
+ }): Promise<Account['id'][]>;
93
+ abstract getName(): string;
42
94
  abstract resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;
43
95
  abstract isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;
44
96
  abstract createAccounts({ entropySource, groupIndex, }: {
45
97
  entropySource: EntropySourceId;
46
98
  groupIndex: number;
47
- }): Promise<Bip44Account<KeyringAccount>[]>;
99
+ }): Promise<Account[]>;
48
100
  abstract discoverAccounts({ entropySource, groupIndex, }: {
49
101
  entropySource: EntropySourceId;
50
102
  groupIndex: number;
51
- }): Promise<Bip44Account<KeyringAccount>[]>;
103
+ }): Promise<Account[]>;
52
104
  }
53
105
  //# sourceMappingURL=BaseBip44AccountProvider.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseBip44AccountProvider.d.cts","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,8BAA8B;AAC3E,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAC7E,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EAChB,qCAAqC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,OAAO,IAAI,YAAY,CAAC,cAAc,CAAC,CAIjD;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,cAAc,EAAE,GACzB,OAAO,CAAC,QAAQ,IAAI,YAAY,CAAC,cAAc,CAAC,EAAE,CAEpD;AAED,MAAM,MAAM,oBAAoB,CAC9B,OAAO,SAAS,YAAY,CAAC,cAAc,CAAC,GAAG,YAAY,CAAC,cAAc,CAAC,IACzE,eAAe,CAAC,OAAO,CAAC,GAAG;IAC7B,OAAO,IAAI,MAAM,CAAC;IAElB;;;;;;OAMG;IACH,cAAc,CAAC,QAAQ,EAAE,YAAY,CAAC,eAAe,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1E,CAAC;AAEF,8BAAsB,wBAAyB,YAAW,oBAAoB;;IAC5E,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,iCAAiC,CAAC;gB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AG,OAAO,CAAC,OAAO,EAAE,CAAC;IAEtB,QAAQ,CAAC,gBAAgB,CAAC,EACxB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;CACvB"}
@@ -18,7 +18,34 @@ export declare function assertIsBip44Account(account: KeyringAccount): asserts a
18
18
  */
19
19
  export declare function assertAreBip44Accounts(accounts: KeyringAccount[]): asserts accounts is Bip44Account<KeyringAccount>[];
20
20
  export type Bip44AccountProvider<Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>> = AccountProvider<Account> & {
21
+ /**
22
+ * Get the name of the provider.
23
+ *
24
+ * @returns The name of the provider.
25
+ */
21
26
  getName(): string;
27
+ /**
28
+ * Initialize the provider with the given accounts.
29
+ *
30
+ * @param accounts - The accounts to initialize the provider with.
31
+ */
32
+ init(accounts: Bip44Account<KeyringAccount>['id'][]): void;
33
+ /**
34
+ * Check if the account is compatible with the provider.
35
+ */
36
+ isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;
37
+ /**
38
+ * Align the accounts with the given entropy source and group index.
39
+ *
40
+ * @param options - The options for aligning the accounts.
41
+ * @param options.entropySource - The entropy source.
42
+ * @param options.groupIndex - The group index.
43
+ * @returns The already and newly aligned accounts IDs.
44
+ */
45
+ alignAccounts({ entropySource, groupIndex, }: {
46
+ entropySource: EntropySourceId;
47
+ groupIndex: number;
48
+ }): Promise<Account['id'][]>;
22
49
  /**
23
50
  * Re-synchronize MetaMask accounts and the providers accounts if needed.
24
51
  *
@@ -28,26 +55,51 @@ export type Bip44AccountProvider<Account extends Bip44Account<KeyringAccount> =
28
55
  */
29
56
  resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;
30
57
  };
31
- export declare abstract class BaseBip44AccountProvider implements Bip44AccountProvider {
58
+ export declare abstract class BaseBip44AccountProvider<Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>> implements Bip44AccountProvider {
32
59
  #private;
33
60
  protected readonly messenger: MultichainAccountServiceMessenger;
61
+ protected accounts: Set<Bip44Account<KeyringAccount>['id']>;
34
62
  constructor(messenger: MultichainAccountServiceMessenger);
35
- abstract getName(): string;
36
- getAccounts(): Bip44Account<KeyringAccount>[];
37
- getAccount(id: Bip44Account<KeyringAccount>['id']): Bip44Account<KeyringAccount>;
63
+ /**
64
+ * Add accounts to the provider.
65
+ *
66
+ * Note: There's an implicit assumption that the accounts are BIP-44 compatible.
67
+ *
68
+ * @param accounts - The accounts to add.
69
+ */
70
+ init(accounts: Account['id'][]): void;
71
+ /**
72
+ * Get the accounts list for the provider from the AccountsController.
73
+ *
74
+ * @returns The accounts list.
75
+ */
76
+ getAccounts(): Account[];
77
+ /**
78
+ * Get the account for the provider.
79
+ *
80
+ * @param id - The account ID.
81
+ * @returns The account.
82
+ * @throws If the account is not found.
83
+ */
84
+ getAccount(id: Account['id']): Account;
38
85
  protected withKeyring<SelectedKeyring, CallbackResult = void>(selector: KeyringSelector, operation: ({ keyring, metadata, }: {
39
86
  keyring: SelectedKeyring;
40
87
  metadata: KeyringMetadata;
41
88
  }) => Promise<CallbackResult>): Promise<CallbackResult>;
89
+ alignAccounts({ entropySource, groupIndex, }: {
90
+ entropySource: EntropySourceId;
91
+ groupIndex: number;
92
+ }): Promise<Account['id'][]>;
93
+ abstract getName(): string;
42
94
  abstract resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;
43
95
  abstract isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;
44
96
  abstract createAccounts({ entropySource, groupIndex, }: {
45
97
  entropySource: EntropySourceId;
46
98
  groupIndex: number;
47
- }): Promise<Bip44Account<KeyringAccount>[]>;
99
+ }): Promise<Account[]>;
48
100
  abstract discoverAccounts({ entropySource, groupIndex, }: {
49
101
  entropySource: EntropySourceId;
50
102
  groupIndex: number;
51
- }): Promise<Bip44Account<KeyringAccount>[]>;
103
+ }): Promise<Account[]>;
52
104
  }
53
105
  //# sourceMappingURL=BaseBip44AccountProvider.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseBip44AccountProvider.d.mts","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,8BAA8B;AAC3E,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAC7E,OAAO,KAAK,EACV,eAAe,EACf,eAAe,EAChB,qCAAqC;AACtC,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAEtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;;;GAKG;AACH,wBAAgB,oBAAoB,CAClC,OAAO,EAAE,cAAc,GACtB,OAAO,CAAC,OAAO,IAAI,YAAY,CAAC,cAAc,CAAC,CAIjD;AAED;;;;;GAKG;AACH,wBAAgB,sBAAsB,CACpC,QAAQ,EAAE,cAAc,EAAE,GACzB,OAAO,CAAC,QAAQ,IAAI,YAAY,CAAC,cAAc,CAAC,EAAE,CAEpD;AAED,MAAM,MAAM,oBAAoB,CAC9B,OAAO,SAAS,YAAY,CAAC,cAAc,CAAC,GAAG,YAAY,CAAC,cAAc,CAAC,IACzE,eAAe,CAAC,OAAO,CAAC,GAAG;IAC7B,OAAO,IAAI,MAAM,CAAC;IAElB;;;;;;OAMG;IACH,cAAc,CAAC,QAAQ,EAAE,YAAY,CAAC,eAAe,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,CAAC;CAC1E,CAAC;AAEF,8BAAsB,wBAAyB,YAAW,oBAAoB;;IAC5E,SAAS,CAAC,QAAQ,CAAC,SAAS,EAAE,iCAAiC,CAAC;gBAEpD,SAAS,EAAE,iCAAiC;IAIxD,QAAQ,CAAC,OAAO,IAAI,MAAM;IAyB1B,WAAW,IAAI,YAAY,CAAC,cAAc,CAAC,EAAE;IAI7C,UAAU,CACR,EAAE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UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,OAAO,CAAC,OAAO,EAAE,CAAC;CACvB"}
@@ -3,7 +3,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
3
3
  if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
4
4
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
5
5
  };
6
- var _BaseBip44AccountProvider_instances, _BaseBip44AccountProvider_getAccounts;
6
+ var _BaseBip44AccountProvider_instances, _BaseBip44AccountProvider_getAccountIds;
7
7
  import { isBip44Account } from "@metamask/account-api";
8
8
  /**
9
9
  * Asserts a keyring account is BIP-44 compatible.
@@ -28,18 +28,47 @@ export function assertAreBip44Accounts(accounts) {
28
28
  export class BaseBip44AccountProvider {
29
29
  constructor(messenger) {
30
30
  _BaseBip44AccountProvider_instances.add(this);
31
+ this.accounts = new Set();
31
32
  this.messenger = messenger;
32
33
  }
34
+ /**
35
+ * Add accounts to the provider.
36
+ *
37
+ * Note: There's an implicit assumption that the accounts are BIP-44 compatible.
38
+ *
39
+ * @param accounts - The accounts to add.
40
+ */
41
+ init(accounts) {
42
+ for (const account of accounts) {
43
+ this.accounts.add(account);
44
+ }
45
+ }
46
+ /**
47
+ * Get the accounts list for the provider from the AccountsController.
48
+ *
49
+ * @returns The accounts list.
50
+ */
33
51
  getAccounts() {
34
- return __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccounts).call(this);
52
+ const accountsIds = __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccountIds).call(this);
53
+ const internalAccounts = this.messenger.call('AccountsController:getAccounts', accountsIds);
54
+ // we cast here because we know that the accounts are BIP-44 compatible
55
+ return internalAccounts;
35
56
  }
57
+ /**
58
+ * Get the account for the provider.
59
+ *
60
+ * @param id - The account ID.
61
+ * @returns The account.
62
+ * @throws If the account is not found.
63
+ */
36
64
  getAccount(id) {
37
- // TODO: Maybe just use a proper find for faster lookup?
38
- const [found] = __classPrivateFieldGet(this, _BaseBip44AccountProvider_instances, "m", _BaseBip44AccountProvider_getAccounts).call(this, (account) => account.id === id);
39
- if (!found) {
65
+ const hasAccount = this.accounts.has(id);
66
+ if (!hasAccount) {
40
67
  throw new Error(`Unable to find account: ${id}`);
41
68
  }
42
- return found;
69
+ // We need to upcast here since InternalAccounts are not always BIP-44 compatible
70
+ // but we know that the account is BIP-44 compatible here so it is safe to do so
71
+ return this.messenger.call('AccountsController:getAccount', id);
43
72
  }
44
73
  async withKeyring(selector, operation) {
45
74
  const result = await this.messenger.call('KeyringController:withKeyring', selector, ({ keyring, metadata }) => operation({
@@ -48,20 +77,16 @@ export class BaseBip44AccountProvider {
48
77
  }));
49
78
  return result;
50
79
  }
51
- }
52
- _BaseBip44AccountProvider_instances = new WeakSet(), _BaseBip44AccountProvider_getAccounts = function _BaseBip44AccountProvider_getAccounts(filter = () => true) {
53
- const accounts = [];
54
- for (const account of this.messenger.call(
55
- // NOTE: Even though the name is misleading, this only fetches all internal
56
- // accounts, including EVM and non-EVM. We might wanna change this action
57
- // name once we fully support multichain accounts.
58
- 'AccountsController:listMultichainAccounts')) {
59
- if (isBip44Account(account) &&
60
- this.isAccountCompatible(account) &&
61
- filter(account)) {
62
- accounts.push(account);
63
- }
80
+ async alignAccounts({ entropySource, groupIndex, }) {
81
+ const accounts = await this.createAccounts({
82
+ entropySource,
83
+ groupIndex,
84
+ });
85
+ const accountIds = accounts.map((account) => account.id);
86
+ return accountIds;
64
87
  }
65
- return accounts;
88
+ }
89
+ _BaseBip44AccountProvider_instances = new WeakSet(), _BaseBip44AccountProvider_getAccountIds = function _BaseBip44AccountProvider_getAccountIds() {
90
+ return [...this.accounts];
66
91
  };
67
92
  //# sourceMappingURL=BaseBip44AccountProvider.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"BaseBip44AccountProvider.mjs","sourceRoot":"","s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IAnFG,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,CAAC;QACF,IACE,cAAc,CAAC,OAAO,CAAC;YACvB,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,EACf,CAAC;YACD,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import { isBip44Account } from '@metamask/account-api';\nimport type { AccountProvider, Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\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 type Bip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> = AccountProvider<Account> & {\n getName(): string;\n\n /**\n * Re-synchronize MetaMask accounts and the providers accounts if needed.\n *\n * NOTE: This is mostly required if one of the providers (keyrings or Snaps)\n * have different sets of accounts. This method would ensure that both are\n * in-sync and use the same accounts (and same IDs).\n */\n resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;\n};\n\nexport abstract class BaseBip44AccountProvider implements Bip44AccountProvider {\n protected readonly messenger: MultichainAccountServiceMessenger;\n\n constructor(messenger: MultichainAccountServiceMessenger) {\n this.messenger = messenger;\n }\n\n abstract getName(): string;\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 resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void>;\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 discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]>;\n}\n"]}
1
+ {"version":3,"file":"BaseBip44AccountProvider.mjs","sourceRoot":"","sources":["../../src/providers/BaseBip44AccountProvider.ts"],"names":[],"mappings":";;;;;;AAAA,OAAO,EAAE,cAAc,EAAE,8BAA8B;AAWvD;;;;;GAKG;AACH,MAAM,UAAU,oBAAoB,CAClC,OAAuB;IAEvB,IAAI,CAAC,cAAc,CAAC,OAAO,CAAC,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;IAC9D,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,sBAAsB,CACpC,QAA0B;IAE1B,QAAQ,CAAC,OAAO,CAAC,oBAAoB,CAAC,CAAC;AACzC,CAAC;AA8CD,MAAM,OAAgB,wBAAwB;IAQ5C,YAAY,SAA4C;;QAF9C,aAAQ,GAA4C,IAAI,GAAG,EAAE,CAAC;QAGtE,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAED;;;;;;OAMG;IACH,IAAI,CAAC,QAAyB;QAC5B,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE,CAAC;YAC/B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAC7B,CAAC;IACH,CAAC;IAWD;;;;OAIG;IACH,WAAW;QACT,MAAM,WAAW,GAAG,uBAAA,IAAI,oFAAe,MAAnB,IAAI,CAAiB,CAAC;QAC1C,MAAM,gBAAgB,GAAG,IAAI,CAAC,SAAS,CAAC,IAAI,CAC1C,gCAAgC,EAChC,WAAW,CACZ,CAAC;QACF,uEAAuE;QACvE,OAAO,gBAAwC,CAAC;IAClD,CAAC;IAED;;;;;;OAMG;IACH,UAAU,CAAC,EAAiB;QAC1B,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;QAEzC,IAAI,CAAC,UAAU,EAAE,CAAC;YAChB,MAAM,IAAI,KAAK,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAC;QACnD,CAAC;QAED,iFAAiF;QACjF,gFAAgF;QAChF,OAAO,IAAI,CAAC,SAAS,CAAC,IAAI,CACxB,+BAA+B,EAC/B,EAAE,CACmB,CAAC;IAC1B,CAAC;IAES,KAAK,CAAC,WAAW,CACzB,QAAyB,EACzB,SAM6B;QAE7B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,SAAS,CAAC,IAAI,CACtC,+BAA+B,EAC/B,QAAQ,EACR,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,CACxB,SAAS,CAAC;YACR,OAAO,EAAE,OAA0B;YACnC,QAAQ;SACT,CAAC,CACL,CAAC;QAEF,OAAO,MAAwB,CAAC;IAClC,CAAC;IAED,KAAK,CAAC,aAAa,CAAC,EAClB,aAAa,EACb,UAAU,GAIX;QACC,MAAM,QAAQ,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC;YACzC,aAAa;YACb,UAAU;SACX,CAAC,CAAC;QACH,MAAM,UAAU,GAAG,QAAQ,CAAC,GAAG,CAAC,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,CAAC,CAAC;QACzD,OAAO,UAAU,CAAC;IACpB,CAAC;CAyBF;;IArGG,OAAO,CAAC,GAAG,IAAI,CAAC,QAAQ,CAAC,CAAC;AAC5B,CAAC","sourcesContent":["import { isBip44Account } from '@metamask/account-api';\nimport type { AccountProvider, Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport type {\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\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 type Bip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> = AccountProvider<Account> & {\n /**\n * Get the name of the provider.\n *\n * @returns The name of the provider.\n */\n getName(): string;\n /**\n * Initialize the provider with the given accounts.\n *\n * @param accounts - The accounts to initialize the provider with.\n */\n init(accounts: Bip44Account<KeyringAccount>['id'][]): void;\n /**\n * Check if the account is compatible with the provider.\n */\n isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean;\n /**\n * Align the accounts with the given entropy source and group index.\n *\n * @param options - The options for aligning the accounts.\n * @param options.entropySource - The entropy source.\n * @param options.groupIndex - The group index.\n * @returns The already and newly aligned accounts IDs.\n */\n alignAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account['id'][]>;\n /**\n * Re-synchronize MetaMask accounts and the providers accounts if needed.\n *\n * NOTE: This is mostly required if one of the providers (keyrings or Snaps)\n * have different sets of accounts. This method would ensure that both are\n * in-sync and use the same accounts (and same IDs).\n */\n resyncAccounts(accounts: Bip44Account<InternalAccount>[]): Promise<void>;\n};\n\nexport abstract class BaseBip44AccountProvider<\n Account extends Bip44Account<KeyringAccount> = Bip44Account<KeyringAccount>,\n> implements Bip44AccountProvider\n{\n protected readonly messenger: MultichainAccountServiceMessenger;\n\n protected accounts: Set<Bip44Account<KeyringAccount>['id']> = new Set();\n\n constructor(messenger: MultichainAccountServiceMessenger) {\n this.messenger = messenger;\n }\n\n /**\n * Add accounts to the provider.\n *\n * Note: There's an implicit assumption that the accounts are BIP-44 compatible.\n *\n * @param accounts - The accounts to add.\n */\n init(accounts: Account['id'][]): void {\n for (const account of accounts) {\n this.accounts.add(account);\n }\n }\n\n /**\n * Get the accounts list for the provider.\n *\n * @returns The accounts list.\n */\n #getAccountIds(): Account['id'][] {\n return [...this.accounts];\n }\n\n /**\n * Get the accounts list for the provider from the AccountsController.\n *\n * @returns The accounts list.\n */\n getAccounts(): Account[] {\n const accountsIds = this.#getAccountIds();\n const internalAccounts = this.messenger.call(\n 'AccountsController:getAccounts',\n accountsIds,\n );\n // we cast here because we know that the accounts are BIP-44 compatible\n return internalAccounts as unknown as Account[];\n }\n\n /**\n * Get the account for the provider.\n *\n * @param id - The account ID.\n * @returns The account.\n * @throws If the account is not found.\n */\n getAccount(id: Account['id']): Account {\n const hasAccount = this.accounts.has(id);\n\n if (!hasAccount) {\n throw new Error(`Unable to find account: ${id}`);\n }\n\n // We need to upcast here since InternalAccounts are not always BIP-44 compatible\n // but we know that the account is BIP-44 compatible here so it is safe to do so\n return this.messenger.call(\n 'AccountsController:getAccount',\n id,\n ) as unknown as Account;\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 async alignAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account['id'][]> {\n const accounts = await this.createAccounts({\n entropySource,\n groupIndex,\n });\n const accountIds = accounts.map((account) => account.id);\n return accountIds;\n }\n\n abstract getName(): string;\n\n abstract resyncAccounts(\n accounts: Bip44Account<InternalAccount>[],\n ): Promise<void>;\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<Account[]>;\n\n abstract discoverAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Account[]>;\n}\n"]}