@metamask-previews/multichain-account-service 0.2.1-preview-bd86fa14 → 0.3.0-preview-1abef88

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 (58) hide show
  1. package/CHANGELOG.md +15 -2
  2. package/dist/MultichainAccountService.cjs +113 -36
  3. package/dist/MultichainAccountService.cjs.map +1 -1
  4. package/dist/MultichainAccountService.d.cts +36 -5
  5. package/dist/MultichainAccountService.d.cts.map +1 -1
  6. package/dist/MultichainAccountService.d.mts +36 -5
  7. package/dist/MultichainAccountService.d.mts.map +1 -1
  8. package/dist/MultichainAccountService.mjs +112 -35
  9. package/dist/MultichainAccountService.mjs.map +1 -1
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +1 -1
  12. package/dist/index.d.cts.map +1 -1
  13. package/dist/index.d.mts +1 -1
  14. package/dist/index.d.mts.map +1 -1
  15. package/dist/index.mjs.map +1 -1
  16. package/dist/providers/BaseAccountProvider.cjs +4 -19
  17. package/dist/providers/BaseAccountProvider.cjs.map +1 -1
  18. package/dist/providers/BaseAccountProvider.d.cts +5 -19
  19. package/dist/providers/BaseAccountProvider.d.cts.map +1 -1
  20. package/dist/providers/BaseAccountProvider.d.mts +5 -19
  21. package/dist/providers/BaseAccountProvider.d.mts.map +1 -1
  22. package/dist/providers/BaseAccountProvider.mjs +3 -17
  23. package/dist/providers/BaseAccountProvider.mjs.map +1 -1
  24. package/dist/providers/EvmAccountProvider.cjs.map +1 -1
  25. package/dist/providers/EvmAccountProvider.d.cts +2 -1
  26. package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
  27. package/dist/providers/EvmAccountProvider.d.mts +2 -1
  28. package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
  29. package/dist/providers/EvmAccountProvider.mjs.map +1 -1
  30. package/dist/providers/SolAccountProvider.cjs.map +1 -1
  31. package/dist/providers/SolAccountProvider.d.cts +2 -1
  32. package/dist/providers/SolAccountProvider.d.cts.map +1 -1
  33. package/dist/providers/SolAccountProvider.d.mts +2 -1
  34. package/dist/providers/SolAccountProvider.d.mts.map +1 -1
  35. package/dist/providers/SolAccountProvider.mjs.map +1 -1
  36. package/dist/tests/accounts.cjs +7 -3
  37. package/dist/tests/accounts.cjs.map +1 -1
  38. package/dist/tests/accounts.d.cts +1 -0
  39. package/dist/tests/accounts.d.cts.map +1 -1
  40. package/dist/tests/accounts.d.mts +1 -0
  41. package/dist/tests/accounts.d.mts.map +1 -1
  42. package/dist/tests/accounts.mjs +5 -1
  43. package/dist/tests/accounts.mjs.map +1 -1
  44. package/dist/tests/messenger.cjs +5 -1
  45. package/dist/tests/messenger.cjs.map +1 -1
  46. package/dist/tests/messenger.d.cts +2 -2
  47. package/dist/tests/messenger.d.cts.map +1 -1
  48. package/dist/tests/messenger.d.mts +2 -2
  49. package/dist/tests/messenger.d.mts.map +1 -1
  50. package/dist/tests/messenger.mjs +5 -1
  51. package/dist/tests/messenger.mjs.map +1 -1
  52. package/dist/types.cjs.map +1 -1
  53. package/dist/types.d.cts +20 -3
  54. package/dist/types.d.cts.map +1 -1
  55. package/dist/types.d.mts +20 -3
  56. package/dist/types.d.mts.map +1 -1
  57. package/dist/types.mjs.map +1 -1
  58. package/package.json +4 -3
