@metamask-previews/multichain-account-service 0.6.0-preview-cfa64945 → 0.6.0-preview-c76a00c1
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 +4 -3
- package/dist/MultichainAccountGroup.cjs +23 -35
- package/dist/MultichainAccountGroup.cjs.map +1 -1
- package/dist/MultichainAccountGroup.d.cts +3 -5
- package/dist/MultichainAccountGroup.d.cts.map +1 -1
- package/dist/MultichainAccountGroup.d.mts +3 -5
- package/dist/MultichainAccountGroup.d.mts.map +1 -1
- package/dist/MultichainAccountGroup.mjs +23 -35
- package/dist/MultichainAccountGroup.mjs.map +1 -1
- package/dist/MultichainAccountService.cjs +0 -2
- package/dist/MultichainAccountService.cjs.map +1 -1
- package/dist/MultichainAccountService.d.cts +3 -2
- package/dist/MultichainAccountService.d.cts.map +1 -1
- package/dist/MultichainAccountService.d.mts +3 -2
- package/dist/MultichainAccountService.d.mts.map +1 -1
- package/dist/MultichainAccountService.mjs +0 -2
- package/dist/MultichainAccountService.mjs.map +1 -1
- package/dist/MultichainAccountWallet.cjs +172 -121
- package/dist/MultichainAccountWallet.cjs.map +1 -1
- package/dist/MultichainAccountWallet.d.cts +15 -5
- package/dist/MultichainAccountWallet.d.cts.map +1 -1
- package/dist/MultichainAccountWallet.d.mts +15 -5
- package/dist/MultichainAccountWallet.d.mts.map +1 -1
- package/dist/MultichainAccountWallet.mjs +171 -120
- package/dist/MultichainAccountWallet.mjs.map +1 -1
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs.map +1 -1
- package/dist/providers/AccountProviderWrapper.cjs +3 -0
- package/dist/providers/AccountProviderWrapper.cjs.map +1 -1
- package/dist/providers/AccountProviderWrapper.d.cts +1 -0
- package/dist/providers/AccountProviderWrapper.d.cts.map +1 -1
- package/dist/providers/AccountProviderWrapper.d.mts +1 -0
- package/dist/providers/AccountProviderWrapper.d.mts.map +1 -1
- package/dist/providers/AccountProviderWrapper.mjs +3 -0
- package/dist/providers/AccountProviderWrapper.mjs.map +1 -1
- package/dist/providers/BaseBip44AccountProvider.cjs.map +1 -1
- package/dist/providers/BaseBip44AccountProvider.d.cts +5 -1
- package/dist/providers/BaseBip44AccountProvider.d.cts.map +1 -1
- package/dist/providers/BaseBip44AccountProvider.d.mts +5 -1
- package/dist/providers/BaseBip44AccountProvider.d.mts.map +1 -1
- package/dist/providers/BaseBip44AccountProvider.mjs.map +1 -1
- package/dist/providers/EvmAccountProvider.cjs +87 -15
- package/dist/providers/EvmAccountProvider.cjs.map +1 -1
- package/dist/providers/EvmAccountProvider.d.cts +21 -52
- package/dist/providers/EvmAccountProvider.d.cts.map +1 -1
- package/dist/providers/EvmAccountProvider.d.mts +21 -52
- package/dist/providers/EvmAccountProvider.d.mts.map +1 -1
- package/dist/providers/EvmAccountProvider.mjs +88 -16
- package/dist/providers/EvmAccountProvider.mjs.map +1 -1
- package/dist/providers/SolAccountProvider.cjs +62 -21
- package/dist/providers/SolAccountProvider.cjs.map +1 -1
- package/dist/providers/SolAccountProvider.d.cts +3 -1
- package/dist/providers/SolAccountProvider.d.cts.map +1 -1
- package/dist/providers/SolAccountProvider.d.mts +3 -1
- package/dist/providers/SolAccountProvider.d.mts.map +1 -1
- package/dist/providers/SolAccountProvider.mjs +61 -20
- package/dist/providers/SolAccountProvider.mjs.map +1 -1
- package/dist/tests/accounts.cjs +11 -6
- package/dist/tests/accounts.cjs.map +1 -1
- package/dist/tests/accounts.d.cts +4 -1
- package/dist/tests/accounts.d.cts.map +1 -1
- package/dist/tests/accounts.d.mts +4 -1
- package/dist/tests/accounts.d.mts.map +1 -1
- package/dist/tests/accounts.mjs +6 -1
- package/dist/tests/accounts.mjs.map +1 -1
- package/dist/tests/messenger.cjs +2 -0
- package/dist/tests/messenger.cjs.map +1 -1
- package/dist/tests/messenger.d.cts +2 -2
- package/dist/tests/messenger.d.cts.map +1 -1
- package/dist/tests/messenger.d.mts +2 -2
- package/dist/tests/messenger.d.mts.map +1 -1
- package/dist/tests/messenger.mjs +2 -0
- package/dist/tests/messenger.mjs.map +1 -1
- package/dist/tests/providers.cjs +1 -0
- package/dist/tests/providers.cjs.map +1 -1
- package/dist/tests/providers.d.cts +1 -0
- package/dist/tests/providers.d.cts.map +1 -1
- package/dist/tests/providers.d.mts +1 -0
- package/dist/tests/providers.d.mts.map +1 -1
- package/dist/tests/providers.mjs +1 -0
- package/dist/tests/providers.mjs.map +1 -1
- package/dist/types.cjs.map +1 -1
- package/dist/types.d.cts +3 -12
- package/dist/types.d.cts.map +1 -1
- package/dist/types.d.mts +3 -12
- package/dist/types.d.mts.map +1 -1
- package/dist/types.mjs.map +1 -1
- package/package.json +3 -2
|
@@ -1,20 +1,24 @@
|
|
|
1
1
|
import { AccountWalletType } from "@metamask/account-api";
|
|
2
2
|
import type { Bip44Account, MultichainAccountWalletId, MultichainAccountWallet as MultichainAccountWalletDefinition } from "@metamask/account-api";
|
|
3
3
|
import type { AccountGroupId } from "@metamask/account-api";
|
|
4
|
-
import type { AccountProvider } from "@metamask/account-api";
|
|
5
4
|
import { type EntropySourceId, type KeyringAccount } from "@metamask/keyring-api";
|
|
6
5
|
import { MultichainAccountGroup } from "./MultichainAccountGroup.mjs";
|
|
7
|
-
import type {
|
|
6
|
+
import type { NamedAccountProvider } from "./providers/index.mjs";
|
|
7
|
+
/**
|
|
8
|
+
* The metrics resulting from account discovery.
|
|
9
|
+
*/
|
|
10
|
+
export type AccountDiscoveryMetrics = {
|
|
11
|
+
[providerName: string]: number;
|
|
12
|
+
};
|
|
8
13
|
/**
|
|
9
14
|
* A multichain account wallet that holds multiple multichain accounts (one multichain account per
|
|
10
15
|
* group index).
|
|
11
16
|
*/
|
|
12
17
|
export declare class MultichainAccountWallet<Account extends Bip44Account<KeyringAccount>> implements MultichainAccountWalletDefinition<Account> {
|
|
13
18
|
#private;
|
|
14
|
-
constructor({ providers, entropySource,
|
|
15
|
-
providers:
|
|
19
|
+
constructor({ providers, entropySource, }: {
|
|
20
|
+
providers: NamedAccountProvider<Account>[];
|
|
16
21
|
entropySource: EntropySourceId;
|
|
17
|
-
messenger: MultichainAccountServiceMessenger;
|
|
18
22
|
});
|
|
19
23
|
/**
|
|
20
24
|
* Force wallet synchronization.
|
|
@@ -105,5 +109,11 @@ export declare class MultichainAccountWallet<Account extends Bip44Account<Keyrin
|
|
|
105
109
|
* @param groupIndex - The group index to align.
|
|
106
110
|
*/
|
|
107
111
|
alignGroup(groupIndex: number): Promise<void>;
|
|
112
|
+
/**
|
|
113
|
+
* Discover and create accounts for all providers.
|
|
114
|
+
*
|
|
115
|
+
* @returns The discovered accounts for each provider.
|
|
116
|
+
*/
|
|
117
|
+
discoverAndCreateAccounts(): Promise<AccountDiscoveryMetrics>;
|
|
108
118
|
}
|
|
109
119
|
//# sourceMappingURL=MultichainAccountWallet.d.mts.map
|
|
@@ -1 +1 @@
|
|
|
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,
|
|
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,EACL,KAAK,eAAe,EACpB,KAAK,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CAAC,CAChC;IAID;;;;OAIG;IACH,wBAAwB,IAAI,OAAO;IAInC;;OAEG;IACG,WAAW,IAAI,OAAO,CAAC,IAAI,CAAC;IAclC;;;;OAIG;IACG,UAAU,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBnD;;;;OAIG;IACG,yBAAyB,IAAI,OAAO,CAAC,uBAAuB,CAAC;CA6EpE"}
|
|
@@ -9,34 +9,30 @@ 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
|
|
12
|
+
var _MultichainAccountWallet_id, _MultichainAccountWallet_providers, _MultichainAccountWallet_entropySource, _MultichainAccountWallet_accountGroups, _MultichainAccountWallet_isAlignmentInProgress;
|
|
13
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
|
+
import { createProjectLogger } from "@metamask/utils";
|
|
16
17
|
import { MultichainAccountGroup } from "./MultichainAccountGroup.mjs";
|
|
18
|
+
const log = createProjectLogger('multichain-account-service');
|
|
17
19
|
/**
|
|
18
20
|
* A multichain account wallet that holds multiple multichain accounts (one multichain account per
|
|
19
21
|
* group index).
|
|
20
22
|
*/
|
|
21
23
|
export class MultichainAccountWallet {
|
|
22
|
-
constructor({ providers, entropySource,
|
|
23
|
-
_MultichainAccountWallet_instances.add(this);
|
|
24
|
+
constructor({ providers, entropySource, }) {
|
|
24
25
|
_MultichainAccountWallet_id.set(this, void 0);
|
|
25
26
|
_MultichainAccountWallet_providers.set(this, void 0);
|
|
26
27
|
_MultichainAccountWallet_entropySource.set(this, void 0);
|
|
27
28
|
_MultichainAccountWallet_accountGroups.set(this, void 0);
|
|
28
|
-
_MultichainAccountWallet_messenger.set(this, void 0);
|
|
29
|
-
// eslint-disable-next-line @typescript-eslint/prefer-readonly
|
|
30
|
-
_MultichainAccountWallet_initialized.set(this, false);
|
|
31
29
|
_MultichainAccountWallet_isAlignmentInProgress.set(this, false);
|
|
32
30
|
__classPrivateFieldSet(this, _MultichainAccountWallet_id, toMultichainAccountWalletId(entropySource), "f");
|
|
33
31
|
__classPrivateFieldSet(this, _MultichainAccountWallet_providers, providers, "f");
|
|
34
32
|
__classPrivateFieldSet(this, _MultichainAccountWallet_entropySource, entropySource, "f");
|
|
35
|
-
__classPrivateFieldSet(this, _MultichainAccountWallet_messenger, messenger, "f");
|
|
36
33
|
__classPrivateFieldSet(this, _MultichainAccountWallet_accountGroups, new Map(), "f");
|
|
37
|
-
// Initial synchronization
|
|
38
|
-
|
|
39
|
-
__classPrivateFieldSet(this, _MultichainAccountWallet_initialized, true, "f");
|
|
34
|
+
// Initial synchronization.
|
|
35
|
+
this.sync();
|
|
40
36
|
}
|
|
41
37
|
/**
|
|
42
38
|
* Force wallet synchronization.
|
|
@@ -45,7 +41,42 @@ export class MultichainAccountWallet {
|
|
|
45
41
|
* doesn't know about.
|
|
46
42
|
*/
|
|
47
43
|
sync() {
|
|
48
|
-
__classPrivateFieldGet(this,
|
|
44
|
+
for (const provider of __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f")) {
|
|
45
|
+
for (const account of provider.getAccounts()) {
|
|
46
|
+
const { entropy } = account.options;
|
|
47
|
+
// Filter for this wallet only.
|
|
48
|
+
if (entropy.id !== this.entropySource) {
|
|
49
|
+
continue;
|
|
50
|
+
}
|
|
51
|
+
// This multichain account might exists already.
|
|
52
|
+
let multichainAccount = __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").get(entropy.groupIndex);
|
|
53
|
+
if (!multichainAccount) {
|
|
54
|
+
multichainAccount = new MultichainAccountGroup({
|
|
55
|
+
groupIndex: entropy.groupIndex,
|
|
56
|
+
wallet: this,
|
|
57
|
+
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
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
|
+
__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").set(entropy.groupIndex, multichainAccount);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
// Now force-sync all remaining multichain accounts.
|
|
73
|
+
for (const [groupIndex, multichainAccount,] of __classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").entries()) {
|
|
74
|
+
multichainAccount.sync();
|
|
75
|
+
// Clean up old multichain accounts.
|
|
76
|
+
if (!multichainAccount.hasAccounts()) {
|
|
77
|
+
__classPrivateFieldGet(this, _MultichainAccountWallet_accountGroups, "f").delete(groupIndex);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
49
80
|
}
|
|
50
81
|
/**
|
|
51
82
|
* Gets the multichain account wallet ID.
|
|
@@ -134,7 +165,71 @@ export class MultichainAccountWallet {
|
|
|
134
165
|
* @returns The multichain account group for this group index.
|
|
135
166
|
*/
|
|
136
167
|
async createMultichainAccountGroup(groupIndex) {
|
|
137
|
-
|
|
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({
|
|
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;
|
|
138
233
|
}
|
|
139
234
|
/**
|
|
140
235
|
* Creates the next multichain account group.
|
|
@@ -143,7 +238,7 @@ export class MultichainAccountWallet {
|
|
|
143
238
|
* @returns The multichain account group for the next group index available.
|
|
144
239
|
*/
|
|
145
240
|
async createNextMultichainAccountGroup() {
|
|
146
|
-
return
|
|
241
|
+
return this.createMultichainAccountGroup(this.getNextGroupIndex());
|
|
147
242
|
}
|
|
148
243
|
/**
|
|
149
244
|
* Gets whether alignment is currently in progress for this wallet.
|
|
@@ -189,114 +284,70 @@ export class MultichainAccountWallet {
|
|
|
189
284
|
__classPrivateFieldSet(this, _MultichainAccountWallet_isAlignmentInProgress, false, "f");
|
|
190
285
|
}
|
|
191
286
|
}
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (groupIndex > nextGroupIndex) {
|
|
234
|
-
throw new Error(`You cannot use a group index that is higher than the next available one: expected <=${nextGroupIndex}, got ${groupIndex}`);
|
|
235
|
-
}
|
|
236
|
-
let group = this.getMultichainAccountGroup(groupIndex);
|
|
237
|
-
if (group) {
|
|
238
|
-
// If the group already exists, we just `sync` it and returns the same
|
|
239
|
-
// reference.
|
|
240
|
-
group.sync();
|
|
241
|
-
return group;
|
|
242
|
-
}
|
|
243
|
-
const results = await Promise.allSettled(__classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f").map((provider) => provider.createAccounts({
|
|
244
|
-
entropySource: __classPrivateFieldGet(this, _MultichainAccountWallet_entropySource, "f"),
|
|
245
|
-
groupIndex,
|
|
246
|
-
})));
|
|
247
|
-
// --------------------------------------------------------------------------------
|
|
248
|
-
// READ THIS CAREFULLY:
|
|
249
|
-
//
|
|
250
|
-
// Since we're not "fully supporting multichain" for now, we still rely on single
|
|
251
|
-
// :accountCreated events to sync multichain account groups and wallets. Which means
|
|
252
|
-
// that even if of the provider fails, some accounts will still be created on some
|
|
253
|
-
// other providers and will become "available" on the `AccountsController`, like:
|
|
254
|
-
//
|
|
255
|
-
// 1. Creating a multichain account group for index 1
|
|
256
|
-
// 2. EvmAccountProvider.createAccounts returns the EVM account for index 1
|
|
257
|
-
// * AccountsController WILL fire :accountCreated for this account
|
|
258
|
-
// * This account WILL BE "available" on the AccountsController state
|
|
259
|
-
// 3. SolAccountProvider.createAccounts fails to create a Solana account for index 1
|
|
260
|
-
// * AccountsController WON't fire :accountCreated for this account
|
|
261
|
-
// * This account WON'T be "available" on the Account
|
|
262
|
-
// 4. MultichainAccountService will receive a :accountCreated for the EVM account from
|
|
263
|
-
// step 2 and will create a new multichain account group for index 1, but it won't
|
|
264
|
-
// receive any event for the Solana account of this group. Thus, this group won't be
|
|
265
|
-
// "aligned" (missing "blockchain account" on this group).
|
|
266
|
-
//
|
|
267
|
-
// --------------------------------------------------------------------------------
|
|
268
|
-
// If any of the provider failed to create their accounts, then we consider the
|
|
269
|
-
// multichain account group to have failed too.
|
|
270
|
-
if (results.some((result) => result.status === 'rejected')) {
|
|
271
|
-
// NOTE: Some accounts might still have been created on other account providers. We
|
|
272
|
-
// don't rollback them.
|
|
273
|
-
const error = `Unable to create multichain account group for index: ${groupIndex}`;
|
|
274
|
-
let warn = `${error}:`;
|
|
275
|
-
for (const result of results) {
|
|
276
|
-
if (result.status === 'rejected') {
|
|
277
|
-
warn += `\n- ${result.reason}`;
|
|
287
|
+
/**
|
|
288
|
+
* Discover and create accounts for all providers.
|
|
289
|
+
*
|
|
290
|
+
* @returns The discovered accounts for each provider.
|
|
291
|
+
*/
|
|
292
|
+
async discoverAndCreateAccounts() {
|
|
293
|
+
// Start with the next available group index (so we can resume the discovery
|
|
294
|
+
// from there).
|
|
295
|
+
let maxGroupIndex = this.getNextGroupIndex();
|
|
296
|
+
// One serialized loop per provider; all run concurrently
|
|
297
|
+
const runProviderDiscovery = async (context) => {
|
|
298
|
+
const message = (stepName, groupIndex) => `[${context.provider.getName()}] Discovery ${stepName} (groupIndex=${groupIndex})`;
|
|
299
|
+
while (!context.stopped) {
|
|
300
|
+
// Fast‑forward to current high‑water mark
|
|
301
|
+
const targetGroupIndex = Math.max(context.groupIndex, maxGroupIndex);
|
|
302
|
+
log(message('STARTED', targetGroupIndex));
|
|
303
|
+
let accounts = [];
|
|
304
|
+
try {
|
|
305
|
+
accounts = await context.provider.discoverAndCreateAccounts({
|
|
306
|
+
entropySource: __classPrivateFieldGet(this, _MultichainAccountWallet_entropySource, "f"),
|
|
307
|
+
groupIndex: targetGroupIndex,
|
|
308
|
+
});
|
|
309
|
+
}
|
|
310
|
+
catch (error) {
|
|
311
|
+
context.stopped = true;
|
|
312
|
+
console.error(error);
|
|
313
|
+
log(message('FAILED', targetGroupIndex), error);
|
|
314
|
+
break;
|
|
315
|
+
}
|
|
316
|
+
if (!accounts.length) {
|
|
317
|
+
log(message('STOPPED', targetGroupIndex));
|
|
318
|
+
context.stopped = true;
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
log(message('SUCCEEDED', targetGroupIndex));
|
|
322
|
+
context.count += accounts.length;
|
|
323
|
+
const nextGroupIndex = targetGroupIndex + 1;
|
|
324
|
+
context.groupIndex = nextGroupIndex;
|
|
325
|
+
if (nextGroupIndex > maxGroupIndex) {
|
|
326
|
+
maxGroupIndex = nextGroupIndex;
|
|
327
|
+
}
|
|
278
328
|
}
|
|
329
|
+
};
|
|
330
|
+
const providerContexts = __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f").map((provider) => ({
|
|
331
|
+
provider,
|
|
332
|
+
stopped: false,
|
|
333
|
+
groupIndex: maxGroupIndex,
|
|
334
|
+
count: 0,
|
|
335
|
+
}));
|
|
336
|
+
// Start discovery for each providers.
|
|
337
|
+
await Promise.all(providerContexts.map(runProviderDiscovery));
|
|
338
|
+
// Sync the wallet after discovery to ensure that the newly added accounts are added into their groups.
|
|
339
|
+
// We can potentially remove this if we know that this race condition is not an issue in practice.
|
|
340
|
+
this.sync();
|
|
341
|
+
// Align missing accounts from group. This is required to create missing account from non-discovered
|
|
342
|
+
// indexes for some providers.
|
|
343
|
+
await this.alignGroups();
|
|
344
|
+
const discoveredAccounts = {};
|
|
345
|
+
for (const context of providerContexts) {
|
|
346
|
+
const providerName = context.provider.getName();
|
|
347
|
+
discoveredAccounts[providerName] = context.count;
|
|
279
348
|
}
|
|
280
|
-
|
|
281
|
-
throw new Error(error);
|
|
282
|
-
}
|
|
283
|
-
// Because of the :accountAdded automatic sync, we might already have created the
|
|
284
|
-
// group, so we first try to get it.
|
|
285
|
-
group = this.getMultichainAccountGroup(groupIndex);
|
|
286
|
-
if (!group) {
|
|
287
|
-
// If for some reason it's still not created, we're creating it explicitly now:
|
|
288
|
-
group = new MultichainAccountGroup({
|
|
289
|
-
wallet: this,
|
|
290
|
-
providers: __classPrivateFieldGet(this, _MultichainAccountWallet_providers, "f"),
|
|
291
|
-
groupIndex,
|
|
292
|
-
messenger: __classPrivateFieldGet(this, _MultichainAccountWallet_messenger, "f"),
|
|
293
|
-
});
|
|
349
|
+
return discoveredAccounts;
|
|
294
350
|
}
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
if (__classPrivateFieldGet(this, _MultichainAccountWallet_initialized, "f")) {
|
|
298
|
-
__classPrivateFieldGet(this, _MultichainAccountWallet_messenger, "f").publish('MultichainAccountService:multichainAccountGroupCreated', group);
|
|
299
|
-
}
|
|
300
|
-
return group;
|
|
301
|
-
};
|
|
351
|
+
}
|
|
352
|
+
_MultichainAccountWallet_id = new WeakMap(), _MultichainAccountWallet_providers = new WeakMap(), _MultichainAccountWallet_entropySource = new WeakMap(), _MultichainAccountWallet_accountGroups = new WeakMap(), _MultichainAccountWallet_isAlignmentInProgress = new WeakMap();
|
|
302
353
|
//# 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,EA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AC;KACJ;IAED,4CAA4C;IAC5C,uBAAA,IAAI,8CAAe,CAAC,GAAG,CAAC,UAAU,EAAE,KAAK,CAAC,CAAC,CAAC,oCAAoC;IAEhF,IAAI,uBAAA,IAAI,4CAAa,EAAE;QACrB,uBAAA,IAAI,0CAAW,CAAC,OAAO,CACrB,wDAAwD,EACxD,KAAK,CACN,CAAC;KACH;IAED,OAAO,KAAK,CAAC;AACf,CAAC","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';\nimport type { MultichainAccountServiceMessenger } from './types';\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 readonly #messenger: MultichainAccountServiceMessenger;\n\n // eslint-disable-next-line @typescript-eslint/prefer-readonly\n #initialized = false;\n\n #isAlignmentInProgress: boolean = false;\n\n constructor({\n providers,\n entropySource,\n messenger,\n }: {\n providers: AccountProvider<Account>[];\n entropySource: EntropySourceId;\n messenger: MultichainAccountServiceMessenger;\n }) {\n this.#id = toMultichainAccountWalletId(entropySource);\n this.#providers = providers;\n this.#entropySource = entropySource;\n this.#messenger = messenger;\n this.#accountGroups = new Map();\n\n // Initial synchronization (don't emit events during initialization).\n this.#sync();\n this.#initialized = true;\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 this.#sync();\n }\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 messenger: this.#messenger,\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 return this.#createMultichainAccountGroup(groupIndex);\n }\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 messenger: this.#messenger,\n });\n }\n\n // Register the account to our internal map.\n this.#accountGroups.set(groupIndex, group); // `group` cannot be undefined here.\n\n if (this.#initialized) {\n this.#messenger.publish(\n 'MultichainAccountService:multichainAccountGroupCreated',\n group,\n );\n }\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 /**\n * Gets whether alignment is currently in progress for this wallet.\n *\n * @returns True if alignment is in progress, false otherwise.\n */\n getIsAlignmentInProgress(): boolean {\n return this.#isAlignmentInProgress;\n }\n\n /**\n * Align all multichain account groups.\n */\n async alignGroups(): Promise<void> {\n if (this.#isAlignmentInProgress) {\n return; // Prevent concurrent alignments\n }\n\n this.#isAlignmentInProgress = true;\n try {\n const groups = this.getMultichainAccountGroups();\n await Promise.all(groups.map((g) => g.align()));\n } finally {\n this.#isAlignmentInProgress = false;\n }\n }\n\n /**\n * Align a specific multichain account group.\n *\n * @param groupIndex - The group index to align.\n */\n async alignGroup(groupIndex: number): Promise<void> {\n if (this.#isAlignmentInProgress) {\n return; // Prevent concurrent alignments\n }\n\n this.#isAlignmentInProgress = true;\n try {\n const group = this.getMultichainAccountGroup(groupIndex);\n if (group) {\n await group.align();\n }\n } finally {\n this.#isAlignmentInProgress = false;\n }\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;AAW1D,OAAO,EAAE,mBAAmB,EAAE,wBAAwB;AAEtD,OAAO,EAAE,sB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,0CAAW,CAAC,GAAG,CAAC,CAAC,QAAQ,EAAE,EAAE,CAAC,CAAC;YACjC,QAAQ;YACR,OAAO,EAAE,KAAK;YACd,UAAU,EAAE,aAAa;YACzB,KAAK,EAAE,CAAC;SACT,CAAC,CAAC,CAAC;QAEN,sCAAsC;QACtC,MAAM,OAAO,CAAC,GAAG,CAAC,gBAAgB,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC,CAAC;QAE9D,uGAAuG;QACvG,kGAAkG;QAClG,IAAI,CAAC,IAAI,EAAE,CAAC;QAEZ,oGAAoG;QACpG,8BAA8B;QAC9B,MAAM,IAAI,CAAC,WAAW,EAAE,CAAC;QAEzB,MAAM,kBAAkB,GAA2B,EAAE,CAAC;QACtD,KAAK,MAAM,OAAO,IAAI,gBAAgB,EAAE;YACtC,MAAM,YAAY,GAAG,OAAO,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;YAChD,kBAAkB,CAAC,YAAY,CAAC,GAAG,OAAO,CAAC,KAAK,CAAC;SAClD;QAED,OAAO,kBAAkB,CAAC;IAC5B,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 {\n type EntropySourceId,\n type KeyringAccount,\n} from '@metamask/keyring-api';\nimport { createProjectLogger } from '@metamask/utils';\n\nimport { MultichainAccountGroup } from './MultichainAccountGroup';\nimport type { NamedAccountProvider } from './providers';\n\n/**\n * The context for a provider discovery.\n */\ntype AccountProviderDiscoveryContext = {\n provider: NamedAccountProvider;\n stopped: boolean;\n groupIndex: number;\n count: number;\n};\n\n/**\n * The metrics resulting from account discovery.\n */\nexport type AccountDiscoveryMetrics = {\n [providerName: string]: number;\n};\n\nconst log = createProjectLogger('multichain-account-service');\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: NamedAccountProvider<Account>[];\n\n readonly #entropySource: EntropySourceId;\n\n readonly #accountGroups: Map<number, MultichainAccountGroup<Account>>;\n\n #isAlignmentInProgress: boolean = false;\n\n constructor({\n providers,\n entropySource,\n }: {\n providers: NamedAccountProvider<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 /**\n * Gets whether alignment is currently in progress for this wallet.\n *\n * @returns True if alignment is in progress, false otherwise.\n */\n getIsAlignmentInProgress(): boolean {\n return this.#isAlignmentInProgress;\n }\n\n /**\n * Align all multichain account groups.\n */\n async alignGroups(): Promise<void> {\n if (this.#isAlignmentInProgress) {\n return; // Prevent concurrent alignments\n }\n\n this.#isAlignmentInProgress = true;\n try {\n const groups = this.getMultichainAccountGroups();\n await Promise.all(groups.map((g) => g.align()));\n } finally {\n this.#isAlignmentInProgress = false;\n }\n }\n\n /**\n * Align a specific multichain account group.\n *\n * @param groupIndex - The group index to align.\n */\n async alignGroup(groupIndex: number): Promise<void> {\n if (this.#isAlignmentInProgress) {\n return; // Prevent concurrent alignments\n }\n\n this.#isAlignmentInProgress = true;\n try {\n const group = this.getMultichainAccountGroup(groupIndex);\n if (group) {\n await group.align();\n }\n } finally {\n this.#isAlignmentInProgress = false;\n }\n }\n\n /**\n * Discover and create accounts for all providers.\n *\n * @returns The discovered accounts for each provider.\n */\n async discoverAndCreateAccounts(): Promise<AccountDiscoveryMetrics> {\n // Start with the next available group index (so we can resume the discovery\n // from there).\n let maxGroupIndex = this.getNextGroupIndex();\n\n // One serialized loop per provider; all run concurrently\n const runProviderDiscovery = async (\n context: AccountProviderDiscoveryContext,\n ) => {\n const message = (stepName: string, groupIndex: number) =>\n `[${context.provider.getName()}] Discovery ${stepName} (groupIndex=${groupIndex})`;\n\n while (!context.stopped) {\n // Fast‑forward to current high‑water mark\n const targetGroupIndex = Math.max(context.groupIndex, maxGroupIndex);\n\n log(message('STARTED', targetGroupIndex));\n\n let accounts: Bip44Account<KeyringAccount>[] = [];\n try {\n accounts = await context.provider.discoverAndCreateAccounts({\n entropySource: this.#entropySource,\n groupIndex: targetGroupIndex,\n });\n } catch (error) {\n context.stopped = true;\n console.error(error);\n log(message('FAILED', targetGroupIndex), error);\n break;\n }\n\n if (!accounts.length) {\n log(message('STOPPED', targetGroupIndex));\n context.stopped = true;\n break;\n }\n\n log(message('SUCCEEDED', targetGroupIndex));\n\n context.count += accounts.length;\n\n const nextGroupIndex = targetGroupIndex + 1;\n context.groupIndex = nextGroupIndex;\n\n if (nextGroupIndex > maxGroupIndex) {\n maxGroupIndex = nextGroupIndex;\n }\n }\n };\n\n const providerContexts: AccountProviderDiscoveryContext[] =\n this.#providers.map((provider) => ({\n provider,\n stopped: false,\n groupIndex: maxGroupIndex,\n count: 0,\n }));\n\n // Start discovery for each providers.\n await Promise.all(providerContexts.map(runProviderDiscovery));\n\n // Sync the wallet after discovery to ensure that the newly added accounts are added into their groups.\n // We can potentially remove this if we know that this race condition is not an issue in practice.\n this.sync();\n\n // Align missing accounts from group. This is required to create missing account from non-discovered\n // indexes for some providers.\n await this.alignGroups();\n\n const discoveredAccounts: Record<string, number> = {};\n for (const context of providerContexts) {\n const providerName = context.provider.getName();\n discoveredAccounts[providerName] = context.count;\n }\n\n return discoveredAccounts;\n }\n}\n"]}
|
package/dist/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;
|
|
1
|
+
{"version":3,"file":"index.cjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;AAaA,mDAIqB;AAHnB,mHAAA,sBAAsB,OAAA;AACtB,qHAAA,wBAAwB,OAAA;AACxB,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 MultichainAccountServiceCreateMultichainAccountGroupAction,\n MultichainAccountServiceCreateNextMultichainAccountGroupAction,\n MultichainAccountServiceGetIsAlignmentInProgressAction,\n MultichainAccountServiceSetBasicFunctionalityAction,\n} from './types';\nexport {\n AccountProviderWrapper,\n BaseBip44AccountProvider,\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,4 @@
|
|
|
1
|
-
export type { MultichainAccountServiceActions, MultichainAccountServiceEvents, MultichainAccountServiceMessenger, MultichainAccountServiceGetMultichainAccountGroupAction, MultichainAccountServiceGetMultichainAccountWalletAction, MultichainAccountServiceGetMultichainAccountWalletsAction, MultichainAccountServiceGetMultichainAccountGroupsAction, MultichainAccountServiceCreateMultichainAccountGroupAction, MultichainAccountServiceCreateNextMultichainAccountGroupAction, MultichainAccountServiceGetIsAlignmentInProgressAction, MultichainAccountServiceSetBasicFunctionalityAction,
|
|
1
|
+
export type { MultichainAccountServiceActions, MultichainAccountServiceEvents, MultichainAccountServiceMessenger, MultichainAccountServiceGetMultichainAccountGroupAction, MultichainAccountServiceGetMultichainAccountWalletAction, MultichainAccountServiceGetMultichainAccountWalletsAction, MultichainAccountServiceGetMultichainAccountGroupsAction, MultichainAccountServiceCreateMultichainAccountGroupAction, MultichainAccountServiceCreateNextMultichainAccountGroupAction, MultichainAccountServiceGetIsAlignmentInProgressAction, MultichainAccountServiceSetBasicFunctionalityAction, } from "./types.cjs";
|
|
2
2
|
export { AccountProviderWrapper, BaseBip44AccountProvider, SnapAccountProvider, } from "./providers/index.cjs";
|
|
3
3
|
export { MultichainAccountWallet } from "./MultichainAccountWallet.cjs";
|
|
4
4
|
export { MultichainAccountGroup } from "./MultichainAccountGroup.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,EACxD,0DAA0D,EAC1D,8DAA8D,EAC9D,sDAAsD,EACtD,mDAAmD,
|
|
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,EACxD,0DAA0D,EAC1D,8DAA8D,EAC9D,sDAAsD,EACtD,mDAAmD,GACpD,oBAAgB;AACjB,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,mBAAmB,GACpB,8BAAoB;AACrB,OAAO,EAAE,uBAAuB,EAAE,sCAAkC;AACpE,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC"}
|
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export type { MultichainAccountServiceActions, MultichainAccountServiceEvents, MultichainAccountServiceMessenger, MultichainAccountServiceGetMultichainAccountGroupAction, MultichainAccountServiceGetMultichainAccountWalletAction, MultichainAccountServiceGetMultichainAccountWalletsAction, MultichainAccountServiceGetMultichainAccountGroupsAction, MultichainAccountServiceCreateMultichainAccountGroupAction, MultichainAccountServiceCreateNextMultichainAccountGroupAction, MultichainAccountServiceGetIsAlignmentInProgressAction, MultichainAccountServiceSetBasicFunctionalityAction,
|
|
1
|
+
export type { MultichainAccountServiceActions, MultichainAccountServiceEvents, MultichainAccountServiceMessenger, MultichainAccountServiceGetMultichainAccountGroupAction, MultichainAccountServiceGetMultichainAccountWalletAction, MultichainAccountServiceGetMultichainAccountWalletsAction, MultichainAccountServiceGetMultichainAccountGroupsAction, MultichainAccountServiceCreateMultichainAccountGroupAction, MultichainAccountServiceCreateNextMultichainAccountGroupAction, MultichainAccountServiceGetIsAlignmentInProgressAction, MultichainAccountServiceSetBasicFunctionalityAction, } from "./types.mjs";
|
|
2
2
|
export { AccountProviderWrapper, BaseBip44AccountProvider, SnapAccountProvider, } from "./providers/index.mjs";
|
|
3
3
|
export { MultichainAccountWallet } from "./MultichainAccountWallet.mjs";
|
|
4
4
|
export { MultichainAccountGroup } from "./MultichainAccountGroup.mjs";
|
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,EACxD,0DAA0D,EAC1D,8DAA8D,EAC9D,sDAAsD,EACtD,mDAAmD,
|
|
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,EACxD,0DAA0D,EAC1D,8DAA8D,EAC9D,sDAAsD,EACtD,mDAAmD,GACpD,oBAAgB;AACjB,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,mBAAmB,GACpB,8BAAoB;AACrB,OAAO,EAAE,uBAAuB,EAAE,sCAAkC;AACpE,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC"}
|
package/dist/index.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"index.mjs","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,OAAO,EACL,sBAAsB,EACtB,wBAAwB,EACxB,mBAAmB,EACpB,8BAAoB;AACrB,OAAO,EAAE,uBAAuB,EAAE,sCAAkC;AACpE,OAAO,EAAE,sBAAsB,EAAE,qCAAiC;AAClE,OAAO,EAAE,wBAAwB,EAAE,uCAAmC","sourcesContent":["export type {\n MultichainAccountServiceActions,\n MultichainAccountServiceEvents,\n MultichainAccountServiceMessenger,\n MultichainAccountServiceGetMultichainAccountGroupAction,\n MultichainAccountServiceGetMultichainAccountWalletAction,\n MultichainAccountServiceGetMultichainAccountWalletsAction,\n MultichainAccountServiceGetMultichainAccountGroupsAction,\n MultichainAccountServiceCreateMultichainAccountGroupAction,\n MultichainAccountServiceCreateNextMultichainAccountGroupAction,\n MultichainAccountServiceGetIsAlignmentInProgressAction,\n MultichainAccountServiceSetBasicFunctionalityAction,\n} from './types';\nexport {\n AccountProviderWrapper,\n BaseBip44AccountProvider,\n SnapAccountProvider,\n} from './providers';\nexport { MultichainAccountWallet } from './MultichainAccountWallet';\nexport { MultichainAccountGroup } from './MultichainAccountGroup';\nexport { MultichainAccountService } from './MultichainAccountService';\n"]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountProviderWrapper.cjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":";;;AAGA,6EAAsE;AAGtE;;;GAGG;AACH,MAAa,sBAAuB,SAAQ,mDAAwB;IAKlE,YACE,SAA4C,EAC5C,QAAkC;QAElC,KAAK,CAAC,SAAS,CAAC,CAAC;QARX,cAAS,GAAY,IAAI,CAAC;QAShC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,OAAgB;QACzB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACM,WAAW;QAClB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;IACrC,CAAC;IAED;;;;;;OAMG;IACM,UAAU,CACjB,EAAsC;QAEtC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;SACzC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;OAMG;IACH,mBAAmB,CAAC,OAAqC;QACvD,OAAO,IAAI,CAAC,QAAQ,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,cAAc,CAAC,OAGpB;QACC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,yBAAyB,CAAC,OAG/B;QACC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,yBAAyB,CAAC,OAAO,CAAC,CAAC;IAC1D,CAAC;CACF;
|
|
1
|
+
{"version":3,"file":"AccountProviderWrapper.cjs","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":";;;AAGA,6EAAsE;AAGtE;;;GAGG;AACH,MAAa,sBAAuB,SAAQ,mDAAwB;IAKlE,YACE,SAA4C,EAC5C,QAAkC;QAElC,KAAK,CAAC,SAAS,CAAC,CAAC;QARX,cAAS,GAAY,IAAI,CAAC;QAShC,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;IAC3B,CAAC;IAEQ,OAAO;QACd,OAAO,IAAI,CAAC,QAAQ,CAAC,OAAO,EAAE,CAAC;IACjC,CAAC;IAED;;;;OAIG;IACH,UAAU,CAAC,OAAgB;QACzB,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;IAC3B,CAAC;IAED;;;;OAIG;IACM,WAAW;QAClB,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,WAAW,EAAE,CAAC;IACrC,CAAC;IAED;;;;;;OAMG;IACM,UAAU,CACjB,EAAsC;QAEtC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,MAAM,IAAI,KAAK,CAAC,sBAAsB,CAAC,CAAC;SACzC;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,UAAU,CAAC,EAAE,CAAC,CAAC;IACtC,CAAC;IAED;;;;;;OAMG;IACH,mBAAmB,CAAC,OAAqC;QACvD,OAAO,IAAI,CAAC,QAAQ,CAAC,mBAAmB,CAAC,OAAO,CAAC,CAAC;IACpD,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,cAAc,CAAC,OAGpB;QACC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,cAAc,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED;;;;;;;OAOG;IACH,KAAK,CAAC,yBAAyB,CAAC,OAG/B;QACC,IAAI,CAAC,IAAI,CAAC,SAAS,EAAE;YACnB,OAAO,EAAE,CAAC;SACX;QACD,OAAO,IAAI,CAAC,QAAQ,CAAC,yBAAyB,CAAC,OAAO,CAAC,CAAC;IAC1D,CAAC;CACF;AApGD,wDAoGC;AAED;;;;;GAKG;AACH,SAAgB,wBAAwB,CACtC,QAAiB;IAEjB,OAAO,QAAQ,YAAY,sBAAsB,CAAC;AACpD,CAAC;AAJD,4DAIC","sourcesContent":["import type { Bip44Account } from '@metamask/account-api';\nimport type { EntropySourceId, KeyringAccount } from '@metamask/keyring-api';\n\nimport { BaseBip44AccountProvider } from './BaseBip44AccountProvider';\nimport type { MultichainAccountServiceMessenger } from '../types';\n\n/**\n * A simple wrapper that adds disable functionality to any BaseBip44AccountProvider.\n * When disabled, the provider will not create new accounts and return empty results.\n */\nexport class AccountProviderWrapper extends BaseBip44AccountProvider {\n private isEnabled: boolean = true;\n\n private readonly provider: BaseBip44AccountProvider;\n\n constructor(\n messenger: MultichainAccountServiceMessenger,\n provider: BaseBip44AccountProvider,\n ) {\n super(messenger);\n this.provider = provider;\n }\n\n override getName(): string {\n return this.provider.getName();\n }\n\n /**\n * Set the enabled state for this provider.\n *\n * @param enabled - Whether the provider should be enabled.\n */\n setEnabled(enabled: boolean): void {\n this.isEnabled = enabled;\n }\n\n /**\n * Override getAccounts to return empty array when disabled.\n *\n * @returns Array of accounts, or empty array if disabled.\n */\n override getAccounts(): Bip44Account<KeyringAccount>[] {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.getAccounts();\n }\n\n /**\n * Override getAccount to throw when disabled.\n *\n * @param id - The account ID to retrieve.\n * @returns The account with the specified ID.\n * @throws When disabled or account not found.\n */\n override getAccount(\n id: Bip44Account<KeyringAccount>['id'],\n ): Bip44Account<KeyringAccount> {\n if (!this.isEnabled) {\n throw new Error('Provider is disabled');\n }\n return this.provider.getAccount(id);\n }\n\n /**\n * Implement abstract method: Check if account is compatible.\n * Delegates directly to wrapped provider - no runtime checks needed!\n *\n * @param account - The account to check.\n * @returns True if the account is compatible.\n */\n isAccountCompatible(account: Bip44Account<KeyringAccount>): boolean {\n return this.provider.isAccountCompatible(account);\n }\n\n /**\n * Implement abstract method: Create accounts, returns empty array when disabled.\n *\n * @param options - Account creation options.\n * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to created accounts, or empty array if disabled.\n */\n async createAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.createAccounts(options);\n }\n\n /**\n * Implement abstract method: Discover and create accounts, returns empty array when disabled.\n *\n * @param options - Account discovery options.\n * @param options.entropySource - The entropy source to use.\n * @param options.groupIndex - The group index to use.\n * @returns Promise resolving to discovered accounts, or empty array if disabled.\n */\n async discoverAndCreateAccounts(options: {\n entropySource: EntropySourceId;\n groupIndex: number;\n }): Promise<Bip44Account<KeyringAccount>[]> {\n if (!this.isEnabled) {\n return [];\n }\n return this.provider.discoverAndCreateAccounts(options);\n }\n}\n\n/**\n * Simple type guard to check if a provider is wrapped.\n *\n * @param provider - The provider to check.\n * @returns True if the provider is an AccountProviderWrapper.\n */\nexport function isAccountProviderWrapper(\n provider: unknown,\n): provider is AccountProviderWrapper {\n return provider instanceof AccountProviderWrapper;\n}\n"]}
|
|
@@ -10,6 +10,7 @@ export declare class AccountProviderWrapper extends BaseBip44AccountProvider {
|
|
|
10
10
|
private isEnabled;
|
|
11
11
|
private readonly provider;
|
|
12
12
|
constructor(messenger: MultichainAccountServiceMessenger, provider: BaseBip44AccountProvider);
|
|
13
|
+
getName(): string;
|
|
13
14
|
/**
|
|
14
15
|
* Set the enabled state for this provider.
|
|
15
16
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AccountProviderWrapper.d.cts","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,wBAAwB;IAClE,OAAO,CAAC,SAAS,CAAiB;IAElC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA2B;gBAGlD,SAAS,EAAE,iCAAiC,EAC5C,QAAQ,EAAE,wBAAwB;
|
|
1
|
+
{"version":3,"file":"AccountProviderWrapper.d.cts","sourceRoot":"","sources":["../../src/providers/AccountProviderWrapper.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,8BAA8B;AAC1D,OAAO,KAAK,EAAE,eAAe,EAAE,cAAc,EAAE,8BAA8B;AAE7E,OAAO,EAAE,wBAAwB,EAAE,uCAAmC;AACtE,OAAO,KAAK,EAAE,iCAAiC,EAAE,qBAAiB;AAElE;;;GAGG;AACH,qBAAa,sBAAuB,SAAQ,wBAAwB;IAClE,OAAO,CAAC,SAAS,CAAiB;IAElC,OAAO,CAAC,QAAQ,CAAC,QAAQ,CAA2B;gBAGlD,SAAS,EAAE,iCAAiC,EAC5C,QAAQ,EAAE,wBAAwB;IAM3B,OAAO,IAAI,MAAM;IAI1B;;;;OAIG;IACH,UAAU,CAAC,OAAO,EAAE,OAAO,GAAG,IAAI;IAIlC;;;;OAIG;IACM,WAAW,IAAI,YAAY,CAAC,cAAc,CAAC,EAAE;IAOtD;;;;;;OAMG;IACM,UAAU,CACjB,EAAE,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