@metamask-previews/keyring-controller 15.0.0-preview-11ae513 → 15.0.0-preview-f6e8783

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.
@@ -3,7 +3,7 @@
3
3
 
4
4
 
5
5
 
6
- var _chunkDNQK26H6js = require('./chunk-DNQK26H6.js');
6
+ var _chunkQDPHKQONjs = require('./chunk-QDPHKQON.js');
7
7
 
8
8
  // src/KeyringController.ts
9
9
  var _util = require('@ethereumjs/util');
@@ -86,7 +86,7 @@ async function displayForKeyring(keyring) {
86
86
  accounts: accounts.map(_ethsigutil.normalize)
87
87
  };
88
88
  }
89
- var _initVaultMutex, _vaultOperationMutex, _keyringBuilders, _keyrings, _unsupportedKeyrings, _password, _encryptor, _cacheEncryptionKey, _qrKeyringStateListener, _registerMessageHandlers, registerMessageHandlers_fn, _getKeyringBuilderForType, getKeyringBuilderForType_fn, _addQRKeyring, addQRKeyring_fn, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn, _getUpdatedKeyrings, getUpdatedKeyrings_fn, _unlockKeyrings, unlockKeyrings_fn, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn, _newKeyring, newKeyring_fn, _clearKeyrings, clearKeyrings_fn, _restoreKeyring, restoreKeyring_fn, _destroyKeyring, destroyKeyring_fn, _removeEmptyKeyrings, removeEmptyKeyrings_fn, _checkForDuplicate, checkForDuplicate_fn, _setUnlocked, setUnlocked_fn, _getMemState, getMemState_fn, _withVaultLock, withVaultLock_fn;
89
+ var _controllerOperationMutex, _vaultOperationMutex, _keyringBuilders, _keyrings, _unsupportedKeyrings, _password, _encryptor, _cacheEncryptionKey, _qrKeyringStateListener, _registerMessageHandlers, registerMessageHandlers_fn, _getKeyringBuilderForType, getKeyringBuilderForType_fn, _addQRKeyring, addQRKeyring_fn, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn, _getUpdatedKeyrings, getUpdatedKeyrings_fn, _unlockKeyrings, unlockKeyrings_fn, _updateVault, updateVault_fn, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn, _newKeyring, newKeyring_fn, _clearKeyrings, clearKeyrings_fn, _restoreKeyring, restoreKeyring_fn, _destroyKeyring, destroyKeyring_fn, _removeEmptyKeyrings, removeEmptyKeyrings_fn, _checkForDuplicate, checkForDuplicate_fn, _setUnlocked, setUnlocked_fn, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn, _withControllerLock, withControllerLock_fn, _withVaultLock, withVaultLock_fn;
90
90
  var KeyringController = class extends _basecontroller.BaseController {
91
91
  /**
92
92
  * Creates a KeyringController instance.
@@ -124,14 +124,14 @@ var KeyringController = class extends _basecontroller.BaseController {
124
124
  * Constructor helper for registering this controller's messaging system
125
125
  * actions.
126
126
  */
127
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _registerMessageHandlers);
127
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _registerMessageHandlers);
128
128
  /**
129
129
  * Get the keyring builder for the given `type`.
130
130
  *
131
131
  * @param type - The type of keyring to get the builder for.
132
132
  * @returns The keyring builder, or undefined if none exists.
133
133
  */
134
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _getKeyringBuilderForType);
134
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _getKeyringBuilderForType);
135
135
  /**
136
136
  * Add qr hardware keyring.
137
137
  *
@@ -139,15 +139,15 @@ var KeyringController = class extends _basecontroller.BaseController {
139
139
  * @throws If a QRKeyring builder is not provided
140
140
  * when initializing the controller
141
141
  */
142
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _addQRKeyring);
142
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _addQRKeyring);
143
143
  /**
144
144
  * Subscribe to a QRKeyring state change events and
145
145
  * forward them through the messaging system.
146
146
  *
147
147
  * @param qrKeyring - The QRKeyring instance to subscribe to
148
148
  */
149
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _subscribeToQRKeyringEvents);
150
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _unsubscribeFromQRKeyringsEvents);
149
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _subscribeToQRKeyringEvents);
150
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _unsubscribeFromQRKeyringsEvents);
151
151
  /**
152
152
  * Create new vault with an initial keyring
153
153
  *
@@ -162,14 +162,14 @@ var KeyringController = class extends _basecontroller.BaseController {
162
162
  * @param keyring.opts - Optional parameters required to instantiate the keyring.
163
163
  * @returns A promise that resolves to the state.
164
164
  */
165
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _createNewVaultWithKeyring);
165
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _createNewVaultWithKeyring);
166
166
  /**
167
167
  * Get the updated array of each keyring's type and
168
168
  * accounts list.
169
169
  *
170
170
  * @returns A promise resolving to the updated keyrings array.
171
171
  */
172
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _getUpdatedKeyrings);
172
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _getUpdatedKeyrings);
173
173
  /**
174
174
  * Unlock Keyrings, decrypting the vault and deserializing all
175
175
  * keyrings contained in it, using a password or an encryption key with salt.
@@ -179,7 +179,20 @@ var KeyringController = class extends _basecontroller.BaseController {
179
179
  * @param encryptionSalt - The salt used to encrypt the vault.
180
180
  * @returns A promise resolving to the deserialized keyrings array.
181
181
  */
182
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _unlockKeyrings);
182
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _unlockKeyrings);
183
+ /**
184
+ * Update the vault with the current keyrings.
185
+ *
186
+ * @returns A promise resolving to `true` if the operation is successful.
187
+ */
188
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _updateVault);
189
+ /**
190
+ * Retrieves all the accounts from keyrings instances
191
+ * that are currently in memory.
192
+ *
193
+ * @returns A promise resolving to an array of accounts.
194
+ */
195
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _getAccountsFromKeyrings);
183
196
  /**
184
197
  * Create a new keyring, ensuring that the first account is
185
198
  * also created.
@@ -188,17 +201,20 @@ var KeyringController = class extends _basecontroller.BaseController {
188
201
  * @param opts - Optional parameters required to instantiate the keyring.
189
202
  * @returns A promise that resolves if the operation is successful.
190
203
  */
191
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _createKeyringWithFirstAccount);
204
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _createKeyringWithFirstAccount);
192
205
  /**
193
206
  * Instantiate, initialize and return a new keyring of the given `type`,
194
207
  * using the given `opts`. The keyring is built using the keyring builder
195
208
  * registered for the given `type`.
196
209
  *
210
+ *
197
211
  * @param type - The type of keyring to add.
198
212
  * @param data - The data to restore a previously serialized keyring.
213
+ * @param persist - Whether to persist the keyring to the vault.
199
214
  * @returns The new keyring.
215
+ * @throws If the keyring includes duplicated accounts.
200
216
  */
