@metamask-previews/multichain-account-controller 0.0.0-preview-7b670bb5

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 (44) hide show
  1. package/CHANGELOG.md +10 -0
  2. package/LICENSE +20 -0
  3. package/README.md +15 -0
  4. package/dist/MultichainAccountController.cjs +88 -0
  5. package/dist/MultichainAccountController.cjs.map +1 -0
  6. package/dist/MultichainAccountController.d.cts +66 -0
  7. package/dist/MultichainAccountController.d.cts.map +1 -0
  8. package/dist/MultichainAccountController.d.mts +66 -0
  9. package/dist/MultichainAccountController.d.mts.map +1 -0
  10. package/dist/MultichainAccountController.mjs +84 -0
  11. package/dist/MultichainAccountController.mjs.map +1 -0
  12. package/dist/index.cjs +13 -0
  13. package/dist/index.cjs.map +1 -0
  14. package/dist/index.d.cts +8 -0
  15. package/dist/index.d.cts.map +1 -0
  16. package/dist/index.d.mts +8 -0
  17. package/dist/index.d.mts.map +1 -0
  18. package/dist/index.mjs +10 -0
  19. package/dist/index.mjs.map +1 -0
  20. package/dist/providers/EvmAccountProvider.cjs +99 -0
  21. package/dist/providers/EvmAccountProvider.cjs.map +1 -0
  22. package/dist/providers/EvmAccountProvider.d.cts +64 -0
  23. package/dist/providers/EvmAccountProvider.d.cts.map +1 -0
  24. package/dist/providers/EvmAccountProvider.d.mts +64 -0
  25. package/dist/providers/EvmAccountProvider.d.mts.map +1 -0
  26. package/dist/providers/EvmAccountProvider.mjs +95 -0
  27. package/dist/providers/EvmAccountProvider.mjs.map +1 -0
  28. package/dist/providers/SolAccountProvider.cjs +102 -0
  29. package/dist/providers/SolAccountProvider.cjs.map +1 -0
  30. package/dist/providers/SolAccountProvider.d.cts +65 -0
  31. package/dist/providers/SolAccountProvider.d.cts.map +1 -0
  32. package/dist/providers/SolAccountProvider.d.mts +65 -0
  33. package/dist/providers/SolAccountProvider.d.mts.map +1 -0
  34. package/dist/providers/SolAccountProvider.mjs +98 -0
  35. package/dist/providers/SolAccountProvider.mjs.map +1 -0
  36. package/dist/types.cjs +3 -0
  37. package/dist/types.cjs.map +1 -0
  38. package/dist/types.d.cts +2 -0
  39. package/dist/types.d.cts.map +1 -0
  40. package/dist/types.d.mts +2 -0
  41. package/dist/types.d.mts.map +1 -0
  42. package/dist/types.mjs +2 -0
  43. package/dist/types.mjs.map +1 -0
  44. package/package.json +80 -0
