@metamask/accounts-controller 18.1.1 → 18.2.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (59) hide show
  1. package/CHANGELOG.md +40 -1
  2. package/dist/AccountsController.cjs +608 -0
  3. package/dist/AccountsController.cjs.map +1 -0
  4. package/dist/{types/AccountsController.d.ts → AccountsController.d.cts} +13 -10
  5. package/dist/AccountsController.d.cts.map +1 -0
  6. package/dist/AccountsController.d.mts +215 -0
  7. package/dist/AccountsController.d.mts.map +1 -0
  8. package/dist/AccountsController.mjs +604 -9
  9. package/dist/AccountsController.mjs.map +1 -1
  10. package/dist/index.cjs +9 -0
  11. package/dist/index.cjs.map +1 -0
  12. package/dist/{types/index.d.ts → index.d.cts} +4 -4
  13. package/dist/index.d.cts.map +1 -0
  14. package/dist/index.d.mts +4 -0
  15. package/dist/index.d.mts.map +1 -0
  16. package/dist/index.mjs +2 -13
  17. package/dist/index.mjs.map +1 -1
  18. package/dist/tests/mocks.cjs +49 -0
  19. package/dist/tests/mocks.cjs.map +1 -0
  20. package/dist/{types/tests/mocks.d.ts → tests/mocks.d.cts} +3 -3
  21. package/dist/tests/mocks.d.cts.map +1 -0
  22. package/dist/tests/mocks.d.mts +17 -0
  23. package/dist/tests/mocks.d.mts.map +1 -0
  24. package/dist/tests/mocks.mjs +41 -60
  25. package/dist/tests/mocks.mjs.map +1 -1
  26. package/dist/utils.cjs +80 -0
  27. package/dist/utils.cjs.map +1 -0
  28. package/dist/{types/utils.d.ts → utils.d.cts} +3 -3
  29. package/dist/utils.d.cts.map +1 -0
  30. package/dist/utils.d.mts +28 -0
  31. package/dist/utils.d.mts.map +1 -0
  32. package/dist/utils.mjs +73 -13
  33. package/dist/utils.mjs.map +1 -1
  34. package/package.json +19 -14
  35. package/dist/AccountsController.js +0 -11
  36. package/dist/AccountsController.js.map +0 -1
  37. package/dist/chunk-5MTWAWAS.js +0 -753
  38. package/dist/chunk-5MTWAWAS.js.map +0 -1
  39. package/dist/chunk-BYPP7G2N.js +0 -56
  40. package/dist/chunk-BYPP7G2N.js.map +0 -1
  41. package/dist/chunk-UJIPPGP6.js +0 -19
  42. package/dist/chunk-UJIPPGP6.js.map +0 -1
  43. package/dist/chunk-Y2QVUNIA.mjs +0 -56
  44. package/dist/chunk-Y2QVUNIA.mjs.map +0 -1
  45. package/dist/chunk-ZNSHBDHA.mjs +0 -19
  46. package/dist/chunk-ZNSHBDHA.mjs.map +0 -1
  47. package/dist/chunk-ZPUB3DMH.mjs +0 -753
  48. package/dist/chunk-ZPUB3DMH.mjs.map +0 -1
  49. package/dist/index.js +0 -14
  50. package/dist/index.js.map +0 -1
  51. package/dist/tests/mocks.js +0 -65
  52. package/dist/tests/mocks.js.map +0 -1
  53. package/dist/tsconfig.build.tsbuildinfo +0 -1
  54. package/dist/types/AccountsController.d.ts.map +0 -1
  55. package/dist/types/index.d.ts.map +0 -1
  56. package/dist/types/tests/mocks.d.ts.map +0 -1
  57. package/dist/types/utils.d.ts.map +0 -1
  58. package/dist/utils.js +0 -14
  59. package/dist/utils.js.map +0 -1