201
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _newKeyring);
217
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _newKeyring);
202
218
  /**
203
219
  * Remove all managed keyrings, destroying all their
204
220
  * instances in memory.
@@ -206,7 +222,7 @@ var KeyringController = class extends _basecontroller.BaseController {
206
222
  * @param options - Operations options.
207
223
  * @param options.skipStateUpdate - Whether to skip updating the controller state.
208
224
  */
209
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _clearKeyrings);
225
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _clearKeyrings);
210
226
  /**
211
227
  * Restore a Keyring from a provided serialized payload.
212
228
  * On success, returns the resulting keyring instance.
@@ -214,7 +230,7 @@ var KeyringController = class extends _basecontroller.BaseController {
214
230
  * @param serialized - The serialized keyring.
215
231
  * @returns The deserialized keyring or undefined if the keyring type is unsupported.
216
232
  */
217
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _restoreKeyring);
233
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _restoreKeyring);
218
234
  /**
219
235
  * Destroy Keyring
220
236
  *
@@ -224,14 +240,14 @@ var KeyringController = class extends _basecontroller.BaseController {
224
240
  *
225
241
  * @param keyring - The keyring to destroy.
226
242
  */
227
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _destroyKeyring);
243
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _destroyKeyring);
228
244
  /**
229
245
  * Remove empty keyrings.
230
246
  *
231
247
  * Loops through the keyrings and removes the ones with empty accounts
232
248
  * (usually after removing the last / only account) from a keyring.
233
249
  */
234
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _removeEmptyKeyrings);
250
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _removeEmptyKeyrings);
235
251
  /**
236
252
  * Checks for duplicate keypairs, using the the first account in the given
237
253
  * array. Rejects if a duplicate is found.
@@ -242,15 +258,33 @@ var KeyringController = class extends _basecontroller.BaseController {
242
258
  * @param newAccountArray - Array of new accounts.
243
259
  * @returns The account, if no duplicate is found.
244
260
  */
245
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _checkForDuplicate);
261
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _checkForDuplicate);
246
262
  /**
247
263
  * Set the `isUnlocked` to true and notify listeners
248
264
  * through the messenger.
249
265
  *
250
266
  * @fires KeyringController:unlock
251
267
  */
252
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _setUnlocked);
253
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _getMemState);
268
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _setUnlocked);
269
+ /**
270
+ * Assert that the controller mutex is locked.
271
+ *
272
+ * @throws If the controller mutex is not locked.
273
+ */
274
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _assertControllerMutexIsLocked);
275
+ /**
276
+ * Lock the controller mutex before executing the given function,
277
+ * and release it after the function is resolved or after an
278
+ * error is thrown.
279
+ *
280
+ * This wrapper ensures that each mutable operation that interacts with the
281
+ * controller and that changes its state is executed in a mutually exclusive way,
282
+ * preventing unsafe concurrent access that could lead to unpredictable behavior.
283
+ *
284
+ * @param fn - The function to execute while the controller mutex is locked.
285
+ * @returns The result of the function.
286
+ */
287
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _withControllerLock);
254
288
  /**
255
289
  * Lock the vault mutex before executing the given function,
256
290
  * and release it after the function is resolved or after an
@@ -262,101 +296,99 @@ var KeyringController = class extends _basecontroller.BaseController {
262
296
  * @param fn - The function to execute while the vault mutex is locked.
263
297
  * @returns The result of the function.
264
298
  */
265
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _withVaultLock);
266
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _initVaultMutex, new (0, _asyncmutex.Mutex)());
267
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _vaultOperationMutex, new (0, _asyncmutex.Mutex)());
268
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _keyringBuilders, void 0);
269
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _keyrings, void 0);
270
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _unsupportedKeyrings, void 0);
271
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _password, void 0);
272
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _encryptor, void 0);
273
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _cacheEncryptionKey, void 0);
274
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _qrKeyringStateListener, void 0);
275
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyringBuilders, keyringBuilders ? defaultKeyringBuilders.concat(keyringBuilders) : defaultKeyringBuilders);
276
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _encryptor, encryptor);
277
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, []);
278
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _unsupportedKeyrings, []);
279
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _cacheEncryptionKey, Boolean(options.cacheEncryptionKey));
280
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
299
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _withVaultLock);
300
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _controllerOperationMutex, new (0, _asyncmutex.Mutex)());
301
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _vaultOperationMutex, new (0, _asyncmutex.Mutex)());
302
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _keyringBuilders, void 0);
303
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _keyrings, void 0);
304
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _unsupportedKeyrings, void 0);
305
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _password, void 0);
306
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _encryptor, void 0);
307
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _cacheEncryptionKey, void 0);
308
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _qrKeyringStateListener, void 0);
309
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyringBuilders, keyringBuilders ? defaultKeyringBuilders.concat(keyringBuilders) : defaultKeyringBuilders);
310
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _encryptor, encryptor);
311
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, []);
312
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _unsupportedKeyrings, []);
313
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _cacheEncryptionKey, Boolean(options.cacheEncryptionKey));
314
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
281
315
  assertIsExportableKeyEncryptor(encryptor);
282
316
  }
283
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
317
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
284
318
  }
285
319
  /**
286
320
  * Adds a new account to the default (first) HD seed phrase keyring.
287
321
  *
288
322
  * @param accountCount - Number of accounts before adding a new one, used to
289
323
  * make the method idempotent.
290
- * @returns Promise resolving to keyring current state and added account
291
- * address.
324
+ * @returns Promise resolving to the added account address.
292
325
  */
293
326
  async addNewAccount(accountCount) {
294
- const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
295
- if (!primaryKeyring) {
296
- throw new Error("No HD keyring found");
297
- }
298
- const oldAccounts = await primaryKeyring.getAccounts();
299
- if (accountCount && oldAccounts.length !== accountCount) {
300
- if (accountCount > oldAccounts.length) {
301
- throw new Error("Account out of sequence");
327
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
328
+ const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
329
+ if (!primaryKeyring) {
330
+ throw new Error("No HD keyring found");
302
331
  }
303
- const existingAccount = oldAccounts[accountCount];
304
- if (!existingAccount) {
305
- throw new Error(`Can't find account at index ${accountCount}`);
332
+ const oldAccounts = await primaryKeyring.getAccounts();
333
+ if (accountCount && oldAccounts.length !== accountCount) {
334
+ if (accountCount > oldAccounts.length) {
335
+ throw new Error("Account out of sequence");
336
+ }
337
+ const existingAccount = oldAccounts[accountCount];
338
+ if (!existingAccount) {
339
+ throw new Error(`Can't find account at index ${accountCount}`);
340
+ }
341
+ return existingAccount;
306
342
  }
307
- return {
308
- keyringState: _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this),
309
- addedAccountAddress: existingAccount
310
- };
311
- }
312
- const addedAccountAddress = await this.addNewAccountForKeyring(
313
- primaryKeyring
314
- );
315
- await this.verifySeedPhrase();
316
- return {
317
- keyringState: _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this),
318
- addedAccountAddress
319
- };
343
+ const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
344
+ await this.verifySeedPhrase();
345
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
346
+ return addedAccountAddress;
347
+ });
320
348
  }
