@metamask/accounts-controller 10.0.0 → 12.0.0

Sign up to get free protection for your applications and to get access to all the features.
package/CHANGELOG.md CHANGED
@@ -1,4 +1,5 @@
1
1
  # Changelog
2
+
2
3
  All notable changes to this project will be documented in this file.
3
4
 
4
5
  The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
@@ -6,86 +7,151 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6
7
 
7
8
  ## [Unreleased]
8
9
 
10
+ ## [12.0.0]
11
+
12
+ ### Added
13
+
14
+ - **BREAKING**: Add ESM build ([#3998](https://github.com/MetaMask/core/pull/3998))
15
+ - It's no longer possible to import files from `./dist` directly.
16
+
17
+ ### Changed
18
+
19
+ - **BREAKING:** Bump `@metamask/base-controller` to `^5.0.0` ([#4039](https://github.com/MetaMask/core/pull/4039))
20
+ - This version has a number of breaking changes. See the changelog for more.
21
+ - **BREAKING:** Bump peer dependency on `@metamask/keyring-controller` to `^14.0.0` ([#4039](https://github.com/MetaMask/core/pull/4039))
22
+
23
+ ### Removed
24
+
25
+ - **BREAKING:** Remove action and event types unrelated to `AccountsController` from `AccountsControllerActions` and `AccountsControllerEvents` ([#4031](https://github.com/MetaMask/core/pull/4031))
26
+
27
+ ### Fixed
28
+
29
+ - **BREAKING:** Narrow allowed actions and event type for `AccountsController` messenger ([#4021](https://github.com/MetaMask/core/pull/4021), [#4031](https://github.com/MetaMask/core/pull/4031))
30
+ - Narrow type parameter `AllowedAction` from `string` to `(KeyringControllerGetKeyringForAccountAction | KeyringControllerGetKeyringsByTypeAction | KeyringControllerGetAccountsAction)['type']`.
31
+ - Narrow type parameter `AllowedEvent` from `string` to `(SnapStateChange | KeyringControllerStateChangeEvent)['type']`, removing other events from `SnapController` and `KeyringController`.
32
+
33
+ ## [11.0.0]
34
+
35
+ ### Changed
36
+
37
+ - **BREAKING:** Bump `@metamask/keyring-controller` peer dependency to `^13.0.0` ([#4007](https://github.com/MetaMask/core/pull/4007))
38
+ - Replace `ethereumjs-util` with `@ethereumjs/util` and `ethereum-cryptography` ([#3943](https://github.com/MetaMask/core/pull/3943))
39
+
40
+ ### Fixed
41
+
42
+ - Update `keyringTypeToName` to return the correct name for custody keyrings ([#3899](https://github.com/MetaMask/core/pull/3899))
43
+
9
44
  ## [10.0.0]
45
+
10
46
  ### Changed
47
+
11
48
  - **BREAKING:** Bump `@metamask/keyring-controller` peer dependency to `^12.2.0` ([#3821](https://github.com/MetaMask/core/pull/3821))
12
49
  - Bump `@metamask/base-controller` to `^4.1.1` ([#3821](https://github.com/MetaMask/core/pull/3821))
13
50
 
14
51
  ## [9.0.0]
52
+
15
53
  ### Added
54
+
16
55
  - Add methods to support ERC-4337 accounts ([#3602](https://github.com/MetaMask/core/pull/3602))
17
56
  - Add getAccount action to AccountsController ([#1892](https://github.com/MetaMask/core/pull/1892))
18
57
 
19
58
  ### Changed
20
- - **BREAKING:** Bump `@metamask/keyring-controller` peer dependency to ^12.1.0 ([#3747](https://github.com/MetaMask/core/pull/3747), [#3810](https://github.com/MetaMask/core/pull/3810))
21
- - **BREAKING:** Bump `@metamask/snaps-controllers` peer dependency to ^4.0.0 ([#3747](https://github.com/MetaMask/core/pull/3747))
59
+
60
+ - **BREAKING:** Bump `@metamask/keyring-controller` peer dependency to ^12.1.0 ([#3747](https://github.com/MetaMask/core/pull/3747), [#3810](https://github.com/MetaMask/core/pull/3810))
61
+ - **BREAKING:** Bump `@metamask/snaps-controllers` peer dependency to ^4.0.0 ([#3747](https://github.com/MetaMask/core/pull/3747))
22
62
  - Bump `@metamask/keyring-api` to ^3.0.0 ([#3747](https://github.com/MetaMask/core/pull/3747))
23
63
  - Bump `@metamask/utils` to `^8.3.0`([#3769](https://github.com/MetaMask/core/pull/3769))
24
64
 
25
65
  ### Fixed
66
+
26
67
  - Fix quick succession of submit password causing Accounts Controller state to be cleared ([#3802](https://github.com/MetaMask/core/pull/3802))
27
68
 
28
69
  ## [8.0.0]
70
+
29
71
  ### Changed
72
+
30
73
  - **BREAKING:** Bump `@metamask/keyring-controller` to ^12.0.0
31
74
 
32
75
  ## [7.0.1]
76
+
33
77
  ### Changed
78
+
34
79
  - Bump snaps dependencies ([#3734](https://github.com/MetaMask/core/pull/3734))
35
80
 
36
81
  ## [7.0.0]
82
+
37
83
  ### Changed
84
+
38
85
  - **BREAKING:** Bump `@metamask/keyring-controller` peer dependency from `^10.0.0` to `^11.0.0` ([#3695](https://github.com/MetaMask/core/pull/3695))
39
86
  - **BREAKING:** Add `@metamask/snaps-controllers` as a peer dependency ([#3607](https://github.com/MetaMask/core/pull/3607))
40
87
  - Bump `@metamask/base-controller` to `^4.0.1` ([#3695](https://github.com/MetaMask/core/pull/3695))
41
88
 
42
89
  ## [6.0.0]
90
+
43
91
  ### Changed
92
+
44
93
  - **BREAKING:** Bump `@metamask/base-controller` to ^4.0.0 ([#2063](https://github.com/MetaMask/core/pull/2063))
45
94
  - This is breaking because the type of the `messenger` has backward-incompatible changes. See the changelog for this package for more.
46
95
  - Bump `@metamask/keyring-controller` to ^10.0.0 ([#2063](https://github.com/MetaMask/core/pull/2063))
47
96
 
48
97
  ## [5.0.0]
98
+
49
99
  ### Changed
100
+
50
101
  - **BREAKING:** Bump dependency and peer dependency on `@metamask/keyring-controller` to ^9.0.0
51
102
  - Bump `@metamask/snaps-utils` and `@metamask/snaps-controller` to 3.2.0 ([#1917](https://github.com/MetaMask/core/pull/1917), [#1944](https://github.com/MetaMask/core/pull/1944), [#1977](https://github.com/MetaMask/core/pull/1977))
52
103
  - Bump @metamask/utils from 8.1.0 to 8.2.0 ([#1957](https://github.com/MetaMask/core/pull/1957))
53
104
  - Bump @metamask/keyring-api from 1.0.0 to 1.1.0 ([#1951](https://github.com/MetaMask/core/pull/1951))
54
105
 
55
106
  ## [4.0.0]
107
+
56
108
  ### Changed
109
+
57
110
  - **BREAKING** Update the `onKeyringStateChange` and `onSnapStateChange` methods, and remove the `keyringApiEnabled` from the AccountsController ([#1839](https://github.com/MetaMask/core/pull/1839))
58
111
  - Add getSelectedAccount and getAccountByAddress actions to AccountsController ([#1858](https://github.com/MetaMask/core/pull/1858))
59
112
 
60
113
  ## [3.0.0]
114
+
61
115
  ### Changed
116
+
62
117
  - **BREAKING:** Bump dependency on `@metamask/eth-snap-keyring` to ^1.0.0 ([#1735](https://github.com/MetaMask/core/pull/1735))
63
118
  - **BREAKING:** Bump dependency on `@metamask/keyring-api` to ^1.0.0 ([#1735](https://github.com/MetaMask/core/pull/1735))
64
119
  - **BREAKING:** Bump dependency on `@metamask/snaps-utils` to ^3.0.0 ([#1735](https://github.com/MetaMask/core/pull/1735))
65
120
  - Bump dependency and peer dependency on `@metamask/keyring-controller` to ^8.0.3
66
121
 
67
122
  ## [2.0.2]
123
+
68
124
  ### Changed
125
+
69
126
  - Bump dependency on `@metamask/utils` to ^8.1.0 ([#1639](https://github.com/MetaMask/core/pull/1639))
70
127
  - Bump dependency on `@metamask/base-controller` to ^3.2.3
71
128
  - Bump peer dependency on `@metamask/keyring-controller` to ^8.0.2
72
129
 
73
130
  ## [2.0.1]
131
+
74
132
  ### Changed
133
+
75
134
  - Update TypeScript to v4.8.x ([#1718](https://github.com/MetaMask/core/pull/1718))
76
135
 
77
136
  ### Fixed
137
+
78
138
  - Remove unused `selectedAccount` from state metadata ([#1734](https://github.com/MetaMask/core/pull/1734))
79
139
 
80
140
  ## [2.0.0]
141
+
81
142
  ### Changed
143
+
82
144
  - **BREAKING:** Bump peer dependency on `@metamask/keyring-controller` to ^8.0.0
83
145
 
84
146
  ## [1.0.0]
147
+
85
148
  ### Added
149
+
86
150
  - Initial release ([#1637](https://github.com/MetaMask/core/pull/1637))
87
151
 
88
- [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@10.0.0...HEAD
152
+ [Unreleased]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@12.0.0...HEAD
153
+ [12.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@11.0.0...@metamask/accounts-controller@12.0.0
154
+ [11.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@10.0.0...@metamask/accounts-controller@11.0.0
89
155
  [10.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@9.0.0...@metamask/accounts-controller@10.0.0
90
156
  [9.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@8.0.0...@metamask/accounts-controller@9.0.0
91
157
  [8.0.0]: https://github.com/MetaMask/core/compare/@metamask/accounts-controller@7.0.1...@metamask/accounts-controller@8.0.0
@@ -1,449 +1,8 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
12
- if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
13
- if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
14
- return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
15
- };
16
- var _AccountsController_instances, _AccountsController_generateInternalAccountForNonSnapAccount, _AccountsController_listSnapAccounts, _AccountsController_listNormalAccounts, _AccountsController_handleOnKeyringStateChange, _AccountsController_handleOnSnapStateChange, _AccountsController_getNextAccountNumber, _AccountsController_handleNewAccountAdded, _AccountsController_handleAccountRemoved, _AccountsController_registerMessageHandlers;
17
- Object.defineProperty(exports, "__esModule", { value: true });
18
- exports.AccountsController = void 0;
19
- const base_controller_1 = require("@metamask/base-controller");
20
- const eth_snap_keyring_1 = require("@metamask/eth-snap-keyring");
21
- const keyring_api_1 = require("@metamask/keyring-api");
22
- const keyring_controller_1 = require("@metamask/keyring-controller");
23
- const ethereumjs_util_1 = require("ethereumjs-util");
24
- const uuid_1 = require("uuid");
25
- const utils_1 = require("./utils");
26
- const controllerName = 'AccountsController';
27
- const accountsControllerMetadata = {
28
- internalAccounts: {
29
- persist: true,
30
- anonymous: false,
31
- },
32
- };
33
- const defaultState = {
34
- internalAccounts: {
35
- accounts: {},
36
- selectedAccount: '',
37
- },
38
- };
39
- /**
40
- * Controller that manages internal accounts.
41
- * The accounts controller is responsible for creating and managing internal accounts.
42
- * It also provides convenience methods for accessing and updating the internal accounts.
43
- * The accounts controller also listens for keyring state changes and updates the internal accounts accordingly.
44
- * The accounts controller also listens for snap state changes and updates the internal accounts accordingly.
45
- *
46
- */
47
- class AccountsController extends base_controller_1.BaseController {
48
- /**
49
- * Constructor for AccountsController.
50
- *
51
- * @param options - The controller options.
52
- * @param options.messenger - The messenger object.
53
- * @param options.state - Initial state to set on this controller
54
- */
55
- constructor({ messenger, state, }) {
56
- super({
57
- messenger,
58
- name: controllerName,
59
- metadata: accountsControllerMetadata,
60
- state: Object.assign(Object.assign({}, defaultState), state),
61
- });
62
- _AccountsController_instances.add(this);
63
- this.messagingSystem.subscribe('SnapController:stateChange', (snapStateState) => __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_handleOnSnapStateChange).call(this, snapStateState));
64
- this.messagingSystem.subscribe('KeyringController:stateChange', (keyringState) => __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_handleOnKeyringStateChange).call(this, keyringState));
65
- __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_registerMessageHandlers).call(this);
66
- }
67
- /**
68
- * Returns the internal account object for the given account ID, if it exists.
69
- *
70
- * @param accountId - The ID of the account to retrieve.
71
- * @returns The internal account object, or undefined if the account does not exist.
72
- */
73
- getAccount(accountId) {
74
- return this.state.internalAccounts.accounts[accountId];
75
- }
76
- /**
77
- * Returns an array of all internal accounts.
78
- *
79
- * @returns An array of InternalAccount objects.
80
- */
81
- listAccounts() {
82
- return Object.values(this.state.internalAccounts.accounts);
83
- }
84
- /**
85
- * Returns the internal account object for the given account ID.
86
- *
87
- * @param accountId - The ID of the account to retrieve.
88
- * @returns The internal account object.
89
- * @throws An error if the account ID is not found.
90
- */
91
- getAccountExpect(accountId) {
92
- // Edge case where the extension is setup but the srp is not yet created
93
- // certain ui elements will query the selected address before any accounts are created.
94
- if (!accountId) {
95
- return {
96
- id: '',
97
- address: '',
98
- options: {},
99
- methods: [],
100
- type: keyring_api_1.EthAccountType.Eoa,
101
- metadata: {
102
- name: '',
103
- keyring: {
104
- type: '',
105
- },
106
- },
107
- };
108
- }
109
- const account = this.getAccount(accountId);
110
- if (account === undefined) {
111
- throw new Error(`Account Id ${accountId} not found`);
112
- }
113
- return account;
114
- }
115
- /**
116
- * Returns the selected internal account.
117
- *
118
- * @returns The selected internal account.
119
- */
120
- getSelectedAccount() {
121
- return this.getAccountExpect(this.state.internalAccounts.selectedAccount);
122
- }
123
- /**
124
- * Returns the account with the specified address.
125
- * ! This method will only return the first account that matches the address
126
- * @param address - The address of the account to retrieve.
127
- * @returns The account with the specified address, or undefined if not found.
128
- */
129
- getAccountByAddress(address) {
130
- return this.listAccounts().find((account) => account.address.toLowerCase() === address.toLowerCase());
131
- }
132
- /**
133
- * Sets the selected account by its ID.
134
- *
135
- * @param accountId - The ID of the account to be selected.
136
- */
137
- setSelectedAccount(accountId) {
138
- const account = this.getAccount(accountId);
139
- this.update((currentState) => {
140
- if (account) {
141
- currentState.internalAccounts.accounts[account.id].metadata.lastSelected = Date.now();
142
- currentState.internalAccounts.selectedAccount = account.id;
143
- }
144
- else {
145
- currentState.internalAccounts.selectedAccount = '';
146
- }
147
- });
148
- if (account) {
149
- this.messagingSystem.publish('AccountsController:selectedAccountChange', account);
150
- }
151
- }
152
- /**
153
- * Sets the name of the account with the given ID.
154
- *
155
- * @param accountId - The ID of the account to set the name for.
156
- * @param accountName - The new name for the account.
157
- * @throws An error if an account with the same name already exists.
158
- */
159
- setAccountName(accountId, accountName) {
160
- const account = this.getAccountExpect(accountId);
161
- if (this.listAccounts().find((internalAccount) => internalAccount.metadata.name === accountName &&
162
- internalAccount.id !== accountId)) {
163
- throw new Error('Account name already exists');
164
- }
165
- this.update((currentState) => {
166
- const internalAccount = Object.assign(Object.assign({}, account), { metadata: Object.assign(Object.assign({}, account.metadata), { name: accountName }) });
167
- currentState.internalAccounts.accounts[accountId] =
168
- // @ts-expect-error Assigning a complex type `T` to `Draft<T>` causes an excessive type instantiation depth error.
169
- internalAccount;
170
- });
171
- }
172
- /**
173
- * Updates the internal accounts list by retrieving normal and snap accounts,
174
- * removing duplicates, and updating the metadata of each account.
175
- *
176
- * @returns A Promise that resolves when the accounts have been updated.
177
- */
178
- updateAccounts() {
179
- return __awaiter(this, void 0, void 0, function* () {
180
- const snapAccounts = yield __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_listSnapAccounts).call(this);
181
- const normalAccounts = (yield __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_listNormalAccounts).call(this)).filter((account) => !snapAccounts.find((snapAccount) => snapAccount.address === account.address));
182
- // keyring type map.
183
- const keyringTypes = new Map();
184
- const previousAccounts = this.state.internalAccounts.accounts;
185
- const accounts = [
186
- ...normalAccounts,
187
- ...snapAccounts,
188
- ].reduce((internalAccountMap, internalAccount) => {
189
- var _a, _b;
190
- const keyringTypeName = (0, utils_1.keyringTypeToName)(internalAccount.metadata.keyring.type);
191
- const keyringAccountIndex = (_a = keyringTypes.get(keyringTypeName)) !== null && _a !== void 0 ? _a : 0;
192
- if (keyringAccountIndex) {
193
- keyringTypes.set(keyringTypeName, keyringAccountIndex + 1);
194
- }
195
- else {
196
- keyringTypes.set(keyringTypeName, 1);
197
- }
198
- const existingAccount = previousAccounts[internalAccount.id];
199
- internalAccountMap[internalAccount.id] = Object.assign(Object.assign({}, internalAccount), { metadata: Object.assign(Object.assign({}, internalAccount.metadata), { name: existingAccount && existingAccount.metadata.name !== ''
200
- ? existingAccount.metadata.name
201
- : `${keyringTypeName} ${keyringAccountIndex + 1}`, lastSelected: (_b = existingAccount === null || existingAccount === void 0 ? void 0 : existingAccount.metadata) === null || _b === void 0 ? void 0 : _b.lastSelected }) });
202
- return internalAccountMap;
203
- }, {});
204
- this.update((currentState) => {
205
- currentState.internalAccounts.accounts =
206
- accounts;
207
- });
208
- });
209
- }
210
- /**
211
- * Loads the backup state of the accounts controller.
212
- *
213
- * @param backup - The backup state to load.
214
- */
215
- loadBackup(backup) {
216
- if (backup.internalAccounts) {
217
- this.update((currentState) => {
218
- currentState.internalAccounts =
219
- backup.internalAccounts;
220
- });
221
- }
222
- }
223
- }
224
- exports.AccountsController = AccountsController;
225
- _AccountsController_instances = new WeakSet(), _AccountsController_generateInternalAccountForNonSnapAccount = function _AccountsController_generateInternalAccountForNonSnapAccount(address, type) {
226
- return {
227
- id: (0, utils_1.getUUIDFromAddressOfNormalAccount)(address),
228
- address,
229
- options: {},
230
- methods: [
231
- keyring_api_1.EthMethod.PersonalSign,
232
- keyring_api_1.EthMethod.Sign,
233
- keyring_api_1.EthMethod.SignTransaction,
234
- keyring_api_1.EthMethod.SignTypedDataV1,
235
- keyring_api_1.EthMethod.SignTypedDataV3,
236
- keyring_api_1.EthMethod.SignTypedDataV4,
237
- ],
238
- type: keyring_api_1.EthAccountType.Eoa,
239
- metadata: {
240
- name: '',
241
- keyring: {
242
- type,
243
- },
244
- },
245
- };
246
- }, _AccountsController_listSnapAccounts = function _AccountsController_listSnapAccounts() {
247
- return __awaiter(this, void 0, void 0, function* () {
248
- const [snapKeyring] = this.messagingSystem.call('KeyringController:getKeyringsByType', eth_snap_keyring_1.SnapKeyring.type);
249
- // snap keyring is not available until the first account is created in the keyring controller
250
- if (!snapKeyring) {
251
- return [];
252
- }
253
- const snapAccounts = snapKeyring.listAccounts();
254
- return snapAccounts;
255
- });
256
- }, _AccountsController_listNormalAccounts = function _AccountsController_listNormalAccounts() {
257
- return __awaiter(this, void 0, void 0, function* () {
258
- const addresses = yield this.messagingSystem.call('KeyringController:getAccounts');
259
- const internalAccounts = [];
260
- for (const address of addresses) {
261
- const keyring = yield this.messagingSystem.call('KeyringController:getKeyringForAccount', address);
262
- const v4options = {
263
- random: (0, ethereumjs_util_1.sha256FromString)(address).slice(0, 16),
264
- };
265
- internalAccounts.push({
266
- id: (0, uuid_1.v4)(v4options),
267
- address,
268
- options: {},
269
- methods: [
270
- keyring_api_1.EthMethod.PersonalSign,
271
- keyring_api_1.EthMethod.Sign,
272
- keyring_api_1.EthMethod.SignTransaction,
273
- keyring_api_1.EthMethod.SignTypedDataV1,
274
- keyring_api_1.EthMethod.SignTypedDataV3,
275
- keyring_api_1.EthMethod.SignTypedDataV4,
276
- ],
277
- type: keyring_api_1.EthAccountType.Eoa,
278
- metadata: {
279
- name: '',
280
- keyring: {
281
- type: keyring.type,
282
- },
283
- },
284
- });
285
- }
286
- return internalAccounts.filter((account) => account.metadata.keyring.type !== keyring_controller_1.KeyringTypes.snap);
287
- });
288
- }, _AccountsController_handleOnKeyringStateChange = function _AccountsController_handleOnKeyringStateChange(keyringState) {
289
- // check if there are any new accounts added
290
- // TODO: change when accountAdded event is added to the keyring controller
291
- // We check for keyrings length to be greater than 0 because the extension client may try execute
292
- // submit password twice and clear the keyring state.
293
- // https://github.com/MetaMask/KeyringController/blob/2d73a4deed8d013913f6ef0c9f5c0bb7c614f7d3/src/KeyringController.ts#L910
294
- if (keyringState.isUnlocked && keyringState.keyrings.length > 0) {
295
- const updatedNormalKeyringAddresses = [];
296
- const updatedSnapKeyringAddresses = [];
297
- for (const keyring of keyringState.keyrings) {
298
- if (keyring.type === keyring_controller_1.KeyringTypes.snap) {
299
- updatedSnapKeyringAddresses.push(...keyring.accounts.map((address) => {
300
- return {
301
- address,
302
- type: keyring.type,
303
- };
304
- }));
305
- }
306
- else {
307
- updatedNormalKeyringAddresses.push(...keyring.accounts.map((address) => {
308
- return {
309
- address,
310
- type: keyring.type,
311
- };
312
- }));
313
- }
314
- }
315
- const { previousNormalInternalAccounts, previousSnapInternalAccounts } = this.listAccounts().reduce((accumulator, account) => {
316
- if (account.metadata.keyring.type === keyring_controller_1.KeyringTypes.snap) {
317
- accumulator.previousSnapInternalAccounts.push(account);
318
- }
319
- else {
320
- accumulator.previousNormalInternalAccounts.push(account);
321
- }
322
- return accumulator;
323
- }, {
324
- previousNormalInternalAccounts: [],
325
- previousSnapInternalAccounts: [],
326
- });
327
- const addedAccounts = [];
328
- const deletedAccounts = [];
329
- // snap account ids are random uuid while normal accounts
330
- // are determininistic based on the address
331
- // ^NOTE: This will be removed when normal accounts also implement internal accounts
332
- // finding all the normal accounts that were added
333
- for (const account of updatedNormalKeyringAddresses) {
334
- if (!this.state.internalAccounts.accounts[(0, utils_1.getUUIDFromAddressOfNormalAccount)(account.address)]) {
335
- addedAccounts.push(account);
336
- }
337
- }
338
- // finding all the snap accounts that were added
339
- for (const account of updatedSnapKeyringAddresses) {
340
- if (!previousSnapInternalAccounts.find((internalAccount) => internalAccount.address.toLowerCase() ===
341
- account.address.toLowerCase())) {
342
- addedAccounts.push(account);
343
- }
344
- }
345
- // finding all the normal accounts that were deleted
346
- for (const account of previousNormalInternalAccounts) {
347
- if (!updatedNormalKeyringAddresses.find(({ address }) => address.toLowerCase() === account.address.toLowerCase())) {
348
- deletedAccounts.push(account);
349
- }
350
- }
351
- // finding all the snap accounts that were deleted
352
- for (const account of previousSnapInternalAccounts) {
353
- if (!updatedSnapKeyringAddresses.find(({ address }) => address.toLowerCase() === account.address.toLowerCase())) {
354
- deletedAccounts.push(account);
355
- }
356
- }
357
- if (deletedAccounts.length > 0) {
358
- for (const account of deletedAccounts) {
359
- __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_handleAccountRemoved).call(this, account.id);
360
- }
361
- }
362
- if (addedAccounts.length > 0) {
363
- for (const account of addedAccounts) {
364
- __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_handleNewAccountAdded).call(this, account);
365
- }
366
- }
367
- // handle if the selected account was deleted
368
- if (!this.getAccount(this.state.internalAccounts.selectedAccount)) {
369
- const [accountToSelect] = this.listAccounts().sort((accountA, accountB) => {
370
- var _a, _b;
371
- // sort by lastSelected descending
372
- return (((_a = accountB.metadata.lastSelected) !== null && _a !== void 0 ? _a : 0) -
373
- ((_b = accountA.metadata.lastSelected) !== null && _b !== void 0 ? _b : 0));
374
- });
375
- // if the accountToSelect is undefined, then there are no accounts
376
- // it mean the keyring was reinitialized.
377
- this.setSelectedAccount(accountToSelect === null || accountToSelect === void 0 ? void 0 : accountToSelect.id);
378
- }
379
- }
380
- }, _AccountsController_handleOnSnapStateChange = function _AccountsController_handleOnSnapStateChange(snapState) {
381
- // only check if snaps changed in status
382
- const { snaps } = snapState;
383
- const accounts = this.listAccounts().filter((account) => account.metadata.snap);
384
- this.update((currentState) => {
385
- accounts.forEach((account) => {
386
- const currentAccount = currentState.internalAccounts.accounts[account.id];
387
- if (currentAccount.metadata.snap) {
388
- const snapId = currentAccount.metadata.snap.id;
389
- const storedSnap = snaps[snapId];
390
- if (storedSnap) {
391
- currentAccount.metadata.snap.enabled =
392
- storedSnap.enabled && !storedSnap.blocked;
393
- }
394
- }
395
- });
396
- });
397
- }, _AccountsController_getNextAccountNumber = function _AccountsController_getNextAccountNumber(keyringType) {
398
- const keyringName = (0, utils_1.keyringTypeToName)(keyringType);
399
- const previousKeyringAccounts = this.listAccounts().filter((internalAccount) => {
400
- if (keyringType === keyring_controller_1.KeyringTypes.hd ||
401
- keyringType === keyring_controller_1.KeyringTypes.simple) {
402
- return (internalAccount.metadata.keyring.type === keyring_controller_1.KeyringTypes.hd ||
403
- internalAccount.metadata.keyring.type === keyring_controller_1.KeyringTypes.simple);
404
- }
405
- return internalAccount.metadata.keyring.type === keyringType;
406
- });
407
- const lastDefaultIndexUsedForKeyringType = previousKeyringAccounts
408
- .filter((internalAccount) => new RegExp(`${keyringName} \\d+$`, 'u').test(internalAccount.metadata.name))
409
- .map((internalAccount) => {
410
- const nameToWords = internalAccount.metadata.name.split(' '); // get the index of a default account name
411
- return parseInt(nameToWords[nameToWords.length], 10);
412
- })
413
- .sort((a, b) => b - a)[0] || 0;
414
- const indexToUse = Math.max(previousKeyringAccounts.length + 1, lastDefaultIndexUsedForKeyringType + 1);
415
- return { accountPrefix: keyringName, indexToUse };
416
- }, _AccountsController_handleNewAccountAdded = function _AccountsController_handleNewAccountAdded(account) {
417
- let newAccount;
418
- if (account.type !== keyring_controller_1.KeyringTypes.snap) {
419
- newAccount = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_generateInternalAccountForNonSnapAccount).call(this, account.address, account.type);
420
- }
421
- else {
422
- const [snapKeyring] = this.messagingSystem.call('KeyringController:getKeyringsByType', eth_snap_keyring_1.SnapKeyring.type);
423
- newAccount = snapKeyring.getAccountByAddress(account.address);
424
- // The snap deleted the account before the keyring controller could add it
425
- if (!newAccount) {
426
- return;
427
- }
428
- }
429
- // get next index number for the keyring type
430
- const { accountPrefix, indexToUse } = __classPrivateFieldGet(this, _AccountsController_instances, "m", _AccountsController_getNextAccountNumber).call(this, newAccount.metadata.keyring.type);
431
- const accountName = `${accountPrefix} ${indexToUse}`;
432
- this.update((currentState) => {
433
- currentState.internalAccounts.accounts[newAccount.id] = Object.assign(Object.assign({}, newAccount), { metadata: Object.assign(Object.assign({}, newAccount.metadata), { name: accountName, lastSelected: Date.now() }) });
434
- });
435
- this.setSelectedAccount(newAccount.id);
436
- }, _AccountsController_handleAccountRemoved = function _AccountsController_handleAccountRemoved(accountId) {
437
- this.update((currentState) => {
438
- delete currentState.internalAccounts.accounts[accountId];
439
- });
440
- }, _AccountsController_registerMessageHandlers = function _AccountsController_registerMessageHandlers() {
441
- this.messagingSystem.registerActionHandler(`${controllerName}:setSelectedAccount`, this.setSelectedAccount.bind(this));
442
- this.messagingSystem.registerActionHandler(`${controllerName}:listAccounts`, this.listAccounts.bind(this));
443
- this.messagingSystem.registerActionHandler(`${controllerName}:setAccountName`, this.setAccountName.bind(this));
444
- this.messagingSystem.registerActionHandler(`${controllerName}:updateAccounts`, this.updateAccounts.bind(this));
445
- this.messagingSystem.registerActionHandler(`${controllerName}:getSelectedAccount`, this.getSelectedAccount.bind(this));
446
- this.messagingSystem.registerActionHandler(`${controllerName}:getAccountByAddress`, this.getAccountByAddress.bind(this));
447
- this.messagingSystem.registerActionHandler(`AccountsController:getAccount`, this.getAccount.bind(this));
448
- };
1
+ "use strict";Object.defineProperty(exports, "__esModule", {value: true});
2
+
3
+ var _chunk7FX4HSTVjs = require('./chunk-7FX4HSTV.js');
4
+ require('./chunk-MF4BFCSU.js');
5
+
6
+
7
+ exports.AccountsController = _chunk7FX4HSTVjs.AccountsController;
449
8
  //# sourceMappingURL=AccountsController.js.map