@@ -1,753 +0,0 @@
1
- "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
-
3
-
4
-
5
- var _chunkBYPP7G2Njs = require('./chunk-BYPP7G2N.js');
6
-
7
-
8
-
9
- var _chunkUJIPPGP6js = require('./chunk-UJIPPGP6.js');
10
-
11
- // src/AccountsController.ts
12
- var _basecontroller = require('@metamask/base-controller');
13
- var _ethsnapkeyring = require('@metamask/eth-snap-keyring');
14
-
15
-
16
-
17
-
18
- var _keyringapi = require('@metamask/keyring-api');
19
- var _keyringcontroller = require('@metamask/keyring-controller');
20
-
21
-
22
-
23
- var _utils = require('@metamask/utils');
24
- var controllerName = "AccountsController";
25
- var accountsControllerMetadata = {
26
- internalAccounts: {
27
- persist: true,
28
- anonymous: false
29
- }
30
- };
31
- var defaultState = {
32
- internalAccounts: {
33
- accounts: {},
34
- selectedAccount: ""
35
- }
36
- };
37
- var EMPTY_ACCOUNT = {
38
- id: "",
39
- address: "",
40
- options: {},
41
- methods: [],
42
- type: _keyringapi.EthAccountType.Eoa,
43
- metadata: {
44
- name: "",
45
- keyring: {
46
- type: ""
47
- },
48
- importTime: 0
49
- }
50
- };
51
- var _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn, _listSnapAccounts, listSnapAccounts_fn, _listNormalAccounts, listNormalAccounts_fn, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn, _handleOnSnapStateChange, handleOnSnapStateChange_fn, _getAccountsByKeyringType, getAccountsByKeyringType_fn, _getLastSelectedAccount, getLastSelectedAccount_fn, _isAccountCompatibleWithChain, isAccountCompatibleWithChain_fn, _getLastSelectedIndex, getLastSelectedIndex_fn, _handleNewAccountAdded, handleNewAccountAdded_fn, _publishAccountChangeEvent, publishAccountChangeEvent_fn, _handleAccountRemoved, handleAccountRemoved_fn, _populateExistingMetadata, populateExistingMetadata_fn, _registerMessageHandlers, registerMessageHandlers_fn;
52
- var AccountsController = class extends _basecontroller.BaseController {
53
- /**
54
- * Constructor for AccountsController.
55
- *
56
- * @param options - The controller options.
57
- * @param options.messenger - The messenger object.
58
- * @param options.state - Initial state to set on this controller
59
- */
60
- constructor({
61
- messenger,
62
- state
63
- }) {
64
- super({
65
- messenger,
66
- name: controllerName,
67
- metadata: accountsControllerMetadata,
68
- state: {
69
- ...defaultState,
70
- ...state
71
- }
72
- });
73
- /**
74
- * Generates an internal account for a non-Snap account.
75
- * @param address - The address of the account.
76
- * @param type - The type of the account.
77
- * @returns The generated internal account.
78
- */
79
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _generateInternalAccountForNonSnapAccount);
80
- /**
81
- * Returns a list of internal accounts created using the SnapKeyring.
82
- *
83
- * @returns A promise that resolves to an array of InternalAccount objects.
84
- */
85
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _listSnapAccounts);
86
- /**
87
- * Returns a list of normal accounts.
88
- * Note: listNormalAccounts is a temporary method until the keyrings all implement the InternalAccount interface.
89
- * Once all keyrings implement the InternalAccount interface, this method can be removed and getAccounts can be used instead.
90
- *
91
- * @returns A Promise that resolves to an array of InternalAccount objects.
92
- */
93
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _listNormalAccounts);
94
- /**
95
- * Handles changes in the keyring state, specifically when new accounts are added or removed.
96
- *
97
- * @param keyringState - The new state of the keyring controller.
98
- */
99
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _handleOnKeyringStateChange);
100
- /**
101
- * Handles the change in SnapControllerState by updating the metadata of accounts that have a snap enabled.
102
- *
103
- * @param snapState - The new SnapControllerState.
104
- */
105
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _handleOnSnapStateChange);
106
- /**
107
- * Returns the list of accounts for a given keyring type.
108
- * @param keyringType - The type of keyring.
109
- * @param accounts - Accounts to filter by keyring type.
110
- * @returns The list of accounts associcated with this keyring type.
111
- */
112
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _getAccountsByKeyringType);
113
- /**
114
- * Returns the last selected account from the given array of accounts.
115
- *
116
- * @param accounts - An array of InternalAccount objects.
117
- * @returns The InternalAccount object that was last selected, or undefined if the array is empty.
118
- */
119
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _getLastSelectedAccount);
120
- /**
121
- * Checks if an account is compatible with a given chain namespace.
122
- * @private
123
- * @param account - The account to check compatibility for.
124
- * @param chainId - The CAIP2 to check compatibility with.
125
- * @returns Returns true if the account is compatible with the chain namespace, otherwise false.
126
- */
127
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _isAccountCompatibleWithChain);
128
- /**
129
- * Retrieves the index value for `metadata.lastSelected`.
130
- *
131
- * @returns The index value.
132
- */
133
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _getLastSelectedIndex);
134
- /**
135
- * Handles the addition of a new account to the controller.
136
- * If the account is not a Snap Keyring account, generates an internal account for it and adds it to the controller.
137
- * If the account is a Snap Keyring account, retrieves the account from the keyring and adds it to the controller.
138
- * @param accountsState - AccountsController accounts state that is to be mutated.
139
- * @param account - The address and keyring type object of the new account.
140
- * @returns The updated AccountsController accounts state.
141
- */
142
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _handleNewAccountAdded);
143
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _publishAccountChangeEvent);
144
- /**
145
- * Handles the removal of an account from the internal accounts list.
146
- * @param accountsState - AccountsController accounts state that is to be mutated.
147
- * @param accountId - The ID of the account to be removed.
148
- * @returns The updated AccountsController state.
149
- */
150
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _handleAccountRemoved);
151
- /**
152
- * Retrieves the value of a specific metadata key for an existing account.
153
- * @param accountId - The ID of the account.
154
- * @param metadataKey - The key of the metadata to retrieve.
155
- * @param account - The account object to retrieve the metadata key from.
156
- * @returns The value of the specified metadata key, or undefined if the account or metadata key does not exist.
157
- */
158
- // TODO: Either fix this lint violation or explain why it's necessary to ignore.
159
- // eslint-disable-next-line @typescript-eslint/naming-convention
160
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _populateExistingMetadata);
161
- /**
162
- * Registers message handlers for the AccountsController.
163
- * @private
164
- */
165
- _chunkUJIPPGP6js.__privateAdd.call(void 0, this, _registerMessageHandlers);
166
- this.messagingSystem.subscribe(
167
- "SnapController:stateChange",
168
- (snapStateState) => _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _handleOnSnapStateChange, handleOnSnapStateChange_fn).call(this, snapStateState)
169
- );
170
- this.messagingSystem.subscribe(
171
- "KeyringController:stateChange",
172
- (keyringState) => _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn).call(this, keyringState)
173
- );
174
- _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
175
- }
176
- /**
177
- * Returns the internal account object for the given account ID, if it exists.
178
- *
179
- * @param accountId - The ID of the account to retrieve.
180
- * @returns The internal account object, or undefined if the account does not exist.
181
- */
182
- getAccount(accountId) {
183
- return this.state.internalAccounts.accounts[accountId];
184
- }
185
- /**
186
- * Returns an array of all evm internal accounts.
187
- *
188
- * @returns An array of InternalAccount objects.
189
- */
190
- listAccounts() {
191
- const accounts = Object.values(this.state.internalAccounts.accounts);
192
- return accounts.filter((account) => _keyringapi.isEvmAccountType.call(void 0, account.type));
193
- }
194
- /**
195
- * Returns an array of all internal accounts.
196
- *
197
- * @param chainId - The chain ID.
198
- * @returns An array of InternalAccount objects.
199
- */
200
- listMultichainAccounts(chainId) {
201
- const accounts = Object.values(this.state.internalAccounts.accounts);
202
- if (!chainId) {
203
- return accounts;
204
- }
205
- if (!_utils.isCaipChainId.call(void 0, chainId)) {
206
- throw new Error(`Invalid CAIP-2 chain ID: ${String(chainId)}`);
207
- }
208
- return accounts.filter(
209
- (account) => _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _isAccountCompatibleWithChain, isAccountCompatibleWithChain_fn).call(this, account, chainId)
210
- );
211
- }
212
- /**
213
- * Returns the internal account object for the given account ID.
214
- *
215
- * @param accountId - The ID of the account to retrieve.
216
- * @returns The internal account object.
217
- * @throws An error if the account ID is not found.
218
- */
219
- getAccountExpect(accountId) {
220
- const account = this.getAccount(accountId);
221
- if (account === void 0) {
222
- throw new Error(`Account Id "${accountId}" not found`);
223
- }
224
- return account;
225
- }
226
- /**
227
- * Returns the last selected EVM account.
228
- *
229
- * @returns The selected internal account.
230
- */
231
- getSelectedAccount() {
232
- if (this.state.internalAccounts.selectedAccount === "") {
233
- return EMPTY_ACCOUNT;
234
- }
235
- const selectedAccount = this.getAccountExpect(
236
- this.state.internalAccounts.selectedAccount
237
- );
238
- if (_keyringapi.isEvmAccountType.call(void 0, selectedAccount.type)) {
239
- return selectedAccount;
240
- }
241
- const accounts = this.listAccounts();
242
- if (!accounts.length) {
243
- throw new Error("No EVM accounts");
244
- }
245
- return _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _getLastSelectedAccount, getLastSelectedAccount_fn).call(this, accounts);
246
- }
247
- /**
248
- * __WARNING The return value may be undefined if there isn't an account for that chain id.__
249
- *
250
- * Retrieves the last selected account by chain ID.
251
- *
252
- * @param chainId - The chain ID to filter the accounts.
253
- * @returns The last selected account compatible with the specified chain ID or undefined.
254
- */
255
- getSelectedMultichainAccount(chainId) {
256
- if (this.state.internalAccounts.selectedAccount === "") {
257
- return EMPTY_ACCOUNT;
258
- }
259
- if (!chainId) {
260
- return this.getAccountExpect(this.state.internalAccounts.selectedAccount);
261
- }
262
- if (!_utils.isCaipChainId.call(void 0, chainId)) {
263
- throw new Error(`Invalid CAIP-2 chain ID: ${chainId}`);
264
- }
265
- const accounts = Object.values(this.state.internalAccounts.accounts).filter(
266
- (account) => _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _isAccountCompatibleWithChain, isAccountCompatibleWithChain_fn).call(this, account, chainId)
267
- );
268
- return _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _getLastSelectedAccount, getLastSelectedAccount_fn).call(this, accounts);
269
- }
270
- /**
271
- * Returns the account with the specified address.
272
- * ! This method will only return the first account that matches the address
273
- * @param address - The address of the account to retrieve.
274
- * @returns The account with the specified address, or undefined if not found.
275
- */
276
- getAccountByAddress(address) {
277
- return this.listMultichainAccounts().find(
278
- (account) => account.address.toLowerCase() === address.toLowerCase()
279
- );
280
- }
281
- /**
282
- * Sets the selected account by its ID.
283
- *
284
- * @param accountId - The ID of the account to be selected.
285
- */
286
- setSelectedAccount(accountId) {
287
- const account = this.getAccountExpect(accountId);
288
- this.update((currentState) => {
289
- currentState.internalAccounts.accounts[account.id].metadata.lastSelected = Date.now();
290
- currentState.internalAccounts.selectedAccount = account.id;
291
- });
292
- _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _publishAccountChangeEvent, publishAccountChangeEvent_fn).call(this, account);
293
- }
294
- /**
295
- * Sets the name of the account with the given ID.
296
- *
297
- * @param accountId - The ID of the account to set the name for.
298
- * @param accountName - The new name for the account.
299
- * @throws An error if an account with the same name already exists.
300
- */
301
- setAccountName(accountId, accountName) {
302
- if (this.listMultichainAccounts().find(
303
- (internalAccount) => internalAccount.metadata.name === accountName && internalAccount.id !== accountId
304
- )) {
305
- throw new Error("Account name already exists");
306
- }
307
- this.updateAccountMetadata(accountId, { name: accountName });
308
- }
309
- /**
310
- * Updates the metadata of the account with the given ID.
311
- * Use {@link setAccountName} if you only need to update the name of the account.
312
- *
313
- * @param accountId - The ID of the account for which the metadata will be updated.
314
- * @param metadata - The new metadata for the account.
315
- */
316
- updateAccountMetadata(accountId, metadata) {
317
- const account = this.getAccountExpect(accountId);
318
- this.update((currentState) => {
319
- const internalAccount = {
320
- ...account,
321
- metadata: { ...account.metadata, ...metadata }
322
- };
323
- currentState.internalAccounts.accounts[accountId] = internalAccount;
324
- });
325
- }
326
- /**
327
- * Updates the internal accounts list by retrieving normal and snap accounts,
328
- * removing duplicates, and updating the metadata of each account.
329
- *
330
- * @returns A Promise that resolves when the accounts have been updated.
331
- */
332
- async updateAccounts() {
333
- const snapAccounts = await _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _listSnapAccounts, listSnapAccounts_fn).call(this);
334
- const normalAccounts = await _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _listNormalAccounts, listNormalAccounts_fn).call(this);
335
- const keyringTypes = /* @__PURE__ */ new Map();
336
- const previousAccounts = this.state.internalAccounts.accounts;
337
- const accounts = [
338
- ...normalAccounts,
339
- ...snapAccounts
340
- ].reduce((internalAccountMap, internalAccount) => {
341
- const keyringTypeName = _chunkBYPP7G2Njs.keyringTypeToName.call(void 0,
342
- internalAccount.metadata.keyring.type
343
- );
344
- const keyringAccountIndex = keyringTypes.get(keyringTypeName) ?? 0;
345
- if (keyringAccountIndex) {
346
- keyringTypes.set(keyringTypeName, keyringAccountIndex + 1);
347
- } else {
348
- keyringTypes.set(keyringTypeName, 1);
349
- }
350
- const existingAccount = previousAccounts[internalAccount.id];
351
- internalAccountMap[internalAccount.id] = {
352
- ...internalAccount,
353
- metadata: {
354
- ...internalAccount.metadata,
355
- name: _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _populateExistingMetadata, populateExistingMetadata_fn).call(this, existingAccount?.id, "name") ?? `${keyringTypeName} ${keyringAccountIndex + 1}`,
356
- importTime: _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _populateExistingMetadata, populateExistingMetadata_fn).call(this, existingAccount?.id, "importTime") ?? Date.now(),
357
- lastSelected: _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _populateExistingMetadata, populateExistingMetadata_fn).call(this, existingAccount?.id, "lastSelected") ?? 0
358
- }
359
- };
360
- return internalAccountMap;
361
- }, {});
362
- this.update((currentState) => {
363
- currentState.internalAccounts.accounts = accounts;
364
- if (!currentState.internalAccounts.accounts[currentState.internalAccounts.selectedAccount]) {
365
- const lastSelectedAccount = _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _getLastSelectedAccount, getLastSelectedAccount_fn).call(this, Object.values(accounts));
366
- if (lastSelectedAccount) {
367
- currentState.internalAccounts.selectedAccount = lastSelectedAccount.id;
368
- currentState.internalAccounts.accounts[lastSelectedAccount.id].metadata.lastSelected = _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _getLastSelectedIndex, getLastSelectedIndex_fn).call(this);
369
- _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _publishAccountChangeEvent, publishAccountChangeEvent_fn).call(this, lastSelectedAccount);
370
- } else {
371
- currentState.internalAccounts.selectedAccount = "";
372
- }
373
- }
374
- });
375
- }
376
- /**
377
- * Loads the backup state of the accounts controller.
378
- *
379
- * @param backup - The backup state to load.
380
- */
381
- loadBackup(backup) {
382
- if (backup.internalAccounts) {
383
- this.update((currentState) => {
384
- currentState.internalAccounts = backup.internalAccounts;
385
- });
386
- }
387
- }
388
- /**
389
- * Returns the next account number for a given keyring type.
390
- * @param keyringType - The type of keyring.
391
- * @param accounts - Existing accounts to check for the next available account number.
392
- * @returns An object containing the account prefix and index to use.
393
- */
394
- getNextAvailableAccountName(keyringType = _keyringcontroller.KeyringTypes.hd, accounts) {
395
- const keyringName = _chunkBYPP7G2Njs.keyringTypeToName.call(void 0, keyringType);
396
- const keyringAccounts = _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _getAccountsByKeyringType, getAccountsByKeyringType_fn).call(this, keyringType, accounts);
397
- const lastDefaultIndexUsedForKeyringType = keyringAccounts.reduce(
398
- (maxInternalAccountIndex, internalAccount) => {
399
- const match = new RegExp(`${keyringName} ([0-9]+)$`, "u").exec(
400
- internalAccount.metadata.name
401
- );
402
- if (match) {
403
- const internalAccountIndex = parseInt(match[1], 10);
404
- return Math.max(maxInternalAccountIndex, internalAccountIndex);
405
- }
406
- return maxInternalAccountIndex;
407
- },
408
- 0
409
- );
410
- const index = Math.max(
411
- keyringAccounts.length + 1,
412
- // ESLint is confused; this is a number.
413
- // eslint-disable-next-line @typescript-eslint/restrict-plus-operands
414
- lastDefaultIndexUsedForKeyringType + 1
415
- );
416
- return `${keyringName} ${index}`;
417
- }
418
- };
419
- _generateInternalAccountForNonSnapAccount = new WeakSet();
420
- generateInternalAccountForNonSnapAccount_fn = function(address, type) {
421
- return {
422
- id: _chunkBYPP7G2Njs.getUUIDFromAddressOfNormalAccount.call(void 0, address),
423
- address,
424
- options: {},
425
- methods: [
426
- _keyringapi.EthMethod.PersonalSign,
427
- _keyringapi.EthMethod.Sign,
428
- _keyringapi.EthMethod.SignTransaction,
429
- _keyringapi.EthMethod.SignTypedDataV1,
430
- _keyringapi.EthMethod.SignTypedDataV3,
431
- _keyringapi.EthMethod.SignTypedDataV4
432
- ],
433
- type: _keyringapi.EthAccountType.Eoa,
434
- metadata: {
435
- name: "",
436
- importTime: Date.now(),
437
- keyring: {
438
- type
439
- }
440
- }
441
- };
442
- };
443
- _listSnapAccounts = new WeakSet();
444
- listSnapAccounts_fn = async function() {
445
- const [snapKeyring] = this.messagingSystem.call(
446
- "KeyringController:getKeyringsByType",
447
- _ethsnapkeyring.SnapKeyring.type
448
- );
449
- if (!snapKeyring) {
450
- return [];
451
- }
452
- const snapAccounts = snapKeyring.listAccounts();
453
- return snapAccounts;
454
- };
455
- _listNormalAccounts = new WeakSet();
456
- listNormalAccounts_fn = async function() {
457
- const addresses = await this.messagingSystem.call(
458
- "KeyringController:getAccounts"
459
- );
460
- const internalAccounts = [];
461
- for (const address of addresses) {
462
- const keyring = await this.messagingSystem.call(
463
- "KeyringController:getKeyringForAccount",
464
- address
465
- );
466
- const keyringType = keyring.type;
467
- if (!_chunkBYPP7G2Njs.isNormalKeyringType.call(void 0, keyringType)) {
468
- continue;
469
- }
470
- const id = _chunkBYPP7G2Njs.getUUIDFromAddressOfNormalAccount.call(void 0, address);
471
- internalAccounts.push({
472
- id,
473
- address,
474
- options: {},
475
- methods: [
476
- _keyringapi.EthMethod.PersonalSign,
477
- _keyringapi.EthMethod.Sign,
478
- _keyringapi.EthMethod.SignTransaction,
479
- _keyringapi.EthMethod.SignTypedDataV1,
480
- _keyringapi.EthMethod.SignTypedDataV3,
481
- _keyringapi.EthMethod.SignTypedDataV4
482
- ],
483
- type: _keyringapi.EthAccountType.Eoa,
484
- metadata: {
485
- name: _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _populateExistingMetadata, populateExistingMetadata_fn).call(this, id, "name") ?? "",
486
- importTime: _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _populateExistingMetadata, populateExistingMetadata_fn).call(this, id, "importTime") ?? Date.now(),
487
- lastSelected: _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _populateExistingMetadata, populateExistingMetadata_fn).call(this, id, "lastSelected") ?? 0,
488
- keyring: {
489
- type: keyring.type
490
- }
491
- }
492
- });
493
- }
494
- return internalAccounts;
495
- };
496
- _handleOnKeyringStateChange = new WeakSet();
497
- handleOnKeyringStateChange_fn = function(keyringState) {
498
- if (keyringState.isUnlocked && keyringState.keyrings.length > 0) {
499
- const updatedNormalKeyringAddresses = [];
500
- const updatedSnapKeyringAddresses = [];
501
- for (const keyring of keyringState.keyrings) {
502
- if (keyring.type === _keyringcontroller.KeyringTypes.snap) {
503
- updatedSnapKeyringAddresses.push(
504
- ...keyring.accounts.map((address) => {
505
- return {
506
- address,
507
- type: keyring.type
508
- };
509
- })
510
- );
511
- } else {
512
- updatedNormalKeyringAddresses.push(
513
- ...keyring.accounts.map((address) => {
514
- return {
515
- address,
516
- type: keyring.type
517
- };
518
- })
519
- );
520
- }
521
- }
522
- const { previousNormalInternalAccounts, previousSnapInternalAccounts } = this.listMultichainAccounts().reduce(
523
- (accumulator, account) => {
524
- if (account.metadata.keyring.type === _keyringcontroller.KeyringTypes.snap) {
525
- accumulator.previousSnapInternalAccounts.push(account);
526
- } else {
527
- accumulator.previousNormalInternalAccounts.push(account);
528
- }
529
- return accumulator;
530
- },
531
- {
532
- previousNormalInternalAccounts: [],
533
- previousSnapInternalAccounts: []
534
- }
535
- );
536
- const addedAccounts = [];
537
- const deletedAccounts = [];
538
- for (const account of updatedNormalKeyringAddresses) {
539
- if (!this.state.internalAccounts.accounts[_chunkBYPP7G2Njs.getUUIDFromAddressOfNormalAccount.call(void 0, account.address)]) {
540
- addedAccounts.push(account);
541
- }
542
- }
543
- for (const account of updatedSnapKeyringAddresses) {
544
- if (!previousSnapInternalAccounts.find(
545
- (internalAccount) => internalAccount.address.toLowerCase() === account.address.toLowerCase()
546
- )) {
547
- addedAccounts.push(account);
548
- }
549
- }
550
- for (const account of previousNormalInternalAccounts) {
551
- if (!updatedNormalKeyringAddresses.find(
552
- ({ address }) => address.toLowerCase() === account.address.toLowerCase()
553
- )) {
554
- deletedAccounts.push(account);
555
- }
556
- }
557
- for (const account of previousSnapInternalAccounts) {
558
- if (!updatedSnapKeyringAddresses.find(
559
- ({ address }) => address.toLowerCase() === account.address.toLowerCase()
560
- )) {
561
- deletedAccounts.push(account);
562
- }
563
- }
564
- this.update((currentState) => {
565
- if (deletedAccounts.length > 0) {
566
- for (const account of deletedAccounts) {
567
- currentState.internalAccounts.accounts = _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _handleAccountRemoved, handleAccountRemoved_fn).call(this, currentState.internalAccounts.accounts, account.id);
568
- }
569
- }
570
- if (addedAccounts.length > 0) {
571
- for (const account of addedAccounts) {
572
- currentState.internalAccounts.accounts = _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _handleNewAccountAdded, handleNewAccountAdded_fn).call(this, currentState.internalAccounts.accounts, account);
573
- }
574
- }
575
- const existingAccounts = Object.values(
576
- currentState.internalAccounts.accounts
577
- );
578
- if (!currentState.internalAccounts.accounts[this.state.internalAccounts.selectedAccount]) {
579
- const lastSelectedAccount = _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _getLastSelectedAccount, getLastSelectedAccount_fn).call(this, existingAccounts);
580
- if (lastSelectedAccount) {
581
- currentState.internalAccounts.selectedAccount = lastSelectedAccount.id;
582
- currentState.internalAccounts.accounts[lastSelectedAccount.id].metadata.lastSelected = _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _getLastSelectedIndex, getLastSelectedIndex_fn).call(this);
583
- _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _publishAccountChangeEvent, publishAccountChangeEvent_fn).call(this, lastSelectedAccount);
584
- } else {
585
- currentState.internalAccounts.selectedAccount = "";
586
- }
587
- }
588
- });
589
- }
590
- };
591
- _handleOnSnapStateChange = new WeakSet();
592
- handleOnSnapStateChange_fn = function(snapState) {
593
- const { snaps } = snapState;
594
- const accounts = this.listMultichainAccounts().filter(
595
- (account) => account.metadata.snap
596
- );
597
- this.update((currentState) => {
598
- accounts.forEach((account) => {
599
- const currentAccount = currentState.internalAccounts.accounts[account.id];
600
- if (currentAccount.metadata.snap) {
601
- const snapId = currentAccount.metadata.snap.id;
602
- const storedSnap = snaps[snapId];
603
- if (storedSnap) {
604
- currentAccount.metadata.snap.enabled = storedSnap.enabled && !storedSnap.blocked;
605
- }
606
- }
607
- });
608
- });
609
- };
610
- _getAccountsByKeyringType = new WeakSet();
611
- getAccountsByKeyringType_fn = function(keyringType, accounts) {
612
- return (accounts ?? this.listMultichainAccounts()).filter(
613
- (internalAccount) => {
614
- if (keyringType === _keyringcontroller.KeyringTypes.hd || keyringType === _keyringcontroller.KeyringTypes.simple) {
615
- return internalAccount.metadata.keyring.type === _keyringcontroller.KeyringTypes.hd || internalAccount.metadata.keyring.type === _keyringcontroller.KeyringTypes.simple;
616
- }
617
- return internalAccount.metadata.keyring.type === keyringType;
618
- }
619
- );
620
- };
621
- _getLastSelectedAccount = new WeakSet();
622
- getLastSelectedAccount_fn = function(accounts) {
623
- const [accountToSelect] = accounts.sort((accountA, accountB) => {
624
- return (accountB.metadata.lastSelected ?? 0) - (accountA.metadata.lastSelected ?? 0);
625
- });
626
- return accountToSelect;
627
- };
628
- _isAccountCompatibleWithChain = new WeakSet();
629
- isAccountCompatibleWithChain_fn = function(account, chainId) {
630
- return account.type.startsWith(_utils.parseCaipChainId.call(void 0, chainId).namespace);
631
- };
632
- _getLastSelectedIndex = new WeakSet();
633
- getLastSelectedIndex_fn = function() {
634
- return Date.now();
635
- };
636
- _handleNewAccountAdded = new WeakSet();
637
- handleNewAccountAdded_fn = function(accountsState, account) {
638
- let newAccount;
639
- if (account.type !== _keyringcontroller.KeyringTypes.snap) {
640
- newAccount = _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn).call(this, account.address, account.type);
641
- } else {
642
- const [snapKeyring] = this.messagingSystem.call(
643
- "KeyringController:getKeyringsByType",
644
- _ethsnapkeyring.SnapKeyring.type
645
- );
646
- newAccount = snapKeyring.getAccountByAddress(
647
- account.address
648
- );
649
- if (!newAccount) {
650
- return accountsState;
651
- }
652
- }
653
- const isFirstAccount = Object.keys(accountsState).length === 0;
654
- const accountName = this.getNextAvailableAccountName(
655
- newAccount.metadata.keyring.type,
656
- Object.values(accountsState)
657
- );
658
- const newAccountWithUpdatedMetadata = {
659
- ...newAccount,
660
- metadata: {
661
- ...newAccount.metadata,
662
- name: accountName,
663
- importTime: Date.now(),
664
- lastSelected: isFirstAccount ? _chunkUJIPPGP6js.__privateMethod.call(void 0, this, _getLastSelectedIndex, getLastSelectedIndex_fn).call(this) : 0
665
- }
666
- };
667
- accountsState[newAccount.id] = newAccountWithUpdatedMetadata;
668
- this.messagingSystem.publish(
669
- "AccountsController:accountAdded",
670
- newAccountWithUpdatedMetadata
671
- );
672
- return accountsState;
673
- };
674
- _publishAccountChangeEvent = new WeakSet();
675
- publishAccountChangeEvent_fn = function(account) {
676
- if (_keyringapi.isEvmAccountType.call(void 0, account.type)) {
677
- this.messagingSystem.publish(
678
- "AccountsController:selectedEvmAccountChange",
679
- account
680
- );
681
- }
682
- this.messagingSystem.publish(
683
- "AccountsController:selectedAccountChange",
684
- account
685
- );
686
- };
687
- _handleAccountRemoved = new WeakSet();
688
- handleAccountRemoved_fn = function(accountsState, accountId) {
689
- delete accountsState[accountId];
690
- this.messagingSystem.publish(
691
- "AccountsController:accountRemoved",
692
- accountId
693
- );
694
- return accountsState;
695
- };
696
- _populateExistingMetadata = new WeakSet();
697
- populateExistingMetadata_fn = function(accountId, metadataKey, account) {
698
- const internalAccount = account ?? this.getAccount(accountId);
699
- return internalAccount ? internalAccount.metadata[metadataKey] : void 0;
700
- };
701
- _registerMessageHandlers = new WeakSet();
702
- registerMessageHandlers_fn = function() {
703
- this.messagingSystem.registerActionHandler(
704
- `${controllerName}:setSelectedAccount`,
705
- this.setSelectedAccount.bind(this)
706
- );
707
- this.messagingSystem.registerActionHandler(
708
- `${controllerName}:listAccounts`,
709
- this.listAccounts.bind(this)
710
- );
711
- this.messagingSystem.registerActionHandler(
712
- `${controllerName}:listMultichainAccounts`,
713
- this.listMultichainAccounts.bind(this)
714
- );
715
- this.messagingSystem.registerActionHandler(
716
- `${controllerName}:setAccountName`,
717
- this.setAccountName.bind(this)
718
- );
719
- this.messagingSystem.registerActionHandler(
720
- `${controllerName}:updateAccounts`,
721
- this.updateAccounts.bind(this)
722
- );
723
- this.messagingSystem.registerActionHandler(
724
- `${controllerName}:getSelectedAccount`,
725
- this.getSelectedAccount.bind(this)
726
- );
727
- this.messagingSystem.registerActionHandler(
728
- `${controllerName}:getSelectedMultichainAccount`,
729
- this.getSelectedMultichainAccount.bind(this)
730
- );
731
- this.messagingSystem.registerActionHandler(
732
- `${controllerName}:getAccountByAddress`,
733
- this.getAccountByAddress.bind(this)
734
- );
735
- this.messagingSystem.registerActionHandler(
736
- `${controllerName}:getNextAvailableAccountName`,
737
- this.getNextAvailableAccountName.bind(this)
738
- );
739
- this.messagingSystem.registerActionHandler(
740
- `AccountsController:getAccount`,
741
- this.getAccount.bind(this)
742
- );
743
- this.messagingSystem.registerActionHandler(
744
- `AccountsController:updateAccountMetadata`,
745
- this.updateAccountMetadata.bind(this)
746
- );
747
- };
748
-
749
-
750
-
751
-
752
- exports.EMPTY_ACCOUNT = EMPTY_ACCOUNT; exports.AccountsController = AccountsController;
753
- //# sourceMappingURL=chunk-5MTWAWAS.js.map