321
349
  /**
322
350
  * Adds a new account to the specified keyring.
323
351
  *
324
352
  * @param keyring - Keyring to add the account to.
325
353
  * @param accountCount - Number of accounts before adding a new one, used to make the method idempotent.
326
- * @returns Promise resolving to keyring current state and added account
354
+ * @returns Promise resolving to the added account address
327
355
  */
328
356
  async addNewAccountForKeyring(keyring, accountCount) {
329
- const oldAccounts = await this.getAccounts();
330
- if (accountCount && oldAccounts.length !== accountCount) {
331
- if (accountCount > oldAccounts.length) {
332
- throw new Error("Account out of sequence");
357
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
358
+ const oldAccounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
359
+ if (accountCount && oldAccounts.length !== accountCount) {
360
+ if (accountCount > oldAccounts.length) {
361
+ throw new Error("Account out of sequence");
362
+ }
363
+ const existingAccount = oldAccounts[accountCount];
364
+ _utils.assertIsStrictHexString.call(void 0, existingAccount);
365
+ return existingAccount;
333
366
  }
334
- const existingAccount = oldAccounts[accountCount];
335
- _utils.assertIsStrictHexString.call(void 0, existingAccount);
336
- return existingAccount;
337
- }
338
- await keyring.addAccounts(1);
339
- await this.persistAllKeyrings();
340
- const addedAccountAddress = (await this.getAccounts()).find(
341
- (selectedAddress) => !oldAccounts.includes(selectedAddress)
342
- );
343
- _utils.assertIsStrictHexString.call(void 0, addedAccountAddress);
344
- return addedAccountAddress;
367
+ await keyring.addAccounts(1);
368
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
369
+ const addedAccountAddress = (await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this)).find(
370
+ (selectedAddress) => !oldAccounts.includes(selectedAddress)
371
+ );
372
+ _utils.assertIsStrictHexString.call(void 0, addedAccountAddress);
373
+ return addedAccountAddress;
374
+ });
345
375
  }
346
376
  /**
347
377
  * Adds a new account to the default (first) HD seed phrase keyring without updating identities in preferences.
348
378
  *
349
- * @returns Promise resolving to current state when the account is added.
379
+ * @returns Promise resolving to the added account address.
350
380
  */
351
381
  async addNewAccountWithoutUpdate() {
352
- const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
353
- if (!primaryKeyring) {
354
- throw new Error("No HD keyring found");
355
- }
356
- await primaryKeyring.addAccounts(1);
357
- await this.persistAllKeyrings();
358
- await this.verifySeedPhrase();
359
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
382
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
383
+ const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
384
+ if (!primaryKeyring) {
385
+ throw new Error("No HD keyring found");
386
+ }
387
+ const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
388
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
389
+ await this.verifySeedPhrase();
390
+ return addedAccountAddress;
391
+ });
360
392
  }
361
393
  /**
362
394
  * Effectively the same as creating a new keychain then populating it
@@ -365,45 +397,37 @@ var KeyringController = class extends _basecontroller.BaseController {
365
397
  * @param password - Password to unlock keychain.
366
398
  * @param seed - A BIP39-compliant seed phrase as Uint8Array,
367
399
  * either as a string or an array of UTF-8 bytes that represent the string.
368
- * @returns Promise resolving to the restored keychain object.
400
+ * @returns Promise resolving when the operation ends successfully.
369
401
  */
370
402
  async createNewVaultAndRestore(password, seed) {
371
- const releaseLock = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _initVaultMutex).acquire();
372
- if (!password || !password.length) {
373
- throw new Error("Invalid password");
374
- }
375
- try {
376
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
403
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
404
+ if (!password || !password.length) {
405
+ throw new Error("Invalid password");
406
+ }
407
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
377
408
  type: "HD Key Tree" /* hd */,
378
409
  opts: {
379
410
  mnemonic: seed,
380
411
  numberOfAccounts: 1
381
412
  }
382
413
  });
383
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
384
- } finally {
385
- releaseLock();
386
- }
414
+ });
387
415
  }
388
416
  /**
389
417
  * Create a new primary keychain and wipe any previous keychains.
390
418
  *
391
419
  * @param password - Password to unlock the new vault.
392
- * @returns Newly-created keychain object.
420
+ * @returns Promise resolving when the operation ends successfully.
393
421
  */
394
422
  async createNewVaultAndKeychain(password) {
395
- const releaseLock = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _initVaultMutex).acquire();
396
- try {
397
- const accounts = await this.getAccounts();
423
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
424
+ const accounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
398
425
  if (!accounts.length) {
399
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
426
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
400
427
  type: "HD Key Tree" /* hd */
401
428
  });
402
429
  }
403
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
404
- } finally {
405
- releaseLock();
406
- }
430
+ });
407
431
  }
408
432
  /**
409
433
  * Adds a new keyring of the given `type`.
@@ -417,21 +441,7 @@ var KeyringController = class extends _basecontroller.BaseController {
417
441
  if (type === "QR Hardware Wallet Device" /* qr */) {
418
442
  return this.getOrAddQRKeyring();
419
443
  }
420
- const keyring = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts);
421
- if (type === "HD Key Tree" /* hd */ && (!_utils.isObject.call(void 0, opts) || !opts.mnemonic)) {
422
- if (!keyring.generateRandomMnemonic) {
423
- throw new Error(
424
- "KeyringController - The current keyring does not support the method generateRandomMnemonic." /* UnsupportedGenerateRandomMnemonic */
425
- );
426
- }
427
- keyring.generateRandomMnemonic();
428
- await keyring.addAccounts(1);
429
- }
430
- const accounts = await keyring.getAccounts();
431
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, accounts);
432
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).push(keyring);
433
- await this.persistAllKeyrings();
434
- return keyring;
444
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts, true));
435
445
  }
436
446
  /**
437
447
  * Method to verify a given password validity. Throws an
@@ -443,7 +453,7 @@ var KeyringController = class extends _basecontroller.BaseController {
443
453
  if (!this.state.vault) {
444
454
  throw new Error("KeyringController - Cannot unlock without a previous vault." /* VaultError */);
445
455
  }
446
- await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decrypt(password, this.state.vault);
456
+ await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decrypt(password, this.state.vault);
447
457
  }
448
458
  /**
449
459
  * Returns the status of the vault.
@@ -461,8 +471,8 @@ var KeyringController = class extends _basecontroller.BaseController {
461
471
  */
462
472
  async exportSeedPhrase(password) {
463
473
  await this.verifyPassword(password);
464
- assertHasUint8ArrayMnemonic(_chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings)[0]);
465
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings)[0].mnemonic;
474
+ assertHasUint8ArrayMnemonic(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings)[0]);
475
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings)[0].mnemonic;
466
476
  }
