@metamask/accounts-controller 12.0.0 → 13.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [13.0.0]
11
+
12
+ ### Changed
13
+
14
+ - Fix update setSelectedAccount to throw if the id is not found ([#4167](https://github.com/MetaMask/core/pull/4167))
15
+ - Fix normal account indexing naming with index gap ([#4089](https://github.com/MetaMask/core/pull/4089))
16
+ - **BREAKING** Bump peer dependency `@metamask/snaps-controllers` to `^6.0.3` and dependencies `@metamask/snaps-sdk` to `^3.1.1`, `@metamask/eth-snap-keyring` to `^3.0.0`([#4090](https://github.com/MetaMask/core/pull/4090))
17
+
18
+ ## [12.0.1]
19
+
20
+ ### Fixed
21
+
22
+ - Fix `types` field in `package.json` ([#4047](https://github.com/MetaMask/core/pull/4047))
23
+
10
24
  ## [12.0.0]
11
25
 
12
26
  ### Added
@@ -149,7 +163,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
149
163
 
150
164
  - Initial release ([#1637](https://github.com/MetaMask/core/pull/1637))
151
165
 
152
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@12.0.0...HEAD
166
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@13.0.0...HEAD
167
+ [13.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@12.0.1...@metamask/accounts-controller@13.0.0
168
+ [12.0.1]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@12.0.0...@metamask/accounts-controller@12.0.1
153
169
  [12.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@11.0.0...@metamask/accounts-controller@12.0.0
154
170
  [11.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@10.0.0...@metamask/accounts-controller@11.0.0
155
171
  [10.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@9.0.0...@metamask/accounts-controller@10.0.0
@@ -1,8 +1,8 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
2
 
3
- var _chunk7FX4HSTVjs = require('./chunk-7FX4HSTV.js');
4
- require('./chunk-MF4BFCSU.js');
3
+ var _chunkHS62N4JHjs = require('./chunk-HS62N4JH.js');
4
+ require('./chunk-KCST55AQ.js');
5
5
 
6
6
 
7
- exports.AccountsController = _chunk7FX4HSTVjs.AccountsController;
7
+ exports.AccountsController = _chunkHS62N4JHjs.AccountsController;
8
8
  //# sourceMappingURL=AccountsController.js.map
@@ -1,7 +1,7 @@
1
1
  import {
2
2
  AccountsController
3
- } from "./chunk-GATPL76V.mjs";
4
- import "./chunk-HD5HN6GK.mjs";
3
+ } from "./chunk-JNCGDLI7.mjs";
4
+ import "./chunk-E7SBT5BV.mjs";
5
5
  export {
6
6
  AccountsController
7
7
  };
@@ -48,17 +48,21 @@ function keyringTypeToName(keyringType) {
48
48
  }
49
49
  }
50
50
  }
51
- function getUUIDFromAddressOfNormalAccount(address) {
51
+ function getUUIDOptionsFromAddressOfNormalAccount(address) {
52
52
  const v4options = {
53
53
  random: sha256(toBuffer(address)).slice(0, 16)
54
54
  };
55
- return uuid(v4options);
55
+ return v4options;
56
+ }
57
+ function getUUIDFromAddressOfNormalAccount(address) {
58
+ return uuid(getUUIDOptionsFromAddressOfNormalAccount(address));
56
59
  }
57
60
 
58
61
  export {
59
62
  __privateAdd,
60
63
  __privateMethod,
61
64
  keyringTypeToName,
65
+ getUUIDOptionsFromAddressOfNormalAccount,
62
66
  getUUIDFromAddressOfNormalAccount
63
67
  };
64
- //# sourceMappingURL=chunk-HD5HN6GK.mjs.map
68
+ //# sourceMappingURL=chunk-E7SBT5BV.mjs.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/utils.ts"],"sourcesContent":["import { toBuffer } from '@ethereumjs/util';\nimport { isCustodyKeyring, KeyringTypes } from '@metamask/keyring-controller';\nimport { sha256 } from 'ethereum-cryptography/sha256';\nimport type { V4Options } from 'uuid';\nimport { v4 as uuid } from 'uuid';\n\n/**\n * Returns the name of the keyring type.\n *\n * @param keyringType - The type of the keyring.\n * @returns The name of the keyring type.\n */\nexport function keyringTypeToName(keyringType: string): string {\n // Custody keyrings are a special case, as they are not a single type\n // they just start with the prefix `Custody`\n if (isCustodyKeyring(keyringType)) {\n return 'Custody';\n }\n\n switch (keyringType) {\n case KeyringTypes.simple: {\n return 'Account';\n }\n case KeyringTypes.hd: {\n return 'Account';\n }\n case KeyringTypes.trezor: {\n return 'Trezor';\n }\n case KeyringTypes.ledger: {\n return 'Ledger';\n }\n case KeyringTypes.lattice: {\n return 'Lattice';\n }\n case KeyringTypes.qr: {\n return 'QR';\n }\n case KeyringTypes.snap: {\n return 'Snap Account';\n }\n default: {\n throw new Error(`Unknown keyring ${keyringType}`);\n }\n }\n}\n\n/**\n * Generates a UUID v4 options from a given Ethereum address.\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The UUID v4 options.\n */\nexport function getUUIDOptionsFromAddressOfNormalAccount(\n address: string,\n): V4Options {\n const v4options = {\n random: sha256(toBuffer(address)).slice(0, 16),\n };\n\n return v4options;\n}\n\n/**\n * Generates a UUID from a given Ethereum address.\n * @param address - The Ethereum address to generate the UUID from.\n * @returns The generated UUID.\n */\nexport function getUUIDFromAddressOfNormalAccount(address: string): string {\n return uuid(getUUIDOptionsFromAddressOfNormalAccount(address));\n}\n"],"mappings":";;;;;;;;;;;;;;;AAAA,SAAS,gBAAgB;AACzB,SAAS,kBAAkB,oBAAoB;AAC/C,SAAS,cAAc;AAEvB,SAAS,MAAM,YAAY;AAQpB,SAAS,kBAAkB,aAA6B;AAG7D,MAAI,iBAAiB,WAAW,GAAG;AACjC,WAAO;AAAA,EACT;AAEA,UAAQ,aAAa;AAAA,IACnB,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,IAAI;AACpB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,QAAQ;AACxB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,SAAS;AACzB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,IAAI;AACpB,aAAO;AAAA,IACT;AAAA,IACA,KAAK,aAAa,MAAM;AACtB,aAAO;AAAA,IACT;AAAA,IACA,SAAS;AACP,YAAM,IAAI,MAAM,mBAAmB,WAAW,EAAE;AAAA,IAClD;AAAA,EACF;AACF;AAOO,SAAS,yCACd,SACW;AACX,QAAM,YAAY;AAAA,IAChB,QAAQ,OAAO,SAAS,OAAO,CAAC,EAAE,MAAM,GAAG,EAAE;AAAA,EAC/C;AAEA,SAAO;AACT;AAOO,SAAS,kCAAkC,SAAyB;AACzE,SAAO,KAAK,yCAAyC,OAAO,CAAC;AAC/D;","names":[]}
@@ -3,16 +3,13 @@
3
3
 
4
4
 
5
5
 
6
- var _chunkMF4BFCSUjs = require('./chunk-MF4BFCSU.js');
6
+ var _chunkKCST55AQjs = require('./chunk-KCST55AQ.js');
7
7
 
8
8
  // src/AccountsController.ts
9
- var _util = require('@ethereumjs/util');
10
9
  var _basecontroller = require('@metamask/base-controller');
11
10
  var _ethsnapkeyring = require('@metamask/eth-snap-keyring');
12
11
  var _keyringapi = require('@metamask/keyring-api');
13
12
  var _keyringcontroller = require('@metamask/keyring-controller');
14
- var _sha256 = require('ethereum-cryptography/sha256');
15
- var _uuid = require('uuid');
16
13
  var controllerName = "AccountsController";
17
14
  var accountsControllerMetadata = {
18
15
  internalAccounts: {
@@ -26,7 +23,7 @@ var defaultState = {
26
23
  selectedAccount: ""
27
24
  }
28
25
  };
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;
26
+ var _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn, _listSnapAccounts, listSnapAccounts_fn, _listNormalAccounts, listNormalAccounts_fn, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn, _handleOnSnapStateChange, handleOnSnapStateChange_fn, _getAccountsByKeyringType, getAccountsByKeyringType_fn, _getNextAccountNumber, getNextAccountNumber_fn, _handleNewAccountAdded, handleNewAccountAdded_fn, _handleAccountRemoved, handleAccountRemoved_fn, _registerMessageHandlers, registerMessageHandlers_fn;
30
27
  var AccountsController = class extends _basecontroller.BaseController {
31
28
  /**
32
29
  * Constructor for AccountsController.
@@ -54,13 +51,13 @@ var AccountsController = class extends _basecontroller.BaseController {
54
51
  * @param type - The type of the account.
55
52
  * @returns The generated internal account.
56
53
  */
57
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _generateInternalAccountForNonSnapAccount);
54
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _generateInternalAccountForNonSnapAccount);
58
55
  /**
59
56
  * Returns a list of internal accounts created using the SnapKeyring.
60
57
  *
61
58
  * @returns A promise that resolves to an array of InternalAccount objects.
62
59
  */
63
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _listSnapAccounts);
60
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _listSnapAccounts);
64
61
  /**
65
62
  * Returns a list of normal accounts.
66
63
  * Note: listNormalAccounts is a temporary method until the keyrings all implement the InternalAccount interface.
@@ -68,51 +65,57 @@ var AccountsController = class extends _basecontroller.BaseController {
68
65
  *
69
66
  * @returns A Promise that resolves to an array of InternalAccount objects.
70
67
  */
71
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _listNormalAccounts);
68
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _listNormalAccounts);
72
69
  /**
73
70
  * Handles changes in the keyring state, specifically when new accounts are added or removed.
74
71
  *
75
72
  * @param keyringState - The new state of the keyring controller.
76
73
  */
77
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _handleOnKeyringStateChange);
74
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _handleOnKeyringStateChange);
78
75
  /**
79
76
  * Handles the change in SnapControllerState by updating the metadata of accounts that have a snap enabled.
80
77
  *
81
78
  * @param snapState - The new SnapControllerState.
82
79
  */
83
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _handleOnSnapStateChange);
80
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _handleOnSnapStateChange);
81
+ /**
82
+ * Returns the list of accounts for a given keyring type.
83
+ * @param keyringType - The type of keyring.
84
+ * @returns The list of accounts associcated with this keyring type.
85
+ */
86
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _getAccountsByKeyringType);
84
87
  /**
85
88
  * Returns the next account number for a given keyring type.
86
89
  * @param keyringType - The type of keyring.
87
90
  * @returns An object containing the account prefix and index to use.
88
91
  */
89
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _getNextAccountNumber);
92
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _getNextAccountNumber);
90
93
  /**
91
94
  * Handles the addition of a new account to the controller.
92
95
  * If the account is not a Snap Keyring account, generates an internal account for it and adds it to the controller.
93
96
  * If the account is a Snap Keyring account, retrieves the account from the keyring and adds it to the controller.
94
97
  * @param account - The address and keyring type object of the new account.
95
98
  */
96
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _handleNewAccountAdded);
99
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _handleNewAccountAdded);
97
100
  /**
98
101
  * Handles the removal of an account from the internal accounts list.
99
102
  * @param accountId - The ID of the account to be removed.
100
103
  */