package/CHANGELOG.md CHANGED
@@ -7,6 +7,18 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.3.0]
11
+
12
+ ### Added
13
+
14
+ - Add multichain account/wallet syncs ([#6165](https://github.com/MetaMask/core/pull/6165))
15
+ - Those are getting sync'd during `AccountsController:account{Added,Removed}` events.
16
+ - Add actions `MultichainAccountService:getMultichain{Account,Accounts,AccountWallet,AccountWallets}` ([#6193](https://github.com/MetaMask/core/pull/6193))
17
+
18
+ ### Changed
19
+
20
+ - **BREAKING:** Add `@metamask/account-api` peer dependency ([#6115](https://github.com/MetaMask/core/pull/6115)), ([#6146](https://github.com/MetaMask/core/pull/6146))
21
+
10
22
  ## [0.2.1]
11
23
 
12
24
  ### Fixed
@@ -23,10 +35,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
23
35
 
24
36
  ### Added
25
37
 
26
- - Add `MultichainAccountService` ([#6141](https://github.com/MetaMask/core/pull/6141))
38
+ - Add `MultichainAccountService` ([#6141](https://github.com/MetaMask/core/pull/6141)), ([#6165](https://github.com/MetaMask/core/pull/6165))
27
39
  - This service manages multichain accounts/wallets.
28
40
 
29
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/multichain-account-service@0.2.1...HEAD
41
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/multichain-account-service@0.3.0...HEAD
42
+ [0.3.0]: https://github.com/MetaMask/core/compare/@metamask/multichain-account-service@0.2.1...@metamask/multichain-account-service@0.3.0
30
43
  [0.2.1]: https://github.com/MetaMask/core/compare/@metamask/multichain-account-service@0.2.0...@metamask/multichain-account-service@0.2.1
31
44
  [0.2.0]: https://github.com/MetaMask/core/compare/@metamask/multichain-account-service@0.1.0...@metamask/multichain-account-service@0.2.0
32
45
  [0.1.0]: https://github.com/MetaMask/core/releases/tag/@metamask/multichain-account-service@0.1.0
@@ -10,23 +10,14 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
10
10
  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");
11
11
  return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
12
  };
13
- var _MultichainAccountService_instances, _MultichainAccountService_messenger, _MultichainAccountService_providers, _MultichainAccountService_wallets, _MultichainAccountService_setMultichainAccountWallets, _MultichainAccountService_getWallet;
13
+ var _MultichainAccountService_instances, _MultichainAccountService_messenger, _MultichainAccountService_providers, _MultichainAccountService_wallets, _MultichainAccountService_accountIdToContext, _MultichainAccountService_handleOnAccountAdded, _MultichainAccountService_handleOnAccountRemoved, _MultichainAccountService_getWallet;
14
14
  Object.defineProperty(exports, "__esModule", { value: true });
15
- exports.MultichainAccountService = void 0;
15
+ exports.MultichainAccountService = exports.serviceName = void 0;
16
16
  const account_api_1 = require("@metamask/account-api");
17
17
  const keyring_controller_1 = require("@metamask/keyring-controller");
18
18
  const EvmAccountProvider_1 = require("./providers/EvmAccountProvider.cjs");
19
19
  const SolAccountProvider_1 = require("./providers/SolAccountProvider.cjs");
20
- const serviceName = 'MultichainAccountService';
21
- /**
22
- * Select keyrings from keyring controller state.
23
- *
24
- * @param state - The keyring controller state.
25
- * @returns The keyrings.
26
- */
27
- function selectKeyringControllerKeyrings(state) {
28
- return state.keyrings;
29
- }
20
+ exports.serviceName = 'MultichainAccountService';
30
21
  /**
31
22
  * Service to expose multichain accounts capabilities.
32
23
  */
@@ -43,29 +34,84 @@ class MultichainAccountService {
43
34
  _MultichainAccountService_messenger.set(this, void 0);
44
35
  _MultichainAccountService_providers.set(this, void 0);
45
36
  _MultichainAccountService_wallets.set(this, void 0);
37
+ _MultichainAccountService_accountIdToContext.set(this, void 0);
46
38
  /**
47
39
  * The name of the service.
48
40
  */
49
- this.name = serviceName;
41
+ this.name = exports.serviceName;
50
42
  __classPrivateFieldSet(this, _MultichainAccountService_messenger, messenger, "f");
51
43
  __classPrivateFieldSet(this, _MultichainAccountService_wallets, new Map(), "f");
44
+ __classPrivateFieldSet(this, _MultichainAccountService_accountIdToContext, new Map(), "f");
52
45
  // TODO: Rely on keyring capabilities once the keyring API is used by all keyrings.
53
46
  __classPrivateFieldSet(this, _MultichainAccountService_providers, [
54
47
  new EvmAccountProvider_1.EvmAccountProvider(__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f")),
55
48
  new SolAccountProvider_1.SolAccountProvider(__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f")),
56
49
  ], "f");
50
+ __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccount', (...args) => this.getMultichainAccount(...args));
51
+ __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccounts', (...args) => this.getMultichainAccounts(...args));
52
+ __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountWallet', (...args) => this.getMultichainAccountWallet(...args));
53
+ __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountWallets', (...args) => this.getMultichainAccountWallets(...args));
57
54
  }
58
55
  /**
59
56
  * Initialize the service and constructs the internal reprensentation of
60
57
  * multichain accounts and wallets.
61
58
  */
62
59
  init() {
63
- // Gather all entropy sources first.
64
- const state = __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").call('KeyringController:getState');
65
- __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_setMultichainAccountWallets).call(this, state.keyrings);
66
- __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").subscribe('KeyringController:stateChange', (keyrings) => {
67
- __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_setMultichainAccountWallets).call(this, keyrings);
68
- }, selectKeyringControllerKeyrings);
60
+ // Create initial wallets.
61
+ const { keyrings } = __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").call('KeyringController:getState');
62
+ for (const keyring of keyrings) {
63
+ if (keyring.type === keyring_controller_1.KeyringTypes.hd) {
64
+ // Only HD keyrings have an entropy source/SRP.
65
+ const entropySource = keyring.metadata.id;
66
+ // This will automatically "associate" all multichain accounts for that wallet
67
+ // (based on the accounts owned by each account providers).
68
+ const wallet = new account_api_1.MultichainAccountWallet({
69
+ entropySource,
70
+ providers: __classPrivateFieldGet(this, _MultichainAccountService_providers, "f"),
71
+ });
72
+ __classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").set(wallet.id, wallet);
73
+ // Reverse mapping between account ID and their multichain wallet/account:
74
+ for (const multichainAccount of wallet.getMultichainAccounts()) {
75
+ for (const account of multichainAccount.getAccounts()) {
76
+ __classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").set(account.id, {
77
+ wallet,
78
+ multichainAccount,
79
+ });
80
+ }
81
+ }
82
+ }
83
+ }
84
+ __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").subscribe('AccountsController:accountAdded', (account) => __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_handleOnAccountAdded).call(this, account));
85
+ __classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").subscribe('AccountsController:accountRemoved', (id) => __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_handleOnAccountRemoved).call(this, id));
86
+ }
87
+ /**
88
+ * Gets a reference to the wallet and multichain account for a given account ID.
89
+ *
90
+ * @param id - Account ID.
91
+ * @returns An object with references to the wallet and multichain account associated for
92
+ * that account ID, or undefined if this account ID is not part of any.
93
+ */
94
+ getMultichainAccountAndWallet(id) {
95
+ return __classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").get(id);
96
+ }
97
+ /**
98
+ * Gets a reference to the multichain account wallet matching this entropy source.
99
+ *
100
+ * @param options - Options.
101
+ * @param options.entropySource - The entropy source of the multichain account.
102
+ * @throws If none multichain account match this entropy.
103
+ * @returns A reference to the multichain account wallet.
104
+ */
105
+ getMultichainAccountWallet({ entropySource, }) {
106
+ return __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource);
107
+ }
108
+ /**
109
+ * Gets an array of all multichain account wallets.
110
+ *
111
+ * @returns An array of all multichain account wallets.
112
+ */
113
+ getMultichainAccountWallets() {
114
+ return Array.from(__classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").values());
69
115
  }
70
116
  /**
71
117
  * Gets a reference to the multichain account matching this entropy source and group index.
@@ -96,25 +142,56 @@ class MultichainAccountService {
96
142
  }
97
143
  }
98
144
  exports.MultichainAccountService = MultichainAccountService;
99
- _MultichainAccountService_messenger = new WeakMap(), _MultichainAccountService_providers = new WeakMap(), _MultichainAccountService_wallets = new WeakMap(), _MultichainAccountService_instances = new WeakSet(), _MultichainAccountService_setMultichainAccountWallets = function _MultichainAccountService_setMultichainAccountWallets(keyrings) {
100
- for (const keyring of keyrings) {
101
- if (keyring.type === keyring_controller_1.KeyringTypes.hd) {
102
- // Only HD keyrings have an entropy source/SRP.
103
- const entropySource = keyring.metadata.id;
104
- // Do not re-create wallets if they exists. Even if a keyrings got new accounts, this
105
- // will be handled by the `*AccountProvider`s which are always in-sync with their
106
- // keyrings and controllers (like the `AccountsController`).
107
- if (!__classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").has((0, account_api_1.toMultichainAccountWalletId)(entropySource))) {
108
- // This will automatically "associate" all multichain accounts for that wallet
109
- // (based on the accounts owned by each account providers).
110
- const wallet = new account_api_1.MultichainAccountWallet({
111
- entropySource,
112
- providers: __classPrivateFieldGet(this, _MultichainAccountService_providers, "f"),
113
- });
114
- __classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").set(wallet.id, wallet);
115
- }
145
+ _MultichainAccountService_messenger = new WeakMap(), _MultichainAccountService_providers = new WeakMap(), _MultichainAccountService_wallets = new WeakMap(), _MultichainAccountService_accountIdToContext = new WeakMap(), _MultichainAccountService_instances = new WeakSet(), _MultichainAccountService_handleOnAccountAdded = function _MultichainAccountService_handleOnAccountAdded(account) {
146
+ // We completely omit non-BIP-44 accounts!
147
+ if (!(0, account_api_1.isBip44Account)(account)) {
148
+ return;
149
+ }
150
+ let sync = true;
151
+ let wallet = __classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").get((0, account_api_1.toMultichainAccountWalletId)(account.options.entropy.id));
152
+ if (!wallet) {
153
+ // That's a new wallet.
154
+ wallet = new account_api_1.MultichainAccountWallet({
155
+ entropySource: account.options.entropy.id,
156
+ providers: __classPrivateFieldGet(this, _MultichainAccountService_providers, "f"),
157
+ });
158
+ __classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").set(wallet.id, wallet);
159
+ // If that's a new wallet wallet. There's nothing to "force-sync".
160
+ sync = false;
161
+ }
162
+ let multichainAccount = wallet.getMultichainAccount(account.options.entropy.groupIndex);
163
+ if (!multichainAccount) {
164
+ // This new account is a new multichain account, let the wallet know
165
+ // it has to re-sync with its providers.
166
+ if (sync) {
167
+ wallet.sync();
116
168
  }
169
+ multichainAccount = wallet.getMultichainAccount(account.options.entropy.groupIndex);
170
+ // If that's a new multichain account. There's nothing to "force-sync".
171
+ sync = false;
172
+ }
173
+ // We have to check against `undefined` in case `getMultichainAccount` is
174
+ // not able to find this multichain account (which should not be possible...)
175
+ if (multichainAccount) {
176
+ if (sync) {
177
+ multichainAccount.sync();
178
+ }
179
+ // Same here, this account should have been already grouped in that
180
+ // multichain account.
181
+ __classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").set(account.id, {
182
+ wallet,
183
+ multichainAccount,
184
+ });
185
+ }
186
+ }, _MultichainAccountService_handleOnAccountRemoved = function _MultichainAccountService_handleOnAccountRemoved(id) {
187
+ // Force sync of the appropriate wallet if an account got removed.
188
+ const found = __classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").get(id);
189
+ if (found) {
190
+ const { wallet } = found;
191
+ wallet.sync();
117
192
  }
193
+ // Safe to call delete even if the `id` was not referencing a BIP-44 account.
194
+ __classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").delete(id);
118
195
  }, _MultichainAccountService_getWallet = function _MultichainAccountService_getWallet(entropySource) {
119
196
  const wallet = __classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").get((0, account_api_1.toMultichainAccountWalletId)(entropySource));
120
197
  if (!wallet) {
@@ -1 +1 @@
1
- {"version":3,"file":"MultichainAccountService.cjs","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAIA,uDAI+B;AAM/B,qEAA4D;AAG5D,2EAAoE;AACpE,2EAAoE;AAGpE,MAAM,WAAW,GAAG,0BAA0B,CAAC;AAS/C;;;;;GAKG;AACH,SA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CAAC;IAC3B,CAAC;IAED;;;;;;;OAOG;IACH,qBAAqB,CAAC,EACpB,aAAa,GAGd;QACC,OAAO,uBAAA,IAAI,gFAAW,MAAf,IAAI,EAAY,aAAa,CAAC,CAAC,qBAAqB,EAAE,CAAC;IAChE,CAAC;CACF;AAhID,4DAgIC;yUA9E8B,QAAyB;IACpD,KAAK,MAAM,OAAO,IAAI,QAAQ,EAAE;QAC9B,IAAI,OAAO,CAAC,IAAI,KAAM,iCAAY,CAAC,EAAa,EAAE;YAChD,+CAA+C;YAC/C,MAAM,aAAa,GAAG,OAAO,CAAC,QAAQ,CAAC,EAAE,CAAC;YAE1C,qFAAqF;YACrF,iFAAiF;YACjF,4DAA4D;YAC5D,IAAI,CAAC,uBAAA,IAAI,yCAAS,CAAC,GAAG,CAAC,IAAA,yCAA2B,EAAC,aAAa,CAAC,CAAC,EAAE;gBAClE,8EAA8E;gBAC9E,2DAA2D;gBAC3D,MAAM,MAAM,GAAG,IAAI,qCAAuB,CAAC;oBACzC,aAAa;oBACb,SAAS,EAAE,uBAAA,IAAI,2CAAW;iBAC3B,CAAC,CAAC;gBAEH,uBAAA,IAAI,yCAAS,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,EAAE,MAAM,CAAC,CAAC;aACtC;SACF;KACF;AACH,CAAC,qFAGC,aAA8B;IAE9B,MAAM,MAAM,GAAG,uBAAA,IAAI,yCAAS,CAAC,GAAG,CAC9B,IAAA,yCAA2B,EAAC,aAAa,CAAC,CAC3C,CAAC;IAEF,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;KAC3E;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n MultichainAccountWalletId,\n AccountProvider,\n} from '@metamask/account-api';\nimport {\n MultichainAccountWallet,\n toMultichainAccountWalletId,\n type MultichainAccount,\n} from '@metamask/account-api';\nimport type { EntropySourceId } from '@metamask/keyring-api';\nimport type {\n KeyringControllerState,\n KeyringObject,\n} from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { EvmAccountProvider } from './providers/EvmAccountProvider';\nimport { SolAccountProvider } from './providers/SolAccountProvider';\nimport type { MultichainAccountServiceMessenger } from './types';\n\nconst serviceName = 'MultichainAccountService';\n\n/**\n * The options that {@link MultichainAccountService} takes.\n */\ntype MultichainAccountServiceOptions = {\n messenger: MultichainAccountServiceMessenger;\n};\n\n/**\n * Select keyrings from keyring controller state.\n *\n * @param state - The keyring controller state.\n * @returns The keyrings.\n */\nfunction selectKeyringControllerKeyrings(state: KeyringControllerState) {\n return state.keyrings;\n}\n\n/**\n * Service to expose multichain accounts capabilities.\n */\nexport class MultichainAccountService {\n readonly #messenger: MultichainAccountServiceMessenger;\n\n readonly #providers: AccountProvider<InternalAccount>[];\n\n readonly #wallets: Map<\n MultichainAccountWalletId,\n MultichainAccountWallet<InternalAccount>\n >;\n\n /**\n * The name of the service.\n */\n name: typeof serviceName = serviceName;\n\n /**\n * Constructs a new MultichainAccountService.\n *\n * @param options - The options.\n * @param options.messenger - The messenger suited to this\n * MultichainAccountService.\n */\n constructor({ messenger }: MultichainAccountServiceOptions) {\n this.#messenger = messenger;\n this.#wallets = new Map();\n // TODO: Rely on keyring capabilities once the keyring API is used by all keyrings.\n this.#providers = [\n new EvmAccountProvider(this.#messenger),\n new SolAccountProvider(this.#messenger),\n ];\n }\n\n /**\n * Initialize the service and constructs the internal reprensentation of\n * multichain accounts and wallets.\n */\n init(): void {\n // Gather all entropy sources first.\n const state = this.#messenger.call('KeyringController:getState');\n this.#setMultichainAccountWallets(state.keyrings);\n\n this.#messenger.subscribe(\n 'KeyringController:stateChange',\n (keyrings) => {\n this.#setMultichainAccountWallets(keyrings);\n },\n selectKeyringControllerKeyrings,\n );\n }\n\n #setMultichainAccountWallets(keyrings: KeyringObject[]) {\n for (const keyring of keyrings) {\n if (keyring.type === (KeyringTypes.hd as string)) {\n // Only HD keyrings have an entropy source/SRP.\n const entropySource = keyring.metadata.id;\n\n // Do not re-create wallets if they exists. Even if a keyrings got new accounts, this\n // will be handled by the `*AccountProvider`s which are always in-sync with their\n // keyrings and controllers (like the `AccountsController`).\n if (!this.#wallets.has(toMultichainAccountWalletId(entropySource))) {\n // This will automatically \"associate\" all multichain accounts for that wallet\n // (based on the accounts owned by each account providers).\n const wallet = new MultichainAccountWallet({\n entropySource,\n providers: this.#providers,\n });\n\n this.#wallets.set(wallet.id, wallet);\n }\n }\n }\n }\n\n #getWallet(\n entropySource: EntropySourceId,\n ): MultichainAccountWallet<InternalAccount> {\n const wallet = this.#wallets.get(\n toMultichainAccountWalletId(entropySource),\n );\n\n if (!wallet) {\n throw new Error('Unknown wallet, no wallet matching this entropy source');\n }\n\n return wallet;\n }\n\n /**\n * Gets a reference to the multichain account matching this entropy source and group index.\n *\n * @param options - Options.\n * @param options.entropySource - The entropy source of the multichain account.\n * @param options.groupIndex - The group index of the multichain account.\n * @throws If none multichain account match this entropy source and group index.\n * @returns A reference to the multichain account.\n */\n getMultichainAccount({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): MultichainAccount<InternalAccount> {\n const multichainAccount =\n this.#getWallet(entropySource).getMultichainAccount(groupIndex);\n\n if (!multichainAccount) {\n throw new Error(`No multichain account for index: ${groupIndex}`);\n }\n\n return multichainAccount;\n }\n\n /**\n * Gets all multichain accounts for a given entropy source.\n *\n * @param options - Options.\n * @param options.entropySource - The entropy source to query.\n * @throws If no multichain accounts match this entropy source.\n * @returns A list of all multichain accounts.\n */\n getMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): MultichainAccount<InternalAccount>[] {\n return this.#getWallet(entropySource).getMultichainAccounts();\n }\n}\n"]}
1
+ {"version":3,"file":"MultichainAccountService.cjs","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,u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AG,uBAAA,IAAI,yCAAS,CAAC,GAAG,CAC9B,IAAA,yCAA2B,EAAC,aAAa,CAAC,CAC3C,CAAC;IAEF,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CAAC,wDAAwD,CAAC,CAAC;KAC3E;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n MultichainAccountWalletId,\n AccountProvider,\n Bip44Account,\n} from '@metamask/account-api';\nimport {\n isBip44Account,\n MultichainAccountWallet,\n toMultichainAccountWalletId,\n type MultichainAccount,\n} from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport { EvmAccountProvider } from './providers/EvmAccountProvider';\nimport { SolAccountProvider } from './providers/SolAccountProvider';\nimport type { MultichainAccountServiceMessenger } from './types';\n\nexport const serviceName = 'MultichainAccountService';\n\n/**\n * The options that {@link MultichainAccountService} takes.\n */\ntype MultichainAccountServiceOptions = {\n messenger: MultichainAccountServiceMessenger;\n};\n\n/** Reverse mapping object used to map account IDs and their wallet/multichain account. */\ntype AccountContext<Account extends Bip44Account<KeyringAccount>> = {\n wallet: MultichainAccountWallet<Account>;\n multichainAccount: MultichainAccount<Account>;\n};\n\n/**\n * Service to expose multichain accounts capabilities.\n */\nexport class MultichainAccountService {\n readonly #messenger: MultichainAccountServiceMessenger;\n\n readonly #providers: AccountProvider<Bip44Account<InternalAccount>>[];\n\n readonly #wallets: Map<\n MultichainAccountWalletId,\n MultichainAccountWallet<Bip44Account<InternalAccount>>\n >;\n\n readonly #accountIdToContext: Map<\n Bip44Account<InternalAccount>['id'],\n AccountContext<Bip44Account<InternalAccount>>\n >;\n\n /**\n * The name of the service.\n */\n name: typeof serviceName = serviceName;\n\n /**\n * Constructs a new MultichainAccountService.\n *\n * @param options - The options.\n * @param options.messenger - The messenger suited to this\n * MultichainAccountService.\n */\n constructor({ messenger }: MultichainAccountServiceOptions) {\n this.#messenger = messenger;\n this.#wallets = new Map();\n this.#accountIdToContext = new Map();\n // TODO: Rely on keyring capabilities once the keyring API is used by all keyrings.\n this.#providers = [\n new EvmAccountProvider(this.#messenger),\n new SolAccountProvider(this.#messenger),\n ];\n\n this.#messenger.registerActionHandler(\n 'MultichainAccountService:getMultichainAccount',\n (...args) => this.getMultichainAccount(...args),\n );\n this.#messenger.registerActionHandler(\n 'MultichainAccountService:getMultichainAccounts',\n (...args) => this.getMultichainAccounts(...args),\n );\n this.#messenger.registerActionHandler(\n 'MultichainAccountService:getMultichainAccountWallet',\n (...args) => this.getMultichainAccountWallet(...args),\n );\n this.#messenger.registerActionHandler(\n 'MultichainAccountService:getMultichainAccountWallets',\n (...args) => this.getMultichainAccountWallets(...args),\n );\n }\n\n /**\n * Initialize the service and constructs the internal reprensentation of\n * multichain accounts and wallets.\n */\n init(): void {\n // Create initial wallets.\n const { keyrings } = this.#messenger.call('KeyringController:getState');\n for (const keyring of keyrings) {\n if (keyring.type === (KeyringTypes.hd as string)) {\n // Only HD keyrings have an entropy source/SRP.\n const entropySource = keyring.metadata.id;\n\n // This will automatically \"associate\" all multichain accounts for that wallet\n // (based on the accounts owned by each account providers).\n const wallet = new MultichainAccountWallet({\n entropySource,\n providers: this.#providers,\n });\n this.#wallets.set(wallet.id, wallet);\n\n // Reverse mapping between account ID and their multichain wallet/account:\n for (const multichainAccount of wallet.getMultichainAccounts()) {\n for (const account of multichainAccount.getAccounts()) {\n this.#accountIdToContext.set(account.id, {\n wallet,\n multichainAccount,\n });\n }\n }\n }\n }\n\n this.#messenger.subscribe('AccountsController:accountAdded', (account) =>\n this.#handleOnAccountAdded(account),\n );\n this.#messenger.subscribe('AccountsController:accountRemoved', (id) =>\n this.#handleOnAccountRemoved(id),\n );\n }\n\n #handleOnAccountAdded(account: InternalAccount): void {\n // We completely omit non-BIP-44 accounts!\n if (!isBip44Account(account)) {\n return;\n }\n\n let sync = true;\n\n let wallet = this.#wallets.get(\n toMultichainAccountWalletId(account.options.entropy.id),\n );\n if (!wallet) {\n // That's a new wallet.\n wallet = new MultichainAccountWallet({\n entropySource: account.options.entropy.id,\n providers: this.#providers,\n });\n this.#wallets.set(wallet.id, wallet);\n\n // If that's a new wallet wallet. There's nothing to \"force-sync\".\n sync = false;\n }\n\n let multichainAccount = wallet.getMultichainAccount(\n account.options.entropy.groupIndex,\n );\n if (!multichainAccount) {\n // This new account is a new multichain account, let the wallet know\n // it has to re-sync with its providers.\n if (sync) {\n wallet.sync();\n }\n\n multichainAccount = wallet.getMultichainAccount(\n account.options.entropy.groupIndex,\n );\n\n // If that's a new multichain account. There's nothing to \"force-sync\".\n sync = false;\n }\n\n // We have to check against `undefined` in case `getMultichainAccount` is\n // not able to find this multichain account (which should not be possible...)\n if (multichainAccount) {\n if (sync) {\n multichainAccount.sync();\n }\n\n // Same here, this account should have been already grouped in that\n // multichain account.\n this.#accountIdToContext.set(account.id, {\n wallet,\n multichainAccount,\n });\n }\n }\n\n #handleOnAccountRemoved(id: InternalAccount['id']): void {\n // Force sync of the appropriate wallet if an account got removed.\n const found = this.#accountIdToContext.get(id);\n if (found) {\n const { wallet } = found;\n\n wallet.sync();\n }\n\n // Safe to call delete even if the `id` was not referencing a BIP-44 account.\n this.#accountIdToContext.delete(id);\n }\n\n #getWallet(\n entropySource: EntropySourceId,\n ): MultichainAccountWallet<Bip44Account<InternalAccount>> {\n const wallet = this.#wallets.get(\n toMultichainAccountWalletId(entropySource),\n );\n\n if (!wallet) {\n throw new Error('Unknown wallet, no wallet matching this entropy source');\n }\n\n return wallet;\n }\n\n /**\n * Gets a reference to the wallet and multichain account for a given account ID.\n *\n * @param id - Account ID.\n * @returns An object with references to the wallet and multichain account associated for\n * that account ID, or undefined if this account ID is not part of any.\n */\n getMultichainAccountAndWallet(\n id: InternalAccount['id'],\n ): AccountContext<Bip44Account<InternalAccount>> | undefined {\n return this.#accountIdToContext.get(id);\n }\n\n /**\n * Gets a reference to the multichain account wallet matching this entropy source.\n *\n * @param options - Options.\n * @param options.entropySource - The entropy source of the multichain account.\n * @throws If none multichain account match this entropy.\n * @returns A reference to the multichain account wallet.\n */\n getMultichainAccountWallet({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): MultichainAccountWallet<Bip44Account<InternalAccount>> {\n return this.#getWallet(entropySource);\n }\n\n /**\n * Gets an array of all multichain account wallets.\n *\n * @returns An array of all multichain account wallets.\n */\n getMultichainAccountWallets(): MultichainAccountWallet<\n Bip44Account<InternalAccount>\n >[] {\n return Array.from(this.#wallets.values());\n }\n\n /**\n * Gets a reference to the multichain account matching this entropy source and group index.\n *\n * @param options - Options.\n * @param options.entropySource - The entropy source of the multichain account.\n * @param options.groupIndex - The group index of the multichain account.\n * @throws If none multichain account match this entropy source and group index.\n * @returns A reference to the multichain account.\n */\n getMultichainAccount({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): MultichainAccount<Bip44Account<InternalAccount>> {\n const multichainAccount =\n this.#getWallet(entropySource).getMultichainAccount(groupIndex);\n\n if (!multichainAccount) {\n throw new Error(`No multichain account for index: ${groupIndex}`);\n }\n\n return multichainAccount;\n }\n\n /**\n * Gets all multichain accounts for a given entropy source.\n *\n * @param options - Options.\n * @param options.entropySource - The entropy source to query.\n * @throws If no multichain accounts match this entropy source.\n * @returns A list of all multichain accounts.\n */\n getMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): MultichainAccount<Bip44Account<InternalAccount>>[] {\n return this.#getWallet(entropySource).getMultichainAccounts();\n }\n}\n"]}
@@ -1,14 +1,20 @@
1
- import { type MultichainAccount } from "@metamask/account-api";
2
- import type { EntropySourceId } from "@metamask/keyring-api";
1
+ import type { Bip44Account } from "@metamask/account-api";
2
+ import { MultichainAccountWallet, type MultichainAccount } from "@metamask/account-api";
3
+ import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
3
4
  import type { InternalAccount } from "@metamask/keyring-internal-api";
4
5
  import type { MultichainAccountServiceMessenger } from "./types.cjs";
5
- declare const serviceName = "MultichainAccountService";
6
+ export declare const serviceName = "MultichainAccountService";
6
7
  /**
7
8
  * The options that {@link MultichainAccountService} takes.
8
9
  */
9
10
  type MultichainAccountServiceOptions = {
10
11
  messenger: MultichainAccountServiceMessenger;
11
12
  };
13
+ /** Reverse mapping object used to map account IDs and their wallet/multichain account. */
14
+ type AccountContext<Account extends Bip44Account<KeyringAccount>> = {
15
+ wallet: MultichainAccountWallet<Account>;
16
+ multichainAccount: MultichainAccount<Account>;
17
+ };
12
18
  /**
13
19
  * Service to expose multichain accounts capabilities.
14
20
  */
@@ -31,6 +37,31 @@ export declare class MultichainAccountService {
31
37
  * multichain accounts and wallets.
32
38
  */
33
39
  init(): void;
40
+ /**
41
+ * Gets a reference to the wallet and multichain account for a given account ID.
42
+ *
43
+ * @param id - Account ID.
44
+ * @returns An object with references to the wallet and multichain account associated for
45
+ * that account ID, or undefined if this account ID is not part of any.
46
+ */
47
+ getMultichainAccountAndWallet(id: InternalAccount['id']): AccountContext<Bip44Account<InternalAccount>> | undefined;
48
+ /**
49
+ * Gets a reference to the multichain account wallet matching this entropy source.
50
+ *
51
+ * @param options - Options.
52
+ * @param options.entropySource - The entropy source of the multichain account.
53
+ * @throws If none multichain account match this entropy.
54
+ * @returns A reference to the multichain account wallet.
55
+ */
56
+ getMultichainAccountWallet({ entropySource, }: {
57
+ entropySource: EntropySourceId;
58
+ }): MultichainAccountWallet<Bip44Account<InternalAccount>>;
59
+ /**
60
+ * Gets an array of all multichain account wallets.
61
+ *
62
+ * @returns An array of all multichain account wallets.
63
+ */
64
+ getMultichainAccountWallets(): MultichainAccountWallet<Bip44Account<InternalAccount>>[];
34
65
  /**
35
66
  * Gets a reference to the multichain account matching this entropy source and group index.
36
67
  *
@@ -43,7 +74,7 @@ export declare class MultichainAccountService {
43
74
  getMultichainAccount({ entropySource, groupIndex, }: {
44
75
  entropySource: EntropySourceId;
45
76
  groupIndex: number;
46
- }): MultichainAccount<InternalAccount>;
77
+ }): MultichainAccount<Bip44Account<InternalAccount>>;
47
78
  /**
48
79
  * Gets all multichain accounts for a given entropy source.
49
80
  *
@@ -54,7 +85,7 @@ export declare class MultichainAccountService {
54
85
  */
55
86
  getMultichainAccounts({ entropySource, }: {
56
87
  entropySource: EntropySourceId;
57
- }): MultichainAccount<InternalAccount>[];
88
+ }): MultichainAccount<Bip44Account<InternalAccount>>[];
58
89
  }
59
90
  export {};
60
91
  //# sourceMappingURL=MultichainAccountService.d.cts.map
@@ -1 +1 @@
1
- {"version":3,"file":"MultichainAccountService.d.cts","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":"AAIA,OAAO,EAGL,KAAK,iBAAiB,EACvB,8BAA8B;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAM7D,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAItE,OAAO,KAAK,EAAE,iCAAiC,EAAE,oBAAgB;AAEjE,QAAA,MAAM,WAAW,6BAA6B,CAAC;AAE/C;;GAEG;AACH,KAAK,+BAA+B,GAAG;IACrC,SAAS,EAAE,iCAAiC,CAAC;CAC9C,CAAC;AAYF;;GAEG;AACH,qBAAa,wBAAwB;;IAUnC;;OAEG;IACH,IAAI,EAAE,OAAO,WAAW,CAAe;IAEvC;;;;;;OAMG;gBACS,EAAE,SAAS,EAAE,EAAE,+BAA+B;IAU1D;;;OAGG;IACH,IAAI,IAAI,IAAI;IAmDZ;;;;;;;;OAQG;IACH,oBAAoB,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,iBAAiB,CAAC,eAAe,CAAC;IAWtC;;;;;;;OAOG;IACH,qBAAqB,CAAC,EACpB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,iBAAiB,CAAC,eAAe,CAAC,EAAE;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;;;;;;;OAQG;IACH,oBAAoB,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,iBAAiB,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;IAWpD;;;;;;;OAOG;IACH,qBAAqB,CAAC,EACpB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,iBAAiB,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC,EAAE;CAGvD"}
@@ -1,14 +1,20 @@
1
- import { type MultichainAccount } from "@metamask/account-api";
2
- import type { EntropySourceId } from "@metamask/keyring-api";
1
+ import type { Bip44Account } from "@metamask/account-api";
2
+ import { MultichainAccountWallet, type MultichainAccount } from "@metamask/account-api";
3
+ import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
3
4
  import type { InternalAccount } from "@metamask/keyring-internal-api";
4
5
  import type { MultichainAccountServiceMessenger } from "./types.mjs";
5
- declare const serviceName = "MultichainAccountService";
6
+ export declare const serviceName = "MultichainAccountService";
6
7
  /**
7
8
  * The options that {@link MultichainAccountService} takes.
8
9
  */
9
10
  type MultichainAccountServiceOptions = {
10
11
  messenger: MultichainAccountServiceMessenger;
11
12
  };
13
+ /** Reverse mapping object used to map account IDs and their wallet/multichain account. */
14
+ type AccountContext<Account extends Bip44Account<KeyringAccount>> = {
15
+ wallet: MultichainAccountWallet<Account>;
16
+ multichainAccount: MultichainAccount<Account>;
17
+ };
12
18
  /**
13
19
  * Service to expose multichain accounts capabilities.
14
20
  */
@@ -31,6 +37,31 @@ export declare class MultichainAccountService {
31
37
  * multichain accounts and wallets.
32
38
  */
33
39
  init(): void;
40
+ /**
41
+ * Gets a reference to the wallet and multichain account for a given account ID.
42
+ *
43
+ * @param id - Account ID.
44
+ * @returns An object with references to the wallet and multichain account associated for
45
+ * that account ID, or undefined if this account ID is not part of any.
46
+ */
47
+ getMultichainAccountAndWallet(id: InternalAccount['id']): AccountContext<Bip44Account<InternalAccount>> | undefined;
48
+ /**
49
+ * Gets a reference to the multichain account wallet matching this entropy source.
50
+ *
51
+ * @param options - Options.
52
+ * @param options.entropySource - The entropy source of the multichain account.
53
+ * @throws If none multichain account match this entropy.
54
+ * @returns A reference to the multichain account wallet.
55
+ */
56
+ getMultichainAccountWallet({ entropySource, }: {
57
+ entropySource: EntropySourceId;
58
+ }): MultichainAccountWallet<Bip44Account<InternalAccount>>;
59
+ /**
60
+ * Gets an array of all multichain account wallets.
61
+ *
62
+ * @returns An array of all multichain account wallets.
63
+ */
64
+ getMultichainAccountWallets(): MultichainAccountWallet<Bip44Account<InternalAccount>>[];
34
65
  /**
35
66
  * Gets a reference to the multichain account matching this entropy source and group index.
36
67
  *
@@ -43,7 +74,7 @@ export declare class MultichainAccountService {
43
74
  getMultichainAccount({ entropySource, groupIndex, }: {
44
75
  entropySource: EntropySourceId;
45
76
  groupIndex: number;
46
- }): MultichainAccount<InternalAccount>;
77
+ }): MultichainAccount<Bip44Account<InternalAccount>>;
47
78
  /**
48
79
  * Gets all multichain accounts for a given entropy source.
49
80
  *
@@ -54,7 +85,7 @@ export declare class MultichainAccountService {
54
85
  */
55
86
  getMultichainAccounts({ entropySource, }: {
56
87
  entropySource: EntropySourceId;
57
- }): MultichainAccount<InternalAccount>[];
88
+ }): MultichainAccount<Bip44Account<InternalAccount>>[];
58
89
  }
59
90
  export {};
60
91
  //# sourceMappingURL=MultichainAccountService.d.mts.map
@@ -1 +1 @@
1
- {"version":3,"file":"MultichainAccountService.d.mts","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":"AAIA,OAAO,EAGL,KAAK,iBAAiB,EACvB,8BAA8B;AAC/B,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAM7D,OAAO,KAAK,EAAE,eAAe,EAAE,uCAAuC;AAItE,OAAO,KAAK,EAAE,iCAAiC,EAAE,oBAAgB;AAEjE,QAAA,MAAM,WAAW,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