467
477
  /**
468
478
  * Gets the private key from the keyring controlling an address.
@@ -487,14 +497,10 @@ var KeyringController = class extends _basecontroller.BaseController {
487
497
  * @returns A promise resolving to an array of addresses.
488
498
  */
489
499
  async getAccounts() {
490
- const keyrings = _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings);
491
- const keyringArrays = await Promise.all(
492
- keyrings.map(async (keyring) => keyring.getAccounts())
500
+ return this.state.keyrings.reduce(
501
+ (accounts, keyring) => accounts.concat(keyring.accounts),
502
+ []
493
503
  );
494
- const addresses = keyringArrays.reduce((res, arr) => {
495
- return res.concat(arr);
496
- }, []);
497
- return addresses.map(_ethsigutil.normalize);
498
504
  }
499
505
  /**
500
506
  * Get encryption public key.
@@ -545,7 +551,7 @@ var KeyringController = class extends _basecontroller.BaseController {
545
551
  async getKeyringForAccount(account) {
546
552
  const hexed = _ethsigutil.normalize.call(void 0, account);
547
553
  const candidates = await Promise.all(
548
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
554
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
549
555
  return Promise.all([keyring, keyring.getAccounts()]);
550
556
  })
551
557
  );
@@ -578,7 +584,7 @@ var KeyringController = class extends _basecontroller.BaseController {
578
584
  * @returns An array of keyrings of the given type.
579
585
  */
580
586
  getKeyringsByType(type) {
581
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).filter((keyring) => keyring.type === type);
587
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).filter((keyring) => keyring.type === type);
582
588
  }
583
589
  /**
584
590
  * Persist all serialized keyrings in the vault.
@@ -587,66 +593,7 @@ var KeyringController = class extends _basecontroller.BaseController {
587
593
  * operation completes.
588
594
  */
589
595
  async persistAllKeyrings() {
590
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async () => {
591
- const { encryptionKey, encryptionSalt } = this.state;
592
- if (!_chunkDNQK26H6js.__privateGet.call(void 0, this, _password) && !encryptionKey) {
593
- throw new Error("KeyringController - Cannot persist vault without password and encryption key" /* MissingCredentials */);
594
- }
595
- const serializedKeyrings = await Promise.all(
596
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
597
- const [type, data] = await Promise.all([
598
- keyring.type,
599
- keyring.serialize()
600
- ]);
601
- return { type, data };
602
- })
603
- );
604
- serializedKeyrings.push(..._chunkDNQK26H6js.__privateGet.call(void 0, this, _unsupportedKeyrings));
605
- if (!serializedKeyrings.some((keyring) => keyring.type === "HD Key Tree" /* hd */)) {
606
- throw new Error("KeyringController - No HD Keyring found" /* NoHdKeyring */);
607
- }
608
- const updatedState = {};
609
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
610
- assertIsExportableKeyEncryptor(_chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor));
611
- if (encryptionKey) {
612
- const key = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
613
- const vaultJSON = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).encryptWithKey(
614
- key,
615
- serializedKeyrings
616
- );
617
- vaultJSON.salt = encryptionSalt;
618
- updatedState.vault = JSON.stringify(vaultJSON);
619
- } else if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _password)) {
620
- const { vault: newVault, exportedKeyString } = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).encryptWithDetail(
621
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _password),
622
- serializedKeyrings
623
- );
624
- updatedState.vault = newVault;
625
- updatedState.encryptionKey = exportedKeyString;
626
- }
627
- } else {
628
- if (typeof _chunkDNQK26H6js.__privateGet.call(void 0, this, _password) !== "string") {
629
- throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
630
- }
631
- updatedState.vault = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).encrypt(
632
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _password),
633
- serializedKeyrings
634
- );
635
- }
636
- if (!updatedState.vault) {
637
- throw new Error("KeyringController - Cannot persist vault without vault information" /* MissingVaultData */);
638
- }
639
- const updatedKeyrings = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
640
- this.update((state) => {
641
- state.vault = updatedState.vault;
642
- state.keyrings = updatedKeyrings;
643
- if (updatedState.encryptionKey) {
644
- state.encryptionKey = updatedState.encryptionKey;
645
- state.encryptionSalt = JSON.parse(updatedState.vault).salt;
646
- }
647
- });
648
- return true;
649
- });
596
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this));
650
597
  }
651
598
  /**
652
599
  * Imports an account with the specified import strategy.
@@ -654,90 +601,88 @@ var KeyringController = class extends _basecontroller.BaseController {
654
601
  * @param strategy - Import strategy name.
655
602
  * @param args - Array of arguments to pass to the underlying stategy.
656
603
  * @throws Will throw when passed an unrecognized strategy.
657
- * @returns Promise resolving to keyring current state and imported account
658
- * address.
604
+ * @returns Promise resolving to the imported account address.
659
605
  */
660
606
  async importAccountWithStrategy(strategy, args) {
661
- let privateKey;
662
- switch (strategy) {
663
- case "privateKey":
664
- const [importedKey] = args;
665
- if (!importedKey) {
666
- throw new Error("Cannot import an empty key.");
667
- }
668
- const prefixed = _utils.add0x.call(void 0, importedKey);
669
- let bufferedPrivateKey;
670
- try {
671
- bufferedPrivateKey = _util.toBuffer.call(void 0, prefixed);
672
- } catch {
673
- throw new Error("Cannot import invalid private key.");
674
- }
675
- if (!_util.isValidPrivate.call(void 0, bufferedPrivateKey) || // ensures that the key is 64 bytes long
676
- _util.getBinarySize.call(void 0, prefixed) !== 64 + "0x".length) {
677
- throw new Error("Cannot import invalid private key.");
678
- }
679
- privateKey = _utils.remove0x.call(void 0, prefixed);
680
- break;
681
- case "json":
682
- let wallet;
683
- const [input, password] = args;
684
- try {
685
- wallet = _ethereumjswallet.thirdparty.fromEtherWallet(input, password);
686
- } catch (e) {
687
- wallet = wallet || await _ethereumjswallet2.default.fromV3(input, password, true);
688
- }
689
- privateKey = _utils.bytesToHex.call(void 0, wallet.getPrivateKey());
690
- break;
691
- default:
692
- throw new Error(`Unexpected import strategy: '${strategy}'`);
693
- }
694
- const newKeyring = await this.addNewKeyring("Simple Key Pair" /* simple */, [
695
- privateKey
696
- ]);
697
- const accounts = await newKeyring.getAccounts();
698
- return {
699
- keyringState: _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this),
700
- importedAccountAddress: accounts[0]
701
- };
607
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
608
+ let privateKey;
609
+ switch (strategy) {
610
+ case "privateKey":
611
+ const [importedKey] = args;
612
+ if (!importedKey) {
613
+ throw new Error("Cannot import an empty key.");
614
+ }
615
+ const prefixed = _utils.add0x.call(void 0, importedKey);
616
+ let bufferedPrivateKey;
617
+ try {
618
+ bufferedPrivateKey = _util.toBuffer.call(void 0, prefixed);
619
+ } catch {
620
+ throw new Error("Cannot import invalid private key.");
621
+ }
622
+ if (!_util.isValidPrivate.call(void 0, bufferedPrivateKey) || // ensures that the key is 64 bytes long
623
+ _util.getBinarySize.call(void 0, prefixed) !== 64 + "0x".length) {
624
+ throw new Error("Cannot import invalid private key.");
625
+ }
626
+ privateKey = _utils.remove0x.call(void 0, prefixed);
627
+ break;
628
+ case "json":
629
+ let wallet;
630
+ const [input, password] = args;
631
+ try {
632
+ wallet = _ethereumjswallet.thirdparty.fromEtherWallet(input, password);
633
+ } catch (e) {
634
+ wallet = wallet || await _ethereumjswallet2.default.fromV3(input, password, true);
635
+ }
636
+ privateKey = _utils.bytesToHex.call(void 0, wallet.getPrivateKey());
637
+ break;
638
+ default:
639
+ throw new Error(`Unexpected import strategy: '${strategy}'`);
640
+ }
641
+ const newKeyring = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, "Simple Key Pair" /* simple */, [privateKey], true);
642
+ const accounts = await newKeyring.getAccounts();
643
+ return accounts[0];
644
+ });
702
645
  }