101
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _handleAccountRemoved);
104
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _handleAccountRemoved);
102
105
  /**
103
106
  * Registers message handlers for the AccountsController.
104
107
  * @private
105
108
  */
106
- _chunkMF4BFCSUjs.__privateAdd.call(void 0, this, _registerMessageHandlers);
109
+ _chunkKCST55AQjs.__privateAdd.call(void 0, this, _registerMessageHandlers);
107
110
  this.messagingSystem.subscribe(
108
111
  "SnapController:stateChange",
109
- (snapStateState) => _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _handleOnSnapStateChange, handleOnSnapStateChange_fn).call(this, snapStateState)
112
+ (snapStateState) => _chunkKCST55AQjs.__privateMethod.call(void 0, this, _handleOnSnapStateChange, handleOnSnapStateChange_fn).call(this, snapStateState)
110
113
  );
111
114
  this.messagingSystem.subscribe(
112
115
  "KeyringController:stateChange",
113
- (keyringState) => _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn).call(this, keyringState)
116
+ (keyringState) => _chunkKCST55AQjs.__privateMethod.call(void 0, this, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn).call(this, keyringState)
114
117
  );
115
- _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
118
+ _chunkKCST55AQjs.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
116
119
  }
117
120
  /**
118
121
  * Returns the internal account object for the given account ID, if it exists.
@@ -156,7 +159,7 @@ var AccountsController = class extends _basecontroller.BaseController {
156
159
  }
157
160
  const account = this.getAccount(accountId);
158
161
  if (account === void 0) {
159
- throw new Error(`Account Id ${accountId} not found`);
162
+ throw new Error(`Account Id "${accountId}" not found`);
160
163
  }
161
164
  return account;
162
165
  }
@@ -185,21 +188,15 @@ var AccountsController = class extends _basecontroller.BaseController {
185
188
  * @param accountId - The ID of the account to be selected.
186
189
  */
