@metamask/accounts-controller 11.0.0 → 12.0.0

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.
@@ -0,0 +1,561 @@
1
+ import {
2
+ __privateAdd,
3
+ __privateMethod,
4
+ getUUIDFromAddressOfNormalAccount,
5
+ keyringTypeToName
6
+ } from "./chunk-HD5HN6GK.mjs";
7
+
8
+ // src/AccountsController.ts
9
+ import { toBuffer } from "@ethereumjs/util";
10
+ import { BaseController } from "@metamask/base-controller";
11
+ import { SnapKeyring } from "@metamask/eth-snap-keyring";
12
+ import { EthAccountType, EthMethod } from "@metamask/keyring-api";
13
+ import { KeyringTypes } from "@metamask/keyring-controller";
14
+ import { sha256 } from "ethereum-cryptography/sha256";
15
+ import { v4 as uuid } from "uuid";
16
+ var controllerName = "AccountsController";
17
+ var accountsControllerMetadata = {
18
+ internalAccounts: {
19
+ persist: true,
20
+ anonymous: false
21
+ }
22
+ };
23
+ var defaultState = {
24
+ internalAccounts: {
25
+ accounts: {},
26
+ selectedAccount: ""
27
+ }
28
+ };
29
+ var _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn, _listSnapAccounts, listSnapAccounts_fn, _listNormalAccounts, listNormalAccounts_fn, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn, _handleOnSnapStateChange, handleOnSnapStateChange_fn, _getNextAccountNumber, getNextAccountNumber_fn, _handleNewAccountAdded, handleNewAccountAdded_fn, _handleAccountRemoved, handleAccountRemoved_fn, _registerMessageHandlers, registerMessageHandlers_fn;
30
+ var AccountsController = class extends BaseController {
31
+ /**
32
+ * Constructor for AccountsController.
33
+ *
34
+ * @param options - The controller options.
35
+ * @param options.messenger - The messenger object.
36
+ * @param options.state - Initial state to set on this controller
37
+ */
38
+ constructor({
39
+ messenger,
40
+ state
41
+ }) {
42
+ super({
43
+ messenger,
44
+ name: controllerName,
45
+ metadata: accountsControllerMetadata,
46
+ state: {
47
+ ...defaultState,
48
+ ...state
49
+ }
50
+ });
51
+ /**
52
+ * Generates an internal account for a non-Snap account.
53
+ * @param address - The address of the account.
54
+ * @param type - The type of the account.
55
+ * @returns The generated internal account.
56
+ */
57
+ __privateAdd(this, _generateInternalAccountForNonSnapAccount);
58
+ /**
59
+ * Returns a list of internal accounts created using the SnapKeyring.
60
+ *
61
+ * @returns A promise that resolves to an array of InternalAccount objects.
62
+ */
63
+ __privateAdd(this, _listSnapAccounts);
64
+ /**
65
+ * Returns a list of normal accounts.
66
+ * Note: listNormalAccounts is a temporary method until the keyrings all implement the InternalAccount interface.
67
+ * Once all keyrings implement the InternalAccount interface, this method can be removed and getAccounts can be used instead.
68
+ *
69
+ * @returns A Promise that resolves to an array of InternalAccount objects.
70
+ */
71
+ __privateAdd(this, _listNormalAccounts);
72
+ /**
73
+ * Handles changes in the keyring state, specifically when new accounts are added or removed.
74
+ *
75
+ * @param keyringState - The new state of the keyring controller.
76
+ */
77
+ __privateAdd(this, _handleOnKeyringStateChange);
78
+ /**
79
+ * Handles the change in SnapControllerState by updating the metadata of accounts that have a snap enabled.
80
+ *
81
+ * @param snapState - The new SnapControllerState.
82
+ */
83
+ __privateAdd(this, _handleOnSnapStateChange);
84
+ /**
85
+ * Returns the next account number for a given keyring type.
86
+ * @param keyringType - The type of keyring.
87
+ * @returns An object containing the account prefix and index to use.
88
+ */
89
+ __privateAdd(this, _getNextAccountNumber);
90
+ /**
91
+ * Handles the addition of a new account to the controller.
92
+ * If the account is not a Snap Keyring account, generates an internal account for it and adds it to the controller.
93
+ * If the account is a Snap Keyring account, retrieves the account from the keyring and adds it to the controller.
94
+ * @param account - The address and keyring type object of the new account.
95
+ */
96
+ __privateAdd(this, _handleNewAccountAdded);
97
+ /**
98
+ * Handles the removal of an account from the internal accounts list.
99
+ * @param accountId - The ID of the account to be removed.
100
+ */
101
+ __privateAdd(this, _handleAccountRemoved);
102
+ /**
103
+ * Registers message handlers for the AccountsController.
104
+ * @private
105
+ */
106
+ __privateAdd(this, _registerMessageHandlers);
107
+ this.messagingSystem.subscribe(
108
+ "SnapController:stateChange",
109
+ (snapStateState) => __privateMethod(this, _handleOnSnapStateChange, handleOnSnapStateChange_fn).call(this, snapStateState)
110
+ );
111
+ this.messagingSystem.subscribe(
112
+ "KeyringController:stateChange",
113
+ (keyringState) => __privateMethod(this, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn).call(this, keyringState)
114
+ );
115
+ __privateMethod(this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
116
+ }
117
+ /**
118
+ * Returns the internal account object for the given account ID, if it exists.
119
+ *
120
+ * @param accountId - The ID of the account to retrieve.
121
+ * @returns The internal account object, or undefined if the account does not exist.
122
+ */
123
+ getAccount(accountId) {
124
+ return this.state.internalAccounts.accounts[accountId];
125
+ }
126
+ /**
127
+ * Returns an array of all internal accounts.
128
+ *
129
+ * @returns An array of InternalAccount objects.
130
+ */
131
+ listAccounts() {
132
+ return Object.values(this.state.internalAccounts.accounts);
133
+ }
134
+ /**
135
+ * Returns the internal account object for the given account ID.
136
+ *
137
+ * @param accountId - The ID of the account to retrieve.
138
+ * @returns The internal account object.
139
+ * @throws An error if the account ID is not found.
140
+ */
141
+ getAccountExpect(accountId) {
142
+ if (!accountId) {
143
+ return {
144
+ id: "",
145
+ address: "",
146
+ options: {},
147
+ methods: [],
148
+ type: EthAccountType.Eoa,
149
+ metadata: {
150
+ name: "",
151
+ keyring: {
152
+ type: ""
153
+ }
154
+ }
155
+ };
156
+ }
157
+ const account = this.getAccount(accountId);
158
+ if (account === void 0) {
159
+ throw new Error(`Account Id ${accountId} not found`);
160
+ }
161
+ return account;
162
+ }
163
+ /**
164
+ * Returns the selected internal account.
165
+ *
166
+ * @returns The selected internal account.
167
+ */
168
+ getSelectedAccount() {
169
+ return this.getAccountExpect(this.state.internalAccounts.selectedAccount);
170
+ }
171
+ /**
172
+ * Returns the account with the specified address.
173
+ * ! This method will only return the first account that matches the address
174
+ * @param address - The address of the account to retrieve.
175
+ * @returns The account with the specified address, or undefined if not found.
176
+ */
177
+ getAccountByAddress(address) {
178
+ return this.listAccounts().find(
179
+ (account) => account.address.toLowerCase() === address.toLowerCase()
180
+ );
181
+ }
182
+ /**
183
+ * Sets the selected account by its ID.
184
+ *
185
+ * @param accountId - The ID of the account to be selected.
186
+ */
187
+ setSelectedAccount(accountId) {
188
+ const account = this.getAccount(accountId);
189
+ this.update((currentState) => {
190
+ if (account) {
191
+ currentState.internalAccounts.accounts[account.id].metadata.lastSelected = Date.now();
192
+ currentState.internalAccounts.selectedAccount = account.id;
193
+ } else {
194
+ currentState.internalAccounts.selectedAccount = "";
195
+ }
196
+ });
197
+ if (account) {
198
+ this.messagingSystem.publish(
199
+ "AccountsController:selectedAccountChange",
200
+ account
201
+ );
202
+ }
203
+ }
204
+ /**
205
+ * Sets the name of the account with the given ID.
206
+ *
207
+ * @param accountId - The ID of the account to set the name for.
208
+ * @param accountName - The new name for the account.
209
+ * @throws An error if an account with the same name already exists.
210
+ */
211
+ setAccountName(accountId, accountName) {
212
+ const account = this.getAccountExpect(accountId);
213
+ if (this.listAccounts().find(
214
+ (internalAccount) => internalAccount.metadata.name === accountName && internalAccount.id !== accountId
215
+ )) {
216
+ throw new Error("Account name already exists");
217
+ }
218
+ this.update((currentState) => {
219
+ const internalAccount = {
220
+ ...account,
221
+ metadata: { ...account.metadata, name: accountName }
222
+ };
223
+ currentState.internalAccounts.accounts[accountId] = // @ts-expect-error Assigning a complex type `T` to `Draft<T>` causes an excessive type instantiation depth error.
224
+ internalAccount;
225
+ });
226
+ }
227
+ /**
228
+ * Updates the internal accounts list by retrieving normal and snap accounts,
229
+ * removing duplicates, and updating the metadata of each account.
230
+ *
231
+ * @returns A Promise that resolves when the accounts have been updated.
232
+ */
233
+ async updateAccounts() {
234
+ const snapAccounts = await __privateMethod(this, _listSnapAccounts, listSnapAccounts_fn).call(this);
235
+ const normalAccounts = (await __privateMethod(this, _listNormalAccounts, listNormalAccounts_fn).call(this)).filter(
236
+ (account) => !snapAccounts.find(
237
+ (snapAccount) => snapAccount.address === account.address
238
+ )
239
+ );
240
+ const keyringTypes = /* @__PURE__ */ new Map();
241
+ const previousAccounts = this.state.internalAccounts.accounts;
242
+ const accounts = [
243
+ ...normalAccounts,
244
+ ...snapAccounts
245
+ ].reduce((internalAccountMap, internalAccount) => {
246
+ const keyringTypeName = keyringTypeToName(
247
+ internalAccount.metadata.keyring.type
248
+ );
249
+ const keyringAccountIndex = keyringTypes.get(keyringTypeName) ?? 0;
250
+ if (keyringAccountIndex) {
251
+ keyringTypes.set(keyringTypeName, keyringAccountIndex + 1);
252
+ } else {
253
+ keyringTypes.set(keyringTypeName, 1);
254
+ }
255
+ const existingAccount = previousAccounts[internalAccount.id];
256
+ internalAccountMap[internalAccount.id] = {
257
+ ...internalAccount,
258
+ metadata: {
259
+ ...internalAccount.metadata,
260
+ name: existingAccount && existingAccount.metadata.name !== "" ? existingAccount.metadata.name : `${keyringTypeName} ${keyringAccountIndex + 1}`,
261
+ lastSelected: existingAccount?.metadata?.lastSelected
262
+ }
263
+ };
264
+ return internalAccountMap;
265
+ }, {});
266
+ this.update((currentState) => {
267
+ currentState.internalAccounts.accounts = accounts;
268
+ });
269
+ }
270
+ /**
271
+ * Loads the backup state of the accounts controller.
272
+ *
273
+ * @param backup - The backup state to load.
274
+ */
275
+ loadBackup(backup) {
276
+ if (backup.internalAccounts) {
277
+ this.update((currentState) => {
278
+ currentState.internalAccounts = backup.internalAccounts;
279
+ });
280
+ }
281
+ }
282
+ };
283
+ _generateInternalAccountForNonSnapAccount = new WeakSet();
284
+ generateInternalAccountForNonSnapAccount_fn = function(address, type) {
285
+ return {
286
+ id: getUUIDFromAddressOfNormalAccount(address),
287
+ address,
288
+ options: {},
289
+ methods: [
290
+ EthMethod.PersonalSign,
291
+ EthMethod.Sign,
292
+ EthMethod.SignTransaction,
293
+ EthMethod.SignTypedDataV1,
294
+ EthMethod.SignTypedDataV3,
295
+ EthMethod.SignTypedDataV4
296
+ ],
297
+ type: EthAccountType.Eoa,
298
+ metadata: {
299
+ name: "",
300
+ keyring: {
301
+ type
302
+ }
303
+ }
304
+ };
305
+ };
306
+ _listSnapAccounts = new WeakSet();
307
+ listSnapAccounts_fn = async function() {
308
+ const [snapKeyring] = this.messagingSystem.call(
309
+ "KeyringController:getKeyringsByType",
310
+ SnapKeyring.type
311
+ );
312
+ if (!snapKeyring) {
313
+ return [];
314
+ }
315
+ const snapAccounts = snapKeyring.listAccounts();
316
+ return snapAccounts;
317
+ };
318
+ _listNormalAccounts = new WeakSet();
319
+ listNormalAccounts_fn = async function() {
320
+ const addresses = await this.messagingSystem.call(
321
+ "KeyringController:getAccounts"
322
+ );
323
+ const internalAccounts = [];
324
+ for (const address of addresses) {
325
+ const keyring = await this.messagingSystem.call(
326
+ "KeyringController:getKeyringForAccount",
327
+ address
328
+ );
329
+ const v4options = {
330
+ random: sha256(toBuffer(address)).slice(0, 16)
331
+ };
332
+ internalAccounts.push({
333
+ id: uuid(v4options),
334
+ address,
335
+ options: {},
336
+ methods: [
337
+ EthMethod.PersonalSign,
338
+ EthMethod.Sign,
339
+ EthMethod.SignTransaction,
340
+ EthMethod.SignTypedDataV1,
341
+ EthMethod.SignTypedDataV3,
342
+ EthMethod.SignTypedDataV4
343
+ ],
344
+ type: EthAccountType.Eoa,
345
+ metadata: {
346
+ name: "",
347
+ keyring: {
348
+ type: keyring.type
349
+ }
350
+ }
351
+ });
352
+ }
353
+ return internalAccounts.filter(
354
+ (account) => account.metadata.keyring.type !== KeyringTypes.snap
355
+ );
356
+ };
357
+ _handleOnKeyringStateChange = new WeakSet();
358
+ handleOnKeyringStateChange_fn = function(keyringState) {
359
+ if (keyringState.isUnlocked && keyringState.keyrings.length > 0) {
360
+ const updatedNormalKeyringAddresses = [];
361
+ const updatedSnapKeyringAddresses = [];
362
+ for (const keyring of keyringState.keyrings) {
363
+ if (keyring.type === KeyringTypes.snap) {
364
+ updatedSnapKeyringAddresses.push(
365
+ ...keyring.accounts.map((address) => {
366
+ return {
367
+ address,
368
+ type: keyring.type
369
+ };
370
+ })
371
+ );
372
+ } else {
373
+ updatedNormalKeyringAddresses.push(
374
+ ...keyring.accounts.map((address) => {
375
+ return {
376
+ address,
377
+ type: keyring.type
378
+ };
379
+ })
380
+ );
381
+ }
382
+ }
383
+ const { previousNormalInternalAccounts, previousSnapInternalAccounts } = this.listAccounts().reduce(
384
+ (accumulator, account) => {
385
+ if (account.metadata.keyring.type === KeyringTypes.snap) {
386
+ accumulator.previousSnapInternalAccounts.push(account);
387
+ } else {
388
+ accumulator.previousNormalInternalAccounts.push(account);
389
+ }
390
+ return accumulator;
391
+ },
392
+ {
393
+ previousNormalInternalAccounts: [],
394
+ previousSnapInternalAccounts: []
395
+ }
396
+ );
397
+ const addedAccounts = [];
398
+ const deletedAccounts = [];
399
+ for (const account of updatedNormalKeyringAddresses) {
400
+ if (!this.state.internalAccounts.accounts[getUUIDFromAddressOfNormalAccount(account.address)]) {
401
+ addedAccounts.push(account);
402
+ }
403
+ }
404
+ for (const account of updatedSnapKeyringAddresses) {
405
+ if (!previousSnapInternalAccounts.find(
406
+ (internalAccount) => internalAccount.address.toLowerCase() === account.address.toLowerCase()
407
+ )) {
408
+ addedAccounts.push(account);
409
+ }
410
+ }
411
+ for (const account of previousNormalInternalAccounts) {
412
+ if (!updatedNormalKeyringAddresses.find(
413
+ ({ address }) => address.toLowerCase() === account.address.toLowerCase()
414
+ )) {
415
+ deletedAccounts.push(account);
416
+ }
417
+ }
418
+ for (const account of previousSnapInternalAccounts) {
419
+ if (!updatedSnapKeyringAddresses.find(
420
+ ({ address }) => address.toLowerCase() === account.address.toLowerCase()
421
+ )) {
422
+ deletedAccounts.push(account);
423
+ }
424
+ }
425
+ if (deletedAccounts.length > 0) {
426
+ for (const account of deletedAccounts) {
427
+ __privateMethod(this, _handleAccountRemoved, handleAccountRemoved_fn).call(this, account.id);
428
+ }
429
+ }
430
+ if (addedAccounts.length > 0) {
431
+ for (const account of addedAccounts) {
432
+ __privateMethod(this, _handleNewAccountAdded, handleNewAccountAdded_fn).call(this, account);
433
+ }
434
+ }
435
+ if (!this.getAccount(this.state.internalAccounts.selectedAccount)) {
436
+ const [accountToSelect] = this.listAccounts().sort(
437
+ (accountA, accountB) => {
438
+ return (accountB.metadata.lastSelected ?? 0) - (accountA.metadata.lastSelected ?? 0);
439
+ }
440
+ );
441
+ this.setSelectedAccount(accountToSelect?.id);
442
+ }
443
+ }
444
+ };
445
+ _handleOnSnapStateChange = new WeakSet();
446
+ handleOnSnapStateChange_fn = function(snapState) {
447
+ const { snaps } = snapState;
448
+ const accounts = this.listAccounts().filter(
449
+ (account) => account.metadata.snap
450
+ );
451
+ this.update((currentState) => {
452
+ accounts.forEach((account) => {
453
+ const currentAccount = currentState.internalAccounts.accounts[account.id];
454
+ if (currentAccount.metadata.snap) {
455
+ const snapId = currentAccount.metadata.snap.id;
456
+ const storedSnap = snaps[snapId];
457
+ if (storedSnap) {
458
+ currentAccount.metadata.snap.enabled = storedSnap.enabled && !storedSnap.blocked;
459
+ }
460
+ }
461
+ });
462
+ });
463
+ };
464
+ _getNextAccountNumber = new WeakSet();
465
+ getNextAccountNumber_fn = function(keyringType) {
466
+ const keyringName = keyringTypeToName(keyringType);
467
+ const previousKeyringAccounts = this.listAccounts().filter(
468
+ (internalAccount) => {
469
+ if (keyringType === KeyringTypes.hd || keyringType === KeyringTypes.simple) {
470
+ return internalAccount.metadata.keyring.type === KeyringTypes.hd || internalAccount.metadata.keyring.type === KeyringTypes.simple;
471
+ }
472
+ return internalAccount.metadata.keyring.type === keyringType;
473
+ }
474
+ );
475
+ const lastDefaultIndexUsedForKeyringType = previousKeyringAccounts.filter(
476
+ (internalAccount) => new RegExp(`${keyringName} \\d+$`, "u").test(
477
+ internalAccount.metadata.name
478
+ )
479
+ ).map((internalAccount) => {
480
+ const nameToWords = internalAccount.metadata.name.split(" ");
481
+ return parseInt(nameToWords[nameToWords.length], 10);
482
+ }).sort((a, b) => b - a)[0] || 0;
483
+ const indexToUse = Math.max(
484
+ previousKeyringAccounts.length + 1,
485
+ lastDefaultIndexUsedForKeyringType + 1
486
+ );
487
+ return { accountPrefix: keyringName, indexToUse };
488
+ };
489
+ _handleNewAccountAdded = new WeakSet();
490
+ handleNewAccountAdded_fn = function(account) {
491
+ let newAccount;
492
+ if (account.type !== KeyringTypes.snap) {
493
+ newAccount = __privateMethod(this, _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn).call(this, account.address, account.type);
494
+ } else {
495
+ const [snapKeyring] = this.messagingSystem.call(
496
+ "KeyringController:getKeyringsByType",
497
+ SnapKeyring.type
498
+ );
499
+ newAccount = snapKeyring.getAccountByAddress(
500
+ account.address
501
+ );
502
+ if (!newAccount) {
503
+ return;
504
+ }
505
+ }
506
+ const { accountPrefix, indexToUse } = __privateMethod(this, _getNextAccountNumber, getNextAccountNumber_fn).call(this, newAccount.metadata.keyring.type);
507
+ const accountName = `${accountPrefix} ${indexToUse}`;
508
+ this.update((currentState) => {
509
+ currentState.internalAccounts.accounts[newAccount.id] = {
510
+ ...newAccount,
511
+ metadata: {
512
+ ...newAccount.metadata,
513
+ name: accountName,
514
+ lastSelected: Date.now()
515
+ }
516
+ };
517
+ });
518
+ this.setSelectedAccount(newAccount.id);
519
+ };
520
+ _handleAccountRemoved = new WeakSet();
521
+ handleAccountRemoved_fn = function(accountId) {
522
+ this.update((currentState) => {
523
+ delete currentState.internalAccounts.accounts[accountId];
524
+ });
525
+ };
526
+ _registerMessageHandlers = new WeakSet();
527
+ registerMessageHandlers_fn = function() {
528
+ this.messagingSystem.registerActionHandler(
529
+ `${controllerName}:setSelectedAccount`,
530
+ this.setSelectedAccount.bind(this)
531
+ );
532
+ this.messagingSystem.registerActionHandler(
533
+ `${controllerName}:listAccounts`,
534
+ this.listAccounts.bind(this)
535
+ );
536
+ this.messagingSystem.registerActionHandler(
537
+ `${controllerName}:setAccountName`,
538
+ this.setAccountName.bind(this)
539
+ );
540
+ this.messagingSystem.registerActionHandler(
541
+ `${controllerName}:updateAccounts`,
542
+ this.updateAccounts.bind(this)
543
+ );
544
+ this.messagingSystem.registerActionHandler(
545
+ `${controllerName}:getSelectedAccount`,
546
+ this.getSelectedAccount.bind(this)
547
+ );
548
+ this.messagingSystem.registerActionHandler(
549
+ `${controllerName}:getAccountByAddress`,
550
+ this.getAccountByAddress.bind(this)
551
+ );
552
+ this.messagingSystem.registerActionHandler(
553
+ `AccountsController:getAccount`,
554
+ this.getAccount.bind(this)
555
+ );
556
+ };
557
+
558
+ export {
559
+ AccountsController
560
+ };
561
+ //# sourceMappingURL=chunk-GATPL76V.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/AccountsController.ts"],"sourcesContent":["import { toBuffer } from '@ethereumjs/util';\nimport type {\n ControllerGetStateAction,\n ControllerStateChangeEvent,\n RestrictedControllerMessenger,\n} from '@metamask/base-controller';\nimport { BaseController } from '@metamask/base-controller';\nimport { SnapKeyring } from '@metamask/eth-snap-keyring';\nimport type { InternalAccount } from '@metamask/keyring-api';\nimport { EthAccountType, EthMethod } from '@metamask/keyring-api';\nimport { KeyringTypes } from '@metamask/keyring-controller';\nimport type {\n KeyringControllerState,\n KeyringControllerGetKeyringForAccountAction,\n KeyringControllerGetKeyringsByTypeAction,\n KeyringControllerGetAccountsAction,\n KeyringControllerStateChangeEvent,\n} from '@metamask/keyring-controller';\nimport type {\n SnapControllerState,\n SnapStateChange,\n} from '@metamask/snaps-controllers';\nimport type { SnapId } from '@metamask/snaps-sdk';\nimport type { Snap } from '@metamask/snaps-utils';\nimport type { Keyring, Json } from '@metamask/utils';\nimport { sha256 } from 'ethereum-cryptography/sha256';\nimport type { Draft } from 'immer';\nimport { v4 as uuid } from 'uuid';\n\nimport { getUUIDFromAddressOfNormalAccount, keyringTypeToName } from './utils';\n\nconst controllerName = 'AccountsController';\n\nexport type AccountsControllerState = {\n internalAccounts: {\n accounts: Record<string, InternalAccount>;\n selectedAccount: string; // id of the selected account\n };\n};\n\nexport type AccountsControllerGetStateAction = ControllerGetStateAction<\n typeof controllerName,\n AccountsControllerState\n>;\n\nexport type AccountsControllerSetSelectedAccountAction = {\n type: `${typeof controllerName}:setSelectedAccount`;\n handler: AccountsController['setSelectedAccount'];\n};\n\nexport type AccountsControllerSetAccountNameAction = {\n type: `${typeof controllerName}:setAccountName`;\n handler: AccountsController['setAccountName'];\n};\n\nexport type AccountsControllerListAccountsAction = {\n type: `${typeof controllerName}:listAccounts`;\n handler: AccountsController['listAccounts'];\n};\n\nexport type AccountsControllerUpdateAccountsAction = {\n type: `${typeof controllerName}:updateAccounts`;\n handler: AccountsController['updateAccounts'];\n};\n\nexport type AccountsControllerGetSelectedAccountAction = {\n type: `${typeof controllerName}:getSelectedAccount`;\n handler: AccountsController['getSelectedAccount'];\n};\n\nexport type AccountsControllerGetAccountByAddressAction = {\n type: `${typeof controllerName}:getAccountByAddress`;\n handler: AccountsController['getAccountByAddress'];\n};\n\nexport type AccountsControllerGetAccountAction = {\n type: `${typeof controllerName}:getAccount`;\n handler: AccountsController['getAccount'];\n};\n\nexport type AllowedActions =\n | KeyringControllerGetKeyringForAccountAction\n | KeyringControllerGetKeyringsByTypeAction\n | KeyringControllerGetAccountsAction;\n\nexport type AccountsControllerActions =\n | AccountsControllerGetStateAction\n | AccountsControllerSetSelectedAccountAction\n | AccountsControllerListAccountsAction\n | AccountsControllerSetAccountNameAction\n | AccountsControllerUpdateAccountsAction\n | AccountsControllerGetAccountByAddressAction\n | AccountsControllerGetSelectedAccountAction\n | AccountsControllerGetAccountAction;\n\nexport type AccountsControllerChangeEvent = ControllerStateChangeEvent<\n typeof controllerName,\n AccountsControllerState\n>;\n\nexport type AccountsControllerSelectedAccountChangeEvent = {\n type: `${typeof controllerName}:selectedAccountChange`;\n payload: [InternalAccount];\n};\n\nexport type AllowedEvents = SnapStateChange | KeyringControllerStateChangeEvent;\n\nexport type AccountsControllerEvents =\n | AccountsControllerChangeEvent\n | AccountsControllerSelectedAccountChangeEvent;\n\nexport type AccountsControllerMessenger = RestrictedControllerMessenger<\n typeof controllerName,\n AccountsControllerActions | AllowedActions,\n AccountsControllerEvents | AllowedEvents,\n AllowedActions['type'],\n AllowedEvents['type']\n>;\n\ntype AddressAndKeyringTypeObject = {\n address: string;\n type: string;\n};\n\nconst accountsControllerMetadata = {\n internalAccounts: {\n persist: true,\n anonymous: false,\n },\n};\n\nconst defaultState: AccountsControllerState = {\n internalAccounts: {\n accounts: {},\n selectedAccount: '',\n },\n};\n\n/**\n * Controller that manages internal accounts.\n * The accounts controller is responsible for creating and managing internal accounts.\n * It also provides convenience methods for accessing and updating the internal accounts.\n * The accounts controller also listens for keyring state changes and updates the internal accounts accordingly.\n * The accounts controller also listens for snap state changes and updates the internal accounts accordingly.\n *\n */\nexport class AccountsController extends BaseController<\n typeof controllerName,\n AccountsControllerState,\n AccountsControllerMessenger\n> {\n /**\n * Constructor for AccountsController.\n *\n * @param options - The controller options.\n * @param options.messenger - The messenger object.\n * @param options.state - Initial state to set on this controller\n */\n constructor({\n messenger,\n state,\n }: {\n messenger: AccountsControllerMessenger;\n state: AccountsControllerState;\n }) {\n super({\n messenger,\n name: controllerName,\n metadata: accountsControllerMetadata,\n state: {\n ...defaultState,\n ...state,\n },\n });\n\n this.messagingSystem.subscribe(\n 'SnapController:stateChange',\n (snapStateState) => this.#handleOnSnapStateChange(snapStateState),\n );\n\n this.messagingSystem.subscribe(\n 'KeyringController:stateChange',\n (keyringState) => this.#handleOnKeyringStateChange(keyringState),\n );\n\n this.#registerMessageHandlers();\n }\n\n /**\n * Returns the internal account object for the given account ID, if it exists.\n *\n * @param accountId - The ID of the account to retrieve.\n * @returns The internal account object, or undefined if the account does not exist.\n */\n getAccount(accountId: string): InternalAccount | undefined {\n return this.state.internalAccounts.accounts[accountId];\n }\n\n /**\n * Returns an array of all internal accounts.\n *\n * @returns An array of InternalAccount objects.\n */\n listAccounts(): InternalAccount[] {\n return Object.values(this.state.internalAccounts.accounts);\n }\n\n /**\n * Returns the internal account object for the given account ID.\n *\n * @param accountId - The ID of the account to retrieve.\n * @returns The internal account object.\n * @throws An error if the account ID is not found.\n */\n getAccountExpect(accountId: string): InternalAccount {\n // Edge case where the extension is setup but the srp is not yet created\n // certain ui elements will query the selected address before any accounts are created.\n if (!accountId) {\n return {\n id: '',\n address: '',\n options: {},\n methods: [],\n type: EthAccountType.Eoa,\n metadata: {\n name: '',\n keyring: {\n type: '',\n },\n },\n };\n }\n\n const account = this.getAccount(accountId);\n if (account === undefined) {\n throw new Error(`Account Id ${accountId} not found`);\n }\n return account;\n }\n\n /**\n * Returns the selected internal account.\n *\n * @returns The selected internal account.\n */\n getSelectedAccount(): InternalAccount {\n return this.getAccountExpect(this.state.internalAccounts.selectedAccount);\n }\n\n /**\n * Returns the account with the specified address.\n * ! This method will only return the first account that matches the address\n * @param address - The address of the account to retrieve.\n * @returns The account with the specified address, or undefined if not found.\n */\n getAccountByAddress(address: string): InternalAccount | undefined {\n return this.listAccounts().find(\n (account) => account.address.toLowerCase() === address.toLowerCase(),\n );\n }\n\n /**\n * Sets the selected account by its ID.\n *\n * @param accountId - The ID of the account to be selected.\n */\n setSelectedAccount(accountId: string): void {\n const account = this.getAccount(accountId);\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n if (account) {\n currentState.internalAccounts.accounts[\n account.id\n ].metadata.lastSelected = Date.now();\n currentState.internalAccounts.selectedAccount = account.id;\n } else {\n currentState.internalAccounts.selectedAccount = '';\n }\n });\n\n if (account) {\n this.messagingSystem.publish(\n 'AccountsController:selectedAccountChange',\n account,\n );\n }\n }\n\n /**\n * Sets the name of the account with the given ID.\n *\n * @param accountId - The ID of the account to set the name for.\n * @param accountName - The new name for the account.\n * @throws An error if an account with the same name already exists.\n */\n setAccountName(accountId: string, accountName: string): void {\n const account = this.getAccountExpect(accountId);\n\n if (\n this.listAccounts().find(\n (internalAccount) =>\n internalAccount.metadata.name === accountName &&\n internalAccount.id !== accountId,\n )\n ) {\n throw new Error('Account name already exists');\n }\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n const internalAccount = {\n ...account,\n metadata: { ...account.metadata, name: accountName },\n };\n currentState.internalAccounts.accounts[accountId] =\n // @ts-expect-error Assigning a complex type `T` to `Draft<T>` causes an excessive type instantiation depth error.\n internalAccount as Draft<InternalAccount>;\n });\n }\n\n /**\n * Updates the internal accounts list by retrieving normal and snap accounts,\n * removing duplicates, and updating the metadata of each account.\n *\n * @returns A Promise that resolves when the accounts have been updated.\n */\n async updateAccounts(): Promise<void> {\n const snapAccounts: InternalAccount[] = await this.#listSnapAccounts();\n const normalAccounts = (await this.#listNormalAccounts()).filter(\n (account) =>\n !snapAccounts.find(\n (snapAccount) => snapAccount.address === account.address,\n ),\n );\n\n // keyring type map.\n const keyringTypes = new Map<string, number>();\n const previousAccounts = this.state.internalAccounts.accounts;\n\n const accounts: Record<string, InternalAccount> = [\n ...normalAccounts,\n ...snapAccounts,\n ].reduce((internalAccountMap, internalAccount) => {\n const keyringTypeName = keyringTypeToName(\n internalAccount.metadata.keyring.type,\n );\n const keyringAccountIndex = keyringTypes.get(keyringTypeName) ?? 0;\n if (keyringAccountIndex) {\n keyringTypes.set(keyringTypeName, keyringAccountIndex + 1);\n } else {\n keyringTypes.set(keyringTypeName, 1);\n }\n\n const existingAccount = previousAccounts[internalAccount.id];\n\n internalAccountMap[internalAccount.id] = {\n ...internalAccount,\n\n metadata: {\n ...internalAccount.metadata,\n name:\n existingAccount && existingAccount.metadata.name !== ''\n ? existingAccount.metadata.name\n : `${keyringTypeName} ${keyringAccountIndex + 1}`,\n lastSelected: existingAccount?.metadata?.lastSelected,\n },\n };\n\n return internalAccountMap;\n }, {} as Record<string, InternalAccount>);\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n (currentState as AccountsControllerState).internalAccounts.accounts =\n accounts;\n });\n }\n\n /**\n * Loads the backup state of the accounts controller.\n *\n * @param backup - The backup state to load.\n */\n loadBackup(backup: AccountsControllerState): void {\n if (backup.internalAccounts) {\n this.update((currentState: Draft<AccountsControllerState>) => {\n (currentState as AccountsControllerState).internalAccounts =\n backup.internalAccounts;\n });\n }\n }\n\n /**\n * Generates an internal account for a non-Snap account.\n * @param address - The address of the account.\n * @param type - The type of the account.\n * @returns The generated internal account.\n */\n #generateInternalAccountForNonSnapAccount(\n address: string,\n type: string,\n ): InternalAccount {\n return {\n id: getUUIDFromAddressOfNormalAccount(address),\n address,\n options: {},\n methods: [\n EthMethod.PersonalSign,\n EthMethod.Sign,\n EthMethod.SignTransaction,\n EthMethod.SignTypedDataV1,\n EthMethod.SignTypedDataV3,\n EthMethod.SignTypedDataV4,\n ],\n type: EthAccountType.Eoa,\n metadata: {\n name: '',\n keyring: {\n type,\n },\n },\n };\n }\n\n /**\n * Returns a list of internal accounts created using the SnapKeyring.\n *\n * @returns A promise that resolves to an array of InternalAccount objects.\n */\n async #listSnapAccounts(): Promise<InternalAccount[]> {\n const [snapKeyring] = this.messagingSystem.call(\n 'KeyringController:getKeyringsByType',\n SnapKeyring.type,\n );\n // snap keyring is not available until the first account is created in the keyring controller\n if (!snapKeyring) {\n return [];\n }\n\n const snapAccounts = (snapKeyring as SnapKeyring).listAccounts();\n\n return snapAccounts;\n }\n\n /**\n * Returns a list of normal accounts.\n * Note: listNormalAccounts is a temporary method until the keyrings all implement the InternalAccount interface.\n * Once all keyrings implement the InternalAccount interface, this method can be removed and getAccounts can be used instead.\n *\n * @returns A Promise that resolves to an array of InternalAccount objects.\n */\n async #listNormalAccounts(): Promise<InternalAccount[]> {\n const addresses = await this.messagingSystem.call(\n 'KeyringController:getAccounts',\n );\n const internalAccounts: InternalAccount[] = [];\n for (const address of addresses) {\n const keyring = await this.messagingSystem.call(\n 'KeyringController:getKeyringForAccount',\n address,\n );\n const v4options = {\n random: sha256(toBuffer(address)).slice(0, 16),\n };\n\n internalAccounts.push({\n id: uuid(v4options),\n address,\n options: {},\n methods: [\n EthMethod.PersonalSign,\n EthMethod.Sign,\n EthMethod.SignTransaction,\n EthMethod.SignTypedDataV1,\n EthMethod.SignTypedDataV3,\n EthMethod.SignTypedDataV4,\n ],\n type: EthAccountType.Eoa,\n metadata: {\n name: '',\n keyring: {\n type: (keyring as Keyring<Json>).type,\n },\n },\n });\n }\n\n return internalAccounts.filter(\n (account) => account.metadata.keyring.type !== KeyringTypes.snap,\n );\n }\n\n /**\n * Handles changes in the keyring state, specifically when new accounts are added or removed.\n *\n * @param keyringState - The new state of the keyring controller.\n */\n #handleOnKeyringStateChange(keyringState: KeyringControllerState): void {\n // check if there are any new accounts added\n // TODO: change when accountAdded event is added to the keyring controller\n\n // We check for keyrings length to be greater than 0 because the extension client may try execute\n // submit password twice and clear the keyring state.\n // https://github.com/MetaMask/KeyringController/blob/2d73a4deed8d013913f6ef0c9f5c0bb7c614f7d3/src/KeyringController.ts#L910\n if (keyringState.isUnlocked && keyringState.keyrings.length > 0) {\n const updatedNormalKeyringAddresses: AddressAndKeyringTypeObject[] = [];\n const updatedSnapKeyringAddresses: AddressAndKeyringTypeObject[] = [];\n\n for (const keyring of keyringState.keyrings) {\n if (keyring.type === KeyringTypes.snap) {\n updatedSnapKeyringAddresses.push(\n ...keyring.accounts.map((address) => {\n return {\n address,\n type: keyring.type,\n };\n }),\n );\n } else {\n updatedNormalKeyringAddresses.push(\n ...keyring.accounts.map((address) => {\n return {\n address,\n type: keyring.type,\n };\n }),\n );\n }\n }\n\n const { previousNormalInternalAccounts, previousSnapInternalAccounts } =\n this.listAccounts().reduce(\n (accumulator, account) => {\n if (account.metadata.keyring.type === KeyringTypes.snap) {\n accumulator.previousSnapInternalAccounts.push(account);\n } else {\n accumulator.previousNormalInternalAccounts.push(account);\n }\n return accumulator;\n },\n {\n previousNormalInternalAccounts: [] as InternalAccount[],\n previousSnapInternalAccounts: [] as InternalAccount[],\n },\n );\n\n const addedAccounts: AddressAndKeyringTypeObject[] = [];\n const deletedAccounts: InternalAccount[] = [];\n\n // snap account ids are random uuid while normal accounts\n // are determininistic based on the address\n\n // ^NOTE: This will be removed when normal accounts also implement internal accounts\n // finding all the normal accounts that were added\n for (const account of updatedNormalKeyringAddresses) {\n if (\n !this.state.internalAccounts.accounts[\n getUUIDFromAddressOfNormalAccount(account.address)\n ]\n ) {\n addedAccounts.push(account);\n }\n }\n\n // finding all the snap accounts that were added\n for (const account of updatedSnapKeyringAddresses) {\n if (\n !previousSnapInternalAccounts.find(\n (internalAccount) =>\n internalAccount.address.toLowerCase() ===\n account.address.toLowerCase(),\n )\n ) {\n addedAccounts.push(account);\n }\n }\n\n // finding all the normal accounts that were deleted\n for (const account of previousNormalInternalAccounts) {\n if (\n !updatedNormalKeyringAddresses.find(\n ({ address }) =>\n address.toLowerCase() === account.address.toLowerCase(),\n )\n ) {\n deletedAccounts.push(account);\n }\n }\n\n // finding all the snap accounts that were deleted\n for (const account of previousSnapInternalAccounts) {\n if (\n !updatedSnapKeyringAddresses.find(\n ({ address }) =>\n address.toLowerCase() === account.address.toLowerCase(),\n )\n ) {\n deletedAccounts.push(account);\n }\n }\n\n if (deletedAccounts.length > 0) {\n for (const account of deletedAccounts) {\n this.#handleAccountRemoved(account.id);\n }\n }\n\n if (addedAccounts.length > 0) {\n for (const account of addedAccounts) {\n this.#handleNewAccountAdded(account);\n }\n }\n\n // handle if the selected account was deleted\n if (!this.getAccount(this.state.internalAccounts.selectedAccount)) {\n const [accountToSelect] = this.listAccounts().sort(\n (accountA, accountB) => {\n // sort by lastSelected descending\n return (\n (accountB.metadata.lastSelected ?? 0) -\n (accountA.metadata.lastSelected ?? 0)\n );\n },\n );\n\n // if the accountToSelect is undefined, then there are no accounts\n // it mean the keyring was reinitialized.\n this.setSelectedAccount(accountToSelect?.id);\n }\n }\n }\n\n /**\n * Handles the change in SnapControllerState by updating the metadata of accounts that have a snap enabled.\n *\n * @param snapState - The new SnapControllerState.\n */\n #handleOnSnapStateChange(snapState: SnapControllerState) {\n // only check if snaps changed in status\n const { snaps } = snapState;\n const accounts = this.listAccounts().filter(\n (account) => account.metadata.snap,\n );\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n accounts.forEach((account) => {\n const currentAccount =\n currentState.internalAccounts.accounts[account.id];\n if (currentAccount.metadata.snap) {\n const snapId = currentAccount.metadata.snap.id;\n const storedSnap: Snap = snaps[snapId as SnapId];\n if (storedSnap) {\n currentAccount.metadata.snap.enabled =\n storedSnap.enabled && !storedSnap.blocked;\n }\n }\n });\n });\n }\n\n /**\n * Returns the next account number for a given keyring type.\n * @param keyringType - The type of keyring.\n * @returns An object containing the account prefix and index to use.\n */\n #getNextAccountNumber(keyringType: string): {\n accountPrefix: string;\n indexToUse: number;\n } {\n const keyringName = keyringTypeToName(keyringType);\n const previousKeyringAccounts = this.listAccounts().filter(\n (internalAccount) => {\n if (\n keyringType === KeyringTypes.hd ||\n keyringType === KeyringTypes.simple\n ) {\n return (\n internalAccount.metadata.keyring.type === KeyringTypes.hd ||\n internalAccount.metadata.keyring.type === KeyringTypes.simple\n );\n }\n return internalAccount.metadata.keyring.type === keyringType;\n },\n );\n const lastDefaultIndexUsedForKeyringType =\n previousKeyringAccounts\n .filter((internalAccount) =>\n new RegExp(`${keyringName} \\\\d+$`, 'u').test(\n internalAccount.metadata.name,\n ),\n )\n .map((internalAccount) => {\n const nameToWords = internalAccount.metadata.name.split(' '); // get the index of a default account name\n return parseInt(nameToWords[nameToWords.length], 10);\n })\n .sort((a, b) => b - a)[0] || 0;\n\n const indexToUse = Math.max(\n previousKeyringAccounts.length + 1,\n lastDefaultIndexUsedForKeyringType + 1,\n );\n\n return { accountPrefix: keyringName, indexToUse };\n }\n\n /**\n * Handles the addition of a new account to the controller.\n * If the account is not a Snap Keyring account, generates an internal account for it and adds it to the controller.\n * If the account is a Snap Keyring account, retrieves the account from the keyring and adds it to the controller.\n * @param account - The address and keyring type object of the new account.\n */\n #handleNewAccountAdded(account: AddressAndKeyringTypeObject) {\n let newAccount: InternalAccount;\n if (account.type !== KeyringTypes.snap) {\n newAccount = this.#generateInternalAccountForNonSnapAccount(\n account.address,\n account.type,\n );\n } else {\n const [snapKeyring] = this.messagingSystem.call(\n 'KeyringController:getKeyringsByType',\n SnapKeyring.type,\n );\n\n newAccount = (snapKeyring as SnapKeyring).getAccountByAddress(\n account.address,\n ) as InternalAccount;\n\n // The snap deleted the account before the keyring controller could add it\n if (!newAccount) {\n return;\n }\n }\n\n // get next index number for the keyring type\n const { accountPrefix, indexToUse } = this.#getNextAccountNumber(\n newAccount.metadata.keyring.type,\n );\n\n const accountName = `${accountPrefix} ${indexToUse}`;\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n (currentState as AccountsControllerState).internalAccounts.accounts[\n newAccount.id\n ] = {\n ...newAccount,\n metadata: {\n ...newAccount.metadata,\n name: accountName,\n lastSelected: Date.now(),\n },\n };\n });\n\n this.setSelectedAccount(newAccount.id);\n }\n\n /**\n * Handles the removal of an account from the internal accounts list.\n * @param accountId - The ID of the account to be removed.\n */\n #handleAccountRemoved(accountId: string) {\n this.update((currentState: Draft<AccountsControllerState>) => {\n delete currentState.internalAccounts.accounts[accountId];\n });\n }\n\n /**\n * Registers message handlers for the AccountsController.\n * @private\n */\n #registerMessageHandlers() {\n this.messagingSystem.registerActionHandler(\n `${controllerName}:setSelectedAccount`,\n this.setSelectedAccount.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:listAccounts`,\n this.listAccounts.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:setAccountName`,\n this.setAccountName.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:updateAccounts`,\n this.updateAccounts.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getSelectedAccount`,\n this.getSelectedAccount.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `${controllerName}:getAccountByAddress`,\n this.getAccountByAddress.bind(this),\n );\n\n this.messagingSystem.registerActionHandler(\n `AccountsController:getAccount`,\n this.getAccount.bind(this),\n );\n }\n}\n"],"mappings":";;;;;;;;AAAA,SAAS,gBAAgB;AAMzB,SAAS,s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