703
646
  /**
704
647
  * Removes an account from keyring state.
705
648
  *
706
649
  * @param address - Address of the account to remove.
707
650
  * @fires KeyringController:accountRemoved
708
- * @returns Promise resolving current state when this account removal completes.
651
+ * @returns Promise resolving when the account is removed.
709
652
  */
710
653
  async removeAccount(address) {
711
- const keyring = await this.getKeyringForAccount(
712
- address
713
- );
714
- if (!keyring.removeAccount) {
715
- throw new Error("`KeyringController - The keyring for the current address does not support the method removeAccount" /* UnsupportedRemoveAccount */);
716
- }
717
- await keyring.removeAccount(address);
718
- const accounts = await keyring.getAccounts();
719
- if (accounts.length === 0) {
720
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _removeEmptyKeyrings, removeEmptyKeyrings_fn).call(this);
721
- }
722
- await this.persistAllKeyrings();
654
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
655
+ const keyring = await this.getKeyringForAccount(
656
+ address
657
+ );
658
+ if (!keyring.removeAccount) {
659
+ throw new Error("`KeyringController - The keyring for the current address does not support the method removeAccount" /* UnsupportedRemoveAccount */);
660
+ }
661
+ await keyring.removeAccount(address);
662
+ const accounts = await keyring.getAccounts();
663
+ if (accounts.length === 0) {
664
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _removeEmptyKeyrings, removeEmptyKeyrings_fn).call(this);
665
+ }
666
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
667
+ });
723
668
  this.messagingSystem.publish(`${name}:accountRemoved`, address);
724
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
725
669
  }
726
670
  /**
727
671
  * Deallocates all secrets and locks the wallet.
728
672
  *
729
- * @returns Promise resolving to current state.
673
+ * @returns Promise resolving when the operation completes.
730
674
  */
731
675
  async setLocked() {
732
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn).call(this);
733
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, void 0);
734
- this.update((state) => {
735
- state.isUnlocked = false;
736
- state.keyrings = [];
676
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
677
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn).call(this);
678
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, void 0);
679
+ this.update((state) => {
680
+ state.isUnlocked = false;
681
+ state.keyrings = [];
682
+ });
683
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
684
+ this.messagingSystem.publish(`${name}:lock`);
737
685
  });
738
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
739
- this.messagingSystem.publish(`${name}:lock`);
740
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
741
686
  }
742
687
  /**
743
688
  * Signs message by calling down into a specific keyring.
@@ -891,32 +836,34 @@ var KeyringController = class extends _basecontroller.BaseController {
891
836
  *
892
837
  * @param encryptionKey - Key to unlock the keychain.
893
838
  * @param encryptionSalt - Salt to unlock the keychain.
894
- * @returns Promise resolving to the current state.
839
+ * @returns Promise resolving when the operation completes.
895
840
  */
896
841
  async submitEncryptionKey(encryptionKey, encryptionSalt) {
897
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _unlockKeyrings, unlockKeyrings_fn).call(this, void 0, encryptionKey, encryptionSalt));
898
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
899
- const qrKeyring = this.getQRKeyring();
900
- if (qrKeyring) {
901
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
902
- }
903
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
842
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
843
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _unlockKeyrings, unlockKeyrings_fn).call(this, void 0, encryptionKey, encryptionSalt));
844
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
845
+ const qrKeyring = this.getQRKeyring();
846
+ if (qrKeyring) {
847
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
848
+ }
849
+ });
904
850
  }
905
851
  /**
906
852
  * Attempts to decrypt the current vault and load its keyrings,
907
853
  * using the given password.
908
854
  *
909
855
  * @param password - Password to unlock the keychain.
910
- * @returns Promise resolving to the current state.
856
+ * @returns Promise resolving when the operation completes.
911
857
  */
912
858
  async submitPassword(password) {
913
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _unlockKeyrings, unlockKeyrings_fn).call(this, password));
914
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
915
- const qrKeyring = this.getQRKeyring();
916
- if (qrKeyring) {
917
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
918
- }
919
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
859
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
860
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _unlockKeyrings, unlockKeyrings_fn).call(this, password));
861
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
862
+ const qrKeyring = this.getQRKeyring();
863
+ if (qrKeyring) {
864
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
865
+ }
866
+ });
920
867
  }
921
868
  /**
922
869
  * Verifies the that the seed phrase restores the current keychain's accounts.
@@ -934,7 +881,7 @@ var KeyringController = class extends _basecontroller.BaseController {
934
881
  if (accounts.length === 0) {
935
882
  throw new Error("Cannot verify an empty keyring.");
936
883
  }
937
- const hdKeyringBuilder = _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, "HD Key Tree" /* hd */);
884
+ const hdKeyringBuilder = _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, "HD Key Tree" /* hd */);
938
885
  const hdKeyring = hdKeyringBuilder();
939
886
  await hdKeyring.deserialize({
940
887
  mnemonic: seedWords,
@@ -966,13 +913,16 @@ var KeyringController = class extends _basecontroller.BaseController {
966
913
  * @returns The added keyring
967
914
  */
968
915
  async getOrAddQRKeyring() {
969
- return this.getQRKeyring() || await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
916
+ return this.getQRKeyring() || await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this));
970
917
  }
971
918
  // TODO: Replace `any` with type
972
919
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
973
920
  async restoreQRKeyring(serialized) {
974
- (await this.getOrAddQRKeyring()).deserialize(serialized);
975
- await this.persistAllKeyrings();
921
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
922
+ const keyring = this.getQRKeyring() || await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
923
+ keyring.deserialize(serialized);
924
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
925
+ });
976
926
  }
