@metamask-previews/multichain-account-service 0.3.0-preview-713be623 → 0.3.0-preview-43d47013
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.
- package/CHANGELOG.md +15 -0
- package/dist/MultichainAccountGroup.cjs +192 -0
- package/dist/MultichainAccountGroup.cjs.map +1 -0
- package/dist/MultichainAccountGroup.d.cts +84 -0
- package/dist/MultichainAccountGroup.d.cts.map +1 -0
- package/dist/MultichainAccountGroup.d.mts +84 -0
- package/dist/MultichainAccountGroup.d.mts.map +1 -0
- package/dist/MultichainAccountGroup.mjs +188 -0
- package/dist/MultichainAccountGroup.mjs.map +1 -0
- package/dist/MultichainAccountService.cjs +36 -23
- package/dist/MultichainAccountService.cjs.map +1 -1
- package/dist/MultichainAccountService.d.cts +26 -15
- package/dist/MultichainAccountService.d.cts.map +1 -1
- package/dist/MultichainAccountService.d.mts +26 -15
- package/dist/MultichainAccountService.d.mts.map +1 -1
- package/dist/MultichainAccountService.mjs +35 -22
- package/dist/MultichainAccountService.mjs.map +1 -1
- package/dist/MultichainAccountWallet.cjs +237 -0
- package/dist/MultichainAccountWallet.cjs.map +1 -0
- package/dist/MultichainAccountWallet.d.cts +91 -0
- package/dist/MultichainAccountWallet.d.cts.map +1 -0
- package/dist/MultichainAccountWallet.d.mts +91 -0
- package/dist/MultichainAccountWallet.d.mts.map +1 -0
- package/dist/MultichainAccountWallet.mjs +233 -0
- package/dist/MultichainAccountWallet.mjs.map +1 -0
- package/dist/index.cjs +5 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +3 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +2 -0
- package/dist/index.mjs.map +1 -1
- package/dist/providers/BaseAccountProvider.cjs +20 -1
- package/dist/providers/BaseAccountProvider.cjs.map +1 -1
- package/dist/providers/BaseAccountProvider.d.cts +25 -5
- package/dist/providers/BaseAccountProvider.d.cts.map +1 -1
- package/dist/providers/BaseAccountProvider.d.mts +25 -5
- package/dist/providers/BaseAccountProvider.d.mts.map +1 -1
- package/dist/providers/BaseAccountProvider.mjs +18 -0
- package/dist/providers/BaseAccountProvider.mjs.map +1 -1
- package/dist/providers/EvmAccountProvider.cjs +35 -0
- package/dist/providers/EvmAccountProvider.cjs.map +1 -1
- package/dist/providers/EvmAccountProvider.d.cts +66 -0
- package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
- package/dist/providers/EvmAccountProvider.d.mts +66 -0
- package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
- package/dist/providers/EvmAccountProvider.mjs +36 -1
- package/dist/providers/EvmAccountProvider.mjs.map +1 -1
- package/dist/providers/SolAccountProvider.cjs +39 -0
- package/dist/providers/SolAccountProvider.cjs.map +1 -1
- package/dist/providers/SolAccountProvider.d.cts +28 -1
- package/dist/providers/SolAccountProvider.d.cts.map +1 -1
- package/dist/providers/SolAccountProvider.d.mts +28 -1
- package/dist/providers/SolAccountProvider.d.mts.map +1 -1
- package/dist/providers/SolAccountProvider.mjs +40 -1
- package/dist/providers/SolAccountProvider.mjs.map +1 -1
- package/dist/tests/accounts.cjs +89 -4
- package/dist/tests/accounts.cjs.map +1 -1
- package/dist/tests/accounts.d.cts +235 -36
- package/dist/tests/accounts.d.cts.map +1 -1
- package/dist/tests/accounts.d.mts +235 -36
- package/dist/tests/accounts.d.mts.map +1 -1
- package/dist/tests/accounts.mjs +89 -4
- package/dist/tests/accounts.mjs.map +1 -1
- package/dist/tests/index.cjs +1 -0
- package/dist/tests/index.cjs.map +1 -1
- package/dist/tests/index.d.cts +1 -0
- package/dist/tests/index.d.cts.map +1 -1
- package/dist/tests/index.d.mts +1 -0
- package/dist/tests/index.d.mts.map +1 -1
- package/dist/tests/index.mjs +1 -0
- package/dist/tests/index.mjs.map +1 -1
- package/dist/tests/providers.cjs +28 -0
- package/dist/tests/providers.cjs.map +1 -0
- package/dist/tests/providers.d.cts +16 -0
- package/dist/tests/providers.d.cts.map +1 -0
- package/dist/tests/providers.d.mts +16 -0
- package/dist/tests/providers.d.mts.map +1 -0
- package/dist/tests/providers.mjs +23 -0
- package/dist/tests/providers.mjs.map +1 -0
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +11 -7
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +11 -7
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +7 -4
|
@@ -15,6 +15,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
15
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
|
+
const MultichainAccountWallet_1 = require("./MultichainAccountWallet.cjs");
|
|
18
19
|
const EvmAccountProvider_1 = require("./providers/EvmAccountProvider.cjs");
|
|
19
20
|
const SolAccountProvider_1 = require("./providers/SolAccountProvider.cjs");
|
|
20
21
|
exports.serviceName = 'MultichainAccountService';
|
|
@@ -47,10 +48,11 @@ class MultichainAccountService {
|
|
|
47
48
|
new EvmAccountProvider_1.EvmAccountProvider(__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f")),
|
|
48
49
|
new SolAccountProvider_1.SolAccountProvider(__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f")),
|
|
49
50
|
], "f");
|
|
50
|
-
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:
|
|
51
|
-
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:
|
|
51
|
+
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountGroup', (...args) => this.getMultichainAccountGroup(...args));
|
|
52
|
+
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountGroups', (...args) => this.getMultichainAccountGroups(...args));
|
|
52
53
|
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountWallet', (...args) => this.getMultichainAccountWallet(...args));
|
|
53
54
|
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountWallets', (...args) => this.getMultichainAccountWallets(...args));
|
|
55
|
+
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:createNextMultichainAccountGroup', (...args) => this.createNextMultichainAccountGroup(...args));
|
|
54
56
|
}
|
|
55
57
|
/**
|
|
56
58
|
* Initialize the service and constructs the internal reprensentation of
|
|
@@ -65,17 +67,17 @@ class MultichainAccountService {
|
|
|
65
67
|
const entropySource = keyring.metadata.id;
|
|
66
68
|
// This will automatically "associate" all multichain accounts for that wallet
|
|
67
69
|
// (based on the accounts owned by each account providers).
|
|
68
|
-
const wallet = new
|
|
70
|
+
const wallet = new MultichainAccountWallet_1.MultichainAccountWallet({
|
|
69
71
|
entropySource,
|
|
70
72
|
providers: __classPrivateFieldGet(this, _MultichainAccountService_providers, "f"),
|
|
71
73
|
});
|
|
72
74
|
__classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").set(wallet.id, wallet);
|
|
73
75
|
// Reverse mapping between account ID and their multichain wallet/account:
|
|
74
|
-
for (const
|
|
75
|
-
for (const account of
|
|
76
|
+
for (const group of wallet.getMultichainAccountGroups()) {
|
|
77
|
+
for (const account of group.getAccounts()) {
|
|
76
78
|
__classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").set(account.id, {
|
|
77
79
|
wallet,
|
|
78
|
-
|
|
80
|
+
group,
|
|
79
81
|
});
|
|
80
82
|
}
|
|
81
83
|
}
|
|
@@ -85,13 +87,13 @@ class MultichainAccountService {
|
|
|
85
87
|
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").subscribe('AccountsController:accountRemoved', (id) => __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_handleOnAccountRemoved).call(this, id));
|
|
86
88
|
}
|
|
87
89
|
/**
|
|
88
|
-
* Gets
|
|
90
|
+
* Gets the account's context which contains its multichain wallet and
|
|
91
|
+
* multichain account group references.
|
|
89
92
|
*
|
|
90
93
|
* @param id - Account ID.
|
|
91
|
-
* @returns
|
|
92
|
-
* that account ID, or undefined if this account ID is not part of any.
|
|
94
|
+
* @returns The account context if any, undefined otherwise.
|
|
93
95
|
*/
|
|
94
|
-
|
|
96
|
+
getAccountContext(id) {
|
|
95
97
|
return __classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").get(id);
|
|
96
98
|
}
|
|
97
99
|
/**
|
|
@@ -114,7 +116,8 @@ class MultichainAccountService {
|
|
|
114
116
|
return Array.from(__classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").values());
|
|
115
117
|
}
|
|
116
118
|
/**
|
|
117
|
-
* Gets a reference to the multichain account matching this entropy source
|
|
119
|
+
* Gets a reference to the multichain account group matching this entropy source
|
|
120
|
+
* and a group index.
|
|
118
121
|
*
|
|
119
122
|
* @param options - Options.
|
|
120
123
|
* @param options.entropySource - The entropy source of the multichain account.
|
|
@@ -122,23 +125,33 @@ class MultichainAccountService {
|
|
|
122
125
|
* @throws If none multichain account match this entropy source and group index.
|
|
123
126
|
* @returns A reference to the multichain account.
|
|
124
127
|
*/
|
|
125
|
-
|
|
126
|
-
const multichainAccount = __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).
|
|
128
|
+
getMultichainAccountGroup({ entropySource, groupIndex, }) {
|
|
129
|
+
const multichainAccount = __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).getMultichainAccountGroup(groupIndex);
|
|
127
130
|
if (!multichainAccount) {
|
|
128
131
|
throw new Error(`No multichain account for index: ${groupIndex}`);
|
|
129
132
|
}
|
|
130
133
|
return multichainAccount;
|
|
131
134
|
}
|
|
132
135
|
/**
|
|
133
|
-
* Gets all multichain
|
|
136
|
+
* Gets all multichain account groups for a given entropy source.
|
|
134
137
|
*
|
|
135
138
|
* @param options - Options.
|
|
136
139
|
* @param options.entropySource - The entropy source to query.
|
|
137
140
|
* @throws If no multichain accounts match this entropy source.
|
|
138
141
|
* @returns A list of all multichain accounts.
|
|
139
142
|
*/
|
|
140
|
-
|
|
141
|
-
return __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).
|
|
143
|
+
getMultichainAccountGroups({ entropySource, }) {
|
|
144
|
+
return __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).getMultichainAccountGroups();
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* Creates the next multichain account group.
|
|
148
|
+
*
|
|
149
|
+
* @param options - Options.
|
|
150
|
+
* @param options.entropySource - The wallet's entropy source.
|
|
151
|
+
* @returns The next multichain account group.
|
|
152
|
+
*/
|
|
153
|
+
async createNextMultichainAccountGroup({ entropySource, }) {
|
|
154
|
+
return await __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).createNextMultichainAccountGroup();
|
|
142
155
|
}
|
|
143
156
|
}
|
|
144
157
|
exports.MultichainAccountService = MultichainAccountService;
|
|
@@ -151,7 +164,7 @@ _MultichainAccountService_messenger = new WeakMap(), _MultichainAccountService_p
|
|
|
151
164
|
let wallet = __classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").get((0, account_api_1.toMultichainAccountWalletId)(account.options.entropy.id));
|
|
152
165
|
if (!wallet) {
|
|
153
166
|
// That's a new wallet.
|
|
154
|
-
wallet = new
|
|
167
|
+
wallet = new MultichainAccountWallet_1.MultichainAccountWallet({
|
|
155
168
|
entropySource: account.options.entropy.id,
|
|
156
169
|
providers: __classPrivateFieldGet(this, _MultichainAccountService_providers, "f"),
|
|
157
170
|
});
|
|
@@ -159,28 +172,28 @@ _MultichainAccountService_messenger = new WeakMap(), _MultichainAccountService_p
|
|
|
159
172
|
// If that's a new wallet wallet. There's nothing to "force-sync".
|
|
160
173
|
sync = false;
|
|
161
174
|
}
|
|
162
|
-
let
|
|
163
|
-
if (!
|
|
175
|
+
let group = wallet.getMultichainAccountGroup(account.options.entropy.groupIndex);
|
|
176
|
+
if (!group) {
|
|
164
177
|
// This new account is a new multichain account, let the wallet know
|
|
165
178
|
// it has to re-sync with its providers.
|
|
166
179
|
if (sync) {
|
|
167
180
|
wallet.sync();
|
|
168
181
|
}
|
|
169
|
-
|
|
182
|
+
group = wallet.getMultichainAccountGroup(account.options.entropy.groupIndex);
|
|
170
183
|
// If that's a new multichain account. There's nothing to "force-sync".
|
|
171
184
|
sync = false;
|
|
172
185
|
}
|
|
173
186
|
// We have to check against `undefined` in case `getMultichainAccount` is
|
|
174
187
|
// not able to find this multichain account (which should not be possible...)
|
|
175
|
-
if (
|
|
188
|
+
if (group) {
|
|
176
189
|
if (sync) {
|
|
177
|
-
|
|
190
|
+
group.sync();
|
|
178
191
|
}
|
|
179
192
|
// Same here, this account should have been already grouped in that
|
|
180
193
|
// multichain account.
|
|
181
194
|
__classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").set(account.id, {
|
|
182
195
|
wallet,
|
|
183
|
-
|
|
196
|
+
group,
|
|
184
197
|
});
|
|
185
198
|
}
|
|
186
199
|
}, _MultichainAccountService_handleOnAccountRemoved = function _MultichainAccountService_handleOnAccountRemoved(id) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountService.cjs","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAKA,uDAK+B;AAE/B,qEAA4D;AAG5D,2EAAoE;AACpE,2EAAoE;AAGvD,QAAA,WAAW,GAAG,0BAA0B,CAAC;AAetD;;GAEG;AACH,MAAa,wBAAwB;IAoBnC;;;;;;OAMG;IACH,YAAY,EAAE,SAAS,EA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C3E;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
|
+
{"version":3,"file":"MultichainAccountService.cjs","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,uDAG+B;AAO/B,qEAA4D;AAG5D,2EAAoE;AACpE,2EAAoE;AACpE,2EAAoE;AAGvD,QAAA,WAAW,GAAG,0BAA0B,CAAC;AAetD;;GAEG;AACH,MAAa,wBAAwB;IAoBnC;;;;;;OAMG;IACH,YAAY,EAAE,SAAS,EAAmC;;QA1BjD,sDAA8C;QAE9C,sDAA4D;QAE5D,oDAGP;QAEO,+DAGP;QAEF;;WAEG;QACH,SAAI,GAAuB,mBAAW,CAAC;QAUrC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AM,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 {\n isBip44Account,\n toMultichainAccountWalletId,\n} from '@metamask/account-api';\nimport type {\n MultichainAccountWalletId,\n Bip44Account,\n} from '@metamask/account-api';\nimport type { AccountProvider } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\n\nimport type { MultichainAccountGroup } from './MultichainAccountGroup';\nimport { MultichainAccountWallet } from './MultichainAccountWallet';\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 group: MultichainAccountGroup<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<KeyringAccount>>[];\n\n readonly #wallets: Map<\n MultichainAccountWalletId,\n MultichainAccountWallet<Bip44Account<KeyringAccount>>\n >;\n\n readonly #accountIdToContext: Map<\n Bip44Account<KeyringAccount>['id'],\n AccountContext<Bip44Account<KeyringAccount>>\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:getMultichainAccountGroup',\n (...args) => this.getMultichainAccountGroup(...args),\n );\n this.#messenger.registerActionHandler(\n 'MultichainAccountService:getMultichainAccountGroups',\n (...args) => this.getMultichainAccountGroups(...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 this.#messenger.registerActionHandler(\n 'MultichainAccountService:createNextMultichainAccountGroup',\n (...args) => this.createNextMultichainAccountGroup(...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 group of wallet.getMultichainAccountGroups()) {\n for (const account of group.getAccounts()) {\n this.#accountIdToContext.set(account.id, {\n wallet,\n group,\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: KeyringAccount): 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 group = wallet.getMultichainAccountGroup(\n account.options.entropy.groupIndex,\n );\n if (!group) {\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 group = wallet.getMultichainAccountGroup(\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 (group) {\n if (sync) {\n group.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 group,\n });\n }\n }\n\n #handleOnAccountRemoved(id: KeyringAccount['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<KeyringAccount>> {\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 the account's context which contains its multichain wallet and\n * multichain account group references.\n *\n * @param id - Account ID.\n * @returns The account context if any, undefined otherwise.\n */\n getAccountContext(\n id: KeyringAccount['id'],\n ): AccountContext<Bip44Account<KeyringAccount>> | 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<KeyringAccount>> {\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<KeyringAccount>\n >[] {\n return Array.from(this.#wallets.values());\n }\n\n /**\n * Gets a reference to the multichain account group matching this entropy source\n * and a 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 getMultichainAccountGroup({\n entropySource,\n groupIndex,\n }: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): MultichainAccountGroup<Bip44Account<KeyringAccount>> {\n const multichainAccount =\n this.#getWallet(entropySource).getMultichainAccountGroup(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 account groups 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 getMultichainAccountGroups({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): MultichainAccountGroup<Bip44Account<KeyringAccount>>[] {\n return this.#getWallet(entropySource).getMultichainAccountGroups();\n }\n\n /**\n * Creates the next multichain account group.\n *\n * @param options - Options.\n * @param options.entropySource - The wallet's entropy source.\n * @returns The next multichain account group.\n */\n async createNextMultichainAccountGroup({\n entropySource,\n }: {\n entropySource: EntropySourceId;\n }): Promise<MultichainAccountGroup<Bip44Account<KeyringAccount>>> {\n return await this.#getWallet(\n entropySource,\n ).createNextMultichainAccountGroup();\n }\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Bip44Account } from "@metamask/account-api";
|
|
2
|
-
import { MultichainAccountWallet, type MultichainAccount } from "@metamask/account-api";
|
|
3
2
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
4
|
-
import type {
|
|
3
|
+
import type { MultichainAccountGroup } from "./MultichainAccountGroup.cjs";
|
|
4
|
+
import { MultichainAccountWallet } from "./MultichainAccountWallet.cjs";
|
|
5
5
|
import type { MultichainAccountServiceMessenger } from "./types.cjs";
|
|
6
6
|
export declare const serviceName = "MultichainAccountService";
|
|
7
7
|
/**
|
|
@@ -13,7 +13,7 @@ type MultichainAccountServiceOptions = {
|
|
|
13
13
|
/** Reverse mapping object used to map account IDs and their wallet/multichain account. */
|
|
14
14
|
type AccountContext<Account extends Bip44Account<KeyringAccount>> = {
|
|
15
15
|
wallet: MultichainAccountWallet<Account>;
|
|
16
|
-
|
|
16
|
+
group: MultichainAccountGroup<Account>;
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* Service to expose multichain accounts capabilities.
|
|
@@ -38,13 +38,13 @@ export declare class MultichainAccountService {
|
|
|
38
38
|
*/
|
|
39
39
|
init(): void;
|
|
40
40
|
/**
|
|
41
|
-
* Gets
|
|
41
|
+
* Gets the account's context which contains its multichain wallet and
|
|
42
|
+
* multichain account group references.
|
|
42
43
|
*
|
|
43
44
|
* @param id - Account ID.
|
|
44
|
-
* @returns
|
|
45
|
-
* that account ID, or undefined if this account ID is not part of any.
|
|
45
|
+
* @returns The account context if any, undefined otherwise.
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
getAccountContext(id: KeyringAccount['id']): AccountContext<Bip44Account<KeyringAccount>> | undefined;
|
|
48
48
|
/**
|
|
49
49
|
* Gets a reference to the multichain account wallet matching this entropy source.
|
|
50
50
|
*
|
|
@@ -55,15 +55,16 @@ export declare class MultichainAccountService {
|
|
|
55
55
|
*/
|
|
56
56
|
getMultichainAccountWallet({ entropySource, }: {
|
|
57
57
|
entropySource: EntropySourceId;
|
|
58
|
-
}): MultichainAccountWallet<Bip44Account<
|
|
58
|
+
}): MultichainAccountWallet<Bip44Account<KeyringAccount>>;
|
|
59
59
|
/**
|
|
60
60
|
* Gets an array of all multichain account wallets.
|
|
61
61
|
*
|
|
62
62
|
* @returns An array of all multichain account wallets.
|
|
63
63
|
*/
|
|
64
|
-
getMultichainAccountWallets(): MultichainAccountWallet<Bip44Account<
|
|
64
|
+
getMultichainAccountWallets(): MultichainAccountWallet<Bip44Account<KeyringAccount>>[];
|
|
65
65
|
/**
|
|
66
|
-
* Gets a reference to the multichain account matching this entropy source
|
|
66
|
+
* Gets a reference to the multichain account group matching this entropy source
|
|
67
|
+
* and a group index.
|
|
67
68
|
*
|
|
68
69
|
* @param options - Options.
|
|
69
70
|
* @param options.entropySource - The entropy source of the multichain account.
|
|
@@ -71,21 +72,31 @@ export declare class MultichainAccountService {
|
|
|
71
72
|
* @throws If none multichain account match this entropy source and group index.
|
|
72
73
|
* @returns A reference to the multichain account.
|
|
73
74
|
*/
|
|
74
|
-
|
|
75
|
+
getMultichainAccountGroup({ entropySource, groupIndex, }: {
|
|
75
76
|
entropySource: EntropySourceId;
|
|
76
77
|
groupIndex: number;
|
|
77
|
-
}):
|
|
78
|
+
}): MultichainAccountGroup<Bip44Account<KeyringAccount>>;
|
|
78
79
|
/**
|
|
79
|
-
* Gets all multichain
|
|
80
|
+
* Gets all multichain account groups for a given entropy source.
|
|
80
81
|
*
|
|
81
82
|
* @param options - Options.
|
|
82
83
|
* @param options.entropySource - The entropy source to query.
|
|
83
84
|
* @throws If no multichain accounts match this entropy source.
|
|
84
85
|
* @returns A list of all multichain accounts.
|
|
85
86
|
*/
|
|
86
|
-
|
|
87
|
+
getMultichainAccountGroups({ entropySource, }: {
|
|
87
88
|
entropySource: EntropySourceId;
|
|
88
|
-
}):
|
|
89
|
+
}): MultichainAccountGroup<Bip44Account<KeyringAccount>>[];
|
|
90
|
+
/**
|
|
91
|
+
* Creates the next multichain account group.
|
|
92
|
+
*
|
|
93
|
+
* @param options - Options.
|
|
94
|
+
* @param options.entropySource - The wallet's entropy source.
|
|
95
|
+
* @returns The next multichain account group.
|
|
96
|
+
*/
|
|
97
|
+
createNextMultichainAccountGroup({ entropySource, }: {
|
|
98
|
+
entropySource: EntropySourceId;
|
|
99
|
+
}): Promise<MultichainAccountGroup<Bip44Account<KeyringAccount>>>;
|
|
89
100
|
}
|
|
90
101
|
export {};
|
|
91
102
|
//# sourceMappingURL=MultichainAccountService.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountService.d.cts","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MultichainAccountService.d.cts","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAEV,YAAY,EACb,8BAA8B;AAE/B,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AACvE,OAAO,EAAE,uBAAuB,EAAE,sCAAkC;AAGpE,OAAO,KAAK,EAAE,iCAAiC,EAAE,oBAAgB;AAEjE,eAAO,MAAM,WAAW,6BAA6B,CAAC;AAEtD;;GAEG;AACH,KAAK,+BAA+B,GAAG;IACrC,SAAS,EAAE,iCAAiC,CAAC;CAC9C,CAAC;AAEF,0FAA0F;AAC1F,KAAK,cAAc,CAAC,OAAO,SAAS,YAAY,CAAC,cAAc,CAAC,IAAI;IAClE,MAAM,EAAE,uBAAuB,CAAC,OAAO,CAAC,CAAC;IACzC,KAAK,EAAE,sBAAsB,CAAC,OAAO,CAAC,CAAC;CACxC,CAAC;AAEF;;GAEG;AACH,qBAAa,wBAAwB;;IAenC;;OAEG;IACH,IAAI,EAAE,OAAO,WAAW,CAAe;IAEvC;;;;;;OAMG;gBACS,EAAE,SAAS,EAAE,EAAE,+BAA+B;IAgC1D;;;OAGG;IACH,IAAI,IAAI,IAAI;IAwHZ;;;;;;OAMG;IACH,iBAAiB,CACf,EAAE,EAAE,cAAc,CAAC,IAAI,CAAC,GACvB,cAAc,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,GAAG,SAAS;IAI3D;;;;;;;OAOG;IACH,0BAA0B,CAAC,EACzB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,uBAAuB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IAIzD;;;;OAIG;IACH,2BAA2B,IAAI,uBAAuB,CACpD,YAAY,CAAC,cAAc,CAAC,CAC7B,EAAE;IAIH;;;;;;;;;OASG;IACH,yBAAyB,CAAC,EACxB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,sBAAsB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IAWxD;;;;;;;OAOG;IACH,0BAA0B,CAAC,EACzB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,sBAAsB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,EAAE;IAI1D;;;;;;OAMG;IACG,gCAAgC,CAAC,EACrC,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,OAAO,CAAC,sBAAsB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,CAAC;CAKlE"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { Bip44Account } from "@metamask/account-api";
|
|
2
|
-
import { MultichainAccountWallet, type MultichainAccount } from "@metamask/account-api";
|
|
3
2
|
import type { EntropySourceId, KeyringAccount } from "@metamask/keyring-api";
|
|
4
|
-
import type {
|
|
3
|
+
import type { MultichainAccountGroup } from "./MultichainAccountGroup.mjs";
|
|
4
|
+
import { MultichainAccountWallet } from "./MultichainAccountWallet.mjs";
|
|
5
5
|
import type { MultichainAccountServiceMessenger } from "./types.mjs";
|
|
6
6
|
export declare const serviceName = "MultichainAccountService";
|
|
7
7
|
/**
|
|
@@ -13,7 +13,7 @@ type MultichainAccountServiceOptions = {
|
|
|
13
13
|
/** Reverse mapping object used to map account IDs and their wallet/multichain account. */
|
|
14
14
|
type AccountContext<Account extends Bip44Account<KeyringAccount>> = {
|
|
15
15
|
wallet: MultichainAccountWallet<Account>;
|
|
16
|
-
|
|
16
|
+
group: MultichainAccountGroup<Account>;
|
|
17
17
|
};
|
|
18
18
|
/**
|
|
19
19
|
* Service to expose multichain accounts capabilities.
|
|
@@ -38,13 +38,13 @@ export declare class MultichainAccountService {
|
|
|
38
38
|
*/
|
|
39
39
|
init(): void;
|
|
40
40
|
/**
|
|
41
|
-
* Gets
|
|
41
|
+
* Gets the account's context which contains its multichain wallet and
|
|
42
|
+
* multichain account group references.
|
|
42
43
|
*
|
|
43
44
|
* @param id - Account ID.
|
|
44
|
-
* @returns
|
|
45
|
-
* that account ID, or undefined if this account ID is not part of any.
|
|
45
|
+
* @returns The account context if any, undefined otherwise.
|
|
46
46
|
*/
|
|
47
|
-
|
|
47
|
+
getAccountContext(id: KeyringAccount['id']): AccountContext<Bip44Account<KeyringAccount>> | undefined;
|
|
48
48
|
/**
|
|
49
49
|
* Gets a reference to the multichain account wallet matching this entropy source.
|
|
50
50
|
*
|
|
@@ -55,15 +55,16 @@ export declare class MultichainAccountService {
|
|
|
55
55
|
*/
|
|
56
56
|
getMultichainAccountWallet({ entropySource, }: {
|
|
57
57
|
entropySource: EntropySourceId;
|
|
58
|
-
}): MultichainAccountWallet<Bip44Account<
|
|
58
|
+
}): MultichainAccountWallet<Bip44Account<KeyringAccount>>;
|
|
59
59
|
/**
|
|
60
60
|
* Gets an array of all multichain account wallets.
|
|
61
61
|
*
|
|
62
62
|
* @returns An array of all multichain account wallets.
|
|
63
63
|
*/
|
|
64
|
-
getMultichainAccountWallets(): MultichainAccountWallet<Bip44Account<
|
|
64
|
+
getMultichainAccountWallets(): MultichainAccountWallet<Bip44Account<KeyringAccount>>[];
|
|
65
65
|
/**
|
|
66
|
-
* Gets a reference to the multichain account matching this entropy source
|
|
66
|
+
* Gets a reference to the multichain account group matching this entropy source
|
|
67
|
+
* and a group index.
|
|
67
68
|
*
|
|
68
69
|
* @param options - Options.
|
|
69
70
|
* @param options.entropySource - The entropy source of the multichain account.
|
|
@@ -71,21 +72,31 @@ export declare class MultichainAccountService {
|
|
|
71
72
|
* @throws If none multichain account match this entropy source and group index.
|
|
72
73
|
* @returns A reference to the multichain account.
|
|
73
74
|
*/
|
|
74
|
-
|
|
75
|
+
getMultichainAccountGroup({ entropySource, groupIndex, }: {
|
|
75
76
|
entropySource: EntropySourceId;
|
|
76
77
|
groupIndex: number;
|
|
77
|
-
}):
|
|
78
|
+
}): MultichainAccountGroup<Bip44Account<KeyringAccount>>;
|
|
78
79
|
/**
|
|
79
|
-
* Gets all multichain
|
|
80
|
+
* Gets all multichain account groups for a given entropy source.
|
|
80
81
|
*
|
|
81
82
|
* @param options - Options.
|
|
82
83
|
* @param options.entropySource - The entropy source to query.
|
|
83
84
|
* @throws If no multichain accounts match this entropy source.
|
|
84
85
|
* @returns A list of all multichain accounts.
|
|
85
86
|
*/
|
|
86
|
-
|
|
87
|
+
getMultichainAccountGroups({ entropySource, }: {
|
|
87
88
|
entropySource: EntropySourceId;
|
|
88
|
-
}):
|
|
89
|
+
}): MultichainAccountGroup<Bip44Account<KeyringAccount>>[];
|
|
90
|
+
/**
|
|
91
|
+
* Creates the next multichain account group.
|
|
92
|
+
*
|
|
93
|
+
* @param options - Options.
|
|
94
|
+
* @param options.entropySource - The wallet's entropy source.
|
|
95
|
+
* @returns The next multichain account group.
|
|
96
|
+
*/
|
|
97
|
+
createNextMultichainAccountGroup({ entropySource, }: {
|
|
98
|
+
entropySource: EntropySourceId;
|
|
99
|
+
}): Promise<MultichainAccountGroup<Bip44Account<KeyringAccount>>>;
|
|
89
100
|
}
|
|
90
101
|
export {};
|
|
91
102
|
//# sourceMappingURL=MultichainAccountService.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountService.d.mts","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MultichainAccountService.d.mts","sourceRoot":"","sources":["../src/MultichainAccountService.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAEV,YAAY,EACb,8BAA8B;AAE/B,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAG7E,OAAO,KAAK,EAAE,sBAAsB,EAAE,qCAAiC;AACvE,OAAO,EAAE,uBAAuB,EAAE,sCAAkC;AAGpE,OAAO,KAAK,EAAE,iCAAiC,EAAE,oBAAgB;AAEjE,eAAO,MAAM,WAAW,6BAA6B,CAAC;AAEtD;;GAEG;AACH,KAAK,+BAA+B,GAAG;IACrC,SAAS,EAAE,iCAAiC,CAAC;CAC9C,CAAC;AAEF,0FAA0F;AAC1F,KAAK,cAAc,CAAC,OAAO,SAAS,YAAY,CAAC,cAAc,CAAC,IAAI;IAClE,MAAM,EAAE,uBAAuB,CAAC,OAAO,CAAC,CAAC;IACzC,KAAK,EAAE,sBAAsB,CAAC,OAAO,CAAC,CAAC;CACxC,CAAC;AAEF;;GAEG;AACH,qBAAa,wBAAwB;;IAenC;;OAEG;IACH,IAAI,EAAE,OAAO,WAAW,CAAe;IAEvC;;;;;;OAMG;gBACS,EAAE,SAAS,EAAE,EAAE,+BAA+B;IAgC1D;;;OAGG;IACH,IAAI,IAAI,IAAI;IAwHZ;;;;;;OAMG;IACH,iBAAiB,CACf,EAAE,EAAE,cAAc,CAAC,IAAI,CAAC,GACvB,cAAc,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,GAAG,SAAS;IAI3D;;;;;;;OAOG;IACH,0BAA0B,CAAC,EACzB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,uBAAuB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IAIzD;;;;OAIG;IACH,2BAA2B,IAAI,uBAAuB,CACpD,YAAY,CAAC,cAAc,CAAC,CAC7B,EAAE;IAIH;;;;;;;;;OASG;IACH,yBAAyB,CAAC,EACxB,aAAa,EACb,UAAU,GACX,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;QAC/B,UAAU,EAAE,MAAM,CAAC;KACpB,GAAG,sBAAsB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC;IAWxD;;;;;;;OAOG;IACH,0BAA0B,CAAC,EACzB,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,sBAAsB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,EAAE;IAI1D;;;;;;OAMG;IACG,gCAAgC,CAAC,EACrC,aAAa,GACd,EAAE;QACD,aAAa,EAAE,eAAe,CAAC;KAChC,GAAG,OAAO,CAAC,sBAAsB,CAAC,YAAY,CAAC,cAAc,CAAC,CAAC,CAAC;CAKlE"}
|
|
@@ -10,8 +10,9 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
12
|
var _MultichainAccountService_instances, _MultichainAccountService_messenger, _MultichainAccountService_providers, _MultichainAccountService_wallets, _MultichainAccountService_accountIdToContext, _MultichainAccountService_handleOnAccountAdded, _MultichainAccountService_handleOnAccountRemoved, _MultichainAccountService_getWallet;
|
|
13
|
-
import { isBip44Account,
|
|
13
|
+
import { isBip44Account, toMultichainAccountWalletId } from "@metamask/account-api";
|
|
14
14
|
import { KeyringTypes } from "@metamask/keyring-controller";
|
|
15
|
+
import { MultichainAccountWallet } from "./MultichainAccountWallet.mjs";
|
|
15
16
|
import { EvmAccountProvider } from "./providers/EvmAccountProvider.mjs";
|
|
16
17
|
import { SolAccountProvider } from "./providers/SolAccountProvider.mjs";
|
|
17
18
|
export const serviceName = 'MultichainAccountService';
|
|
@@ -44,10 +45,11 @@ export class MultichainAccountService {
|
|
|
44
45
|
new EvmAccountProvider(__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f")),
|
|
45
46
|
new SolAccountProvider(__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f")),
|
|
46
47
|
], "f");
|
|
47
|
-
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:
|
|
48
|
-
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:
|
|
48
|
+
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountGroup', (...args) => this.getMultichainAccountGroup(...args));
|
|
49
|
+
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountGroups', (...args) => this.getMultichainAccountGroups(...args));
|
|
49
50
|
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountWallet', (...args) => this.getMultichainAccountWallet(...args));
|
|
50
51
|
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:getMultichainAccountWallets', (...args) => this.getMultichainAccountWallets(...args));
|
|
52
|
+
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").registerActionHandler('MultichainAccountService:createNextMultichainAccountGroup', (...args) => this.createNextMultichainAccountGroup(...args));
|
|
51
53
|
}
|
|
52
54
|
/**
|
|
53
55
|
* Initialize the service and constructs the internal reprensentation of
|
|
@@ -68,11 +70,11 @@ export class MultichainAccountService {
|
|
|
68
70
|
});
|
|
69
71
|
__classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").set(wallet.id, wallet);
|
|
70
72
|
// Reverse mapping between account ID and their multichain wallet/account:
|
|
71
|
-
for (const
|
|
72
|
-
for (const account of
|
|
73
|
+
for (const group of wallet.getMultichainAccountGroups()) {
|
|
74
|
+
for (const account of group.getAccounts()) {
|
|
73
75
|
__classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").set(account.id, {
|
|
74
76
|
wallet,
|
|
75
|
-
|
|
77
|
+
group,
|
|
76
78
|
});
|
|
77
79
|
}
|
|
78
80
|
}
|
|
@@ -82,13 +84,13 @@ export class MultichainAccountService {
|
|
|
82
84
|
__classPrivateFieldGet(this, _MultichainAccountService_messenger, "f").subscribe('AccountsController:accountRemoved', (id) => __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_handleOnAccountRemoved).call(this, id));
|
|
83
85
|
}
|
|
84
86
|
/**
|
|
85
|
-
* Gets
|
|
87
|
+
* Gets the account's context which contains its multichain wallet and
|
|
88
|
+
* multichain account group references.
|
|
86
89
|
*
|
|
87
90
|
* @param id - Account ID.
|
|
88
|
-
* @returns
|
|
89
|
-
* that account ID, or undefined if this account ID is not part of any.
|
|
91
|
+
* @returns The account context if any, undefined otherwise.
|
|
90
92
|
*/
|
|
91
|
-
|
|
93
|
+
getAccountContext(id) {
|
|
92
94
|
return __classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").get(id);
|
|
93
95
|
}
|
|
94
96
|
/**
|
|
@@ -111,7 +113,8 @@ export class MultichainAccountService {
|
|
|
111
113
|
return Array.from(__classPrivateFieldGet(this, _MultichainAccountService_wallets, "f").values());
|
|
112
114
|
}
|
|
113
115
|
/**
|
|
114
|
-
* Gets a reference to the multichain account matching this entropy source
|
|
116
|
+
* Gets a reference to the multichain account group matching this entropy source
|
|
117
|
+
* and a group index.
|
|
115
118
|
*
|
|
116
119
|
* @param options - Options.
|
|
117
120
|
* @param options.entropySource - The entropy source of the multichain account.
|
|
@@ -119,23 +122,33 @@ export class MultichainAccountService {
|
|
|
119
122
|
* @throws If none multichain account match this entropy source and group index.
|
|
120
123
|
* @returns A reference to the multichain account.
|
|
121
124
|
*/
|
|
122
|
-
|
|
123
|
-
const multichainAccount = __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).
|
|
125
|
+
getMultichainAccountGroup({ entropySource, groupIndex, }) {
|
|
126
|
+
const multichainAccount = __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).getMultichainAccountGroup(groupIndex);
|
|
124
127
|
if (!multichainAccount) {
|
|
125
128
|
throw new Error(`No multichain account for index: ${groupIndex}`);
|
|
126
129
|
}
|
|
127
130
|
return multichainAccount;
|
|
128
131
|
}
|
|
129
132
|
/**
|
|
130
|
-
* Gets all multichain
|
|
133
|
+
* Gets all multichain account groups for a given entropy source.
|
|
131
134
|
*
|
|
132
135
|
* @param options - Options.
|
|
133
136
|
* @param options.entropySource - The entropy source to query.
|
|
134
137
|
* @throws If no multichain accounts match this entropy source.
|
|
135
138
|
* @returns A list of all multichain accounts.
|
|
136
139
|
*/
|
|
137
|
-
|
|
138
|
-
return __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).
|
|
140
|
+
getMultichainAccountGroups({ entropySource, }) {
|
|
141
|
+
return __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).getMultichainAccountGroups();
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Creates the next multichain account group.
|
|
145
|
+
*
|
|
146
|
+
* @param options - Options.
|
|
147
|
+
* @param options.entropySource - The wallet's entropy source.
|
|
148
|
+
* @returns The next multichain account group.
|
|
149
|
+
*/
|
|
150
|
+
async createNextMultichainAccountGroup({ entropySource, }) {
|
|
151
|
+
return await __classPrivateFieldGet(this, _MultichainAccountService_instances, "m", _MultichainAccountService_getWallet).call(this, entropySource).createNextMultichainAccountGroup();
|
|
139
152
|
}
|
|
140
153
|
}
|
|
141
154
|
_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) {
|
|
@@ -155,28 +168,28 @@ _MultichainAccountService_messenger = new WeakMap(), _MultichainAccountService_p
|
|
|
155
168
|
// If that's a new wallet wallet. There's nothing to "force-sync".
|
|
156
169
|
sync = false;
|
|
157
170
|
}
|
|
158
|
-
let
|
|
159
|
-
if (!
|
|
171
|
+
let group = wallet.getMultichainAccountGroup(account.options.entropy.groupIndex);
|
|
172
|
+
if (!group) {
|
|
160
173
|
// This new account is a new multichain account, let the wallet know
|
|
161
174
|
// it has to re-sync with its providers.
|
|
162
175
|
if (sync) {
|
|
163
176
|
wallet.sync();
|
|
164
177
|
}
|
|
165
|
-
|
|
178
|
+
group = wallet.getMultichainAccountGroup(account.options.entropy.groupIndex);
|
|
166
179
|
// If that's a new multichain account. There's nothing to "force-sync".
|
|
167
180
|
sync = false;
|
|
168
181
|
}
|
|
169
182
|
// We have to check against `undefined` in case `getMultichainAccount` is
|
|
170
183
|
// not able to find this multichain account (which should not be possible...)
|
|
171
|
-
if (
|
|
184
|
+
if (group) {
|
|
172
185
|
if (sync) {
|
|
173
|
-
|
|
186
|
+
group.sync();
|
|
174
187
|
}
|
|
175
188
|
// Same here, this account should have been already grouped in that
|
|
176
189
|
// multichain account.
|
|
177
190
|
__classPrivateFieldGet(this, _MultichainAccountService_accountIdToContext, "f").set(account.id, {
|
|
178
191
|
wallet,
|
|
179
|
-
|
|
192
|
+
group,
|
|
180
193
|
});
|
|
181
194
|
}
|
|
182
195
|
}, _MultichainAccountService_handleOnAccountRemoved = function _MultichainAccountService_handleOnAccountRemoved(id) {
|