187
190
  setSelectedAccount(accountId) {
188
- const account = this.getAccount(accountId);
191
+ const account = this.getAccountExpect(accountId);
189
192
  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
- }
193
+ currentState.internalAccounts.accounts[account.id].metadata.lastSelected = Date.now();
194
+ currentState.internalAccounts.selectedAccount = account.id;
196
195
  });
197
- if (account) {
198
- this.messagingSystem.publish(
199
- "AccountsController:selectedAccountChange",
200
- account
201
- );
202
- }
196
+ this.messagingSystem.publish(
197
+ "AccountsController:selectedAccountChange",
198
+ account
199
+ );
203
200
  }
204
201
  /**
205
202
  * Sets the name of the account with the given ID.
@@ -231,8 +228,8 @@ var AccountsController = class extends _basecontroller.BaseController {
231
228
  * @returns A Promise that resolves when the accounts have been updated.
232
229
  */
233
230
  async updateAccounts() {
234
- const snapAccounts = await _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _listSnapAccounts, listSnapAccounts_fn).call(this);
235
- const normalAccounts = (await _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _listNormalAccounts, listNormalAccounts_fn).call(this)).filter(
231
+ const snapAccounts = await _chunkKCST55AQjs.__privateMethod.call(void 0, this, _listSnapAccounts, listSnapAccounts_fn).call(this);
232
+ const normalAccounts = (await _chunkKCST55AQjs.__privateMethod.call(void 0, this, _listNormalAccounts, listNormalAccounts_fn).call(this)).filter(
236
233
  (account) => !snapAccounts.find(
237
234
  (snapAccount) => snapAccount.address === account.address
238
235
  )
@@ -243,7 +240,7 @@ var AccountsController = class extends _basecontroller.BaseController {
243
240
  ...normalAccounts,
244
241
  ...snapAccounts
245
242
  ].reduce((internalAccountMap, internalAccount) => {
246
- const keyringTypeName = _chunkMF4BFCSUjs.keyringTypeToName.call(void 0,
243
+ const keyringTypeName = _chunkKCST55AQjs.keyringTypeToName.call(void 0,
247
244
  internalAccount.metadata.keyring.type
248
245
  );
249
246
  const keyringAccountIndex = keyringTypes.get(keyringTypeName) ?? 0;
@@ -283,7 +280,7 @@ var AccountsController = class extends _basecontroller.BaseController {
283
280
  _generateInternalAccountForNonSnapAccount = new WeakSet();
284
281
  generateInternalAccountForNonSnapAccount_fn = function(address, type) {
285
282
  return {
286
- id: _chunkMF4BFCSUjs.getUUIDFromAddressOfNormalAccount.call(void 0, address),
283
+ id: _chunkKCST55AQjs.getUUIDFromAddressOfNormalAccount.call(void 0, address),
287
284
  address,
288
285
  options: {},
289
286
  methods: [
@@ -326,11 +323,8 @@ listNormalAccounts_fn = async function() {
326
323
  "KeyringController:getKeyringForAccount",
327
324
  address
328
325
  );
329
- const v4options = {
330
- random: _sha256.sha256.call(void 0, _util.toBuffer.call(void 0, address)).slice(0, 16)
331
- };
332
326
  internalAccounts.push({
333
- id: _uuid.v4.call(void 0, v4options),
327
+ id: _chunkKCST55AQjs.getUUIDFromAddressOfNormalAccount.call(void 0, address),
334
328
  address,
335
329
  options: {},
336
330
  methods: [
@@ -397,7 +391,7 @@ handleOnKeyringStateChange_fn = function(keyringState) {
397
391
  const addedAccounts = [];
398
392
  const deletedAccounts = [];
399
393
  for (const account of updatedNormalKeyringAddresses) {
400
- if (!this.state.internalAccounts.accounts[_chunkMF4BFCSUjs.getUUIDFromAddressOfNormalAccount.call(void 0, account.address)]) {
394
+ if (!this.state.internalAccounts.accounts[_chunkKCST55AQjs.getUUIDFromAddressOfNormalAccount.call(void 0, account.address)]) {
401
395
  addedAccounts.push(account);
402
396
  }
403
397
  }
@@ -424,12 +418,12 @@ handleOnKeyringStateChange_fn = function(keyringState) {
424
418
  }
425
419
  if (deletedAccounts.length > 0) {
426
420
  for (const account of deletedAccounts) {
427
- _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _handleAccountRemoved, handleAccountRemoved_fn).call(this, account.id);
421
+ _chunkKCST55AQjs.__privateMethod.call(void 0, this, _handleAccountRemoved, handleAccountRemoved_fn).call(this, account.id);
428
422
  }
429
423
  }
430
424
  if (addedAccounts.length > 0) {
431
425
  for (const account of addedAccounts) {
432
- _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _handleNewAccountAdded, handleNewAccountAdded_fn).call(this, account);
426
+ _chunkKCST55AQjs.__privateMethod.call(void 0, this, _handleNewAccountAdded, handleNewAccountAdded_fn).call(this, account);
433
427
  }
434
428
  }
435
429
  if (!this.getAccount(this.state.internalAccounts.selectedAccount)) {
@@ -438,7 +432,13 @@ handleOnKeyringStateChange_fn = function(keyringState) {
438
432
  return (accountB.metadata.lastSelected ?? 0) - (accountA.metadata.lastSelected ?? 0);
439
433
  }
440
434
  );
441
- this.setSelectedAccount(accountToSelect?.id);
435
+ if (!accountToSelect) {
436
+ this.update((currentState) => {
437
+ currentState.internalAccounts.selectedAccount = "";
438
+ });
439
+ return;
440
+ }
441
+ this.setSelectedAccount(accountToSelect.id);
442
442
  }
443
443
  }
444
444
  };
@@ -461,27 +461,34 @@ handleOnSnapStateChange_fn = function(snapState) {
461
461
  });
462
462
  });
463
463
  };
464
+ _getAccountsByKeyringType = new WeakSet();
465
+ getAccountsByKeyringType_fn = function(keyringType) {
466
+ return this.listAccounts().filter((internalAccount) => {
467
+ if (keyringType === _keyringcontroller.KeyringTypes.hd || keyringType === _keyringcontroller.KeyringTypes.simple) {
468
+ return internalAccount.metadata.keyring.type === _keyringcontroller.KeyringTypes.hd || internalAccount.metadata.keyring.type === _keyringcontroller.KeyringTypes.simple;
469
+ }
470
+ return internalAccount.metadata.keyring.type === keyringType;
471
+ });
472
+ };
464
473
  _getNextAccountNumber = new WeakSet();
465
474
  getNextAccountNumber_fn = function(keyringType) {
466
- const keyringName = _chunkMF4BFCSUjs.keyringTypeToName.call(void 0, keyringType);
467
- const previousKeyringAccounts = this.listAccounts().filter(
468
- (internalAccount) => {
469
- if (keyringType === _keyringcontroller.KeyringTypes.hd || keyringType === _keyringcontroller.KeyringTypes.simple) {
470
- return internalAccount.metadata.keyring.type === _keyringcontroller.KeyringTypes.hd || internalAccount.metadata.keyring.type === _keyringcontroller.KeyringTypes.simple;
475
+ const keyringName = _chunkKCST55AQjs.keyringTypeToName.call(void 0, keyringType);
476
+ const keyringAccounts = _chunkKCST55AQjs.__privateMethod.call(void 0, this, _getAccountsByKeyringType, getAccountsByKeyringType_fn).call(this, keyringType);
477
+ const lastDefaultIndexUsedForKeyringType = keyringAccounts.reduce(
478
+ (maxInternalAccountIndex, internalAccount) => {
479
+ const match = new RegExp(`${keyringName} ([0-9]+)$`, "u").exec(
480
+ internalAccount.metadata.name
481
+ );
482
+ if (match) {
483
+ const internalAccountIndex = parseInt(match[1], 10);
484
+ return Math.max(maxInternalAccountIndex, internalAccountIndex);
471
485
  }
472
- return internalAccount.metadata.keyring.type === keyringType;
473
- }
486
+ return maxInternalAccountIndex;
487
+ },
488
+ 0
474
489
  );
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
490
  const indexToUse = Math.max(
484
- previousKeyringAccounts.length + 1,
491
+ keyringAccounts.length + 1,
485
492
  lastDefaultIndexUsedForKeyringType + 1
486
493
  );
487
494
  return { accountPrefix: keyringName, indexToUse };
@@ -490,7 +497,7 @@ _handleNewAccountAdded = new WeakSet();
490
497
  handleNewAccountAdded_fn = function(account) {
491
498
  let newAccount;
492
499
  if (account.type !== _keyringcontroller.KeyringTypes.snap) {
493
- newAccount = _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn).call(this, account.address, account.type);
500
+ newAccount = _chunkKCST55AQjs.__privateMethod.call(void 0, this, _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn).call(this, account.address, account.type);
494
501
  } else {
495
502
  const [snapKeyring] = this.messagingSystem.call(
496
503
  "KeyringController:getKeyringsByType",
@@ -503,7 +510,7 @@ handleNewAccountAdded_fn = function(account) {
503
510
  return;
504
511
  }
505
512
  }
506
- const { accountPrefix, indexToUse } = _chunkMF4BFCSUjs.__privateMethod.call(void 0, this, _getNextAccountNumber, getNextAccountNumber_fn).call(this, newAccount.metadata.keyring.type);
513
+ const { accountPrefix, indexToUse } = _chunkKCST55AQjs.__privateMethod.call(void 0, this, _getNextAccountNumber, getNextAccountNumber_fn).call(this, newAccount.metadata.keyring.type);
507
514
  const accountName = `${accountPrefix} ${indexToUse}`;
508
515
  this.update((currentState) => {
509
516
  currentState.internalAccounts.accounts[newAccount.id] = {
@@ -558,4 +565,4 @@ registerMessageHandlers_fn = function() {
558
565
 
559
566
 
560
567
  exports.AccountsController = AccountsController;
561
- //# sourceMappingURL=chunk-7FX4HSTV.js.map
568
+ //# sourceMappingURL=chunk-HS62N4JH.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"sources":["../src/AccountsController.ts"],"names":[],"mappings":";;;;;;;;AAKA,SAAS,sBAAsB;AAC/B,SAAS,mBAAmB;AAE5B,SAAS,gBAAgB,iBAAiB;AAC1C,SAAS,oBAAoB;AAmB7B,IAAM,iBAAiB;AA6FvB,IAAM,6BAA6B;AAAA,EACjC,kBAAkB;AAAA,IAChB,SAAS;AAAA,IACT,WAAW;AAAA,EACb;AACF;AAEA,IAAM,eAAwC;AAAA,EAC5C,kBAAkB;AAAA,IAChB,UAAU,CAAC;AAAA,IACX,iBAAiB;AAAA,EACnB;AACF;AArIA;AA+IO,IAAM,qBAAN,cAAiC,eAItC;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,YAAY;AAAA,IACV;AAAA,IACA;AAAA,EACF,GAGG;AACD,UAAM;AAAA,MACJ;AAAA,MACA,MAAM;AAAA,MACN,UAAU;AAAA,MACV,OAAO;AAAA,QACL,GAAG;AAAA,QACH,GAAG;AAAA,MACL;AAAA,IACF,CAAC;AAwNH;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AA+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AI;AAAA,EAC/B;AAEA,OAAK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,mBAAmB,KAAK,IAAI;AAAA,EACnC;AAEA,OAAK,gBAAgB;AAAA,IACnB,GAAG,cAAc;AAAA,IACjB,KAAK,oBAAoB,KAAK,IAAI;AAAA,EACpC;AAEA,OAAK,gBAAgB;AAAA,IACnB;AAAA,IACA,KAAK,WAAW,KAAK,IAAI;AAAA,EAC3B;AACF","sourcesContent":["import 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 type { Draft } from 'immer';\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.getAccountExpect(accountId);\n\n this.update((currentState: Draft<AccountsControllerState>) => {\n currentState.internalAccounts.accounts[account.id].metadata.lastSelected =\n Date.now();\n currentState.internalAccounts.selectedAccount = account.id;\n });\n\n this.messagingSystem.publish(\n 'AccountsController:selectedAccountChange',\n account,\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\n internalAccounts.push({\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: (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 if (!accountToSelect) {\n this.update((currentState: Draft<AccountsControllerState>) => {\n currentState.internalAccounts.selectedAccount = '';\n });\n return;\n }\n\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 list of accounts for a given keyring type.\n * @param keyringType - The type of keyring.\n * @returns The list of accounts associcated with this keyring type.\n */\n #getAccountsByKeyringType(keyringType: string) {\n return this.listAccounts().filter((internalAccount) => {\n // We do consider `hd` and `simple` keyrings to be of same type. So we check those 2 types\n // to group those accounts together!\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\n return internalAccount.metadata.keyring.type === keyringType;\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 keyringAccounts = this.#getAccountsByKeyringType(keyringType);\n const lastDefaultIndexUsedForKeyringType = keyringAccounts.reduce(\n (maxInternalAccountIndex, internalAccount) => {\n // We **DO NOT USE** `\\d+` here to only consider valid \"human\"\n // number (rounded decimal number)\n const match = new RegExp(`${keyringName} ([0-9]+)$`, 'u').exec(\n internalAccount.metadata.name,\n );\n\n if (match) {\n // Quoting `RegExp.exec` documentation:\n // > The returned array has the matched text as the first item, and then one item for\n // > each capturing group of the matched text.\n // So use `match[1]` to get the captured value\n const internalAccountIndex = parseInt(match[1], 10);\n return Math.max(maxInternalAccountIndex, internalAccountIndex);\n }\n\n return maxInternalAccountIndex;\n },\n 0,\n );\n\n const indexToUse = Math.max(\n keyringAccounts.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"]}
@@ -3,16 +3,13 @@ import {
3
3
  __privateMethod,
4
4
  getUUIDFromAddressOfNormalAccount,
5
5
  keyringTypeToName
6
- } from "./chunk-HD5HN6GK.mjs";
6
+ } from "./chunk-E7SBT5BV.mjs";
7
7
 
8
8
  // src/AccountsController.ts
9
- import { toBuffer } from "@ethereumjs/util";
10
9
  import { BaseController } from "@metamask/base-controller";
11
10
  import { SnapKeyring } from "@metamask/eth-snap-keyring";
12
11
  import { EthAccountType, EthMethod } from "@metamask/keyring-api";
13
12
  import { KeyringTypes } from "@metamask/keyring-controller";
14
- import { sha256 } from "ethereum-cryptography/sha256";
15
- import { v4 as uuid } from "uuid";
16
13
  var controllerName = "AccountsController";
17
14
  var accountsControllerMetadata = {
18
15
  internalAccounts: {
@@ -26,7 +23,7 @@ var defaultState = {
26
23
  selectedAccount: ""
27
24
  }
28
25
  };
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;
26
+ var _generateInternalAccountForNonSnapAccount, generateInternalAccountForNonSnapAccount_fn, _listSnapAccounts, listSnapAccounts_fn, _listNormalAccounts, listNormalAccounts_fn, _handleOnKeyringStateChange, handleOnKeyringStateChange_fn, _handleOnSnapStateChange, handleOnSnapStateChange_fn, _getAccountsByKeyringType, getAccountsByKeyringType_fn, _getNextAccountNumber, getNextAccountNumber_fn, _handleNewAccountAdded, handleNewAccountAdded_fn, _handleAccountRemoved, handleAccountRemoved_fn, _registerMessageHandlers, registerMessageHandlers_fn;
30
27
  var AccountsController = class extends BaseController {
31
28
  /**
32
29
  * Constructor for AccountsController.
@@ -81,6 +78,12 @@ var AccountsController = class extends BaseController {
81
78
  * @param snapState - The new SnapControllerState.
82
79
  */
83
80
  __privateAdd(this, _handleOnSnapStateChange);
81
+ /**
82
+ * Returns the list of accounts for a given keyring type.
83
+ * @param keyringType - The type of keyring.
84
+ * @returns The list of accounts associcated with this keyring type.
85
+ */
86
+ __privateAdd(this, _getAccountsByKeyringType);
84
87
  /**
85
88
  * Returns the next account number for a given keyring type.
86
89
  * @param keyringType - The type of keyring.
@@ -156,7 +159,7 @@ var AccountsController = class extends BaseController {
156
159
  }
157
160
  const account = this.getAccount(accountId);
158
161
  if (account === void 0) {
159
- throw new Error(`Account Id ${accountId} not found`);
162
+ throw new Error(`Account Id "${accountId}" not found`);
160
163
  }
161
164
  return account;
162
165
  }
@@ -185,21 +188,15 @@ var AccountsController = class extends BaseController {
185
188
  * @param accountId - The ID of the account to be selected.
186
189
  */
187
190
  setSelectedAccount(accountId) {
188
- const account = this.getAccount(accountId);
191
+ const account = this.getAccountExpect(accountId);
189
192
  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
- }
193
+ currentState.internalAccounts.accounts[account.id].metadata.lastSelected = Date.now();
194
+ currentState.internalAccounts.selectedAccount = account.id;
196
195
  });
197
- if (account) {
198
- this.messagingSystem.publish(
199
- "AccountsController:selectedAccountChange",
200
- account
201
- );
202
- }
196
+ this.messagingSystem.publish(
197
+ "AccountsController:selectedAccountChange",
198
+ account
199
+ );
203
200
  }
204
201
  /**
205
202
  * Sets the name of the account with the given ID.
@@ -326,11 +323,8 @@ listNormalAccounts_fn = async function() {
326
323
  "KeyringController:getKeyringForAccount",
327
324
  address
328
325
  );
329
- const v4options = {
330
- random: sha256(toBuffer(address)).slice(0, 16)
331
- };
332
326
  internalAccounts.push({
333
- id: uuid(v4options),
327
+ id: getUUIDFromAddressOfNormalAccount(address),
334
328
  address,
335
329
  options: {},
336
330
  methods: [
@@ -438,7 +432,13 @@ handleOnKeyringStateChange_fn = function(keyringState) {
438
432
  return (accountB.metadata.lastSelected ?? 0) - (accountA.metadata.lastSelected ?? 0);
439
433
  }
440
434
  );
441
- this.setSelectedAccount(accountToSelect?.id);
435
+ if (!accountToSelect) {
436
+ this.update((currentState) => {
437
+ currentState.internalAccounts.selectedAccount = "";
438
+ });
439
+ return;
440
+ }
441
+ this.setSelectedAccount(accountToSelect.id);
442
442
  }
443
443
  }
444
444
  };
@@ -461,27 +461,34 @@ handleOnSnapStateChange_fn = function(snapState) {
461
461
  });
462
462
  });
463
463
  };
464
+ _getAccountsByKeyringType = new WeakSet();
465
+ getAccountsByKeyringType_fn = function(keyringType) {
466
+ return this.listAccounts().filter((internalAccount) => {
467
+ if (keyringType === KeyringTypes.hd || keyringType === KeyringTypes.simple) {
468
+ return internalAccount.metadata.keyring.type === KeyringTypes.hd || internalAccount.metadata.keyring.type === KeyringTypes.simple;
469
+ }
470
+ return internalAccount.metadata.keyring.type === keyringType;
471
+ });
472
+ };
464
473
  _getNextAccountNumber = new WeakSet();
465
474
  getNextAccountNumber_fn = function(keyringType) {
466
475
  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;
476
+ const keyringAccounts = __privateMethod(this, _getAccountsByKeyringType, getAccountsByKeyringType_fn).call(this, keyringType);
477
+ const lastDefaultIndexUsedForKeyringType = keyringAccounts.reduce(
478
+ (maxInternalAccountIndex, internalAccount) => {
479
+ const match = new RegExp(`${keyringName} ([0-9]+)$`, "u").exec(
480
+ internalAccount.metadata.name
481
+ );
482
+ if (match) {
483
+ const internalAccountIndex = parseInt(match[1], 10);
484
+ return Math.max(maxInternalAccountIndex, internalAccountIndex);
471
485
  }
472
- return internalAccount.metadata.keyring.type === keyringType;
473
- }
486
+ return maxInternalAccountIndex;
487
+ },
488
+ 0
474
489
  );
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
490
  const indexToUse = Math.max(
484
- previousKeyringAccounts.length + 1,
491
+ keyringAccounts.length + 1,
485
492
  lastDefaultIndexUsedForKeyringType + 1
486
493
  );
487
494
  return { accountPrefix: keyringName, indexToUse };
@@ -558,4 +565,4 @@ registerMessageHandlers_fn = function() {
558
565
  export {
559
566
  AccountsController
560
567
  };
561
- //# sourceMappingURL=chunk-GATPL76V.mjs.map
568
+ //# sourceMappingURL=chunk-JNCGDLI7.mjs.map