977
927
  async resetQRKeyringState() {
978
928
  (await this.getOrAddQRKeyring()).resetStore();
@@ -999,34 +949,38 @@ var KeyringController = class extends _basecontroller.BaseController {
999
949
  (await this.getOrAddQRKeyring()).cancelSync();
1000
950
  }
1001
951
  async connectQRHardware(page) {
1002
- try {
1003
- const keyring = await this.getOrAddQRKeyring();
1004
- let accounts;
1005
- switch (page) {
1006
- case -1:
1007
- accounts = await keyring.getPreviousPage();
1008
- break;
1009
- case 1:
1010
- accounts = await keyring.getNextPage();
1011
- break;
1012
- default:
1013
- accounts = await keyring.getFirstPage();
952
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
953
+ try {
954
+ const keyring = this.getQRKeyring() || await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
955
+ let accounts;
956
+ switch (page) {
957
+ case -1:
958
+ accounts = await keyring.getPreviousPage();
959
+ break;
960
+ case 1:
961
+ accounts = await keyring.getNextPage();
962
+ break;
963
+ default:
964
+ accounts = await keyring.getFirstPage();
965
+ }
966
+ return accounts.map((account) => {
967
+ return {
968
+ ...account,
969
+ balance: "0x0"
970
+ };
971
+ });
972
+ } catch (e) {
973
+ throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
1014
974
  }
1015
- return accounts.map((account) => {
1016
- return {
1017
- ...account,
1018
- balance: "0x0"
1019
- };
1020
- });
1021
- } catch (e) {
1022
- throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
1023
- }
975
+ });
1024
976
  }
1025
977
  async unlockQRHardwareWalletAccount(index) {
1026
- const keyring = await this.getOrAddQRKeyring();
1027
- keyring.setAccountToUnlock(index);
1028
- await this.addNewAccountForKeyring(keyring);
1029
- await this.persistAllKeyrings();
978
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
979
+ const keyring = this.getQRKeyring() || await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
980
+ keyring.setAccountToUnlock(index);
981
+ await keyring.addAccounts(1);
982
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
983
+ });
1030
984
  }
1031
985
  async getAccountKeyringType(account) {
1032
986
  const keyring = await this.getKeyringForAccount(
@@ -1035,18 +989,23 @@ var KeyringController = class extends _basecontroller.BaseController {
1035
989
  return keyring.type;
1036
990
  }
1037
991
  async forgetQRDevice() {
1038
- const keyring = await this.getOrAddQRKeyring();
1039
- const allAccounts = await this.getAccounts();
1040
- keyring.forgetDevice();
1041
- const remainingAccounts = await this.getAccounts();
1042
- const removedAccounts = allAccounts.filter(
1043
- (address) => !remainingAccounts.includes(address)
1044
- );
1045
- await this.persistAllKeyrings();
1046
- return { removedAccounts, remainingAccounts };
992
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async () => {
993
+ const keyring = this.getQRKeyring();
994
+ if (!keyring) {
995
+ return { removedAccounts: [], remainingAccounts: [] };
996
+ }
997
+ const allAccounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
998
+ keyring.forgetDevice();
999
+ const remainingAccounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
1000
+ const removedAccounts = allAccounts.filter(
1001
+ (address) => !remainingAccounts.includes(address)
1002
+ );
1003
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1004
+ return { removedAccounts, remainingAccounts };
1005
+ });
1047
1006
  }
1048
1007
  };
1049
- _initVaultMutex = new WeakMap();
1008
+ _controllerOperationMutex = new WeakMap();
1050
1009
  _vaultOperationMutex = new WeakMap();
1051
1010
  _keyringBuilders = new WeakMap();
1052
1011
  _keyrings = new WeakMap();
@@ -1108,28 +1067,29 @@ registerMessageHandlers_fn = function() {
1108
1067
  };
1109
1068
  _getKeyringBuilderForType = new WeakSet();
1110
1069
  getKeyringBuilderForType_fn = function(type) {
1111
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyringBuilders).find(
1070
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyringBuilders).find(
1112
1071
  (keyringBuilder) => keyringBuilder.type === type
1113
1072
  );
1114
1073
  };
1115
1074
  _addQRKeyring = new WeakSet();
1116
1075
  addQRKeyring_fn = async function() {
1117
- const qrKeyring = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, "QR Hardware Wallet Device" /* qr */, {
1076
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1077
+ const qrKeyring = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, "QR Hardware Wallet Device" /* qr */, {
1118
1078
  accounts: []
1119
1079
  });
1120
1080
  const accounts = await qrKeyring.getAccounts();
1121
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, "QR Hardware Wallet Device" /* qr */, accounts);
1122
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).push(qrKeyring);
1123
- await this.persistAllKeyrings();
1124
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
1081
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, "QR Hardware Wallet Device" /* qr */, accounts);
1082
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).push(qrKeyring);
1083
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1084
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
1125
1085
  return qrKeyring;
1126
1086
  };
1127
1087
  _subscribeToQRKeyringEvents = new WeakSet();
1128
1088
  subscribeToQRKeyringEvents_fn = function(qrKeyring) {
1129
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _qrKeyringStateListener, (state) => {
1089
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _qrKeyringStateListener, (state) => {
1130
1090
  this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
1131
1091
  });
1132
- qrKeyring.getMemStore().subscribe(_chunkDNQK26H6js.__privateGet.call(void 0, this, _qrKeyringStateListener));
1092
+ qrKeyring.getMemStore().subscribe(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _qrKeyringStateListener));
1133
1093
  };
1134
1094
  _unsubscribeFromQRKeyringsEvents = new WeakSet();
1135
1095
  unsubscribeFromQRKeyringsEvents_fn = function() {
@@ -1137,45 +1097,45 @@ unsubscribeFromQRKeyringsEvents_fn = function() {
1137
1097
  "QR Hardware Wallet Device" /* qr */
1138
1098
  );
1139
1099
  qrKeyrings.forEach((qrKeyring) => {
1140
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _qrKeyringStateListener)) {
1141
- qrKeyring.getMemStore().unsubscribe(_chunkDNQK26H6js.__privateGet.call(void 0, this, _qrKeyringStateListener));
1100
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _qrKeyringStateListener)) {
1101
+ qrKeyring.getMemStore().unsubscribe(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _qrKeyringStateListener));
1142
1102
  }
1143
1103
  });
1144
1104
  };
1145
1105
  _createNewVaultWithKeyring = new WeakSet();
1146
1106
  createNewVaultWithKeyring_fn = async function(password, keyring) {
1107
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1147
1108
  if (typeof password !== "string") {
1148
1109
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1149
1110
  }
1150
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, password);
1151
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
1152
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn).call(this, keyring.type, keyring.opts);
1153
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
1154
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
1111
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, password);
1112
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
1113
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn).call(this, keyring.type, keyring.opts);
1114
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
1155
1115
  };
1156
1116
  _getUpdatedKeyrings = new WeakSet();
1157
1117
  getUpdatedKeyrings_fn = async function() {
1158
- return Promise.all(_chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(displayForKeyring));
1118
+ return Promise.all(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(displayForKeyring));
1159
1119
  };