package/CHANGELOG.md ADDED
@@ -0,0 +1,10 @@
1
+ # Changelog
2
+
3
+ All notable changes to this project will be documented in this file.
4
+
5
+ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
6
+ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
7
+
8
+ ## [Unreleased]
9
+
10
+ [Unreleased]: https://github.com/MetaMask/core/
package/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 MetaMask
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
package/README.md ADDED
@@ -0,0 +1,15 @@
1
+ # `@metamask/multichain-account-controller`
2
+
3
+ example
4
+
5
+ ## Installation
6
+
7
+ `yarn add @metamask/multichain-account-controller`
8
+
9
+ or
10
+
11
+ `npm install @metamask/multichain-account-controller`
12
+
13
+ ## Contributing
14
+
15
+ This package is part of a monorepo. Instructions for contributing can be found in the [monorepo README](https://github.com/MetaMask/core#readme).
@@ -0,0 +1,88 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _MultichainAccountController_instances, _MultichainAccountController_messenger, _MultichainAccountController_providers, _MultichainAccountController_wallets, _MultichainAccountController_getWallet;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.MultichainAccountController = void 0;
16
+ const keyring_api_1 = require("@metamask/keyring-api");
17
+ const keyring_controller_1 = require("@metamask/keyring-controller");
18
+ const keyring_snap_client_1 = require("@metamask/keyring-snap-client");
19
+ const multichain_account_api_1 = require("@metamask/multichain-account-api");
20
+ const snaps_utils_1 = require("@metamask/snaps-utils");
21
+ const EvmAccountProvider_1 = require("./providers/EvmAccountProvider.cjs");
22
+ const SolAccountProvider_1 = require("./providers/SolAccountProvider.cjs");
23
+ /**
24
+ * Stateless controller to expose multichain accounts capabilities.
25
+ */
26
+ class MultichainAccountController {
27
+ /**
28
+ * Constructs a new MultichainAccountController.
29
+ *
30
+ * @param options - The options.
31
+ * @param options.messenger - The messenger suited to this
32
+ * MultichainAccountController.
33
+ */
34
+ constructor({ messenger }) {
35
+ _MultichainAccountController_instances.add(this);
36
+ _MultichainAccountController_messenger.set(this, void 0);
37
+ _MultichainAccountController_providers.set(this, void 0);
38
+ _MultichainAccountController_wallets.set(this, void 0);
39
+ __classPrivateFieldSet(this, _MultichainAccountController_messenger, messenger, "f");
40
+ __classPrivateFieldSet(this, _MultichainAccountController_wallets, new Map(), "f");
41
+ // TODO: Rely on keyring capabilities once the keyring API is used by all keyrings.
42
+ __classPrivateFieldSet(this, _MultichainAccountController_providers, [
43
+ new EvmAccountProvider_1.EvmAccountProvider(__classPrivateFieldGet(this, _MultichainAccountController_messenger, "f")),
44
+ new SolAccountProvider_1.SolAccountProvider(__classPrivateFieldGet(this, _MultichainAccountController_messenger, "f")),
45
+ ], "f");
46
+ // Gather all entropy sources first.
47
+ const entropySources = new Set();
48
+ for (const provider of __classPrivateFieldGet(this, _MultichainAccountController_providers, "f")) {
49
+ for (const entropySource of provider.getEntropySources()) {
50
+ entropySources.add(entropySource);
51
+ }
52
+ }
53
+ for (const entropySource of entropySources) {
54
+ // This will automatically create all multichain accounts for that wallet (based
55
+ // on the accounts owned by each account providers).
56
+ const wallet = new multichain_account_api_1.MultichainAccountWalletAdapter({
57
+ entropySource,
58
+ providers: __classPrivateFieldGet(this, _MultichainAccountController_providers, "f"),
59
+ });
60
+ __classPrivateFieldGet(this, _MultichainAccountController_wallets, "f").set(wallet.id, wallet);
61
+ }
62
+ }
63
+ getMultichainAccount({ entropySource, groupIndex, }) {
64
+ const multichainAccount = __classPrivateFieldGet(this, _MultichainAccountController_instances, "m", _MultichainAccountController_getWallet).call(this, entropySource).accounts[groupIndex];
65
+ if (!multichainAccount) {
66
+ throw new Error(`No multichain account for index: ${groupIndex}`);
67
+ }
68
+ return multichainAccount;
69
+ }
70
+ getMultichainAccounts({ entropySource, }) {
71
+ return __classPrivateFieldGet(this, _MultichainAccountController_instances, "m", _MultichainAccountController_getWallet).call(this, entropySource).accounts;
72
+ }
73
+ async createNextMultichainAccount({ entropySource, }) {
74
+ return await __classPrivateFieldGet(this, _MultichainAccountController_instances, "m", _MultichainAccountController_getWallet).call(this, entropySource).createNextMultichainAccount();
75
+ }
76
+ async discoverAndCreateMultichainAccounts({ entropySource, }) {
77
+ return await __classPrivateFieldGet(this, _MultichainAccountController_instances, "m", _MultichainAccountController_getWallet).call(this, entropySource).discoverAndCreateMultichainAccounts();
78
+ }
79
+ }
80
+ exports.MultichainAccountController = MultichainAccountController;
81
+ _MultichainAccountController_messenger = new WeakMap(), _MultichainAccountController_providers = new WeakMap(), _MultichainAccountController_wallets = new WeakMap(), _MultichainAccountController_instances = new WeakSet(), _MultichainAccountController_getWallet = function _MultichainAccountController_getWallet(entropySource) {
82
+ const wallet = __classPrivateFieldGet(this, _MultichainAccountController_wallets, "f").get((0, multichain_account_api_1.toMultichainAccountWalletId)(entropySource));
83
+ if (!wallet) {
84
+ throw new Error('Unknown wallet, not wallet matching this entropy source');
85
+ }
86
+ return wallet;
87
+ };
88
+ //# sourceMappingURL=MultichainAccountController.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MultichainAccountController.cjs","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAMA,uDAAuE;AAMvE,qEAA4D;AAK5D,uEAA8D;AAK9D,6EAM0C;AAG1C,uDAAoD;AAGpD,2EAAoE;AACpE,2EAAoE;AAiDpE;;GAEG;AACH,MAAa,2BAA2B;IAOtC;;;;;;OAMG;IACH,YAAY,EAAE,SAAS,EAAsC;;QAbpD,yDAAiD;QAEjD,yDAA8B;QAE9B,uDAAkE;QAUzE,uBAAA,IAAI,0CAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,wCAAY,IA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AAA,IAAI,sFAAW,MAAf,IAAI,EAAY,aAAa,CAAC,CAAC,2BAA2B,EAAE,CAAC;IAC5E,CAAC;IAED,KAAK,CAAC,mCAAmC,CAAC,EACxC,aAAa,GAGd;QACC,OAAO,MAAM,uBAAA,IAAI,sFAAW,MAAf,IAAI,EACf,aAAa,CACd,CAAC,mCAAmC,EAAE,CAAC;IAC1C,CAAC;CACF;AAnGD,kEAmGC;uTAxDY,aAA8B;IACvC,MAAM,MAAM,GAAG,uBAAA,IAAI,4CAAS,CAAC,GAAG,CAC9B,IAAA,oDAA2B,EAAC,aAAa,CAAC,CAC3C,CAAC;IAEF,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;KACH;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n AccountsControllerGetAccountAction,\n AccountsControllerGetAccountByAddressAction,\n AccountsControllerListMultichainAccountsAction,\n} from '@metamask/accounts-controller';\nimport type { RestrictedMessenger } from '@metamask/base-controller';\nimport { SolScope, type EntropySourceId } from '@metamask/keyring-api';\nimport type {\n KeyringControllerWithKeyringAction,\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type {\n AccountProvider,\n MultichainAccountWalletId,\n} from '@metamask/multichain-account-api';\nimport {\n MultichainAccountAdapter,\n MultichainAccountWalletAdapter,\n toMultichainAccountWalletId,\n type MultichainAccount,\n type MultichainAccountWallet,\n} from '@metamask/multichain-account-api';\nimport type { HandleSnapRequest as SnapControllerHandleSnapRequestAction } from '@metamask/snaps-controllers';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { EvmAccountProvider } from './providers/EvmAccountProvider';\nimport { SolAccountProvider } from './providers/SolAccountProvider';\n\n/**\n * All actions that {@link MultichainAccountController} registers so that other\n * modules can call them.\n */\nexport type MultichainAccountControllerActions = never;\n/**\n * All events that {@link MultichainAccountController} publishes so that other modules\n * can subscribe to them.\n */\nexport type MultichainAccountControllerEvents = never;\n\n/**\n * All actions registered by other modules that {@link MultichainAccountController}\n * calls.\n */\nexport type AllowedActions =\n | AccountsControllerListMultichainAccountsAction\n | AccountsControllerGetAccountAction\n | AccountsControllerGetAccountByAddressAction\n | SnapControllerHandleSnapRequestAction\n | KeyringControllerWithKeyringAction;\n\n/**\n * All events published by other modules that {@link MultichainAccountController}\n * subscribes to.\n */\nexport type AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events that\n * {@link MultichainAccountController} needs to access.\n */\nexport type MultichainAccountControllerMessenger = RestrictedMessenger<\n 'MultichainAccountController',\n MultichainAccountControllerActions | AllowedActions,\n MultichainAccountControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * The options that {@link MultichainAccountController} takes.\n */\ntype MultichainAccountControllerOptions = {\n messenger: MultichainAccountControllerMessenger;\n};\n\n/**\n * Stateless controller to expose multichain accounts capabilities.\n */\nexport class MultichainAccountController {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n readonly #providers: AccountProvider[];\n\n readonly #wallets: Map<MultichainAccountWalletId, MultichainAccountWallet>;\n\n /**\n * Constructs a new MultichainAccountController.\n *\n * @param options - The options.\n * @param options.messenger - The messenger suited to this\n * MultichainAccountController.\n */\n constructor({ messenger }: MultichainAccountControllerOptions) {\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 // Gather all entropy sources first.\n const entropySources = new Set<EntropySourceId>();\n for (const provider of this.#providers) {\n for (const entropySource of provider.getEntropySources()) {\n entropySources.add(entropySource);\n }\n }\n\n for (const entropySource of entropySources) {\n // This will automatically create all multichain accounts for that wallet (based\n // on the accounts owned by each account providers).\n const wallet = new MultichainAccountWalletAdapter({\n entropySource,\n providers: this.#providers,\n });\n\n this.#wallets.set(wallet.id, wallet);\n }\n }\n\n #getWallet(entropySource: EntropySourceId): MultichainAccountWallet {\n const wallet = this.#wallets.get(\n toMultichainAccountWalletId(entropySource),\n );\n\n if (!wallet) {\n throw new Error(\n 'Unknown wallet, not wallet matching this entropy source',\n );\n }\n\n return wallet;\n }\n\n getMultichainAccount({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): MultichainAccount {\n const multichainAccount =\n this.#getWallet(entropySource).accounts[groupIndex];\n\n if (!multichainAccount) {\n throw new Error(`No multichain account for index: ${groupIndex}`);\n }\n\n return multichainAccount;\n }\n\n getMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): MultichainAccount[] {\n return this.#getWallet(entropySource).accounts;\n }\n\n async createNextMultichainAccount({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccount> {\n return await this.#getWallet(entropySource).createNextMultichainAccount();\n }\n\n async discoverAndCreateMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccount[]> {\n return await this.#getWallet(\n entropySource,\n ).discoverAndCreateMultichainAccounts();\n }\n}\n"]}
@@ -0,0 +1,66 @@
1
+ import type { AccountsControllerGetAccountAction, AccountsControllerGetAccountByAddressAction, AccountsControllerListMultichainAccountsAction } from "@metamask/accounts-controller";
2
+ import type { RestrictedMessenger } from "@metamask/base-controller";
3
+ import { type EntropySourceId } from "@metamask/keyring-api";
4
+ import type { KeyringControllerWithKeyringAction } from "@metamask/keyring-controller";
5
+ import { type MultichainAccount } from "@metamask/multichain-account-api";
6
+ import type { HandleSnapRequest as SnapControllerHandleSnapRequestAction } from "@metamask/snaps-controllers";
7
+ /**
8
+ * All actions that {@link MultichainAccountController} registers so that other
9
+ * modules can call them.
10
+ */
11
+ export type MultichainAccountControllerActions = never;
12
+ /**
13
+ * All events that {@link MultichainAccountController} publishes so that other modules
14
+ * can subscribe to them.
15
+ */
16
+ export type MultichainAccountControllerEvents = never;
17
+ /**
18
+ * All actions registered by other modules that {@link MultichainAccountController}
19
+ * calls.
20
+ */
21
+ export type AllowedActions = AccountsControllerListMultichainAccountsAction | AccountsControllerGetAccountAction | AccountsControllerGetAccountByAddressAction | SnapControllerHandleSnapRequestAction | KeyringControllerWithKeyringAction;
22
+ /**
23
+ * All events published by other modules that {@link MultichainAccountController}
24
+ * subscribes to.
25
+ */
26
+ export type AllowedEvents = never;
27
+ /**
28
+ * The messenger restricted to actions and events that
29
+ * {@link MultichainAccountController} needs to access.
30
+ */
31
+ export type MultichainAccountControllerMessenger = RestrictedMessenger<'MultichainAccountController', MultichainAccountControllerActions | AllowedActions, MultichainAccountControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
32
+ /**
33
+ * The options that {@link MultichainAccountController} takes.
34
+ */
35
+ type MultichainAccountControllerOptions = {
36
+ messenger: MultichainAccountControllerMessenger;
37
+ };
38
+ /**
39
+ * Stateless controller to expose multichain accounts capabilities.
40
+ */
41
+ export declare class MultichainAccountController {
42
+ #private;
43
+ /**
44
+ * Constructs a new MultichainAccountController.
45
+ *
46
+ * @param options - The options.
47
+ * @param options.messenger - The messenger suited to this
48
+ * MultichainAccountController.
49
+ */
50
+ constructor({ messenger }: MultichainAccountControllerOptions);
51
+ getMultichainAccount({ entropySource, groupIndex, }: {
52
+ entropySource: EntropySourceId;
53
+ groupIndex: number;
54
+ }): MultichainAccount;
55
+ getMultichainAccounts({ entropySource, }: {
56
+ entropySource: EntropySourceId;
57
+ }): MultichainAccount[];
58
+ createNextMultichainAccount({ entropySource, }: {
59
+ entropySource: EntropySourceId;
60
+ }): Promise<MultichainAccount>;
61
+ discoverAndCreateMultichainAccounts({ entropySource, }: {
62
+ entropySource: EntropySourceId;
63
+ }): Promise<MultichainAccount[]>;
64
+ }
65
+ export {};
66
+ //# sourceMappingURL=MultichainAccountController.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MultichainAccountController.d.cts","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kCAAkC,EAClC,2CAA2C,EAC3C,8CAA8C,EAC/C,sCAAsC;AACvC,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAAkC;AACrE,OAAO,EAAY,KAAK,eAAe,EAAE,8BAA8B;AACvE,OAAO,KAAK,EACV,kCAAkC,EAGnC,qCAAqC;AAWtC,OAAO,EAIL,KAAK,iBAAiB,EAEvB,yCAAyC;AAC1C,OAAO,KAAK,EAAE,iBAAiB,IAAI,qCAAqC,EAAE,oCAAoC;AAQ9G;;;GAGG;AACH,MAAM,MAAM,kCAAkC,GAAG,KAAK,CAAC;AACvD;;;GAGG;AACH,MAAM,MAAM,iCAAiC,GAAG,KAAK,CAAC;AAEtD;;;GAGG;AACH,MAAM,MAAM,cAAc,GACtB,8CAA8C,GAC9C,kCAAkC,GAClC,2CAA2C,GAC3C,qCAAqC,GACrC,kCAAkC,CAAC;AAEvC;;;GAGG;AACH,MAAM,MAAM,aAAa,GAAG,KAAK,CAAC;AAElC;;;GAGG;AACH,MAAM,MAAM,oCAAoC,GAAG,mBAAmB,CACpE,6BAA6B,EAC7B,kCAAkC,GAAG,cAAc,EACnD,iCAAiC,GAAG,aAAa,EACjD,cAAc,CAAC,MAAM,CAAC,EACtB,aAAa,CAAC,MAAM,CAAC,CACtB,CAAC;AAEF;;GAEG;AACH,KAAK,kCAAkC,GAAG;IACxC,SAAS,EAAE,oCAAoC,CAAC;CACjD,CAAC;AAEF;;GAEG;AACH,qBAAa,2BAA2B;;IAOtC;;;;;;OAMG;gBACS,EAAE,SAAS,EAAE,EAAE,kCAAkC;IA2C7D,oBAAoB,CAAC,EACnB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,iBAAiB;IAWrB,qBAAqB,CAAC,EACpB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,iBAAiB,EAAE;IAIjB,2BAA2B,CAAC,EAChC,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAIxB,mCAAmC,CAAC,EACxC,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,OAAO,CAAC,iBAAiB,EAAE,CAAC;CAKjC"}
@@ -0,0 +1,66 @@
1
+ import type { AccountsControllerGetAccountAction, AccountsControllerGetAccountByAddressAction, AccountsControllerListMultichainAccountsAction } from "@metamask/accounts-controller";
2
+ import type { RestrictedMessenger } from "@metamask/base-controller";
3
+ import { type EntropySourceId } from "@metamask/keyring-api";
4
+ import type { KeyringControllerWithKeyringAction } from "@metamask/keyring-controller";
5
+ import { type MultichainAccount } from "@metamask/multichain-account-api";
6
+ import type { HandleSnapRequest as SnapControllerHandleSnapRequestAction } from "@metamask/snaps-controllers";
7
+ /**
8
+ * All actions that {@link MultichainAccountController} registers so that other
9
+ * modules can call them.
10
+ */
11
+ export type MultichainAccountControllerActions = never;
12
+ /**
13
+ * All events that {@link MultichainAccountController} publishes so that other modules
14
+ * can subscribe to them.
15
+ */
16
+ export type MultichainAccountControllerEvents = never;
17
+ /**
18
+ * All actions registered by other modules that {@link MultichainAccountController}
19
+ * calls.
20
+ */
21
+ export type AllowedActions = AccountsControllerListMultichainAccountsAction | AccountsControllerGetAccountAction | AccountsControllerGetAccountByAddressAction | SnapControllerHandleSnapRequestAction | KeyringControllerWithKeyringAction;
22
+ /**
23
+ * All events published by other modules that {@link MultichainAccountController}
24
+ * subscribes to.
25
+ */
26
+ export type AllowedEvents = never;
27
+ /**
28
+ * The messenger restricted to actions and events that
29
+ * {@link MultichainAccountController} needs to access.
30
+ */
31
+ export type MultichainAccountControllerMessenger = RestrictedMessenger<'MultichainAccountController', MultichainAccountControllerActions | AllowedActions, MultichainAccountControllerEvents | AllowedEvents, AllowedActions['type'], AllowedEvents['type']>;
32
+ /**
33
+ * The options that {@link MultichainAccountController} takes.
34
+ */
35
+ type MultichainAccountControllerOptions = {
36
+ messenger: MultichainAccountControllerMessenger;
37
+ };
38
+ /**
39
+ * Stateless controller to expose multichain accounts capabilities.
40
+ */
41
+ export declare class MultichainAccountController {
42
+ #private;
43
+ /**
44
+ * Constructs a new MultichainAccountController.
45
+ *
46
+ * @param options - The options.
47
+ * @param options.messenger - The messenger suited to this
48
+ * MultichainAccountController.
49
+ */
50
+ constructor({ messenger }: MultichainAccountControllerOptions);
51
+ getMultichainAccount({ entropySource, groupIndex, }: {
52
+ entropySource: EntropySourceId;
53
+ groupIndex: number;
54
+ }): MultichainAccount;
55
+ getMultichainAccounts({ entropySource, }: {
56
+ entropySource: EntropySourceId;
57
+ }): MultichainAccount[];
58
+ createNextMultichainAccount({ entropySource, }: {
59
+ entropySource: EntropySourceId;
60
+ }): Promise<MultichainAccount>;
61
+ discoverAndCreateMultichainAccounts({ entropySource, }: {
62
+ entropySource: EntropySourceId;
63
+ }): Promise<MultichainAccount[]>;
64
+ }
65
+ export {};
66
+ //# sourceMappingURL=MultichainAccountController.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MultichainAccountController.d.mts","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,kCAAkC,EAClC,2CAA2C,EAC3C,8CAA8C,EAC/C,sCAAsC;AACvC,OAAO,KAAK,EAAE,mBAAmB,EAAE,kCAAkC;AACrE,OAAO,EAAY,KAAK,eAAe,EAAE,8BAA8B;AACvE,OAAO,KAAK,EACV,kCAAkC,EAGnC,qCAAqC;AAWtC,OAAO,EAIL,KAAK,iBAAiB,EAEvB,yCAAyC;AAC1C,OAAO,KA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.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
2
+ if (kind === "m") throw new TypeError("Private method is not writable");
3
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
4
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
5
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
6
+ };
7
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
8
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
9
+ 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");
10
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
11
+ };
12
+ var _MultichainAccountController_instances, _MultichainAccountController_messenger, _MultichainAccountController_providers, _MultichainAccountController_wallets, _MultichainAccountController_getWallet;
13
+ import { SolScope } from "@metamask/keyring-api";
14
+ import { KeyringTypes } from "@metamask/keyring-controller";
15
+ import { KeyringClient } from "@metamask/keyring-snap-client";
16
+ import { MultichainAccountAdapter, MultichainAccountWalletAdapter, toMultichainAccountWalletId } from "@metamask/multichain-account-api";
17
+ import { HandlerType } from "@metamask/snaps-utils";
18
+ import { EvmAccountProvider } from "./providers/EvmAccountProvider.mjs";
19
+ import { SolAccountProvider } from "./providers/SolAccountProvider.mjs";
20
+ /**
21
+ * Stateless controller to expose multichain accounts capabilities.
22
+ */
23
+ export class MultichainAccountController {
24
+ /**
25
+ * Constructs a new MultichainAccountController.
26
+ *
27
+ * @param options - The options.
28
+ * @param options.messenger - The messenger suited to this
29
+ * MultichainAccountController.
30
+ */
31
+ constructor({ messenger }) {
32
+ _MultichainAccountController_instances.add(this);
33
+ _MultichainAccountController_messenger.set(this, void 0);
34
+ _MultichainAccountController_providers.set(this, void 0);
35
+ _MultichainAccountController_wallets.set(this, void 0);
36
+ __classPrivateFieldSet(this, _MultichainAccountController_messenger, messenger, "f");
37
+ __classPrivateFieldSet(this, _MultichainAccountController_wallets, new Map(), "f");
38
+ // TODO: Rely on keyring capabilities once the keyring API is used by all keyrings.
39
+ __classPrivateFieldSet(this, _MultichainAccountController_providers, [
40
+ new EvmAccountProvider(__classPrivateFieldGet(this, _MultichainAccountController_messenger, "f")),
41
+ new SolAccountProvider(__classPrivateFieldGet(this, _MultichainAccountController_messenger, "f")),
42
+ ], "f");
43
+ // Gather all entropy sources first.
44
+ const entropySources = new Set();
45
+ for (const provider of __classPrivateFieldGet(this, _MultichainAccountController_providers, "f")) {
46
+ for (const entropySource of provider.getEntropySources()) {
47
+ entropySources.add(entropySource);
48
+ }
49
+ }
50
+ for (const entropySource of entropySources) {
51
+ // This will automatically create all multichain accounts for that wallet (based
52
+ // on the accounts owned by each account providers).
53
+ const wallet = new MultichainAccountWalletAdapter({
54
+ entropySource,
55
+ providers: __classPrivateFieldGet(this, _MultichainAccountController_providers, "f"),
56
+ });
57
+ __classPrivateFieldGet(this, _MultichainAccountController_wallets, "f").set(wallet.id, wallet);
58
+ }
59
+ }
60
+ getMultichainAccount({ entropySource, groupIndex, }) {
61
+ const multichainAccount = __classPrivateFieldGet(this, _MultichainAccountController_instances, "m", _MultichainAccountController_getWallet).call(this, entropySource).accounts[groupIndex];
62
+ if (!multichainAccount) {
63
+ throw new Error(`No multichain account for index: ${groupIndex}`);
64
+ }
65
+ return multichainAccount;
66
+ }
67
+ getMultichainAccounts({ entropySource, }) {
68
+ return __classPrivateFieldGet(this, _MultichainAccountController_instances, "m", _MultichainAccountController_getWallet).call(this, entropySource).accounts;
69
+ }
70
+ async createNextMultichainAccount({ entropySource, }) {
71
+ return await __classPrivateFieldGet(this, _MultichainAccountController_instances, "m", _MultichainAccountController_getWallet).call(this, entropySource).createNextMultichainAccount();
72
+ }
73
+ async discoverAndCreateMultichainAccounts({ entropySource, }) {
74
+ return await __classPrivateFieldGet(this, _MultichainAccountController_instances, "m", _MultichainAccountController_getWallet).call(this, entropySource).discoverAndCreateMultichainAccounts();
75
+ }
76
+ }
77
+ _MultichainAccountController_messenger = new WeakMap(), _MultichainAccountController_providers = new WeakMap(), _MultichainAccountController_wallets = new WeakMap(), _MultichainAccountController_instances = new WeakSet(), _MultichainAccountController_getWallet = function _MultichainAccountController_getWallet(entropySource) {
78
+ const wallet = __classPrivateFieldGet(this, _MultichainAccountController_wallets, "f").get(toMultichainAccountWalletId(entropySource));
79
+ if (!wallet) {
80
+ throw new Error('Unknown wallet, not wallet matching this entropy source');
81
+ }
82
+ return wallet;
83
+ };
84
+ //# sourceMappingURL=MultichainAccountController.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"MultichainAccountController.mjs","sourceRoot":"","sources":["../src/MultichainAccountController.ts"],"names":[],"mappings":";;;;;;;;;;;;AAMA,OAAO,EAAE,QAAQ,EAAwB,8BAA8B;AAMvE,OAAO,EAAE,YAAY,EAAE,qCAAqC;AAK5D,OAAO,EAAE,aAAa,EAAE,sCAAsC;AAK9D,OAAO,EACL,wBAAwB,EACxB,8BAA8B,EAC9B,2BAA2B,EAG5B,yCAAyC;AAG1C,OAAO,EAAE,WAAW,EAAE,8BAA8B;AAGpD,OAAO,EAAE,kBAAkB,EAAE,2CAAuC;AACpE,OAAO,EAAE,kBAAkB,EAAE,2CAAuC;AAiDpE;;GAEG;AACH,MAAM,OAAO,2BAA2B;IAOtC;;;;;;OAMG;IACH,YAAY,EAAE,SAAS,EAAsC;;QAbpD,yDAAiD;QAEjD,yDAA8B;QAE9B,uDAAkE;QAUzE,uBAAA,IAAI,0CAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,wCAAY,IAAI,GAAG,EAAE,MAAA,CAAC;QAC1B,mFAAmF;QACnF,uBAAA,IAAI,0CAAc;YAChB,IAAI,kBAAkB,CAAC,uBAAA,IAAI,8CAAW,CAAC;YACvC,IAAI,kBAAkB,CAAC,uBAAA,IAAI,8CAAW,CAAC;SACxC,MAAA,CAAC;QAE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CACd,CAAC,mCAAmC,EAAE,CAAC;IAC1C,CAAC;CACF;uTAxDY,aAA8B;IACvC,MAAM,MAAM,GAAG,uBAAA,IAAI,4CAAS,CAAC,GAAG,CAC9B,2BAA2B,CAAC,aAAa,CAAC,CAC3C,CAAC;IAEF,IAAI,CAAC,MAAM,EAAE;QACX,MAAM,IAAI,KAAK,CACb,yDAAyD,CAC1D,CAAC;KACH;IAED,OAAO,MAAM,CAAC;AAChB,CAAC","sourcesContent":["import type {\n AccountsControllerGetAccountAction,\n AccountsControllerGetAccountByAddressAction,\n AccountsControllerListMultichainAccountsAction,\n} from '@metamask/accounts-controller';\nimport type { RestrictedMessenger } from '@metamask/base-controller';\nimport { SolScope, type EntropySourceId } from '@metamask/keyring-api';\nimport type {\n KeyringControllerWithKeyringAction,\n KeyringMetadata,\n KeyringSelector,\n} from '@metamask/keyring-controller';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport { KeyringClient } from '@metamask/keyring-snap-client';\nimport type {\n AccountProvider,\n MultichainAccountWalletId,\n} from '@metamask/multichain-account-api';\nimport {\n MultichainAccountAdapter,\n MultichainAccountWalletAdapter,\n toMultichainAccountWalletId,\n type MultichainAccount,\n type MultichainAccountWallet,\n} from '@metamask/multichain-account-api';\nimport type { HandleSnapRequest as SnapControllerHandleSnapRequestAction } from '@metamask/snaps-controllers';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport { HandlerType } from '@metamask/snaps-utils';\nimport type { Json, JsonRpcRequest } from '@metamask/utils';\n\nimport { EvmAccountProvider } from './providers/EvmAccountProvider';\nimport { SolAccountProvider } from './providers/SolAccountProvider';\n\n/**\n * All actions that {@link MultichainAccountController} registers so that other\n * modules can call them.\n */\nexport type MultichainAccountControllerActions = never;\n/**\n * All events that {@link MultichainAccountController} publishes so that other modules\n * can subscribe to them.\n */\nexport type MultichainAccountControllerEvents = never;\n\n/**\n * All actions registered by other modules that {@link MultichainAccountController}\n * calls.\n */\nexport type AllowedActions =\n | AccountsControllerListMultichainAccountsAction\n | AccountsControllerGetAccountAction\n | AccountsControllerGetAccountByAddressAction\n | SnapControllerHandleSnapRequestAction\n | KeyringControllerWithKeyringAction;\n\n/**\n * All events published by other modules that {@link MultichainAccountController}\n * subscribes to.\n */\nexport type AllowedEvents = never;\n\n/**\n * The messenger restricted to actions and events that\n * {@link MultichainAccountController} needs to access.\n */\nexport type MultichainAccountControllerMessenger = RestrictedMessenger<\n 'MultichainAccountController',\n MultichainAccountControllerActions | AllowedActions,\n MultichainAccountControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\n/**\n * The options that {@link MultichainAccountController} takes.\n */\ntype MultichainAccountControllerOptions = {\n messenger: MultichainAccountControllerMessenger;\n};\n\n/**\n * Stateless controller to expose multichain accounts capabilities.\n */\nexport class MultichainAccountController {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n readonly #providers: AccountProvider[];\n\n readonly #wallets: Map<MultichainAccountWalletId, MultichainAccountWallet>;\n\n /**\n * Constructs a new MultichainAccountController.\n *\n * @param options - The options.\n * @param options.messenger - The messenger suited to this\n * MultichainAccountController.\n */\n constructor({ messenger }: MultichainAccountControllerOptions) {\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 // Gather all entropy sources first.\n const entropySources = new Set<EntropySourceId>();\n for (const provider of this.#providers) {\n for (const entropySource of provider.getEntropySources()) {\n entropySources.add(entropySource);\n }\n }\n\n for (const entropySource of entropySources) {\n // This will automatically create all multichain accounts for that wallet (based\n // on the accounts owned by each account providers).\n const wallet = new MultichainAccountWalletAdapter({\n entropySource,\n providers: this.#providers,\n });\n\n this.#wallets.set(wallet.id, wallet);\n }\n }\n\n #getWallet(entropySource: EntropySourceId): MultichainAccountWallet {\n const wallet = this.#wallets.get(\n toMultichainAccountWalletId(entropySource),\n );\n\n if (!wallet) {\n throw new Error(\n 'Unknown wallet, not wallet matching this entropy source',\n );\n }\n\n return wallet;\n }\n\n getMultichainAccount({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): MultichainAccount {\n const multichainAccount =\n this.#getWallet(entropySource).accounts[groupIndex];\n\n if (!multichainAccount) {\n throw new Error(`No multichain account for index: ${groupIndex}`);\n }\n\n return multichainAccount;\n }\n\n getMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): MultichainAccount[] {\n return this.#getWallet(entropySource).accounts;\n }\n\n async createNextMultichainAccount({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccount> {\n return await this.#getWallet(entropySource).createNextMultichainAccount();\n }\n\n async discoverAndCreateMultichainAccounts({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccount[]> {\n return await this.#getWallet(\n entropySource,\n ).discoverAndCreateMultichainAccounts();\n }\n}\n"]}
package/dist/index.cjs ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ /**
4
+ * Example function that returns a greeting for the given name.
5
+ *
6
+ * @param name - The name to greet.
7
+ * @returns The greeting.
8
+ */
9
+ function greeter(name) {
10
+ return `Hello, ${name}!`;
11
+ }
12
+ exports.default = greeter;
13
+ //# sourceMappingURL=index.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;AAAA;;;;;GAKG;AACH,SAAwB,OAAO,CAAC,IAAY;IAC1C,OAAO,UAAU,IAAI,GAAG,CAAC;AAC3B,CAAC;AAFD,0BAEC","sourcesContent":["/**\n * Example function that returns a greeting for the given name.\n *\n * @param name - The name to greet.\n * @returns The greeting.\n */\nexport default function greeter(name: string): string {\n return `Hello, ${name}!`;\n}\n"]}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Example function that returns a greeting for the given name.
3
+ *
4
+ * @param name - The name to greet.
5
+ * @returns The greeting.
6
+ */
7
+ export default function greeter(name: string): string;
8
+ //# sourceMappingURL=index.d.cts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD"}
@@ -0,0 +1,8 @@
1
+ /**
2
+ * Example function that returns a greeting for the given name.
3
+ *
4
+ * @param name - The name to greet.
5
+ * @returns The greeting.
6
+ */
7
+ export default function greeter(name: string): string;
8
+ //# sourceMappingURL=index.d.mts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAI,EAAE,MAAM,GAAG,MAAM,CAEpD"}
package/dist/index.mjs ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Example function that returns a greeting for the given name.
3
+ *
4
+ * @param name - The name to greet.
5
+ * @returns The greeting.
6
+ */
7
+ export default function greeter(name) {
8
+ return `Hello, ${name}!`;
9
+ }
10
+ //# sourceMappingURL=index.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,MAAM,CAAC,OAAO,UAAU,OAAO,CAAC,IAAY;IAC1C,OAAO,UAAU,IAAI,GAAG,CAAC;AAC3B,CAAC","sourcesContent":["/**\n * Example function that returns a greeting for the given name.\n *\n * @param name - The name to greet.\n * @returns The greeting.\n */\nexport default function greeter(name: string): string {\n return `Hello, ${name}!`;\n}\n"]}
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
3
+ if (kind === "m") throw new TypeError("Private method is not writable");
4
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
5
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
6
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
7
+ };
8
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
9
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
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
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
12
+ };
13
+ var _EvmAccountProvider_instances, _EvmAccountProvider_messenger, _EvmAccountProvider_withKeyring, _EvmAccountProvider_getAccounts;
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.EvmAccountProvider = void 0;
16
+ const keyring_api_1 = require("@metamask/keyring-api");
17
+ const keyring_controller_1 = require("@metamask/keyring-controller");
18
+ // Max index used by discovery (until we move the proper discovery here).
19
+ const MAX_GROUP_INDEX = 1;
20
+ // eslint-disable-next-line jsdoc/require-jsdoc
21
+ function assertInternalAccountExists(account) {
22
+ if (!account) {
23
+ throw new Error('Internal account does not exist');
24
+ }
25
+ }
26
+ class EvmAccountProvider {
27
+ constructor(messenger) {
28
+ _EvmAccountProvider_instances.add(this);
29
+ _EvmAccountProvider_messenger.set(this, void 0);
30
+ __classPrivateFieldSet(this, _EvmAccountProvider_messenger, messenger, "f");
31
+ }
32
+ async createAccounts({ entropySource, groupIndex, }) {
33
+ const addresses = await __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_withKeyring).call(this, { id: entropySource }, async ({ keyring }) => {
34
+ const accounts = await keyring.getAccounts();
35
+ if (groupIndex <= accounts.length) {
36
+ // Nothing new to create, we just re-use the existing accounts here,
37
+ return [accounts[groupIndex]];
38
+ }
39
+ // Create new accounts (and returns their addresses).
40
+ return await keyring.addAccounts(groupIndex);
41
+ });
42
+ // Only use the account associated for that index.
43
+ const address = addresses[groupIndex];
44
+ const account = __classPrivateFieldGet(this, _EvmAccountProvider_messenger, "f").call('AccountsController:getAccountByAddress', address);
45
+ // We MUST have the associated internal account.
46
+ assertInternalAccountExists(account);
47
+ return [account];
48
+ }
49
+ async discoverAndCreateAccounts({ entropySource, groupIndex, }) {
50
+ // TODO: Move account discovery here (for EVM).
51
+ if (groupIndex < MAX_GROUP_INDEX) {
52
+ return await this.createAccounts({ entropySource, groupIndex });
53
+ }
54
+ return [];
55
+ }
56
+ getAccounts({ entropySource, groupIndex, }) {
57
+ return __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getAccounts).call(this).filter((account) => {
58
+ return (account.options.entropySource === entropySource &&
59
+ account.options.index === groupIndex);
60
+ });
61
+ }
62
+ getEntropySources() {
63
+ const entropySources = new Set();
64
+ for (const account of __classPrivateFieldGet(this, _EvmAccountProvider_instances, "m", _EvmAccountProvider_getAccounts).call(this)) {
65
+ entropySources.add(account.options.entropySource);
66
+ }
67
+ return Array.from(entropySources);
68
+ }
69
+ }
70
+ exports.EvmAccountProvider = EvmAccountProvider;
71
+ _EvmAccountProvider_messenger = new WeakMap(), _EvmAccountProvider_instances = new WeakSet(), _EvmAccountProvider_withKeyring = async function _EvmAccountProvider_withKeyring(selector, operation) {
72
+ const result = await __classPrivateFieldGet(this, _EvmAccountProvider_messenger, "f").call('KeyringController:withKeyring', selector, ({ keyring, metadata }) => operation({
73
+ keyring: keyring,
74
+ metadata,
75
+ }));
76
+ return result;
77
+ }, _EvmAccountProvider_getAccounts = function _EvmAccountProvider_getAccounts() {
78
+ return __classPrivateFieldGet(this, _EvmAccountProvider_messenger, "f")
79
+ .call('AccountsController:listMultichainAccounts')
80
+ .filter((account) => {
81
+ // We only check for EOA accounts for multichain accounts.
82
+ if (account.type !== keyring_api_1.EthAccountType.Eoa ||
83
+ account.metadata.keyring.type !== keyring_controller_1.KeyringTypes.hd) {
84
+ return false;
85
+ }
86
+ // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?
87
+ if (!account.options.entropySource) {
88
+ console.warn("! Found an HD account with no entropy source: account won't be associated to its wallet.");
89
+ return false;
90
+ }
91
+ // TODO: We need to add this index for native accounts too!
92
+ if (account.options.index === undefined) {
93
+ console.warn("! Found an HD account with no index: account won't be associated to its wallet.");
94
+ return false;
95
+ }
96
+ return true;
97
+ }); // Safe, we did check for options fields during filtering.
98
+ };
99
+ //# sourceMappingURL=EvmAccountProvider.cjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"EvmAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/EvmAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uDAA6E;AAC7E,qEAIsC;AAQtC,yEAAyE;AACzE,MAAM,eAAe,GAAG,CAAC,CAAC;AAS1B,+CAA+C;AAC/C,SAAS,2BAA2B,CAClC,OAAoC;IAEpC,IAAI,CAAC,OAAO,EAAE;QACZ,MAAM,IAAI,KAAK,CAAC,iCAAiC,CAAC,CAAC;KACpD;AACH,CAAC;AAED,MAAa,kBAAkB;IAG7B,YAAY,SAA+C;;QAFlD,gDAAiD;QAGxD,uBAAA,IAAI,iCAAc,SAAS,MAAA,CAAC;IAC9B,CAAC;IA4BD,KAAK,CAAC,cAAc,CAAC,EACnB,aAAa,EACb,UAAU,GAIX;QACC,MAAM,SAAS,GAAG,MAAM,uBAAA,IAAI,sEAAa,MAAjB,IAAI,EAC1B,EAAE,EAAE,EAAE,aAAa,EAAE,EACrB,KAAK,EAAE,EAAE,OAAO,EAAE,EAAE,EAAE;YACpB,MAAM,QAAQ,GAAG,MAAM,OAAO,CAAC,WAAW,EAAE,CAAC;YAC7C,IAAI,UAAU,IAAI,QAAQ,CAAC,MAAM,EAAE;gBACjC,o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C;IACpC,CAAC;CACF;AA1ID,gDA0IC;gIAnIC,KAAK,0CAIH,QAAyB,EACzB,SAM6B;IAE7B,MAAM,MAAM,GAAG,MAAM,uBAAA,IAAI,qCAAW,CAAC,IAAI,CACvC,+BAA+B,EAC/B,QAAQ,EACR,CAAC,EAAE,OAAO,EAAE,QAAQ,EAAE,EAAE,EAAE,CACxB,SAAS,CAAC;QACR,OAAO,EAAE,OAA0B;QACnC,QAAQ;KACT,CAAC,CACL,CAAC;IAEF,OAAO,MAAwB,CAAC;AAClC,CAAC;IAoDC,OAAO,uBAAA,IAAI,qCAAW;SACnB,IAAI,CAAC,2CAA2C,CAAC;SACjD,MAAM,CAAC,CAAC,OAAO,EAAE,EAAE;QAClB,0DAA0D;QAC1D,IACE,OAAO,CAAC,IAAI,KAAK,4BAAc,CAAC,GAAG;YACnC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAC,IAAI,KAAM,iCAAY,CAAC,EAAa,EAC7D;YACA,OAAO,KAAK,CAAC;SACd;QAED,iHAAiH;QACjH,IAAI,CAAC,OAAO,CAAC,OAAO,CAAC,aAAa,EAAE;YAClC,OAAO,CAAC,IAAI,CACV,0FAA0F,CAC3F,CAAC;YACF,OAAO,KAAK,CAAC;SACd;QAED,2DAA2D;QAC3D,IAAI,OAAO,CAAC,OAAO,CAAC,KAAK,KAAK,SAAS,EAAE;YACvC,OAAO,CAAC,IAAI,CACV,iFAAiF,CAClF,CAAC;YACF,OAAO,KAAK,CAAC;SACd;QAED,OAAO,IAAI,CAAC;IACd,CAAC,CAAyB,CAAC,CAAC,0DAA0D;AAC1F,CAAC","sourcesContent":["import { EthAccountType, type EntropySourceId } from '@metamask/keyring-api';\nimport {\n KeyringTypes,\n type KeyringMetadata,\n type KeyringSelector,\n} from '@metamask/keyring-controller';\nimport type {\n EthKeyring,\n InternalAccount,\n} from '@metamask/keyring-internal-api';\nimport type { AccountProvider } from '@metamask/multichain-account-api';\nimport type { MultichainAccountControllerMessenger } from 'src/MultichainAccountController';\n\n// Max index used by discovery (until we move the proper discovery here).\nconst MAX_GROUP_INDEX = 1;\n\ntype EoaInternalAccount = InternalAccount & {\n options: {\n index: number;\n entropySource: EntropySourceId;\n };\n};\n\n// eslint-disable-next-line jsdoc/require-jsdoc\nfunction assertInternalAccountExists(\n account: InternalAccount | undefined,\n): asserts account is InternalAccount {\n if (!account) {\n throw new Error('Internal account does not exist');\n }\n}\n\nexport class EvmAccountProvider implements AccountProvider {\n readonly #messenger: MultichainAccountControllerMessenger;\n\n constructor(messenger: MultichainAccountControllerMessenger) {\n this.#messenger = messenger;\n }\n\n async #withKeyring<\n SelectedKeyring extends EthKeyring = EthKeyring,\n CallbackResult = void,\n >(\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 createAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n const addresses = await this.#withKeyring(\n { id: entropySource },\n async ({ keyring }) => {\n const accounts = await keyring.getAccounts();\n if (groupIndex <= accounts.length) {\n // Nothing new to create, we just re-use the existing accounts here,\n return [accounts[groupIndex]];\n }\n\n // Create new accounts (and returns their addresses).\n return await keyring.addAccounts(groupIndex);\n },\n );\n\n // Only use the account associated for that index.\n const address = addresses[groupIndex];\n const account = this.#messenger.call(\n 'AccountsController:getAccountByAddress',\n address,\n );\n\n // We MUST have the associated internal account.\n assertInternalAccountExists(account);\n\n return [account];\n }\n\n async discoverAndCreateAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }) {\n // TODO: Move account discovery here (for EVM).\n\n if (groupIndex < MAX_GROUP_INDEX) {\n return await this.createAccounts({ entropySource, groupIndex });\n }\n return [];\n }\n\n #getAccounts(): EoaInternalAccount[] {\n return this.#messenger\n .call('AccountsController:listMultichainAccounts')\n .filter((account) => {\n // We only check for EOA accounts for multichain accounts.\n if (\n account.type !== EthAccountType.Eoa ||\n account.metadata.keyring.type !== (KeyringTypes.hd as string)\n ) {\n return false;\n }\n\n // TODO: Maybe use superstruct to validate the structure of HD account since they are not strongly-typed for now?\n if (!account.options.entropySource) {\n console.warn(\n \"! Found an HD account with no entropy source: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n // TODO: We need to add this index for native accounts too!\n if (account.options.index === undefined) {\n console.warn(\n \"! Found an HD account with no index: account won't be associated to its wallet.\",\n );\n return false;\n }\n\n return true;\n }) as EoaInternalAccount[]; // Safe, we did check for options fields during filtering.\n }\n\n getAccounts({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): InternalAccount[] {\n return this.#getAccounts().filter((account) => {\n return (\n account.options.entropySource === entropySource &&\n account.options.index === groupIndex\n );\n });\n }\n\n getEntropySources(): EntropySourceId[] {\n const entropySources = new Set<EntropySourceId>();\n\n for (const account of this.#getAccounts()) {\n entropySources.add(account.options.entropySource);\n }\n\n return Array.from(entropySources);\n }\n}\n"]}