@metamask-previews/multichain-account-service 0.3.0-preview-75996e28 → 0.3.0-preview-0fca3de
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 -1
- 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 +16 -1
- package/dist/MultichainAccountService.cjs.map +1 -1
- package/dist/MultichainAccountService.d.cts +16 -2
- package/dist/MultichainAccountService.d.cts.map +1 -1
- package/dist/MultichainAccountService.d.mts +16 -2
- package/dist/MultichainAccountService.d.mts.map +1 -1
- package/dist/MultichainAccountService.mjs +16 -1
- package/dist/MultichainAccountService.mjs.map +1 -1
- package/dist/MultichainAccountWallet.cjs +115 -13
- package/dist/MultichainAccountWallet.cjs.map +1 -1
- package/dist/MultichainAccountWallet.d.cts +22 -1
- package/dist/MultichainAccountWallet.d.cts.map +1 -1
- package/dist/MultichainAccountWallet.d.mts +22 -1
- package/dist/MultichainAccountWallet.d.mts.map +1 -1
- package/dist/MultichainAccountWallet.mjs +115 -13
- package/dist/MultichainAccountWallet.mjs.map +1 -1
- package/dist/index.cjs +5 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -0
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1 -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 +21 -1
- package/dist/providers/BaseAccountProvider.d.cts.map +1 -1
- package/dist/providers/BaseAccountProvider.d.mts +21 -1
- 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/SnapAccountProvider.cjs +27 -0
- package/dist/providers/SnapAccountProvider.cjs.map +1 -0
- package/dist/providers/SnapAccountProvider.d.cts +22 -0
- package/dist/providers/SnapAccountProvider.d.cts.map +1 -0
- package/dist/providers/SnapAccountProvider.d.mts +22 -0
- package/dist/providers/SnapAccountProvider.d.mts.map +1 -0
- package/dist/providers/SnapAccountProvider.mjs +23 -0
- package/dist/providers/SnapAccountProvider.mjs.map +1 -0
- package/dist/providers/SolAccountProvider.cjs +31 -1
- package/dist/providers/SolAccountProvider.cjs.map +1 -1
- package/dist/providers/SolAccountProvider.d.cts +14 -3
- package/dist/providers/SolAccountProvider.d.cts.map +1 -1
- package/dist/providers/SolAccountProvider.d.mts +14 -3
- package/dist/providers/SolAccountProvider.d.mts.map +1 -1
- package/dist/providers/SolAccountProvider.mjs +33 -3
- package/dist/providers/SolAccountProvider.mjs.map +1 -1
- package/dist/providers/index.cjs +22 -0
- package/dist/providers/index.cjs.map +1 -0
- package/dist/providers/index.d.cts +5 -0
- package/dist/providers/index.d.cts.map +1 -0
- package/dist/providers/index.d.mts +5 -0
- package/dist/providers/index.d.mts.map +1 -0
- package/dist/providers/index.mjs +6 -0
- package/dist/providers/index.mjs.map +1 -0
- package/dist/tests/accounts.cjs +9 -0
- package/dist/tests/accounts.cjs.map +1 -1
- package/dist/tests/accounts.d.cts +27 -55
- package/dist/tests/accounts.d.cts.map +1 -1
- package/dist/tests/accounts.d.mts +27 -55
- package/dist/tests/accounts.d.mts.map +1 -1
- package/dist/tests/accounts.mjs +9 -0
- package/dist/tests/accounts.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +5 -1
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +5 -1
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +6 -4
|
@@ -10,7 +10,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
10
10
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
11
11
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
12
12
|
};
|
|
13
|
-
var _MultichainAccountWallet_id, _MultichainAccountWallet_providers, _MultichainAccountWallet_entropySource,
|
|
13
|
+
var _MultichainAccountWallet_id, _MultichainAccountWallet_providers, _MultichainAccountWallet_entropySource, _MultichainAccountWallet_accountGroups;
|
|
14
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
15
|
exports.MultichainAccountWallet = void 0;
|
|
16
16
|
const account_api_1 = require("@metamask/account-api");
|
|
@@ -26,11 +26,11 @@ class MultichainAccountWallet {
|
|
|
26
26
|
_MultichainAccountWallet_id.set(this, void 0);
|
|
27
27
|
_MultichainAccountWallet_providers.set(this, void 0);
|
|
28
28
|
_MultichainAccountWallet_entropySource.set(this, void 0);
|
|
29
|
-
|
|
29
|
+
_MultichainAccountWallet_accountGroups.set(this, void 0);
|
|
30
30
|
__classPrivateFieldSet(this, _MultichainAccountWallet_id, (0, account_api_1.toMultichainAccountWalletId)(entropySource), "f");
|
|
31
31
|
__classPrivateFieldSet(this, _MultichainAccountWallet_providers, providers, "f");
|
|
32
32
|
__classPrivateFieldSet(this, _MultichainAccountWallet_entropySource, entropySource, "f");
|
|
33
|
-
__classPrivateFieldSet(this,
|
|
33
|
+
__classPrivateFieldSet(this, _MultichainAccountWallet_accountGroups, new Map(), "f");
|
|
34
34
|
// Initial synchronization.
|
|
35
35
|
this.sync();
|
|
36
36
|
}
|
|
@@ -49,23 +49,32 @@ class MultichainAccountWallet {
|
|
|
49
49
|
continue;
|
|
50
50
|
}
|
|
51
51
|
// This multichain account might exists already.
|
|
52
|
-
let multichainAccount = __classPrivateFieldGet(this,
|
|
52
|
+
let multichainAccount = __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(entropy.groupIndex);
|
|
53
53
|
if (!multichainAccount) {
|
|
54
54
|
multichainAccount = new MultichainAccountGroup_1.MultichainAccountGroup({
|
|
55
55
|
groupIndex: entropy.groupIndex,
|
|
56
56
|
wallet: this,
|
|
57
57
|
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
58
58
|
});
|
|
59
|
-
|
|
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
|
+
__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").set(entropy.groupIndex, multichainAccount);
|
|
60
69
|
}
|
|
61
70
|
}
|
|
62
71
|
}
|
|
63
72
|
// Now force-sync all remaining multichain accounts.
|
|
64
|
-
for (const [groupIndex, multichainAccount] of __classPrivateFieldGet(this,
|
|
73
|
+
for (const [groupIndex, multichainAccount,] of __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").entries()) {
|
|
65
74
|
multichainAccount.sync();
|
|
66
75
|
// Clean up old multichain accounts.
|
|
67
76
|
if (!multichainAccount.hasAccounts()) {
|
|
68
|
-
__classPrivateFieldGet(this,
|
|
77
|
+
__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").delete(groupIndex);
|
|
69
78
|
}
|
|
70
79
|
}
|
|
71
80
|
}
|
|
@@ -103,15 +112,15 @@ class MultichainAccountWallet {
|
|
|
103
112
|
getAccountGroup(id) {
|
|
104
113
|
// We consider the "default case" to be mapped to index 0.
|
|
105
114
|
if (id === (0, account_api_2.toDefaultAccountGroupId)(this.id)) {
|
|
106
|
-
return __classPrivateFieldGet(this,
|
|
115
|
+
return __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(0);
|
|
107
116
|
}
|
|
108
117
|
// If it is not a valid ID, we cannot extract the group index
|
|
109
118
|
// from it, so we fail fast.
|
|
110
119
|
if (!(0, account_api_1.isMultichainAccountGroupId)(id)) {
|
|
111
120
|
return undefined;
|
|
112
121
|
}
|
|
113
|
-
const groupIndex = (0, account_api_1.
|
|
114
|
-
return __classPrivateFieldGet(this,
|
|
122
|
+
const groupIndex = (0, account_api_1.getGroupIndexFromMultichainAccountGroupId)(id);
|
|
123
|
+
return __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(groupIndex);
|
|
115
124
|
}
|
|
116
125
|
/**
|
|
117
126
|
* Gets all multichain accounts. Similar to {@link MultichainAccountWallet.getMultichainAccountGroups}.
|
|
@@ -128,7 +137,7 @@ class MultichainAccountWallet {
|
|
|
128
137
|
* @returns The multichain account associated with the given index.
|
|
129
138
|
*/
|
|
130
139
|
getMultichainAccountGroup(groupIndex) {
|
|
131
|
-
return __classPrivateFieldGet(this,
|
|
140
|
+
return __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(groupIndex);
|
|
132
141
|
}
|
|
133
142
|
/**
|
|
134
143
|
* Gets all multichain account groups.
|
|
@@ -136,9 +145,102 @@ class MultichainAccountWallet {
|
|
|
136
145
|
* @returns The multichain accounts.
|
|
137
146
|
*/
|
|
138
147
|
getMultichainAccountGroups() {
|
|
139
|
-
return Array.from(__classPrivateFieldGet(this,
|
|
148
|
+
return Array.from(__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").values()); // TODO: Prevent copy here.
|
|
149
|
+
}
|
|
150
|
+
/**
|
|
151
|
+
* Gets next group index for this wallet.
|
|
152
|
+
*
|
|
153
|
+
* @returns The next group index of this wallet.
|
|
154
|
+
*/
|
|
155
|
+
getNextGroupIndex() {
|
|
156
|
+
// We do not check for gaps.
|
|
157
|
+
return (Math.max(-1, // So it will default to 0 if no groups.
|
|
158
|
+
...__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").keys()) + 1);
|
|
159
|
+
}
|
|
160
|
+
/**
|
|
161
|
+
* Creates a multichain account group for a given group index.
|
|
162
|
+
*
|
|
163
|
+
* @param groupIndex - The group index to use.
|
|
164
|
+
* @throws If any of the account providers fails to create their accounts.
|
|
165
|
+
* @returns The multichain account group for this group index.
|
|
166
|
+
*/
|
|
167
|
+
async createMultichainAccountGroup(groupIndex) {
|
|
168
|
+
const nextGroupIndex = this.getNextGroupIndex();
|
|
169
|
+
if (groupIndex > nextGroupIndex) {
|
|
170
|
+
throw new Error(`You cannot use a group index that is higher than the next available one: expected <=${nextGroupIndex}, got ${groupIndex}`);
|
|
171
|
+
}
|
|
172
|
+
let group = this.getMultichainAccountGroup(groupIndex);
|
|
173
|
+
if (group) {
|
|
174
|
+
// If the group already exists, we just `sync` it and returns the same
|
|
175
|
+
// reference.
|
|
176
|
+
group.sync();
|
|
177
|
+
return group;
|
|
178
|
+
}
|
|
179
|
+
const results = await Promise.allSettled(__classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f").map((provider) => provider.createAccounts({
|
|
180
|
+
entropySource: __classPrivateFieldGet(this, _MultichainAccountWallet_entropySource, "f"),
|
|
181
|
+
groupIndex,
|
|
182
|
+
})));
|
|
183
|
+
// --------------------------------------------------------------------------------
|
|
184
|
+
// READ THIS CAREFULLY:
|
|
185
|
+
//
|
|
186
|
+
// Since we're not "fully supporting multichain" for now, we still rely on single
|
|
187
|
+
// :accountCreated events to sync multichain account groups and wallets. Which means
|
|
188
|
+
// that even if of the provider fails, some accounts will still be created on some
|
|
189
|
+
// other providers and will become "available" on the `AccountsController`, like:
|
|
190
|
+
//
|
|
191
|
+
// 1. Creating a multichain account group for index 1
|
|
192
|
+
// 2. EvmAccountProvider.createAccounts returns the EVM account for index 1
|
|
193
|
+
// * AccountsController WILL fire :accountCreated for this account
|
|
194
|
+
// * This account WILL BE "available" on the AccountsController state
|
|
195
|
+
// 3. SolAccountProvider.createAccounts fails to create a Solana account for index 1
|
|
196
|
+
// * AccountsController WON't fire :accountCreated for this account
|
|
197
|
+
// * This account WON'T be "available" on the Account
|
|
198
|
+
// 4. MultichainAccountService will receive a :accountCreated for the EVM account from
|
|
199
|
+
// step 2 and will create a new multichain account group for index 1, but it won't
|
|
200
|
+
// receive any event for the Solana account of this group. Thus, this group won't be
|
|
201
|
+
// "aligned" (missing "blockchain account" on this group).
|
|
202
|
+
//
|
|
203
|
+
// --------------------------------------------------------------------------------
|
|
204
|
+
// If any of the provider failed to create their accounts, then we consider the
|
|
205
|
+
// multichain account group to have failed too.
|
|
206
|
+
if (results.some((result) => result.status === 'rejected')) {
|
|
207
|
+
// NOTE: Some accounts might still have been created on other account providers. We
|
|
208
|
+
// don't rollback them.
|
|
209
|
+
const error = `Unable to create multichain account group for index: ${groupIndex}`;
|
|
210
|
+
let warn = `${error}:`;
|
|
211
|
+
for (const result of results) {
|
|
212
|
+
if (result.status === 'rejected') {
|
|
213
|
+
warn += `\n- ${result.reason}`;
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
console.warn(warn);
|
|
217
|
+
throw new Error(error);
|
|
218
|
+
}
|
|
219
|
+
// Because of the :accountAdded automatic sync, we might already have created the
|
|
220
|
+
// group, so we first try to get it.
|
|
221
|
+
group = this.getMultichainAccountGroup(groupIndex);
|
|
222
|
+
if (!group) {
|
|
223
|
+
// If for some reason it's still not created, we're creating it explicitly now:
|
|
224
|
+
group = new MultichainAccountGroup_1.MultichainAccountGroup({
|
|
225
|
+
wallet: this,
|
|
226
|
+
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
227
|
+
groupIndex,
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
// Register the account to our internal map.
|
|
231
|
+
__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").set(groupIndex, group); // `group` cannot be undefined here.
|
|
232
|
+
return group;
|
|
233
|
+
}
|
|
234
|
+
/**
|
|
235
|
+
* Creates the next multichain account group.
|
|
236
|
+
*
|
|
237
|
+
* @throws If any of the account providers fails to create their accounts.
|
|
238
|
+
* @returns The multichain account group for the next group index available.
|
|
239
|
+
*/
|
|
240
|
+
async createNextMultichainAccountGroup() {
|
|
241
|
+
return this.createMultichainAccountGroup(this.getNextGroupIndex());
|
|
140
242
|
}
|
|
141
243
|
}
|
|
142
244
|
exports.MultichainAccountWallet = MultichainAccountWallet;
|
|
143
|
-
_MultichainAccountWallet_id = new WeakMap(), _MultichainAccountWallet_providers = new WeakMap(), _MultichainAccountWallet_entropySource = new WeakMap(),
|
|
245
|
+
_MultichainAccountWallet_id = new WeakMap(), _MultichainAccountWallet_providers = new WeakMap(), _MultichainAccountWallet_entropySource = new WeakMap(), _MultichainAccountWallet_accountGroups = new WeakMap();
|
|
144
246
|
//# sourceMappingURL=MultichainAccountWallet.cjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountWallet.cjs","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAOA,uDAI+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,oDA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;gBACpC,uBAAA,IAAI,yCAAU,CAAC,MAAM,CAAC,UAAU,CAAC,CAAC;aACnC;SACF;IACH,CAAC;IAED;;;;OAIG;IACH,IAAI,EAAE;QACJ,OAAO,uBAAA,IAAI,mCAAI,CAAC;IAClB,CAAC;IAED;;;;OAIG;IACH,IAAI,IAAI;QACN,OAAO,+BAAiB,CAAC,OAAO,CAAC;IACnC,CAAC;IAED;;;;OAIG;IACH,IAAI,aAAa;QACf,OAAO,uBAAA,IAAI,8CAAe,CAAC;IAC7B,CAAC;IAED;;;;;;OAMG;IACH,eAAe,CACb,EAAkB;QAElB,0DAA0D;QAC1D,IAAI,EAAE,KAAK,IAAA,qCAAuB,EAAC,IAAI,CAAC,EAAE,CAAC,EAAE;YAC3C,OAAO,uBAAA,IAAI,yCAAU,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;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","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 toMultichainAccountWalletId,\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"]}
|
|
1
|
+
{"version":3,"file":"MultichainAccountWallet.cj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4B,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC,CAAC;IACrE,CAAC;CACF;AA3RD,0DA2RC","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 #accountGroups: 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.#accountGroups = 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.#accountGroups.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.#accountGroups.set(entropy.groupIndex, multichainAccount);\n }\n }\n }\n\n // Now force-sync all remaining multichain accounts.\n for (const [\n groupIndex,\n multichainAccount,\n ] of this.#accountGroups.entries()) {\n multichainAccount.sync();\n\n // Clean up old multichain accounts.\n if (!multichainAccount.hasAccounts()) {\n this.#accountGroups.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.#accountGroups.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.#accountGroups.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.#accountGroups.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.#accountGroups.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 // We do not check for gaps.\n return (\n Math.max(\n -1, // So it will default to 0 if no groups.\n ...this.#accountGroups.keys(),\n ) + 1\n );\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 // --------------------------------------------------------------------------------\n // READ THIS CAREFULLY:\n //\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 accounts will still be created on some\n // other providers and will become \"available\" on the `AccountsController`, like:\n //\n // 1. Creating a multichain account group for index 1\n // 2. EvmAccountProvider.createAccounts returns the EVM account for index 1\n // * AccountsController WILL fire :accountCreated for this account\n // * This account WILL BE \"available\" on the AccountsController state\n // 3. SolAccountProvider.createAccounts fails to create a Solana account for index 1\n // * AccountsController WON't fire :accountCreated for this account\n // * This account WON'T be \"available\" on the Account\n // 4. MultichainAccountService will receive a :accountCreated for the EVM account from\n // step 2 and will create a new multichain account group for index 1, but it won't\n // receive any event for the Solana account of this group. Thus, this group won't be\n // \"aligned\" (missing \"blockchain account\" on this group).\n //\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 if (results.some((result) => result.status === 'rejected')) {\n // NOTE: Some accounts might still have been created on other account providers. We\n // don't rollback them.\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.#accountGroups.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,7 +1,7 @@
|
|
|
1
|
+
import { AccountWalletType } from "@metamask/account-api";
|
|
1
2
|
import type { Bip44Account, MultichainAccountWalletId, MultichainAccountWallet as MultichainAccountWalletDefinition } from "@metamask/account-api";
|
|
2
3
|
import type { AccountGroupId } from "@metamask/account-api";
|
|
3
4
|
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,5 +66,26 @@ 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>>;
|
|
69
90
|
}
|
|
70
91
|
//# 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":"AAMA,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EACV,YAAY,EACZ,yBAAyB,EACzB,uBAAuB,IAAI,iCAAiC,EAC7D,8BAA8B;A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
@@ -1,7 +1,7 @@
|
|
|
1
|
+
import { AccountWalletType } from "@metamask/account-api";
|
|
1
2
|
import type { Bip44Account, MultichainAccountWalletId, MultichainAccountWallet as MultichainAccountWalletDefinition } from "@metamask/account-api";
|
|
2
3
|
import type { AccountGroupId } from "@metamask/account-api";
|
|
3
4
|
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,5 +66,26 @@ 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>>;
|
|
69
90
|
}
|
|
70
91
|
//# 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":"AAMA,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAC1D,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;AAC7D,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;IAgDZ;;;;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;IAI/D;;;;OAIG;IACH,iBAAiB,IAAI,MAAM;IAU3B;;;;;;OAMG;IACG,4BAA4B,CAChC,UAAU,EAAE,MAAM,GACjB,OAAO,CAAC,sBAAsB,CAAC,OAAO,CAAC,CAAC;IAoF3C;;;;;OAKG;IACG,gCAAgC,IAAI,OAAO,CAC/C,sBAAsB,CAAC,OAAO,CAAC,CAChC;CAGF"}
|
|
@@ -9,8 +9,8 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
9
9
|
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
10
10
|
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
11
11
|
};
|
|
12
|
-
var _MultichainAccountWallet_id, _MultichainAccountWallet_providers, _MultichainAccountWallet_entropySource,
|
|
13
|
-
import {
|
|
12
|
+
var _MultichainAccountWallet_id, _MultichainAccountWallet_providers, _MultichainAccountWallet_entropySource, _MultichainAccountWallet_accountGroups;
|
|
13
|
+
import { getGroupIndexFromMultichainAccountGroupId, isMultichainAccountGroupId, toMultichainAccountWalletId } 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";
|
|
@@ -23,11 +23,11 @@ export class MultichainAccountWallet {
|
|
|
23
23
|
_MultichainAccountWallet_id.set(this, void 0);
|
|
24
24
|
_MultichainAccountWallet_providers.set(this, void 0);
|
|
25
25
|
_MultichainAccountWallet_entropySource.set(this, void 0);
|
|
26
|
-
|
|
26
|
+
_MultichainAccountWallet_accountGroups.set(this, void 0);
|
|
27
27
|
__classPrivateFieldSet(this, _MultichainAccountWallet_id, toMultichainAccountWalletId(entropySource), "f");
|
|
28
28
|
__classPrivateFieldSet(this, _MultichainAccountWallet_providers, providers, "f");
|
|
29
29
|
__classPrivateFieldSet(this, _MultichainAccountWallet_entropySource, entropySource, "f");
|
|
30
|
-
__classPrivateFieldSet(this,
|
|
30
|
+
__classPrivateFieldSet(this, _MultichainAccountWallet_accountGroups, new Map(), "f");
|
|
31
31
|
// Initial synchronization.
|
|
32
32
|
this.sync();
|
|
33
33
|
}
|
|
@@ -46,23 +46,32 @@ export class MultichainAccountWallet {
|
|
|
46
46
|
continue;
|
|
47
47
|
}
|
|
48
48
|
// This multichain account might exists already.
|
|
49
|
-
let multichainAccount = __classPrivateFieldGet(this,
|
|
49
|
+
let multichainAccount = __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(entropy.groupIndex);
|
|
50
50
|
if (!multichainAccount) {
|
|
51
51
|
multichainAccount = new MultichainAccountGroup({
|
|
52
52
|
groupIndex: entropy.groupIndex,
|
|
53
53
|
wallet: this,
|
|
54
54
|
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
55
55
|
});
|
|
56
|
-
|
|
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
|
+
__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").set(entropy.groupIndex, multichainAccount);
|
|
57
66
|
}
|
|
58
67
|
}
|
|
59
68
|
}
|
|
60
69
|
// Now force-sync all remaining multichain accounts.
|
|
61
|
-
for (const [groupIndex, multichainAccount] of __classPrivateFieldGet(this,
|
|
70
|
+
for (const [groupIndex, multichainAccount,] of __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").entries()) {
|
|
62
71
|
multichainAccount.sync();
|
|
63
72
|
// Clean up old multichain accounts.
|
|
64
73
|
if (!multichainAccount.hasAccounts()) {
|
|
65
|
-
__classPrivateFieldGet(this,
|
|
74
|
+
__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").delete(groupIndex);
|
|
66
75
|
}
|
|
67
76
|
}
|
|
68
77
|
}
|
|
@@ -100,7 +109,7 @@ export class MultichainAccountWallet {
|
|
|
100
109
|
getAccountGroup(id) {
|
|
101
110
|
// We consider the "default case" to be mapped to index 0.
|
|
102
111
|
if (id === toDefaultAccountGroupId(this.id)) {
|
|
103
|
-
return __classPrivateFieldGet(this,
|
|
112
|
+
return __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(0);
|
|
104
113
|
}
|
|
105
114
|
// If it is not a valid ID, we cannot extract the group index
|
|
106
115
|
// from it, so we fail fast.
|
|
@@ -108,7 +117,7 @@ export class MultichainAccountWallet {
|
|
|
108
117
|
return undefined;
|
|
109
118
|
}
|
|
110
119
|
const groupIndex = getGroupIndexFromMultichainAccountGroupId(id);
|
|
111
|
-
return __classPrivateFieldGet(this,
|
|
120
|
+
return __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(groupIndex);
|
|
112
121
|
}
|
|
113
122
|
/**
|
|
114
123
|
* Gets all multichain accounts. Similar to {@link MultichainAccountWallet.getMultichainAccountGroups}.
|
|
@@ -125,7 +134,7 @@ export class MultichainAccountWallet {
|
|
|
125
134
|
* @returns The multichain account associated with the given index.
|
|
126
135
|
*/
|
|
127
136
|
getMultichainAccountGroup(groupIndex) {
|
|
128
|
-
return __classPrivateFieldGet(this,
|
|
137
|
+
return __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(groupIndex);
|
|
129
138
|
}
|
|
130
139
|
/**
|
|
131
140
|
* Gets all multichain account groups.
|
|
@@ -133,8 +142,101 @@ export class MultichainAccountWallet {
|
|
|
133
142
|
* @returns The multichain accounts.
|
|
134
143
|
*/
|
|
135
144
|
getMultichainAccountGroups() {
|
|
136
|
-
return Array.from(__classPrivateFieldGet(this,
|
|
145
|
+
return Array.from(__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").values()); // TODO: Prevent copy here.
|
|
146
|
+
}
|
|
147
|
+
/**
|
|
148
|
+
* Gets next group index for this wallet.
|
|
149
|
+
*
|
|
150
|
+
* @returns The next group index of this wallet.
|
|
151
|
+
*/
|
|
152
|
+
getNextGroupIndex() {
|
|
153
|
+
// We do not check for gaps.
|
|
154
|
+
return (Math.max(-1, // So it will default to 0 if no groups.
|
|
155
|
+
...__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").keys()) + 1);
|
|
156
|
+
}
|
|
157
|
+
/**
|
|
158
|
+
* Creates a multichain account group for a given group index.
|
|
159
|
+
*
|
|
160
|
+
* @param groupIndex - The group index to use.
|
|
161
|
+
* @throws If any of the account providers fails to create their accounts.
|
|
162
|
+
* @returns The multichain account group for this group index.
|
|
163
|
+
*/
|
|
164
|
+
async createMultichainAccountGroup(groupIndex) {
|
|
165
|
+
const nextGroupIndex = this.getNextGroupIndex();
|
|
166
|
+
if (groupIndex > nextGroupIndex) {
|
|
167
|
+
throw new Error(`You cannot use a group index that is higher than the next available one: expected <=${nextGroupIndex}, got ${groupIndex}`);
|
|
168
|
+
}
|
|
169
|
+
let group = this.getMultichainAccountGroup(groupIndex);
|
|
170
|
+
if (group) {
|
|
171
|
+
// If the group already exists, we just `sync` it and returns the same
|
|
172
|
+
// reference.
|
|
173
|
+
group.sync();
|
|
174
|
+
return group;
|
|
175
|
+
}
|
|
176
|
+
const results = await Promise.allSettled(__classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f").map((provider) => provider.createAccounts({
|
|
177
|
+
entropySource: __classPrivateFieldGet(this, _MultichainAccountWallet_entropySource, "f"),
|
|
178
|
+
groupIndex,
|
|
179
|
+
})));
|
|
180
|
+
// --------------------------------------------------------------------------------
|
|
181
|
+
// READ THIS CAREFULLY:
|
|
182
|
+
//
|
|
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 accounts will still be created on some
|
|
186
|
+
// other providers and will become "available" on the `AccountsController`, like:
|
|
187
|
+
//
|
|
188
|
+
// 1. Creating a multichain account group for index 1
|
|
189
|
+
// 2. EvmAccountProvider.createAccounts returns the EVM account for index 1
|
|
190
|
+
// * AccountsController WILL fire :accountCreated for this account
|
|
191
|
+
// * This account WILL BE "available" on the AccountsController state
|
|
192
|
+
// 3. SolAccountProvider.createAccounts fails to create a Solana account for index 1
|
|
193
|
+
// * AccountsController WON't fire :accountCreated for this account
|
|
194
|
+
// * This account WON'T be "available" on the Account
|
|
195
|
+
// 4. MultichainAccountService will receive a :accountCreated for the EVM account from
|
|
196
|
+
// step 2 and will create a new multichain account group for index 1, but it won't
|
|
197
|
+
// receive any event for the Solana account of this group. Thus, this group won't be
|
|
198
|
+
// "aligned" (missing "blockchain account" on this group).
|
|
199
|
+
//
|
|
200
|
+
// --------------------------------------------------------------------------------
|
|
201
|
+
// If any of the provider failed to create their accounts, then we consider the
|
|
202
|
+
// multichain account group to have failed too.
|
|
203
|
+
if (results.some((result) => result.status === 'rejected')) {
|
|
204
|
+
// NOTE: Some accounts might still have been created on other account providers. We
|
|
205
|
+
// don't rollback them.
|
|
206
|
+
const error = `Unable to create multichain account group for index: ${groupIndex}`;
|
|
207
|
+
let warn = `${error}:`;
|
|
208
|
+
for (const result of results) {
|
|
209
|
+
if (result.status === 'rejected') {
|
|
210
|
+
warn += `\n- ${result.reason}`;
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
console.warn(warn);
|
|
214
|
+
throw new Error(error);
|
|
215
|
+
}
|
|
216
|
+
// Because of the :accountAdded automatic sync, we might already have created the
|
|
217
|
+
// group, so we first try to get it.
|
|
218
|
+
group = this.getMultichainAccountGroup(groupIndex);
|
|
219
|
+
if (!group) {
|
|
220
|
+
// If for some reason it's still not created, we're creating it explicitly now:
|
|
221
|
+
group = new MultichainAccountGroup({
|
|
222
|
+
wallet: this,
|
|
223
|
+
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
224
|
+
groupIndex,
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
// Register the account to our internal map.
|
|
228
|
+
__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").set(groupIndex, group); // `group` cannot be undefined here.
|
|
229
|
+
return group;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Creates the next multichain account group.
|
|
233
|
+
*
|
|
234
|
+
* @throws If any of the account providers fails to create their accounts.
|
|
235
|
+
* @returns The multichain account group for the next group index available.
|
|
236
|
+
*/
|
|
237
|
+
async createNextMultichainAccountGroup() {
|
|
238
|
+
return this.createMultichainAccountGroup(this.getNextGroupIndex());
|
|
137
239
|
}
|
|
138
240
|
}
|
|
139
|
-
_MultichainAccountWallet_id = new WeakMap(), _MultichainAccountWallet_providers = new WeakMap(), _MultichainAccountWallet_entropySource = new WeakMap(),
|
|
241
|
+
_MultichainAccountWallet_id = new WeakMap(), _MultichainAccountWallet_providers = new WeakMap(), _MultichainAccountWallet_entropySource = new WeakMap(), _MultichainAccountWallet_accountGroups = new WeakMap();
|
|
140
242
|
//# sourceMappingURL=MultichainAccountWallet.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"MultichainAccountWallet.mjs","sourceRoot":"","sources":["../src/MultichainAccountWallet.ts"],"names":[],"mappings":";;;;;;;;;;;;AAOA,OAAO,EACL,oCAAoC,IAAI,yCAAyC,EACjF,0BAA0B,EAC1B,2BAA2B,EAC5B,8BAA8B;AAC/B,OAAO,EAAE,uBAAuB,EAAE,8BAA8B;AAChE,OAAO,EAAE,iBAAiB,EAAE,8BAA8B;AAM1D,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAElE;;;GAGG;AACH,MAA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C;CACF","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 toMultichainAccountWalletId,\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"]}
|
|
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,8BAA8B;AAa1D,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,yDAA6D;QASpE,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,0CAAkB,IAAI,GAAG,EAAE,MAAA,CAAC;QAEhC,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,OAAO,CAAC,EAAE,KAAK,IAAI,CAAC,aAAa,EAAE;oBACrC,SAAS;iBACV;gBAED,gDAAgD;gBAChD,IAAI,iBAAiB,GAAG,uBAAA,IAAI,8CAAe,CAAC,GAAG,CAAC,OAAO,CAAC,UAAU,CAAC,CAAC;gBACpE,IAAI,CAAC,iBAAiB,EAAE;oBACtB,iBAAiB,GAAG,IAA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BAAA,IAAI,8CAAe,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,oCAAoC;QAEhF,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 #accountGroups: 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.#accountGroups = 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.#accountGroups.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.#accountGroups.set(entropy.groupIndex, multichainAccount);\n }\n }\n }\n\n // Now force-sync all remaining multichain accounts.\n for (const [\n groupIndex,\n multichainAccount,\n ] of this.#accountGroups.entries()) {\n multichainAccount.sync();\n\n // Clean up old multichain accounts.\n if (!multichainAccount.hasAccounts()) {\n this.#accountGroups.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.#accountGroups.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.#accountGroups.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.#accountGroups.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.#accountGroups.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 // We do not check for gaps.\n return (\n Math.max(\n -1, // So it will default to 0 if no groups.\n ...this.#accountGroups.keys(),\n ) + 1\n );\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 // --------------------------------------------------------------------------------\n // READ THIS CAREFULLY:\n //\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 accounts will still be created on some\n // other providers and will become \"available\" on the `AccountsController`, like:\n //\n // 1. Creating a multichain account group for index 1\n // 2. EvmAccountProvider.createAccounts returns the EVM account for index 1\n // * AccountsController WILL fire :accountCreated for this account\n // * This account WILL BE \"available\" on the AccountsController state\n // 3. SolAccountProvider.createAccounts fails to create a Solana account for index 1\n // * AccountsController WON't fire :accountCreated for this account\n // * This account WON'T be \"available\" on the Account\n // 4. MultichainAccountService will receive a :accountCreated for the EVM account from\n // step 2 and will create a new multichain account group for index 1, but it won't\n // receive any event for the Solana account of this group. Thus, this group won't be\n // \"aligned\" (missing \"blockchain account\" on this group).\n //\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 if (results.some((result) => result.status === 'rejected')) {\n // NOTE: Some accounts might still have been created on other account providers. We\n // don't rollback them.\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.#accountGroups.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"]}
|
package/dist/index.cjs
CHANGED
|
@@ -1,6 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.MultichainAccountService = exports.MultichainAccountGroup = exports.MultichainAccountWallet = void 0;
|
|
3
|
+
exports.MultichainAccountService = exports.MultichainAccountGroup = exports.MultichainAccountWallet = exports.SnapAccountProvider = exports.BaseAccountProvider = exports.assertIsBip44Account = void 0;
|
|
4
|
+
var providers_1 = require("./providers/index.cjs");
|
|
5
|
+
Object.defineProperty(exports, "assertIsBip44Account", { enumerable: true, get: function () { return providers_1.assertIsBip44Account; } });
|
|
6
|
+
Object.defineProperty(exports, "BaseAccountProvider", { enumerable: true, get: function () { return providers_1.BaseAccountProvider; } });
|
|
7
|
+
Object.defineProperty(exports, "SnapAccountProvider", { enumerable: true, get: function () { return providers_1.SnapAccountProvider; } });
|
|
4
8
|
var MultichainAccountWallet_1 = require("./MultichainAccountWallet.cjs");
|
|
5
9
|
Object.defineProperty(exports, "MultichainAccountWallet", { enumerable: true, get: function () { return MultichainAccountWallet_1.MultichainAccountWallet; } });
|
|
6
10
|
var MultichainAccountGroup_1 = require("./MultichainAccountGroup.cjs");
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AASA,yEAAoE;AAA3D,kIAAA,uBAAuB,OAAA;AAChC,uEAAkE;AAAzD,gIAAA,sBAAsB,OAAA;AAC/B,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 { MultichainAccountWallet } from './MultichainAccountWallet';\nexport { MultichainAccountGroup } from './MultichainAccountGroup';\nexport { MultichainAccountService } from './MultichainAccountService';\n"]}
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AASA,mDAIqB;AAHnB,iHAAA,oBAAoB,OAAA;AACpB,gHAAA,mBAAmB,OAAA;AACnB,gHAAA,mBAAmB,OAAA;AAErB,yEAAoE;AAA3D,kIAAA,uBAAuB,OAAA;AAChC,uEAAkE;AAAzD,gIAAA,sBAAsB,OAAA;AAC/B,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 {\n assertIsBip44Account,\n BaseAccountProvider,\n SnapAccountProvider,\n} from './providers';\nexport { MultichainAccountWallet } from './MultichainAccountWallet';\nexport { MultichainAccountGroup } from './MultichainAccountGroup';\nexport { MultichainAccountService } from './MultichainAccountService';\n"]}
|
package/dist/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
export type { MultichainAccountServiceActions, MultichainAccountServiceEvents, MultichainAccountServiceMessenger, MultichainAccountServiceGetMultichainAccountGroupAction, MultichainAccountServiceGetMultichainAccountWalletAction, MultichainAccountServiceGetMultichainAccountWalletsAction, MultichainAccountServiceGetMultichainAccountGroupsAction, } from "./types.cjs";
|
|
2
|
+
export { assertIsBip44Account, BaseAccountProvider, SnapAccountProvider, } from "./providers/index.cjs";
|
|
2
3
|
export { MultichainAccountWallet } from "./MultichainAccountWallet.cjs";
|
|
3
4
|
export { MultichainAccountGroup } from "./MultichainAccountGroup.cjs";
|
|
4
5
|
export { MultichainAccountService } from "./MultichainAccountService.cjs";
|
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,uBAAuB,EAAE,sCAAkC;AACpE,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC"}
|
|
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,EACL,oBAAoB,EACpB,mBAAmB,EACnB,mBAAmB,GACpB,8BAAoB;AACrB,OAAO,EAAE,uBAAuB,EAAE,sCAAkC;AACpE,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC"}
|