1160
1120
  _unlockKeyrings = new WeakSet();
1161
1121
  unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1162
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async ({ releaseLock }) => {
1122
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async ({ releaseLock }) => {
1163
1123
  const encryptedVault = this.state.vault;
1164
1124
  if (!encryptedVault) {
1165
1125
  throw new Error("KeyringController - Cannot unlock without a previous vault." /* VaultError */);
1166
1126
  }
1167
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this, { skipStateUpdate: true });
1127
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this, { skipStateUpdate: true });
1168
1128
  let vault;
1169
1129
  const updatedState = {};
1170
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1171
- assertIsExportableKeyEncryptor(_chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor));
1130
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1131
+ assertIsExportableKeyEncryptor(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor));
1172
1132
  if (password) {
1173
- const result = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decryptWithDetail(
1133
+ const result = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decryptWithDetail(
1174
1134
  password,
1175
1135
  encryptedVault
1176
1136
  );
1177
1137
  vault = result.vault;
1178
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, password);
1138
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, password);
1179
1139
  updatedState.encryptionKey = result.exportedKeyString;
1180
1140
  updatedState.encryptionSalt = result.salt;
1181
1141
  } else {
@@ -1186,8 +1146,8 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1186
1146
  if (typeof encryptionKey !== "string") {
1187
1147
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1188
1148
  }
1189
- const key = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
1190
- vault = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decryptWithKey(
1149
+ const key = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
1150
+ vault = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decryptWithKey(
1191
1151
  key,
1192
1152
  parsedEncryptedVault
1193
1153
  );
@@ -1198,14 +1158,14 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1198
1158
  if (typeof password !== "string") {
1199
1159
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1200
1160
  }
1201
- vault = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decrypt(password, encryptedVault);
1202
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, password);
1161
+ vault = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decrypt(password, encryptedVault);
1162
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, password);
1203
1163
  }
1204
1164
  if (!isSerializedKeyringsArray(vault)) {
1205
1165
  throw new Error("KeyringController - The decrypted vault has an unexpected shape." /* VaultDataError */);
1206
1166
  }
1207
- await Promise.all(vault.map(_chunkDNQK26H6js.__privateMethod.call(void 0, this, _restoreKeyring, restoreKeyring_fn).bind(this)));
1208
- const updatedKeyrings = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1167
+ await Promise.all(vault.map(_chunkQDPHKQONjs.__privateMethod.call(void 0, this, _restoreKeyring, restoreKeyring_fn).bind(this)));
1168
+ const updatedKeyrings = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1209
1169
  this.update((state) => {
1210
1170
  state.keyrings = updatedKeyrings;
1211
1171
  if (updatedState.encryptionKey || updatedState.encryptionSalt) {
@@ -1213,24 +1173,100 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1213
1173
  state.encryptionSalt = updatedState.encryptionSalt;
1214
1174
  }
1215
1175
  });
1216
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _password) && (!_chunkDNQK26H6js.__privateGet.call(void 0, this, _cacheEncryptionKey) || !encryptionKey) && _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).isVaultUpdated && !_chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).isVaultUpdated(encryptedVault)) {
1176
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _password) && (!_chunkQDPHKQONjs.__privateGet.call(void 0, this, _cacheEncryptionKey) || !encryptionKey) && _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).isVaultUpdated && !_chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).isVaultUpdated(encryptedVault)) {
1217
1177
  releaseLock();
1218
- await this.persistAllKeyrings();
1178
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1219
1179
  }
1220
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings);
1180
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings);
1221
1181
  });
1222
1182
  };
1183
+ _updateVault = new WeakSet();
1184
+ updateVault_fn = function() {
1185
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async () => {
1186
+ const { encryptionKey, encryptionSalt } = this.state;
1187
+ if (!_chunkQDPHKQONjs.__privateGet.call(void 0, this, _password) && !encryptionKey) {
1188
+ throw new Error("KeyringController - Cannot persist vault without password and encryption key" /* MissingCredentials */);
1189
+ }
1190
+ const serializedKeyrings = await Promise.all(
1191
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1192
+ const [type, data] = await Promise.all([
1193
+ keyring.type,
1194
+ keyring.serialize()
1195
+ ]);
1196
+ return { type, data };
1197
+ })
1198
+ );
1199
+ serializedKeyrings.push(..._chunkQDPHKQONjs.__privateGet.call(void 0, this, _unsupportedKeyrings));
1200
+ if (!serializedKeyrings.some((keyring) => keyring.type === "HD Key Tree" /* hd */)) {
1201
+ throw new Error("KeyringController - No HD Keyring found" /* NoHdKeyring */);
1202
+ }
1203
+ const updatedState = {};
1204
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1205
+ assertIsExportableKeyEncryptor(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor));
1206
+ if (encryptionKey) {
1207
+ const key = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
1208
+ const vaultJSON = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).encryptWithKey(
1209
+ key,
1210
+ serializedKeyrings
1211
+ );
1212
+ vaultJSON.salt = encryptionSalt;
1213
+ updatedState.vault = JSON.stringify(vaultJSON);
1214
+ } else if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _password)) {
1215
+ const { vault: newVault, exportedKeyString } = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).encryptWithDetail(
1216
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password),
1217
+ serializedKeyrings
1218
+ );
1219
+ updatedState.vault = newVault;
1220
+ updatedState.encryptionKey = exportedKeyString;
1221
+ }
1222
+ } else {
1223
+ if (typeof _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password) !== "string") {
1224
+ throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1225
+ }
1226
+ updatedState.vault = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).encrypt(
1227
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password),
1228
+ serializedKeyrings
1229
+ );
1230
+ }
1231
+ if (!updatedState.vault) {
1232
+ throw new Error("KeyringController - Cannot persist vault without vault information" /* MissingVaultData */);
1233
+ }
1234
+ const updatedKeyrings = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1235
+ this.update((state) => {
1236
+ state.vault = updatedState.vault;
1237
+ state.keyrings = updatedKeyrings;
1238
+ if (updatedState.encryptionKey) {
1239
+ state.encryptionKey = updatedState.encryptionKey;
1240
+ state.encryptionSalt = JSON.parse(updatedState.vault).salt;
1241
+ }
1242
+ });
1243
+ return true;
1244
+ });
1245
+ };
1246
+ _getAccountsFromKeyrings = new WeakSet();
1247
+ getAccountsFromKeyrings_fn = async function() {
1248
+ const keyrings = _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings);
1249
+ const keyringArrays = await Promise.all(
1250
+ keyrings.map(async (keyring) => keyring.getAccounts())
1251
+ );
1252
+ const addresses = keyringArrays.reduce((res, arr) => {
1253
+ return res.concat(arr);
1254
+ }, []);
1255
+ return addresses.map(_ethsigutil.normalize);
1256
+ };
1223
1257
  _createKeyringWithFirstAccount = new WeakSet();
