@metamask-previews/multichain-account-service 0.3.0-preview-43d47013 → 0.3.0-preview-a811c5b7
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 +3 -10
- package/dist/MultichainAccountGroup.cjs.map +1 -1
- package/dist/MultichainAccountGroup.d.cts +3 -3
- package/dist/MultichainAccountGroup.d.cts.map +1 -1
- package/dist/MultichainAccountGroup.d.mts +3 -3
- package/dist/MultichainAccountGroup.d.mts.map +1 -1
- package/dist/MultichainAccountGroup.mjs.map +1 -1
- package/dist/MultichainAccountService.cjs +0 -11
- package/dist/MultichainAccountService.cjs.map +1 -1
- package/dist/MultichainAccountService.d.cts +6 -15
- package/dist/MultichainAccountService.d.cts.map +1 -1
- package/dist/MultichainAccountService.d.mts +6 -15
- package/dist/MultichainAccountService.d.mts.map +1 -1
- package/dist/MultichainAccountService.mjs +0 -11
- package/dist/MultichainAccountService.mjs.map +1 -1
- package/dist/MultichainAccountWallet.cjs +13 -96
- package/dist/MultichainAccountWallet.cjs.map +1 -1
- package/dist/MultichainAccountWallet.d.cts +8 -22
- package/dist/MultichainAccountWallet.d.cts.map +1 -1
- package/dist/MultichainAccountWallet.d.mts +8 -22
- package/dist/MultichainAccountWallet.d.mts.map +1 -1
- package/dist/MultichainAccountWallet.mjs +10 -94
- package/dist/MultichainAccountWallet.mjs.map +1 -1
- package/dist/index.cjs +1 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +0 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +0 -2
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +0 -2
- package/dist/index.mjs.map +1 -1
- package/dist/providers/BaseAccountProvider.cjs +1 -20
- package/dist/providers/BaseAccountProvider.cjs.map +1 -1
- package/dist/providers/BaseAccountProvider.d.cts +5 -25
- package/dist/providers/BaseAccountProvider.d.cts.map +1 -1
- package/dist/providers/BaseAccountProvider.d.mts +5 -25
- package/dist/providers/BaseAccountProvider.d.mts.map +1 -1
- package/dist/providers/BaseAccountProvider.mjs +0 -18
- package/dist/providers/BaseAccountProvider.mjs.map +1 -1
- package/dist/providers/EvmAccountProvider.cjs +0 -35
- package/dist/providers/EvmAccountProvider.cjs.map +1 -1
- package/dist/providers/EvmAccountProvider.d.cts +0 -66
- package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
- package/dist/providers/EvmAccountProvider.d.mts +0 -66
- package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
- package/dist/providers/EvmAccountProvider.mjs +1 -36
- package/dist/providers/EvmAccountProvider.mjs.map +1 -1
- package/dist/providers/SolAccountProvider.cjs +0 -39
- package/dist/providers/SolAccountProvider.cjs.map +1 -1
- package/dist/providers/SolAccountProvider.d.cts +1 -28
- package/dist/providers/SolAccountProvider.d.cts.map +1 -1
- package/dist/providers/SolAccountProvider.d.mts +1 -28
- package/dist/providers/SolAccountProvider.d.mts.map +1 -1
- package/dist/providers/SolAccountProvider.mjs +1 -40
- package/dist/providers/SolAccountProvider.mjs.map +1 -1
- package/dist/tests/accounts.cjs +0 -9
- package/dist/tests/accounts.cjs.map +1 -1
- package/dist/tests/accounts.d.cts +55 -27
- package/dist/tests/accounts.d.cts.map +1 -1
- package/dist/tests/accounts.d.mts +55 -27
- package/dist/tests/accounts.d.mts.map +1 -1
- package/dist/tests/accounts.mjs +0 -9
- package/dist/tests/accounts.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +1 -5
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +1 -5
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +4 -6
|
@@ -12,7 +12,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
12
12
|
};
|
|
13
13
|
var _MultichainAccountWallet_id, _MultichainAccountWallet_providers, _MultichainAccountWallet_entropySource, _MultichainAccountWallet_accounts;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.MultichainAccountWallet = void 0;
|
|
15
|
+
exports.toMultichainAccountWalletId = exports.MultichainAccountWallet = void 0;
|
|
16
16
|
const account_api_1 = require("@metamask/account-api");
|
|
17
17
|
const account_api_2 = require("@metamask/account-api");
|
|
18
18
|
const account_api_3 = require("@metamask/account-api");
|
|
@@ -27,7 +27,7 @@ class MultichainAccountWallet {
|
|
|
27
27
|
_MultichainAccountWallet_providers.set(this, void 0);
|
|
28
28
|
_MultichainAccountWallet_entropySource.set(this, void 0);
|
|
29
29
|
_MultichainAccountWallet_accounts.set(this, void 0);
|
|
30
|
-
__classPrivateFieldSet(this, _MultichainAccountWallet_id,
|
|
30
|
+
__classPrivateFieldSet(this, _MultichainAccountWallet_id, toMultichainAccountWalletId(entropySource), "f");
|
|
31
31
|
__classPrivateFieldSet(this, _MultichainAccountWallet_providers, providers, "f");
|
|
32
32
|
__classPrivateFieldSet(this, _MultichainAccountWallet_entropySource, entropySource, "f");
|
|
33
33
|
__classPrivateFieldSet(this, _MultichainAccountWallet_accounts, new Map(), "f");
|
|
@@ -56,15 +56,6 @@ class MultichainAccountWallet {
|
|
|
56
56
|
wallet: this,
|
|
57
57
|
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
58
58
|
});
|
|
59
|
-
// This existing multichain account group might differ from the
|
|
60
|
-
// `createMultichainAccountGroup` behavior. When creating a new
|
|
61
|
-
// group, we expect the providers to all succeed. But here, we're
|
|
62
|
-
// just fetching the account lists from them, so this group might
|
|
63
|
-
// not be "aligned" yet (e.g having a missing Solana account).
|
|
64
|
-
//
|
|
65
|
-
// Since "aligning" is an async operation, it would have to be run
|
|
66
|
-
// after the first-sync.
|
|
67
|
-
// TODO: Implement align mechanism to create "missing" accounts.
|
|
68
59
|
__classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").set(entropy.groupIndex, multichainAccount);
|
|
69
60
|
}
|
|
70
61
|
}
|
|
@@ -119,7 +110,7 @@ class MultichainAccountWallet {
|
|
|
119
110
|
if (!(0, account_api_1.isMultichainAccountGroupId)(id)) {
|
|
120
111
|
return undefined;
|
|
121
112
|
}
|
|
122
|
-
const groupIndex = (0, account_api_1.
|
|
113
|
+
const groupIndex = (0, account_api_1.getGroupIndexFromMultichainAccountId)(id);
|
|
123
114
|
return __classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").get(groupIndex);
|
|
124
115
|
}
|
|
125
116
|
/**
|
|
@@ -147,91 +138,17 @@ class MultichainAccountWallet {
|
|
|
147
138
|
getMultichainAccountGroups() {
|
|
148
139
|
return Array.from(__classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").values()); // TODO: Prevent copy here.
|
|
149
140
|
}
|
|
150
|
-
/**
|
|
151
|
-
* Gets next group index for this wallet.
|
|
152
|
-
*
|
|
153
|
-
* @returns The next group index of this wallet.
|
|
154
|
-
*/
|
|
155
|
-
getNextGroupIndex() {
|
|
156
|
-
// Assuming we cannot have indexes gaps.
|
|
157
|
-
return __classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").size; // No +1 here, group indexes starts at 0.
|
|
158
|
-
}
|
|
159
|
-
/**
|
|
160
|
-
* Creates a multichain account group for a given group index.
|
|
161
|
-
*
|
|
162
|
-
* @param groupIndex - The group index to use.
|
|
163
|
-
* @throws If any of the account providers fails to create their accounts.
|
|
164
|
-
* @returns The multichain account group for this group index.
|
|
165
|
-
*/
|
|
166
|
-
async createMultichainAccountGroup(groupIndex) {
|
|
167
|
-
const nextGroupIndex = this.getNextGroupIndex();
|
|
168
|
-
if (groupIndex > nextGroupIndex) {
|
|
169
|
-
throw new Error(`You cannot use a group index that is higher than the next available one: expected <=${nextGroupIndex}, got ${groupIndex}`);
|
|
170
|
-
}
|
|
171
|
-
let group = this.getMultichainAccountGroup(groupIndex);
|
|
172
|
-
if (group) {
|
|
173
|
-
// If the group already exists, we just `sync` it and returns the same
|
|
174
|
-
// reference.
|
|
175
|
-
group.sync();
|
|
176
|
-
return group;
|
|
177
|
-
}
|
|
178
|
-
const results = await Promise.allSettled(__classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f").map((provider) => provider.createAccounts({
|
|
179
|
-
entropySource: __classPrivateFieldGet(this, _MultichainAccountWallet_entropySource, "f"),
|
|
180
|
-
groupIndex,
|
|
181
|
-
})));
|
|
182
|
-
// READ THIS CAREFULLY:
|
|
183
|
-
// Since we're not "fully supporting multichain" for now, we still rely on single
|
|
184
|
-
// :accountCreated events to sync multichain account groups and wallets. Which means
|
|
185
|
-
// that even if of the provider fails, some accouns (on the `AccountsController`
|
|
186
|
-
// might be showing up AND that some multichain account wallets/groups might have
|
|
187
|
-
// been automatically sync'd too.
|
|
188
|
-
//
|
|
189
|
-
// We will need to unsubscribe from :accountAdded once we "fully support multichain"
|
|
190
|
-
// (meaning, the entire codebase relies on this new service for multichain account
|
|
191
|
-
// creations).
|
|
192
|
-
// --------------------------------------------------------------------------------
|
|
193
|
-
// If any of the provider failed to create their accounts, then we consider the
|
|
194
|
-
// multichain account group to have failed too.
|
|
195
|
-
// NOTE: Though, we don't rollback existing created accounts and that's totally fine
|
|
196
|
-
// because account creations is assumed to be imdepotent, thus, trying again should
|
|
197
|
-
// just create the missing accounts, and previously created accounts will just be
|
|
198
|
-
// re-used as is.
|
|
199
|
-
if (results.some((result) => result.status === 'rejected')) {
|
|
200
|
-
const error = `Unable to create multichain account group for index: ${groupIndex}`;
|
|
201
|
-
let warn = `${error}:`;
|
|
202
|
-
for (const result of results) {
|
|
203
|
-
if (result.status === 'rejected') {
|
|
204
|
-
warn += `\n- ${result.reason}`;
|
|
205
|
-
}
|
|
206
|
-
}
|
|
207
|
-
console.warn(warn);
|
|
208
|
-
throw new Error(error);
|
|
209
|
-
}
|
|
210
|
-
// Because of the :accountAdded automatic sync, we might already have created the
|
|
211
|
-
// group, so we first try to get it.
|
|
212
|
-
group = this.getMultichainAccountGroup(groupIndex);
|
|
213
|
-
if (!group) {
|
|
214
|
-
// If for some reason it's still not created, we're creating it explicitly now:
|
|
215
|
-
group = new MultichainAccountGroup_1.MultichainAccountGroup({
|
|
216
|
-
wallet: this,
|
|
217
|
-
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
218
|
-
groupIndex,
|
|
219
|
-
});
|
|
220
|
-
}
|
|
221
|
-
// Register the account to our internal map.
|
|
222
|
-
__classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").set(groupIndex, group); // `group` cannot be undefined here.
|
|
223
|
-
return group;
|
|
224
|
-
}
|
|
225
|
-
/**
|
|
226
|
-
* Creates the next multichain account group.
|
|
227
|
-
*
|
|
228
|
-
* @throws If any of the account providers fails to create their accounts.
|
|
229
|
-
* @returns The multichain account group for the next group index available.
|
|
230
|
-
*/
|
|
231
|
-
async createNextMultichainAccountGroup() {
|
|
232
|
-
return this.createMultichainAccountGroup(this.getNextGroupIndex());
|
|
233
|
-
}
|
|
234
141
|
}
|
|
235
142
|
exports.MultichainAccountWallet = MultichainAccountWallet;
|
|
236
143
|
_MultichainAccountWallet_id = new WeakMap(), _MultichainAccountWallet_providers = new WeakMap(), _MultichainAccountWallet_entropySource = new WeakMap(), _MultichainAccountWallet_accounts = new WeakMap();
|
|
144
|
+
/**
|
|
145
|
+
* Gets the multichain account wallet ID from its entropy source.
|
|
146
|
+
*
|
|
147
|
+
* @param entropySource - Entropy source ID of that wallet.
|
|
148
|
+
* @returns The multichain account wallet ID.
|
|
149
|
+
*/
|
|
150
|
+
function toMultichainAccountWalletId(entropySource) {
|
|
151
|
+
return `${account_api_3.AccountWalletType.Entropy}:${entropySource}`;
|
|
152
|
+
}
|
|
153
|
+
exports.toMultichainAccountWalletId = toMultichainAccountWalletId;
|
|
237
154
|
//# sourceMappingURL=MultichainAccountWallet.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountWallet.cjs","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;A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BAAiB;QACjB,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,KAAK,UAAU,CAAC,EAAE;YAC1D,MAAM,KAAK,GAAG,wDAAwD,UAAU,EAAE,CAAC;YAEnF,IAAI,IAAI,GAAG,GAAG,KAAK,GAAG,CAAC;YACvB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;gBAC5B,IAAI,MAAM,CAAC,MAAM,KAAK,UAAU,EAAE;oBAChC,IAAI,IAAI,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC;iBAChC;aACF;YACD,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAEnB,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;SACxB;QAED,iFAAiF;QACjF,oCAAoC;QACpC,KAAK,GAAG,IAAI,CAAC,yBAAyB,CAAC,UAAU,CAAC,CAAC;QACnD,IAAI,CAAC,KAAK,EAAE;YACV,+EAA+E;YAC/E,KAAK,GAAG,IAAI,+CAAsB,CAAC;gBACjC,MAAM,EAAE,IAAI;gBACZ,SAAS,EAAE,uBAAA,IAAI,0CAAW;gBAC1B,UAAU;aACX,CAAC,CAAC;SACJ;QAED,4CAA4C;QAC5C,uBAAA,IAAI,yCAAU,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,oCAAoC;QAE3E,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,gCAAgC;QAGpC,OAAO,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC,CAAC;IACrE,CAAC;CACF;AA3QD,0DA2QC","sourcesContent":["import {\n getGroupIndexFromMultichainAccountGroupId,\n isMultichainAccountGroupId,\n toMultichainAccountWalletId,\n} from '@metamask/account-api';\nimport { toDefaultAccountGroupId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type {\n Bip44Account,\n MultichainAccountWalletId,\n MultichainAccountWallet as MultichainAccountWalletDefinition,\n} from '@metamask/account-api';\nimport type { AccountGroupId } from '@metamask/account-api';\nimport type { AccountProvider } from '@metamask/account-api';\nimport {\n type EntropySourceId,\n type KeyringAccount,\n} from '@metamask/keyring-api';\n\nimport { MultichainAccountGroup } from './MultichainAccountGroup';\n\n/**\n * A multichain account wallet that holds multiple multichain accounts (one multichain account per\n * group index).\n */\nexport class MultichainAccountWallet<\n Account extends Bip44Account<KeyringAccount>,\n> implements MultichainAccountWalletDefinition<Account>\n{\n readonly #id: MultichainAccountWalletId;\n\n readonly #providers: AccountProvider<Account>[];\n\n readonly #entropySource: EntropySourceId;\n\n readonly #accounts: Map<number, MultichainAccountGroup<Account>>;\n\n constructor({\n providers,\n entropySource,\n }: {\n providers: AccountProvider<Account>[];\n entropySource: EntropySourceId;\n }) {\n this.#id = toMultichainAccountWalletId(entropySource);\n this.#providers = providers;\n this.#entropySource = entropySource;\n this.#accounts = new Map();\n\n // Initial synchronization.\n this.sync();\n }\n\n /**\n * Force wallet synchronization.\n *\n * This can be used if account providers got new accounts that the wallet\n * doesn't know about.\n */\n sync(): void {\n for (const provider of this.#providers) {\n for (const account of provider.getAccounts()) {\n const { entropy } = account.options;\n\n // Filter for this wallet only.\n if (entropy.id !== this.entropySource) {\n continue;\n }\n\n // This multichain account might exists already.\n let multichainAccount = this.#accounts.get(entropy.groupIndex);\n if (!multichainAccount) {\n multichainAccount = new MultichainAccountGroup<Account>({\n groupIndex: entropy.groupIndex,\n wallet: this,\n providers: this.#providers,\n });\n\n // This existing multichain account group might differ from the\n // `createMultichainAccountGroup` behavior. When creating a new\n // group, we expect the providers to all succeed. But here, we're\n // just fetching the account lists from them, so this group might\n // not be \"aligned\" yet (e.g having a missing Solana account).\n //\n // Since \"aligning\" is an async operation, it would have to be run\n // after the first-sync.\n // TODO: Implement align mechanism to create \"missing\" accounts.\n\n this.#accounts.set(entropy.groupIndex, multichainAccount);\n }\n }\n }\n\n // Now force-sync all remaining multichain accounts.\n for (const [groupIndex, multichainAccount] of this.#accounts.entries()) {\n multichainAccount.sync();\n\n // Clean up old multichain accounts.\n if (!multichainAccount.hasAccounts()) {\n this.#accounts.delete(groupIndex);\n }\n }\n }\n\n /**\n * Gets the multichain account wallet ID.\n *\n * @returns The multichain account wallet ID.\n */\n get id(): MultichainAccountWalletId {\n return this.#id;\n }\n\n /**\n * Gets the multichain account wallet type, which is always {@link AccountWalletType.Entropy}.\n *\n * @returns The multichain account wallet type.\n */\n get type(): AccountWalletType.Entropy {\n return AccountWalletType.Entropy;\n }\n\n /**\n * Gets the multichain account wallet entropy source.\n *\n * @returns The multichain account wallet entropy source.\n */\n get entropySource(): EntropySourceId {\n return this.#entropySource;\n }\n\n /**\n * Gets multichain account for a given ID.\n * The default group ID will default to the multichain account with index 0.\n *\n * @param id - Account group ID.\n * @returns Account group.\n */\n getAccountGroup(\n id: AccountGroupId,\n ): MultichainAccountGroup<Account> | undefined {\n // We consider the \"default case\" to be mapped to index 0.\n if (id === toDefaultAccountGroupId(this.id)) {\n return this.#accounts.get(0);\n }\n\n // If it is not a valid ID, we cannot extract the group index\n // from it, so we fail fast.\n if (!isMultichainAccountGroupId(id)) {\n return undefined;\n }\n\n const groupIndex = getGroupIndexFromMultichainAccountGroupId(id);\n return this.#accounts.get(groupIndex);\n }\n\n /**\n * Gets all multichain accounts. Similar to {@link MultichainAccountWallet.getMultichainAccountGroups}.\n *\n * @returns The multichain accounts.\n */\n getAccountGroups(): MultichainAccountGroup<Account>[] {\n return this.getMultichainAccountGroups();\n }\n\n /**\n * Gets multichain account group for a given index.\n *\n * @param groupIndex - Multichain account index.\n * @returns The multichain account associated with the given index.\n */\n getMultichainAccountGroup(\n groupIndex: number,\n ): MultichainAccountGroup<Account> | undefined {\n return this.#accounts.get(groupIndex);\n }\n\n /**\n * Gets all multichain account groups.\n *\n * @returns The multichain accounts.\n */\n getMultichainAccountGroups(): MultichainAccountGroup<Account>[] {\n return Array.from(this.#accounts.values()); // TODO: Prevent copy here.\n }\n\n /**\n * Gets next group index for this wallet.\n *\n * @returns The next group index of this wallet.\n */\n getNextGroupIndex(): number {\n // Assuming we cannot have indexes gaps.\n return this.#accounts.size; // No +1 here, group indexes starts at 0.\n }\n\n /**\n * Creates a multichain account group for a given group index.\n *\n * @param groupIndex - The group index to use.\n * @throws If any of the account providers fails to create their accounts.\n * @returns The multichain account group for this group index.\n */\n async createMultichainAccountGroup(\n groupIndex: number,\n ): Promise<MultichainAccountGroup<Account>> {\n const nextGroupIndex = this.getNextGroupIndex();\n if (groupIndex > nextGroupIndex) {\n throw new Error(\n `You cannot use a group index that is higher than the next available one: expected <=${nextGroupIndex}, got ${groupIndex}`,\n );\n }\n\n let group = this.getMultichainAccountGroup(groupIndex);\n if (group) {\n // If the group already exists, we just `sync` it and returns the same\n // reference.\n group.sync();\n\n return group;\n }\n\n const results = await Promise.allSettled(\n this.#providers.map((provider) =>\n provider.createAccounts({\n entropySource: this.#entropySource,\n groupIndex,\n }),\n ),\n );\n\n // READ THIS CAREFULLY:\n // Since we're not \"fully supporting multichain\" for now, we still rely on single\n // :accountCreated events to sync multichain account groups and wallets. Which means\n // that even if of the provider fails, some accouns (on the `AccountsController`\n // might be showing up AND that some multichain account wallets/groups might have\n // been automatically sync'd too.\n //\n // We will need to unsubscribe from :accountAdded once we \"fully support multichain\"\n // (meaning, the entire codebase relies on this new service for multichain account\n // creations).\n // --------------------------------------------------------------------------------\n\n // If any of the provider failed to create their accounts, then we consider the\n // multichain account group to have failed too.\n // NOTE: Though, we don't rollback existing created accounts and that's totally fine\n // because account creations is assumed to be imdepotent, thus, trying again should\n // just create the missing accounts, and previously created accounts will just be\n // re-used as is.\n if (results.some((result) => result.status === 'rejected')) {\n const error = `Unable to create multichain account group for index: ${groupIndex}`;\n\n let warn = `${error}:`;\n for (const result of results) {\n if (result.status === 'rejected') {\n warn += `\\n- ${result.reason}`;\n }\n }\n console.warn(warn);\n\n throw new Error(error);\n }\n\n // Because of the :accountAdded automatic sync, we might already have created the\n // group, so we first try to get it.\n group = this.getMultichainAccountGroup(groupIndex);\n if (!group) {\n // If for some reason it's still not created, we're creating it explicitly now:\n group = new MultichainAccountGroup({\n wallet: this,\n providers: this.#providers,\n groupIndex,\n });\n }\n\n // Register the account to our internal map.\n this.#accounts.set(groupIndex, group); // `group` cannot be undefined here.\n\n return group;\n }\n\n /**\n * Creates the next multichain account group.\n *\n * @throws If any of the account providers fails to create their accounts.\n * @returns The multichain account group for the next group index available.\n */\n async createNextMultichainAccountGroup(): Promise<\n MultichainAccountGroup<Account>\n > {\n return this.createMultichainAccountGroup(this.getNextGroupIndex());\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"MultichainAccountWallet.cjs","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA,uDAG+B;AAC/B,uDAAgE;AAChE,uDAA0D;AAM1D,yEAAkE;AAElE;;;GAGG;AACH,MAAa,uBAAuB;IAYlC,YAAY,EACV,SAAS,EACT,aAAa,GAId;QAdQ,8CAA+B;QAE/B,qDAAuC;QAEvC,yDAAgC;QAEhC,oDAAwD;QAS/D,uBAAA,IAAI,+BAAO,2BAA2B,CAAC,aAAa,CAAC,MAAA,CAAC;QACtD,uBAAA,IAAI,sCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,0CAAkB,aAAa,MAAA,CAAC;QACpC,uBAAA,IAAI,qCAAa,IAAI,GAAG,EAAE,MAAA,CAAC;QAE3B,2BAA2B;QAC3B,IAAI,CAAC,IAAI,EAAE,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACH,IAAI;QACF,KAAK,MAAM,QAAQ,IAAI,uBAAA,IAAI,0CAAW,EAAE;YACtC,KAAK,MAAM,OAAO,IAAI,QAAQ,CAAC,WAAW,EAAE,EAAE;gBAC5C,MAAM,EAAE,OAAO,EAAE,GAAG,OAAO,CAAC,OAAO,CAAC;gBAEpC,+BAA+B;gBAC/B,IAAI,OAA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SAC9B;QAED,6DAA6D;QAC7D,4BAA4B;QAC5B,IAAI,CAAC,IAAA,wCAA0B,EAAC,EAAE,CAAC,EAAE;YACnC,OAAO,SAAS,CAAC;SAClB;QAED,MAAM,UAAU,GAAG,IAAA,kDAAyC,EAAC,EAAE,CAAC,CAAC;QACjE,OAAO,uBAAA,IAAI,yCAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;IAED;;;;OAIG;IACH,gBAAgB;QACd,OAAO,IAAI,CAAC,0BAA0B,EAAE,CAAC;IAC3C,CAAC;IAED;;;;;OAKG;IACH,yBAAyB,CACvB,UAAkB;QAElB,OAAO,uBAAA,IAAI,yCAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;IAED;;;;OAIG;IACH,0BAA0B;QACxB,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,yCAAU,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,2BAA2B;IACzE,CAAC;CACF;AAtJD,0DAsJC;;AAED;;;;;GAKG;AACH,SAAgB,2BAA2B,CACzC,aAA8B;IAE9B,OAAO,GAAG,+BAAiB,CAAC,OAAO,IAAI,aAAa,EAAE,CAAC;AACzD,CAAC;AAJD,kEAIC","sourcesContent":["import type {\n Bip44Account,\n MultichainAccountWalletId,\n MultichainAccountWallet as MultichainAccountWalletDefinition,\n} from '@metamask/account-api';\nimport type { AccountGroupId } from '@metamask/account-api';\nimport type { AccountProvider } from '@metamask/account-api';\nimport {\n getGroupIndexFromMultichainAccountId as getGroupIndexFromMultichainAccountGroupId,\n isMultichainAccountGroupId,\n} from '@metamask/account-api';\nimport { toDefaultAccountGroupId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport {\n type EntropySourceId,\n type KeyringAccount,\n} from '@metamask/keyring-api';\n\nimport { MultichainAccountGroup } from './MultichainAccountGroup';\n\n/**\n * A multichain account wallet that holds multiple multichain accounts (one multichain account per\n * group index).\n */\nexport class MultichainAccountWallet<\n Account extends Bip44Account<KeyringAccount>,\n> implements MultichainAccountWalletDefinition<Account>\n{\n readonly #id: MultichainAccountWalletId;\n\n readonly #providers: AccountProvider<Account>[];\n\n readonly #entropySource: EntropySourceId;\n\n readonly #accounts: Map<number, MultichainAccountGroup<Account>>;\n\n constructor({\n providers,\n entropySource,\n }: {\n providers: AccountProvider<Account>[];\n entropySource: EntropySourceId;\n }) {\n this.#id = toMultichainAccountWalletId(entropySource);\n this.#providers = providers;\n this.#entropySource = entropySource;\n this.#accounts = new Map();\n\n // Initial synchronization.\n this.sync();\n }\n\n /**\n * Force wallet synchronization.\n *\n * This can be used if account providers got new accounts that the wallet\n * doesn't know about.\n */\n sync(): void {\n for (const provider of this.#providers) {\n for (const account of provider.getAccounts()) {\n const { entropy } = account.options;\n\n // Filter for this wallet only.\n if (entropy.id !== this.entropySource) {\n continue;\n }\n\n // This multichain account might exists already.\n let multichainAccount = this.#accounts.get(entropy.groupIndex);\n if (!multichainAccount) {\n multichainAccount = new MultichainAccountGroup<Account>({\n groupIndex: entropy.groupIndex,\n wallet: this,\n providers: this.#providers,\n });\n\n this.#accounts.set(entropy.groupIndex, multichainAccount);\n }\n }\n }\n\n // Now force-sync all remaining multichain accounts.\n for (const [groupIndex, multichainAccount] of this.#accounts.entries()) {\n multichainAccount.sync();\n\n // Clean up old multichain accounts.\n if (!multichainAccount.hasAccounts()) {\n this.#accounts.delete(groupIndex);\n }\n }\n }\n\n /**\n * Gets the multichain account wallet ID.\n *\n * @returns The multichain account wallet ID.\n */\n get id(): MultichainAccountWalletId {\n return this.#id;\n }\n\n /**\n * Gets the multichain account wallet type, which is always {@link AccountWalletType.Entropy}.\n *\n * @returns The multichain account wallet type.\n */\n get type(): AccountWalletType.Entropy {\n return AccountWalletType.Entropy;\n }\n\n /**\n * Gets the multichain account wallet entropy source.\n *\n * @returns The multichain account wallet entropy source.\n */\n get entropySource(): EntropySourceId {\n return this.#entropySource;\n }\n\n /**\n * Gets multichain account for a given ID.\n * The default group ID will default to the multichain account with index 0.\n *\n * @param id - Account group ID.\n * @returns Account group.\n */\n getAccountGroup(\n id: AccountGroupId,\n ): MultichainAccountGroup<Account> | undefined {\n // We consider the \"default case\" to be mapped to index 0.\n if (id === toDefaultAccountGroupId(this.id)) {\n return this.#accounts.get(0);\n }\n\n // If it is not a valid ID, we cannot extract the group index\n // from it, so we fail fast.\n if (!isMultichainAccountGroupId(id)) {\n return undefined;\n }\n\n const groupIndex = getGroupIndexFromMultichainAccountGroupId(id);\n return this.#accounts.get(groupIndex);\n }\n\n /**\n * Gets all multichain accounts. Similar to {@link MultichainAccountWallet.getMultichainAccountGroups}.\n *\n * @returns The multichain accounts.\n */\n getAccountGroups(): MultichainAccountGroup<Account>[] {\n return this.getMultichainAccountGroups();\n }\n\n /**\n * Gets multichain account group for a given index.\n *\n * @param groupIndex - Multichain account index.\n * @returns The multichain account associated with the given index.\n */\n getMultichainAccountGroup(\n groupIndex: number,\n ): MultichainAccountGroup<Account> | undefined {\n return this.#accounts.get(groupIndex);\n }\n\n /**\n * Gets all multichain account groups.\n *\n * @returns The multichain accounts.\n */\n getMultichainAccountGroups(): MultichainAccountGroup<Account>[] {\n return Array.from(this.#accounts.values()); // TODO: Prevent copy here.\n }\n}\n\n/**\n * Gets the multichain account wallet ID from its entropy source.\n *\n * @param entropySource - Entropy source ID of that wallet.\n * @returns The multichain account wallet ID.\n */\nexport function toMultichainAccountWalletId(\n entropySource: EntropySourceId,\n): MultichainAccountWalletId {\n return `${AccountWalletType.Entropy}:${entropySource}`;\n}\n"]}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AccountWalletType } from "@metamask/account-api";
|
|
2
1
|
import type { Bip44Account, MultichainAccountWalletId, MultichainAccountWallet as MultichainAccountWalletDefinition } from "@metamask/account-api";
|
|
3
2
|
import type { AccountGroupId } from "@metamask/account-api";
|
|
4
3
|
import type { AccountProvider } from "@metamask/account-api";
|
|
4
|
+
import { AccountWalletType } from "@metamask/account-api";
|
|
5
5
|
import { type EntropySourceId, type KeyringAccount } from "@metamask/keyring-api";
|
|
6
6
|
import { MultichainAccountGroup } from "./MultichainAccountGroup.cjs";
|
|
7
7
|
/**
|
|
@@ -66,26 +66,12 @@ export declare class MultichainAccountWallet<Account extends Bip44Account<Keyrin
|
|
|
66
66
|
* @returns The multichain accounts.
|
|
67
67
|
*/
|
|
68
68
|
getMultichainAccountGroups(): MultichainAccountGroup<Account>[];
|
|
69
|
-
/**
|
|
70
|
-
* Gets next group index for this wallet.
|
|
71
|
-
*
|
|
72
|
-
* @returns The next group index of this wallet.
|
|
73
|
-
*/
|
|
74
|
-
getNextGroupIndex(): number;
|
|
75
|
-
/**
|
|
76
|
-
* Creates a multichain account group for a given group index.
|
|
77
|
-
*
|
|
78
|
-
* @param groupIndex - The group index to use.
|
|
79
|
-
* @throws If any of the account providers fails to create their accounts.
|
|
80
|
-
* @returns The multichain account group for this group index.
|
|
81
|
-
*/
|
|
82
|
-
createMultichainAccountGroup(groupIndex: number): Promise<MultichainAccountGroup<Account>>;
|
|
83
|
-
/**
|
|
84
|
-
* Creates the next multichain account group.
|
|
85
|
-
*
|
|
86
|
-
* @throws If any of the account providers fails to create their accounts.
|
|
87
|
-
* @returns The multichain account group for the next group index available.
|
|
88
|
-
*/
|
|
89
|
-
createNextMultichainAccountGroup(): Promise<MultichainAccountGroup<Account>>;
|
|
90
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Gets the multichain account wallet ID from its entropy source.
|
|
72
|
+
*
|
|
73
|
+
* @param entropySource - Entropy source ID of that wallet.
|
|
74
|
+
* @returns The multichain account wallet ID.
|
|
75
|
+
*/
|
|
76
|
+
export declare function toMultichainAccountWalletId(entropySource: EntropySourceId): MultichainAccountWalletId;
|
|
91
77
|
//# sourceMappingURL=MultichainAccountWallet.d.cts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountWallet.d.cts","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MultichainAccountWallet.d.cts","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,yBAAyB,EACzB,uBAAuB,IAAI,iCAAiC,EAC7D,8BAA8B;AAC/B,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA8B;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAM7D,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAC1D,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,cAAc,EACpB,8BAA8B;AAE/B,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAElE;;;GAGG;AACH,qBAAa,uBAAuB,CAClC,OAAO,SAAS,YAAY,CAAC,cAAc,CAAC,CAC5C,YAAW,iCAAiC,CAAC,OAAO,CAAC;;gBAUzC,EACV,SAAS,EACT,aAAa,GACd,EAAE;QACD,SAAS,EAAE,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC;QACtC,aAAa,EAAE,eAAe,CAAC;KAChC;IAUD;;;;;OAKG;IACH,IAAI,IAAI,IAAI;IAmCZ;;;;OAIG;IACH,IAAI,EAAE,IAAI,yBAAyB,CAElC;IAED;;;;OAIG;IACH,IAAI,IAAI,IAAI,iBAAiB,CAAC,OAAO,CAEpC;IAED;;;;OAIG;IACH,IAAI,aAAa,IAAI,eAAe,CAEnC;IAED;;;;;;OAMG;IACH,eAAe,CACb,EAAE,EAAE,cAAc,GACjB,sBAAsB,CAAC,OAAO,CAAC,GAAG,SAAS;IAgB9C;;;;OAIG;IACH,gBAAgB,IAAI,sBAAsB,CAAC,OAAO,CAAC,EAAE;IAIrD;;;;;OAKG;IACH,yBAAyB,CACvB,UAAU,EAAE,MAAM,GACjB,sBAAsB,CAAC,OAAO,CAAC,GAAG,SAAS;IAI9C;;;;OAIG;IACH,0BAA0B,IAAI,sBAAsB,CAAC,OAAO,CAAC,EAAE;CAGhE;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,aAAa,EAAE,eAAe,GAC7B,yBAAyB,CAE3B"}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
import { AccountWalletType } from "@metamask/account-api";
|
|
2
1
|
import type { Bip44Account, MultichainAccountWalletId, MultichainAccountWallet as MultichainAccountWalletDefinition } from "@metamask/account-api";
|
|
3
2
|
import type { AccountGroupId } from "@metamask/account-api";
|
|
4
3
|
import type { AccountProvider } from "@metamask/account-api";
|
|
4
|
+
import { AccountWalletType } from "@metamask/account-api";
|
|
5
5
|
import { type EntropySourceId, type KeyringAccount } from "@metamask/keyring-api";
|
|
6
6
|
import { MultichainAccountGroup } from "./MultichainAccountGroup.mjs";
|
|
7
7
|
/**
|
|
@@ -66,26 +66,12 @@ export declare class MultichainAccountWallet<Account extends Bip44Account<Keyrin
|
|
|
66
66
|
* @returns The multichain accounts.
|
|
67
67
|
*/
|
|
68
68
|
getMultichainAccountGroups(): MultichainAccountGroup<Account>[];
|
|
69
|
-
/**
|
|
70
|
-
* Gets next group index for this wallet.
|
|
71
|
-
*
|
|
72
|
-
* @returns The next group index of this wallet.
|
|
73
|
-
*/
|
|
74
|
-
getNextGroupIndex(): number;
|
|
75
|
-
/**
|
|
76
|
-
* Creates a multichain account group for a given group index.
|
|
77
|
-
*
|
|
78
|
-
* @param groupIndex - The group index to use.
|
|
79
|
-
* @throws If any of the account providers fails to create their accounts.
|
|
80
|
-
* @returns The multichain account group for this group index.
|
|
81
|
-
*/
|
|
82
|
-
createMultichainAccountGroup(groupIndex: number): Promise<MultichainAccountGroup<Account>>;
|
|
83
|
-
/**
|
|
84
|
-
* Creates the next multichain account group.
|
|
85
|
-
*
|
|
86
|
-
* @throws If any of the account providers fails to create their accounts.
|
|
87
|
-
* @returns The multichain account group for the next group index available.
|
|
88
|
-
*/
|
|
89
|
-
createNextMultichainAccountGroup(): Promise<MultichainAccountGroup<Account>>;
|
|
90
69
|
}
|
|
70
|
+
/**
|
|
71
|
+
* Gets the multichain account wallet ID from its entropy source.
|
|
72
|
+
*
|
|
73
|
+
* @param entropySource - Entropy source ID of that wallet.
|
|
74
|
+
* @returns The multichain account wallet ID.
|
|
75
|
+
*/
|
|
76
|
+
export declare function toMultichainAccountWalletId(entropySource: EntropySourceId): MultichainAccountWalletId;
|
|
91
77
|
//# sourceMappingURL=MultichainAccountWallet.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountWallet.d.mts","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"MultichainAccountWallet.d.mts","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,YAAY,EACZ,yBAAyB,EACzB,uBAAuB,IAAI,iCAAiC,EAC7D,8BAA8B;AAC/B,OAAO,KAAK,EAAE,cAAc,EAAE,8BAA8B;AAC5D,OAAO,KAAK,EAAE,eAAe,EAAE,8BAA8B;AAM7D,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAC1D,OAAO,EACL,KAAK,eAAe,EACpB,KAAK,cAAc,EACpB,8BAA8B;AAE/B,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAElE;;;GAGG;AACH,qBAAa,uBAAuB,CAClC,OAAO,SAAS,YAAY,CAAC,cAAc,CAAC,CAC5C,YAAW,iCAAiC,CAAC,OAAO,CAAC;;gBAUzC,EACV,SAAS,EACT,aAAa,GACd,EAAE;QACD,SAAS,EAAE,eAAe,CAAC,OAAO,CAAC,EAAE,CAAC;QACtC,aAAa,EAAE,eAAe,CAAC;KAChC;IAUD;;;;;OAKG;IACH,IAAI,IAAI,IAAI;IAmCZ;;;;OAIG;IACH,IAAI,EAAE,IAAI,yBAAyB,CAElC;IAED;;;;OAIG;IACH,IAAI,IAAI,IAAI,iBAAiB,CAAC,OAAO,CAEpC;IAED;;;;OAIG;IACH,IAAI,aAAa,IAAI,eAAe,CAEnC;IAED;;;;;;OAMG;IACH,eAAe,CACb,EAAE,EAAE,cAAc,GACjB,sBAAsB,CAAC,OAAO,CAAC,GAAG,SAAS;IAgB9C;;;;OAIG;IACH,gBAAgB,IAAI,sBAAsB,CAAC,OAAO,CAAC,EAAE;IAIrD;;;;;OAKG;IACH,yBAAyB,CACvB,UAAU,EAAE,MAAM,GACjB,sBAAsB,CAAC,OAAO,CAAC,GAAG,SAAS;IAI9C;;;;OAIG;IACH,0BAA0B,IAAI,sBAAsB,CAAC,OAAO,CAAC,EAAE;CAGhE;AAED;;;;;GAKG;AACH,wBAAgB,2BAA2B,CACzC,aAAa,EAAE,eAAe,GAC7B,yBAAyB,CAE3B"}
|
|
@@ -10,7 +10,7 @@ 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 _MultichainAccountWallet_id, _MultichainAccountWallet_providers, _MultichainAccountWallet_entropySource, _MultichainAccountWallet_accounts;
|
|
13
|
-
import { getGroupIndexFromMultichainAccountGroupId, isMultichainAccountGroupId
|
|
13
|
+
import { getGroupIndexFromMultichainAccountId as getGroupIndexFromMultichainAccountGroupId, isMultichainAccountGroupId } from "@metamask/account-api";
|
|
14
14
|
import { toDefaultAccountGroupId } from "@metamask/account-api";
|
|
15
15
|
import { AccountWalletType } from "@metamask/account-api";
|
|
16
16
|
import { MultichainAccountGroup } from "./MultichainAccountGroup.mjs";
|
|
@@ -53,15 +53,6 @@ export class MultichainAccountWallet {
|
|
|
53
53
|
wallet: this,
|
|
54
54
|
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
55
55
|
});
|
|
56
|
-
// This existing multichain account group might differ from the
|
|
57
|
-
// `createMultichainAccountGroup` behavior. When creating a new
|
|
58
|
-
// group, we expect the providers to all succeed. But here, we're
|
|
59
|
-
// just fetching the account lists from them, so this group might
|
|
60
|
-
// not be "aligned" yet (e.g having a missing Solana account).
|
|
61
|
-
//
|
|
62
|
-
// Since "aligning" is an async operation, it would have to be run
|
|
63
|
-
// after the first-sync.
|
|
64
|
-
// TODO: Implement align mechanism to create "missing" accounts.
|
|
65
56
|
__classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").set(entropy.groupIndex, multichainAccount);
|
|
66
57
|
}
|
|
67
58
|
}
|
|
@@ -144,90 +135,15 @@ export class MultichainAccountWallet {
|
|
|
144
135
|
getMultichainAccountGroups() {
|
|
145
136
|
return Array.from(__classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").values()); // TODO: Prevent copy here.
|
|
146
137
|
}
|
|
147
|
-
/**
|
|
148
|
-
* Gets next group index for this wallet.
|
|
149
|
-
*
|
|
150
|
-
* @returns The next group index of this wallet.
|
|
151
|
-
*/
|
|
152
|
-
getNextGroupIndex() {
|
|
153
|
-
// Assuming we cannot have indexes gaps.
|
|
154
|
-
return __classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").size; // No +1 here, group indexes starts at 0.
|
|
155
|
-
}
|
|
156
|
-
/**
|
|
157
|
-
* Creates a multichain account group for a given group index.
|
|
158
|
-
*
|
|
159
|
-
* @param groupIndex - The group index to use.
|
|
160
|
-
* @throws If any of the account providers fails to create their accounts.
|
|
161
|
-
* @returns The multichain account group for this group index.
|
|
162
|
-
*/
|
|
163
|
-
async createMultichainAccountGroup(groupIndex) {
|
|
164
|
-
const nextGroupIndex = this.getNextGroupIndex();
|
|
165
|
-
if (groupIndex > nextGroupIndex) {
|
|
166
|
-
throw new Error(`You cannot use a group index that is higher than the next available one: expected <=${nextGroupIndex}, got ${groupIndex}`);
|
|
167
|
-
}
|
|
168
|
-
let group = this.getMultichainAccountGroup(groupIndex);
|
|
169
|
-
if (group) {
|
|
170
|
-
// If the group already exists, we just `sync` it and returns the same
|
|
171
|
-
// reference.
|
|
172
|
-
group.sync();
|
|
173
|
-
return group;
|
|
174
|
-
}
|
|
175
|
-
const results = await Promise.allSettled(__classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f").map((provider) => provider.createAccounts({
|
|
176
|
-
entropySource: __classPrivateFieldGet(this, _MultichainAccountWallet_entropySource, "f"),
|
|
177
|
-
groupIndex,
|
|
178
|
-
})));
|
|
179
|
-
// READ THIS CAREFULLY:
|
|
180
|
-
// Since we're not "fully supporting multichain" for now, we still rely on single
|
|
181
|
-
// :accountCreated events to sync multichain account groups and wallets. Which means
|
|
182
|
-
// that even if of the provider fails, some accouns (on the `AccountsController`
|
|
183
|
-
// might be showing up AND that some multichain account wallets/groups might have
|
|
184
|
-
// been automatically sync'd too.
|
|
185
|
-
//
|
|
186
|
-
// We will need to unsubscribe from :accountAdded once we "fully support multichain"
|
|
187
|
-
// (meaning, the entire codebase relies on this new service for multichain account
|
|
188
|
-
// creations).
|
|
189
|
-
// --------------------------------------------------------------------------------
|
|
190
|
-
// If any of the provider failed to create their accounts, then we consider the
|
|
191
|
-
// multichain account group to have failed too.
|
|
192
|
-
// NOTE: Though, we don't rollback existing created accounts and that's totally fine
|
|
193
|
-
// because account creations is assumed to be imdepotent, thus, trying again should
|
|
194
|
-
// just create the missing accounts, and previously created accounts will just be
|
|
195
|
-
// re-used as is.
|
|
196
|
-
if (results.some((result) => result.status === 'rejected')) {
|
|
197
|
-
const error = `Unable to create multichain account group for index: ${groupIndex}`;
|
|
198
|
-
let warn = `${error}:`;
|
|
199
|
-
for (const result of results) {
|
|
200
|
-
if (result.status === 'rejected') {
|
|
201
|
-
warn += `\n- ${result.reason}`;
|
|
202
|
-
}
|
|
203
|
-
}
|
|
204
|
-
console.warn(warn);
|
|
205
|
-
throw new Error(error);
|
|
206
|
-
}
|
|
207
|
-
// Because of the :accountAdded automatic sync, we might already have created the
|
|
208
|
-
// group, so we first try to get it.
|
|
209
|
-
group = this.getMultichainAccountGroup(groupIndex);
|
|
210
|
-
if (!group) {
|
|
211
|
-
// If for some reason it's still not created, we're creating it explicitly now:
|
|
212
|
-
group = new MultichainAccountGroup({
|
|
213
|
-
wallet: this,
|
|
214
|
-
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
215
|
-
groupIndex,
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
// Register the account to our internal map.
|
|
219
|
-
__classPrivateFieldGet(this, _MultichainAccountWallet_accounts, "f").set(groupIndex, group); // `group` cannot be undefined here.
|
|
220
|
-
return group;
|
|
221
|
-
}
|
|
222
|
-
/**
|
|
223
|
-
* Creates the next multichain account group.
|
|
224
|
-
*
|
|
225
|
-
* @throws If any of the account providers fails to create their accounts.
|
|
226
|
-
* @returns The multichain account group for the next group index available.
|
|
227
|
-
*/
|
|
228
|
-
async createNextMultichainAccountGroup() {
|
|
229
|
-
return this.createMultichainAccountGroup(this.getNextGroupIndex());
|
|
230
|
-
}
|
|
231
138
|
}
|
|
232
139
|
_MultichainAccountWallet_id = new WeakMap(), _MultichainAccountWallet_providers = new WeakMap(), _MultichainAccountWallet_entropySource = new WeakMap(), _MultichainAccountWallet_accounts = new WeakMap();
|
|
140
|
+
/**
|
|
141
|
+
* Gets the multichain account wallet ID from its entropy source.
|
|
142
|
+
*
|
|
143
|
+
* @param entropySource - Entropy source ID of that wallet.
|
|
144
|
+
* @returns The multichain account wallet ID.
|
|
145
|
+
*/
|
|
146
|
+
export function toMultichainAccountWalletId(entropySource) {
|
|
147
|
+
return `${AccountWalletType.Entropy}:${entropySource}`;
|
|
148
|
+
}
|
|
233
149
|
//# sourceMappingURL=MultichainAccountWallet.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountWallet.mjs","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;AAAA,OAAO,EACL,yCAAyC,EACzC,0BAA0B,EAC1B,2BAA2B,EAC5B,8BAA8B;AAC/B,OAAO,EAAE,uBAAuB,EAAE,8BAA8B;AAChE,OAAO,EAAE,iBAAiB,EAAE,8B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C,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,MAAM,KAAK,UAAU,CAAC,EAAE;YAC1D,MAAM,KAAK,GAAG,wDAAwD,UAAU,EAAE,CAAC;YAEnF,IAAI,IAAI,GAAG,GAAG,KAAK,GAAG,CAAC;YACvB,KAAK,MAAM,MAAM,IAAI,OAAO,EAAE;gBAC5B,IAAI,MAAM,CAAC,MAAM,KAAK,UAAU,EAAE;oBAChC,IAAI,IAAI,OAAO,MAAM,CAAC,MAAM,EAAE,CAAC;iBAChC;aACF;YACD,OAAO,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;YAEnB,MAAM,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;SACxB;QAED,iFAAiF;QACjF,oCAAoC;QACpC,KAAK,GAAG,IAAI,CAAC,yBAAyB,CAAC,UAAU,CAAC,CAAC;QACnD,IAAI,CAAC,KAAK,EAAE;YACV,+EAA+E;YAC/E,KAAK,GAAG,IAAI,sBAAsB,CAAC;gBACjC,MAAM,EAAE,IAAI;gBACZ,SAAS,EAAE,uBAAA,IAAI,0CAAW;gBAC1B,UAAU;aACX,CAAC,CAAC;SACJ;QAED,4CAA4C;QAC5C,uBAAA,IAAI,yCAAU,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,oCAAoC;QAE3E,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;;OAKG;IACH,KAAK,CAAC,gCAAgC;QAGpC,OAAO,IAAI,CAAC,4BAA4B,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC,CAAC;IACrE,CAAC;CACF","sourcesContent":["import {\n getGroupIndexFromMultichainAccountGroupId,\n isMultichainAccountGroupId,\n toMultichainAccountWalletId,\n} from '@metamask/account-api';\nimport { toDefaultAccountGroupId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport type {\n Bip44Account,\n MultichainAccountWalletId,\n MultichainAccountWallet as MultichainAccountWalletDefinition,\n} from '@metamask/account-api';\nimport type { AccountGroupId } from '@metamask/account-api';\nimport type { AccountProvider } from '@metamask/account-api';\nimport {\n type EntropySourceId,\n type KeyringAccount,\n} from '@metamask/keyring-api';\n\nimport { MultichainAccountGroup } from './MultichainAccountGroup';\n\n/**\n * A multichain account wallet that holds multiple multichain accounts (one multichain account per\n * group index).\n */\nexport class MultichainAccountWallet<\n Account extends Bip44Account<KeyringAccount>,\n> implements MultichainAccountWalletDefinition<Account>\n{\n readonly #id: MultichainAccountWalletId;\n\n readonly #providers: AccountProvider<Account>[];\n\n readonly #entropySource: EntropySourceId;\n\n readonly #accounts: Map<number, MultichainAccountGroup<Account>>;\n\n constructor({\n providers,\n entropySource,\n }: {\n providers: AccountProvider<Account>[];\n entropySource: EntropySourceId;\n }) {\n this.#id = toMultichainAccountWalletId(entropySource);\n this.#providers = providers;\n this.#entropySource = entropySource;\n this.#accounts = new Map();\n\n // Initial synchronization.\n this.sync();\n }\n\n /**\n * Force wallet synchronization.\n *\n * This can be used if account providers got new accounts that the wallet\n * doesn't know about.\n */\n sync(): void {\n for (const provider of this.#providers) {\n for (const account of provider.getAccounts()) {\n const { entropy } = account.options;\n\n // Filter for this wallet only.\n if (entropy.id !== this.entropySource) {\n continue;\n }\n\n // This multichain account might exists already.\n let multichainAccount = this.#accounts.get(entropy.groupIndex);\n if (!multichainAccount) {\n multichainAccount = new MultichainAccountGroup<Account>({\n groupIndex: entropy.groupIndex,\n wallet: this,\n providers: this.#providers,\n });\n\n // This existing multichain account group might differ from the\n // `createMultichainAccountGroup` behavior. When creating a new\n // group, we expect the providers to all succeed. But here, we're\n // just fetching the account lists from them, so this group might\n // not be \"aligned\" yet (e.g having a missing Solana account).\n //\n // Since \"aligning\" is an async operation, it would have to be run\n // after the first-sync.\n // TODO: Implement align mechanism to create \"missing\" accounts.\n\n this.#accounts.set(entropy.groupIndex, multichainAccount);\n }\n }\n }\n\n // Now force-sync all remaining multichain accounts.\n for (const [groupIndex, multichainAccount] of this.#accounts.entries()) {\n multichainAccount.sync();\n\n // Clean up old multichain accounts.\n if (!multichainAccount.hasAccounts()) {\n this.#accounts.delete(groupIndex);\n }\n }\n }\n\n /**\n * Gets the multichain account wallet ID.\n *\n * @returns The multichain account wallet ID.\n */\n get id(): MultichainAccountWalletId {\n return this.#id;\n }\n\n /**\n * Gets the multichain account wallet type, which is always {@link AccountWalletType.Entropy}.\n *\n * @returns The multichain account wallet type.\n */\n get type(): AccountWalletType.Entropy {\n return AccountWalletType.Entropy;\n }\n\n /**\n * Gets the multichain account wallet entropy source.\n *\n * @returns The multichain account wallet entropy source.\n */\n get entropySource(): EntropySourceId {\n return this.#entropySource;\n }\n\n /**\n * Gets multichain account for a given ID.\n * The default group ID will default to the multichain account with index 0.\n *\n * @param id - Account group ID.\n * @returns Account group.\n */\n getAccountGroup(\n id: AccountGroupId,\n ): MultichainAccountGroup<Account> | undefined {\n // We consider the \"default case\" to be mapped to index 0.\n if (id === toDefaultAccountGroupId(this.id)) {\n return this.#accounts.get(0);\n }\n\n // If it is not a valid ID, we cannot extract the group index\n // from it, so we fail fast.\n if (!isMultichainAccountGroupId(id)) {\n return undefined;\n }\n\n const groupIndex = getGroupIndexFromMultichainAccountGroupId(id);\n return this.#accounts.get(groupIndex);\n }\n\n /**\n * Gets all multichain accounts. Similar to {@link MultichainAccountWallet.getMultichainAccountGroups}.\n *\n * @returns The multichain accounts.\n */\n getAccountGroups(): MultichainAccountGroup<Account>[] {\n return this.getMultichainAccountGroups();\n }\n\n /**\n * Gets multichain account group for a given index.\n *\n * @param groupIndex - Multichain account index.\n * @returns The multichain account associated with the given index.\n */\n getMultichainAccountGroup(\n groupIndex: number,\n ): MultichainAccountGroup<Account> | undefined {\n return this.#accounts.get(groupIndex);\n }\n\n /**\n * Gets all multichain account groups.\n *\n * @returns The multichain accounts.\n */\n getMultichainAccountGroups(): MultichainAccountGroup<Account>[] {\n return Array.from(this.#accounts.values()); // TODO: Prevent copy here.\n }\n\n /**\n * Gets next group index for this wallet.\n *\n * @returns The next group index of this wallet.\n */\n getNextGroupIndex(): number {\n // Assuming we cannot have indexes gaps.\n return this.#accounts.size; // No +1 here, group indexes starts at 0.\n }\n\n /**\n * Creates a multichain account group for a given group index.\n *\n * @param groupIndex - The group index to use.\n * @throws If any of the account providers fails to create their accounts.\n * @returns The multichain account group for this group index.\n */\n async createMultichainAccountGroup(\n groupIndex: number,\n ): Promise<MultichainAccountGroup<Account>> {\n const nextGroupIndex = this.getNextGroupIndex();\n if (groupIndex > nextGroupIndex) {\n throw new Error(\n `You cannot use a group index that is higher than the next available one: expected <=${nextGroupIndex}, got ${groupIndex}`,\n );\n }\n\n let group = this.getMultichainAccountGroup(groupIndex);\n if (group) {\n // If the group already exists, we just `sync` it and returns the same\n // reference.\n group.sync();\n\n return group;\n }\n\n const results = await Promise.allSettled(\n this.#providers.map((provider) =>\n provider.createAccounts({\n entropySource: this.#entropySource,\n groupIndex,\n }),\n ),\n );\n\n // READ THIS CAREFULLY:\n // Since we're not \"fully supporting multichain\" for now, we still rely on single\n // :accountCreated events to sync multichain account groups and wallets. Which means\n // that even if of the provider fails, some accouns (on the `AccountsController`\n // might be showing up AND that some multichain account wallets/groups might have\n // been automatically sync'd too.\n //\n // We will need to unsubscribe from :accountAdded once we \"fully support multichain\"\n // (meaning, the entire codebase relies on this new service for multichain account\n // creations).\n // --------------------------------------------------------------------------------\n\n // If any of the provider failed to create their accounts, then we consider the\n // multichain account group to have failed too.\n // NOTE: Though, we don't rollback existing created accounts and that's totally fine\n // because account creations is assumed to be imdepotent, thus, trying again should\n // just create the missing accounts, and previously created accounts will just be\n // re-used as is.\n if (results.some((result) => result.status === 'rejected')) {\n const error = `Unable to create multichain account group for index: ${groupIndex}`;\n\n let warn = `${error}:`;\n for (const result of results) {\n if (result.status === 'rejected') {\n warn += `\\n- ${result.reason}`;\n }\n }\n console.warn(warn);\n\n throw new Error(error);\n }\n\n // Because of the :accountAdded automatic sync, we might already have created the\n // group, so we first try to get it.\n group = this.getMultichainAccountGroup(groupIndex);\n if (!group) {\n // If for some reason it's still not created, we're creating it explicitly now:\n group = new MultichainAccountGroup({\n wallet: this,\n providers: this.#providers,\n groupIndex,\n });\n }\n\n // Register the account to our internal map.\n this.#accounts.set(groupIndex, group); // `group` cannot be undefined here.\n\n return group;\n }\n\n /**\n * Creates the next multichain account group.\n *\n * @throws If any of the account providers fails to create their accounts.\n * @returns The multichain account group for the next group index available.\n */\n async createNextMultichainAccountGroup(): Promise<\n MultichainAccountGroup<Account>\n > {\n return this.createMultichainAccountGroup(this.getNextGroupIndex());\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"MultichainAccountWallet.mjs","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,OAAO,EACL,oCAAoC,IAAI,yCAAyC,EACjF,0BAA0B,EAC3B,8BAA8B;AAC/B,OAAO,EAAE,uBAAuB,EAAE,8BAA8B;AAChE,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAM1D,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAElE;;;GAGG;AACH,MAAM,OAAO,uBAAuB;IAYlC,YAAY,EACV,SAAS,EACT,aAAa,GAId;QAdQ,8CAA+B;QAE/B,qDAAuC;QAEvC,yDAAgC;QAEhC,oDAAwD;QAS/D,uBAAA,IAAI,+BAAO,2BAA2B,CAAC,aAAa,CAAC,MAAA,CAAC;QACtD,uBAAA,IAAI,sCAAc,SAAS,MAAA,CAAC;QAC5B,uBAAA,IAAI,0CAAkB,aAAa,MAAA,CAAC;QACpC,uBAAA,IAAI,qCAAa,IAAI,GAAG,EAAE,MAAA,CAAC;QAE3B,2BAA2B;QAC3B,IAAI,CAAC,IAAI,EAAE,CAAC;IACd,CAAC;IAED;;;;;OAKG;IACH,IAAI;QACF,KAAK,MAAM,QAAQ,IAAI,uBAAA,IAAI,0CAAW,EAAE;YACtC,KAAK,MAAM,OAAO,IAAI,QAAQ,CAAC,WAAW,EAAE,EAAE;gBAC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AAE,CAAC,EAAE;YAC3C,OAAO,uBAAA,IAAI,yCAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;SAC9B;QAED,6DAA6D;QAC7D,4BAA4B;QAC5B,IAAI,CAAC,0BAA0B,CAAC,EAAE,CAAC,EAAE;YACnC,OAAO,SAAS,CAAC;SAClB;QAED,MAAM,UAAU,GAAG,yCAAyC,CAAC,EAAE,CAAC,CAAC;QACjE,OAAO,uBAAA,IAAI,yCAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;IAED;;;;OAIG;IACH,gBAAgB;QACd,OAAO,IAAI,CAAC,0BAA0B,EAAE,CAAC;IAC3C,CAAC;IAED;;;;;OAKG;IACH,yBAAyB,CACvB,UAAkB;QAElB,OAAO,uBAAA,IAAI,yCAAU,CAAC,GAAG,CAAC,UAAU,CAAC,CAAC;IACxC,CAAC;IAED;;;;OAIG;IACH,0BAA0B;QACxB,OAAO,KAAK,CAAC,IAAI,CAAC,uBAAA,IAAI,yCAAU,CAAC,MAAM,EAAE,CAAC,CAAC,CAAC,2BAA2B;IACzE,CAAC;CACF;;AAED;;;;;GAKG;AACH,MAAM,UAAU,2BAA2B,CACzC,aAA8B;IAE9B,OAAO,GAAG,iBAAiB,CAAC,OAAO,IAAI,aAAa,EAAE,CAAC;AACzD,CAAC","sourcesContent":["import type {\n Bip44Account,\n MultichainAccountWalletId,\n MultichainAccountWallet as MultichainAccountWalletDefinition,\n} from '@metamask/account-api';\nimport type { AccountGroupId } from '@metamask/account-api';\nimport type { AccountProvider } from '@metamask/account-api';\nimport {\n getGroupIndexFromMultichainAccountId as getGroupIndexFromMultichainAccountGroupId,\n isMultichainAccountGroupId,\n} from '@metamask/account-api';\nimport { toDefaultAccountGroupId } from '@metamask/account-api';\nimport { AccountWalletType } from '@metamask/account-api';\nimport {\n type EntropySourceId,\n type KeyringAccount,\n} from '@metamask/keyring-api';\n\nimport { MultichainAccountGroup } from './MultichainAccountGroup';\n\n/**\n * A multichain account wallet that holds multiple multichain accounts (one multichain account per\n * group index).\n */\nexport class MultichainAccountWallet<\n Account extends Bip44Account<KeyringAccount>,\n> implements MultichainAccountWalletDefinition<Account>\n{\n readonly #id: MultichainAccountWalletId;\n\n readonly #providers: AccountProvider<Account>[];\n\n readonly #entropySource: EntropySourceId;\n\n readonly #accounts: Map<number, MultichainAccountGroup<Account>>;\n\n constructor({\n providers,\n entropySource,\n }: {\n providers: AccountProvider<Account>[];\n entropySource: EntropySourceId;\n }) {\n this.#id = toMultichainAccountWalletId(entropySource);\n this.#providers = providers;\n this.#entropySource = entropySource;\n this.#accounts = new Map();\n\n // Initial synchronization.\n this.sync();\n }\n\n /**\n * Force wallet synchronization.\n *\n * This can be used if account providers got new accounts that the wallet\n * doesn't know about.\n */\n sync(): void {\n for (const provider of this.#providers) {\n for (const account of provider.getAccounts()) {\n const { entropy } = account.options;\n\n // Filter for this wallet only.\n if (entropy.id !== this.entropySource) {\n continue;\n }\n\n // This multichain account might exists already.\n let multichainAccount = this.#accounts.get(entropy.groupIndex);\n if (!multichainAccount) {\n multichainAccount = new MultichainAccountGroup<Account>({\n groupIndex: entropy.groupIndex,\n wallet: this,\n providers: this.#providers,\n });\n\n this.#accounts.set(entropy.groupIndex, multichainAccount);\n }\n }\n }\n\n // Now force-sync all remaining multichain accounts.\n for (const [groupIndex, multichainAccount] of this.#accounts.entries()) {\n multichainAccount.sync();\n\n // Clean up old multichain accounts.\n if (!multichainAccount.hasAccounts()) {\n this.#accounts.delete(groupIndex);\n }\n }\n }\n\n /**\n * Gets the multichain account wallet ID.\n *\n * @returns The multichain account wallet ID.\n */\n get id(): MultichainAccountWalletId {\n return this.#id;\n }\n\n /**\n * Gets the multichain account wallet type, which is always {@link AccountWalletType.Entropy}.\n *\n * @returns The multichain account wallet type.\n */\n get type(): AccountWalletType.Entropy {\n return AccountWalletType.Entropy;\n }\n\n /**\n * Gets the multichain account wallet entropy source.\n *\n * @returns The multichain account wallet entropy source.\n */\n get entropySource(): EntropySourceId {\n return this.#entropySource;\n }\n\n /**\n * Gets multichain account for a given ID.\n * The default group ID will default to the multichain account with index 0.\n *\n * @param id - Account group ID.\n * @returns Account group.\n */\n getAccountGroup(\n id: AccountGroupId,\n ): MultichainAccountGroup<Account> | undefined {\n // We consider the \"default case\" to be mapped to index 0.\n if (id === toDefaultAccountGroupId(this.id)) {\n return this.#accounts.get(0);\n }\n\n // If it is not a valid ID, we cannot extract the group index\n // from it, so we fail fast.\n if (!isMultichainAccountGroupId(id)) {\n return undefined;\n }\n\n const groupIndex = getGroupIndexFromMultichainAccountGroupId(id);\n return this.#accounts.get(groupIndex);\n }\n\n /**\n * Gets all multichain accounts. Similar to {@link MultichainAccountWallet.getMultichainAccountGroups}.\n *\n * @returns The multichain accounts.\n */\n getAccountGroups(): MultichainAccountGroup<Account>[] {\n return this.getMultichainAccountGroups();\n }\n\n /**\n * Gets multichain account group for a given index.\n *\n * @param groupIndex - Multichain account index.\n * @returns The multichain account associated with the given index.\n */\n getMultichainAccountGroup(\n groupIndex: number,\n ): MultichainAccountGroup<Account> | undefined {\n return this.#accounts.get(groupIndex);\n }\n\n /**\n * Gets all multichain account groups.\n *\n * @returns The multichain accounts.\n */\n getMultichainAccountGroups(): MultichainAccountGroup<Account>[] {\n return Array.from(this.#accounts.values()); // TODO: Prevent copy here.\n }\n}\n\n/**\n * Gets the multichain account wallet ID from its entropy source.\n *\n * @param entropySource - Entropy source ID of that wallet.\n * @returns The multichain account wallet ID.\n */\nexport function toMultichainAccountWalletId(\n entropySource: EntropySourceId,\n): MultichainAccountWalletId {\n return `${AccountWalletType.Entropy}:${entropySource}`;\n}\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MultichainAccountService =
|
|
4
|
-
var MultichainAccountWallet_1 = require("./MultichainAccountWallet.cjs");
|
|
5
|
-
Object.defineProperty(exports, "MultichainAccountWallet", { enumerable: true, get: function () { return MultichainAccountWallet_1.MultichainAccountWallet; } });
|
|
6
|
-
var MultichainAccountGroup_1 = require("./MultichainAccountGroup.cjs");
|
|
7
|
-
Object.defineProperty(exports, "MultichainAccountGroup", { enumerable: true, get: function () { return MultichainAccountGroup_1.MultichainAccountGroup; } });
|
|
3
|
+
exports.MultichainAccountService = void 0;
|
|
8
4
|
var MultichainAccountService_1 = require("./MultichainAccountService.cjs");
|
|
9
5
|
Object.defineProperty(exports, "MultichainAccountService", { enumerable: true, get: function () { return MultichainAccountService_1.MultichainAccountService; } });
|
|
10
6
|
//# sourceMappingURL=index.cjs.map
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AASA,
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AASA,2EAAsE;AAA7D,oIAAA,wBAAwB,OAAA","sourcesContent":["export type {\n MultichainAccountServiceActions,\n MultichainAccountServiceEvents,\n MultichainAccountServiceMessenger,\n MultichainAccountServiceGetMultichainAccountGroupAction,\n MultichainAccountServiceGetMultichainAccountWalletAction,\n MultichainAccountServiceGetMultichainAccountWalletsAction,\n MultichainAccountServiceGetMultichainAccountGroupsAction,\n} from './types';\nexport { MultichainAccountService } from './MultichainAccountService';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
export type { MultichainAccountServiceActions, MultichainAccountServiceEvents, MultichainAccountServiceMessenger, MultichainAccountServiceGetMultichainAccountGroupAction, MultichainAccountServiceGetMultichainAccountWalletAction, MultichainAccountServiceGetMultichainAccountWalletsAction, MultichainAccountServiceGetMultichainAccountGroupsAction, } from "./types.cjs";
|
|
2
|
-
export { MultichainAccountWallet } from "./MultichainAccountWallet.cjs";
|
|
3
|
-
export { MultichainAccountGroup } from "./MultichainAccountGroup.cjs";
|
|
4
2
|
export { MultichainAccountService } from "./MultichainAccountService.cjs";
|
|
5
3
|
//# sourceMappingURL=index.d.cts.map
|
package/dist/index.d.cts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,EAC9B,iCAAiC,EACjC,uDAAuD,EACvD,wDAAwD,EACxD,yDAAyD,EACzD,wDAAwD,GACzD,oBAAgB;AACjB,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.cts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,EAC9B,iCAAiC,EACjC,uDAAuD,EACvD,wDAAwD,EACxD,yDAAyD,EACzD,wDAAwD,GACzD,oBAAgB;AACjB,OAAO,EAAE,wBAAwB,EAAE,uCAAmC"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,5 +1,3 @@
|
|
|
1
1
|
export type { MultichainAccountServiceActions, MultichainAccountServiceEvents, MultichainAccountServiceMessenger, MultichainAccountServiceGetMultichainAccountGroupAction, MultichainAccountServiceGetMultichainAccountWalletAction, MultichainAccountServiceGetMultichainAccountWalletsAction, MultichainAccountServiceGetMultichainAccountGroupsAction, } from "./types.mjs";
|
|
2
|
-
export { MultichainAccountWallet } from "./MultichainAccountWallet.mjs";
|
|
3
|
-
export { MultichainAccountGroup } from "./MultichainAccountGroup.mjs";
|
|
4
2
|
export { MultichainAccountService } from "./MultichainAccountService.mjs";
|
|
5
3
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,EAC9B,iCAAiC,EACjC,uDAAuD,EACvD,wDAAwD,EACxD,yDAAyD,EACzD,wDAAwD,GACzD,oBAAgB;AACjB,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.d.mts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,YAAY,EACV,+BAA+B,EAC/B,8BAA8B,EAC9B,iCAAiC,EACjC,uDAAuD,EACvD,wDAAwD,EACxD,yDAAyD,EACzD,wDAAwD,GACzD,oBAAgB;AACjB,OAAO,EAAE,wBAAwB,EAAE,uCAAmC"}
|
package/dist/index.mjs
CHANGED
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AASA,OAAO,EAAE,wBAAwB,EAAE,uCAAmC","sourcesContent":["export type {\n MultichainAccountServiceActions,\n MultichainAccountServiceEvents,\n MultichainAccountServiceMessenger,\n MultichainAccountServiceGetMultichainAccountGroupAction,\n MultichainAccountServiceGetMultichainAccountWalletAction,\n MultichainAccountServiceGetMultichainAccountWalletsAction,\n MultichainAccountServiceGetMultichainAccountGroupsAction,\n} from './types';\nexport { MultichainAccountService } from './MultichainAccountService';\n"]}
|
|
@@ -6,20 +6,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
6
6
|
};
|
|
7
7
|
var _BaseAccountProvider_instances, _BaseAccountProvider_getAccounts;
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
|
-
exports.BaseAccountProvider =
|
|
9
|
+
exports.BaseAccountProvider = void 0;
|
|
10
10
|
const account_api_1 = require("@metamask/account-api");
|
|
11
|
-
/**
|
|
12
|
-
* Asserts a keyring account is BIP-44 compatible.
|
|
13
|
-
*
|
|
14
|
-
* @param account - Keyring account to check.
|
|
15
|
-
* @throws If the keyring account is not compatible.
|
|
16
|
-
*/
|
|
17
|
-
function assertIsBip44Account(account) {
|
|
18
|
-
if (!(0, account_api_1.isBip44Account)(account)) {
|
|
19
|
-
throw new Error('Created account is not BIP-44 compatible');
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
exports.assertIsBip44Account = assertIsBip44Account;
|
|
23
11
|
class BaseAccountProvider {
|
|
24
12
|
constructor(messenger) {
|
|
25
13
|
_BaseAccountProvider_instances.add(this);
|
|
@@ -36,13 +24,6 @@ class BaseAccountProvider {
|
|
|
36
24
|
}
|
|
37
25
|
return found;
|
|
38
26
|
}
|
|
39
|
-
async withKeyring(selector, operation) {
|
|
40
|
-
const result = await this.messenger.call('KeyringController:withKeyring', selector, ({ keyring, metadata }) => operation({
|
|
41
|
-
keyring: keyring,
|
|
42
|
-
metadata,
|
|
43
|
-
}));
|
|
44
|
-
return result;
|
|
45
|
-
}
|
|
46
27
|
}
|
|
47
28
|
exports.BaseAccountProvider = BaseAccountProvider;
|
|
48
29
|
_BaseAccountProvider_instances = new WeakSet(), _BaseAccountProvider_getAccounts = function _BaseAccountProvider_getAccounts(filter = () => true) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"BaseAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/BaseAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uDAI+B;
|
|
1
|
+
{"version":3,"file":"BaseAccountProvider.cjs","sourceRoot":"","sources":["../../src/providers/BaseAccountProvider.ts"],"names":[],"mappings":";;;;;;;;;AAAA,uDAI+B;AAK/B,MAAsB,mBAAmB;IAKvC,YAAY,SAA4C;;QACtD,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAyBD,WAAW;QACT,OAAO,uBAAA,IAAI,wEAAa,MAAjB,IAAI,CAAe,CAAC;IAC7B,CAAC;IAED,UAAU,CAAC,EAAyB;QAClC,wDAAwD;QACxD,MAAM,CAAC,KAAK,CAAC,GAAG,uBAAA,IAAI,wEAAa,MAAjB,IAAI,EAAc,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;QAElE,IAAI,CAAC,KAAK,EAAE;YACV,MAAM,IAAI,KAAK,CAAC,2BAA2B,EAAE,EAAE,CAAC,CAAC;SAClD;QAED,OAAO,KAAK,CAAC;IACf,CAAC;CAGF;AAhDD,kDAgDC;6HAtCG,SAAgD,GAAG,EAAE,CAAC,IAAI;IAE1D,MAAM,QAAQ,GAAoC,EAAE,CAAC;IAErD,KAAK,MAAM,OAAO,IAAI,IAAI,CAAC,SAAS,CAAC,IAAI;IACvC,2EAA2E;IAC3E,yEAAyE;IACzE,kDAAkD;IAClD,2CAA2C,CAC5C,EAAE;QACD,IACE,IAAA,4BAAc,EAAC,OAAO,CAAC;YACvB,IAAI,CAAC,mBAAmB,CAAC,OAAO,CAAC;YACjC,MAAM,CAAC,OAAO,CAAC,EACf;YACA,QAAQ,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC;SACxB;KACF;IAED,OAAO,QAAQ,CAAC;AAClB,CAAC","sourcesContent":["import {\n isBip44Account,\n type AccountProvider,\n type Bip44Account,\n} from '@metamask/account-api';\nimport type { InternalAccount } from '@metamask/keyring-internal-api';\n\nimport type { MultichainAccountServiceMessenger } from '../types';\n\nexport abstract class BaseAccountProvider\n implements AccountProvider<Bip44Account<InternalAccount>>\n{\n protected readonly messenger: MultichainAccountServiceMessenger;\n\n constructor(messenger: MultichainAccountServiceMessenger) {\n this.messenger = messenger;\n }\n\n #getAccounts(\n filter: (account: InternalAccount) => boolean = () => true,\n ): Bip44Account<InternalAccount>[] {\n const accounts: Bip44Account<InternalAccount>[] = [];\n\n for (const account of this.messenger.call(\n // NOTE: Even though the name is misleading, this only fetches all internal\n // accounts, including EVM and non-EVM. We might wanna change this action\n // name once we fully support multichain accounts.\n 'AccountsController:listMultichainAccounts',\n )) {\n if (\n isBip44Account(account) &&\n this.isAccountCompatible(account) &&\n filter(account)\n ) {\n accounts.push(account);\n }\n }\n\n return accounts;\n }\n\n getAccounts(): Bip44Account<InternalAccount>[] {\n return this.#getAccounts();\n }\n\n getAccount(id: InternalAccount['id']): Bip44Account<InternalAccount> {\n // TODO: Maybe just use a proper find for faster lookup?\n const [found] = this.#getAccounts((account) => account.id === id);\n\n if (!found) {\n throw new Error(`Unable to find account: ${id}`);\n }\n\n return found;\n }\n\n abstract isAccountCompatible(account: Bip44Account<InternalAccount>): boolean;\n}\n"]}
|