1224
1258
  createKeyringWithFirstAccount_fn = async function(type, opts) {
1225
- const keyring = await this.addNewKeyring(type, opts);
1259
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1260
+ const keyring = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts, true);
1226
1261
  const [firstAccount] = await keyring.getAccounts();
1227
1262
  if (!firstAccount) {
1228
1263
  throw new Error("KeyringController - First Account not found." /* NoFirstAccount */);
1229
1264
  }
1230
1265
  };
1231
1266
  _newKeyring = new WeakSet();
1232
- newKeyring_fn = async function(type, data) {
1233
- const keyringBuilder = _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, type);
1267
+ newKeyring_fn = async function(type, data, persist = false) {
1268
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1269
+ const keyringBuilder = _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, type);
1234
1270
  if (!keyringBuilder) {
1235
1271
  throw new Error(
1236
1272
  `${"KeyringController - No keyringBuilder found for keyring" /* NoKeyringBuilder */}. Keyring type: ${type}`
@@ -1241,14 +1277,29 @@ newKeyring_fn = async function(type, data) {
1241
1277
  if (keyring.init) {
1242
1278
  await keyring.init();
1243
1279
  }
1280
+ if (type === "HD Key Tree" /* hd */ && (!_utils.isObject.call(void 0, data) || !data.mnemonic)) {
1281
+ if (!keyring.generateRandomMnemonic) {
1282
+ throw new Error(
1283
+ "KeyringController - The current keyring does not support the method generateRandomMnemonic." /* UnsupportedGenerateRandomMnemonic */
1284
+ );
1285
+ }
1286
+ keyring.generateRandomMnemonic();
1287
+ await keyring.addAccounts(1);
1288
+ }
1289
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, await keyring.getAccounts());
1290
+ if (persist) {
1291
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).push(keyring);
1292
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1293
+ }
1244
1294
  return keyring;
1245
1295
  };
1246
1296
  _clearKeyrings = new WeakSet();
1247
1297
  clearKeyrings_fn = async function(options = { skipStateUpdate: false }) {
1248
- for (const keyring of _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings)) {
1249
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
1298
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1299
+ for (const keyring of _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings)) {
1300
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
1250
1301
  }
1251
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, []);
1302
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, []);
1252
1303
  if (!options.skipStateUpdate) {
1253
1304
  this.update((state) => {
1254
1305
  state.keyrings = [];
@@ -1257,14 +1308,15 @@ clearKeyrings_fn = async function(options = { skipStateUpdate: false }) {
1257
1308
  };
1258
1309
  _restoreKeyring = new WeakSet();
1259
1310
  restoreKeyring_fn = async function(serialized) {
1311
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1260
1312
  try {
1261
1313
  const { type, data } = serialized;
1262
- const keyring = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, data);
1314
+ const keyring = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, data);
1263
1315
  await keyring.getAccounts();
1264
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).push(keyring);
1316
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).push(keyring);
1265
1317
  return keyring;
1266
1318
  } catch (_) {
1267
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _unsupportedKeyrings).push(serialized);
1319
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _unsupportedKeyrings).push(serialized);
1268
1320
  return void 0;
1269
1321
  }
1270
1322
  };
@@ -1274,22 +1326,23 @@ destroyKeyring_fn = async function(keyring) {
1274
1326
  };
1275
1327
  _removeEmptyKeyrings = new WeakSet();
1276
1328
  removeEmptyKeyrings_fn = async function() {
1329
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1277
1330
  const validKeyrings = [];
1278
1331
  await Promise.all(
1279
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1332
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1280
1333
  const accounts = await keyring.getAccounts();
1281
1334
  if (accounts.length > 0) {
1282
1335
  validKeyrings.push(keyring);
1283
1336
  } else {
1284
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
1337
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
1285
1338
  }
1286
1339
  })
1287
1340
  );
1288
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, validKeyrings);
1341
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, validKeyrings);
1289
1342
  };
1290
1343
  _checkForDuplicate = new WeakSet();
1291
1344
  checkForDuplicate_fn = async function(type, newAccountArray) {
1292
- const accounts = await this.getAccounts();
1345
+ const accounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
1293
1346
  switch (type) {
1294
1347
  case "Simple Key Pair" /* simple */: {
1295
1348
  const isIncluded = Boolean(
@@ -1309,27 +1362,35 @@ checkForDuplicate_fn = async function(type, newAccountArray) {
1309
1362
  };
1310
1363
  _setUnlocked = new WeakSet();
1311
1364
  setUnlocked_fn = function() {
1365
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1312
1366
  this.update((state) => {
1313
1367
  state.isUnlocked = true;
1314
1368
  });
1315
1369
  this.messagingSystem.publish(`${name}:unlock`);
1316
1370
  };
1317
- _getMemState = new WeakSet();
1318
- getMemState_fn = function() {
1319
- return {
1320
- isUnlocked: this.state.isUnlocked,
1321
- keyrings: this.state.keyrings
1322
- };
1371
+ _assertControllerMutexIsLocked = new WeakSet();
1372
+ assertControllerMutexIsLocked_fn = function() {
1373
+ if (!_chunkQDPHKQONjs.__privateGet.call(void 0, this, _controllerOperationMutex).isLocked()) {
1374
+ throw new Error("KeyringController - attempt to update vault during a non mutually exclusive operation" /* ControllerLockRequired */);
1375
+ }
1376
+ };
1377
+ _withControllerLock = new WeakSet();
1378
+ withControllerLock_fn = async function(fn) {
1379
+ return withLock(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _controllerOperationMutex), fn);
1323
1380
  };
1324
1381
  _withVaultLock = new WeakSet();
1325
1382
  withVaultLock_fn = async function(fn) {
1326
- const releaseLock = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _vaultOperationMutex).acquire();
1383
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1384
+ return withLock(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _vaultOperationMutex), fn);
1385
+ };
1386
+ async function withLock(mutex, fn) {
1387
+ const releaseLock = await mutex.acquire();
1327
1388
  try {
1328
1389
  return await fn({ releaseLock });
1329
1390
  } finally {
1330
1391
  releaseLock();
1331
1392
  }
1332
- };
1393
+ }
1333
1394
  var KeyringController_default = KeyringController;
1334
1395
 
1335
1396
 
@@ -1342,4 +1403,4 @@ var KeyringController_default = KeyringController;
1342
1403
 
1343
1404
 
1344
1405
  exports.KeyringTypes = KeyringTypes; exports.isCustodyKeyring = isCustodyKeyring; exports.AccountImportStrategy = AccountImportStrategy; exports.SignTypedDataVersion = SignTypedDataVersion; exports.keyringBuilderFactory = keyringBuilderFactory; exports.getDefaultKeyringState = getDefaultKeyringState; exports.KeyringController = KeyringController; exports.KeyringController_default = KeyringController_default;
1345
- //# sourceMappingURL=chunk-LKJCRVNR.js.map
1406
+ //# sourceMappingURL=chunk-ISACMNF3.js.map