@metamask-previews/keyring-controller 15.0.0-preview-2036edd1 → 15.0.0-preview-61ace68c

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, _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, _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,20 +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
183
  /**
184
184
  * Update the vault with the current keyrings.
185
185
  *
186
186
  * @returns A promise resolving to `true` if the operation is successful.
187
187
  */
188
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _updateVault);
188
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _updateVault);
189
189
  /**
190
190
  * Retrieves all the accounts from keyrings instances
191
191
  * that are currently in memory.
192
192
  *
193
193
  * @returns A promise resolving to an array of accounts.
194
194
  */
195
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _getAccountsFromKeyrings);
195
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _getAccountsFromKeyrings);
196
196
  /**
197
197
  * Create a new keyring, ensuring that the first account is
198
198
  * also created.
@@ -201,7 +201,7 @@ var KeyringController = class extends _basecontroller.BaseController {
201
201
  * @param opts - Optional parameters required to instantiate the keyring.
202
202
  * @returns A promise that resolves if the operation is successful.
203
203
  */
204
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _createKeyringWithFirstAccount);
204
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _createKeyringWithFirstAccount);
205
205
  /**
206
206
  * Instantiate, initialize and return a new keyring of the given `type`,
207
207
  * using the given `opts`. The keyring is built using the keyring builder
@@ -214,7 +214,7 @@ var KeyringController = class extends _basecontroller.BaseController {
214
214
  * @returns The new keyring.
215
215
  * @throws If the keyring includes duplicated accounts.
216
216
  */
217
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _newKeyring);
217
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _newKeyring);
218
218
  /**
219
219
  * Remove all managed keyrings, destroying all their
220
220
  * instances in memory.
@@ -222,7 +222,7 @@ var KeyringController = class extends _basecontroller.BaseController {
222
222
  * @param options - Operations options.
223
223
  * @param options.skipStateUpdate - Whether to skip updating the controller state.
224
224
  */
225
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _clearKeyrings);
225
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _clearKeyrings);
226
226
  /**
227
227
  * Restore a Keyring from a provided serialized payload.
228
228
  * On success, returns the resulting keyring instance.
@@ -230,7 +230,7 @@ var KeyringController = class extends _basecontroller.BaseController {
230
230
  * @param serialized - The serialized keyring.
231
231
  * @returns The deserialized keyring or undefined if the keyring type is unsupported.
232
232
  */
233
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _restoreKeyring);
233
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _restoreKeyring);
234
234
  /**
235
235
  * Destroy Keyring
236
236
  *
@@ -240,14 +240,14 @@ var KeyringController = class extends _basecontroller.BaseController {
240
240
  *
241
241
  * @param keyring - The keyring to destroy.
242
242
  */
243
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _destroyKeyring);
243
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _destroyKeyring);
244
244
  /**
245
245
  * Remove empty keyrings.
246
246
  *
247
247
  * Loops through the keyrings and removes the ones with empty accounts
248
248
  * (usually after removing the last / only account) from a keyring.
249
249
  */
250
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _removeEmptyKeyrings);
250
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _removeEmptyKeyrings);
251
251
  /**
252
252
  * Checks for duplicate keypairs, using the the first account in the given
253
253
  * array. Rejects if a duplicate is found.
@@ -258,15 +258,33 @@ var KeyringController = class extends _basecontroller.BaseController {
258
258
  * @param newAccountArray - Array of new accounts.
259
259
  * @returns The account, if no duplicate is found.
260
260
  */
261
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _checkForDuplicate);
261
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _checkForDuplicate);
262
262
  /**
263
263
  * Set the `isUnlocked` to true and notify listeners
264
264
  * through the messenger.
265
265
  *
266
266
  * @fires KeyringController:unlock
267
267
  */
268
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _setUnlocked);
269
- _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);
270
288
  /**
271
289
  * Lock the vault mutex before executing the given function,
272
290
  * and release it after the function is resolved or after an
@@ -278,100 +296,99 @@ var KeyringController = class extends _basecontroller.BaseController {
278
296
  * @param fn - The function to execute while the vault mutex is locked.
279
297
  * @returns The result of the function.
280
298
  */
281
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _withVaultLock);
282
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _initVaultMutex, new (0, _asyncmutex.Mutex)());
283
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _vaultOperationMutex, new (0, _asyncmutex.Mutex)());
284
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _keyringBuilders, void 0);
285
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _keyrings, void 0);
286
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _unsupportedKeyrings, void 0);
287
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _password, void 0);
288
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _encryptor, void 0);
289
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _cacheEncryptionKey, void 0);
290
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _qrKeyringStateListener, void 0);
291
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyringBuilders, keyringBuilders ? defaultKeyringBuilders.concat(keyringBuilders) : defaultKeyringBuilders);
292
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _encryptor, encryptor);
293
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, []);
294
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _unsupportedKeyrings, []);
295
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _cacheEncryptionKey, Boolean(options.cacheEncryptionKey));
296
- 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)) {
297
315
  assertIsExportableKeyEncryptor(encryptor);
298
316
  }
299
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
317
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
300
318
  }
301
319
  /**
302
320
  * Adds a new account to the default (first) HD seed phrase keyring.
303
321
  *
304
322
  * @param accountCount - Number of accounts before adding a new one, used to
305
323
  * make the method idempotent.
306
- * @returns Promise resolving to keyring current state and added account
307
- * address.
324
+ * @returns Promise resolving to the added account address.
308
325
  */
309
326
  async addNewAccount(accountCount) {
310
- const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
311
- if (!primaryKeyring) {
312
- throw new Error("No HD keyring found");
313
- }
314
- const oldAccounts = await primaryKeyring.getAccounts();
315
- if (accountCount && oldAccounts.length !== accountCount) {
316
- if (accountCount > oldAccounts.length) {
317
- 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");
318
331
  }
319
- const existingAccount = oldAccounts[accountCount];
320
- if (!existingAccount) {
321
- 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;
322
342
  }
323
- return {
324
- keyringState: _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this),
325
- addedAccountAddress: existingAccount
326
- };
327
- }
328
- const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
329
- await this.verifySeedPhrase();
330
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
331
- return {
332
- keyringState: _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this),
333
- addedAccountAddress
334
- };
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
+ });
335
348
  }
336
349
  /**
337
350
  * Adds a new account to the specified keyring.
338
351
  *
339
352
  * @param keyring - Keyring to add the account to.
340
353
  * @param accountCount - Number of accounts before adding a new one, used to make the method idempotent.
341
- * @returns Promise resolving to keyring current state and added account
354
+ * @returns Promise resolving to the added account address
342
355
  */
343
356
  async addNewAccountForKeyring(keyring, accountCount) {
344
- const oldAccounts = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
345
- if (accountCount && oldAccounts.length !== accountCount) {
346
- if (accountCount > oldAccounts.length) {
347
- 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;
348
366
  }
349
- const existingAccount = oldAccounts[accountCount];
350
- _utils.assertIsStrictHexString.call(void 0, existingAccount);
351
- return existingAccount;
352
- }
353
- await keyring.addAccounts(1);
354
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
355
- const addedAccountAddress = (await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this)).find(
356
- (selectedAddress) => !oldAccounts.includes(selectedAddress)
357
- );
358
- _utils.assertIsStrictHexString.call(void 0, addedAccountAddress);
359
- 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
+ });
360
375
  }
361
376
  /**
362
377
  * Adds a new account to the default (first) HD seed phrase keyring without updating identities in preferences.
363
378
  *
364
- * @returns Promise resolving to current state when the account is added.
379
+ * @returns Promise resolving to the added account address.
365
380
  */
366
381
  async addNewAccountWithoutUpdate() {
367
- const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
368
- if (!primaryKeyring) {
369
- throw new Error("No HD keyring found");
370
- }
371
- await primaryKeyring.addAccounts(1);
372
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
373
- await this.verifySeedPhrase();
374
- 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
+ });
375
392
  }
376
393
  /**
377
394
  * Effectively the same as creating a new keychain then populating it
@@ -380,45 +397,37 @@ var KeyringController = class extends _basecontroller.BaseController {
380
397
  * @param password - Password to unlock keychain.
381
398
  * @param seed - A BIP39-compliant seed phrase as Uint8Array,
382
399
  * either as a string or an array of UTF-8 bytes that represent the string.
383
- * @returns Promise resolving to the restored keychain object.
400
+ * @returns Promise resolving when the operation ends successfully.
384
401
  */
385
402
  async createNewVaultAndRestore(password, seed) {
386
- const releaseLock = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _initVaultMutex).acquire();
387
- if (!password || !password.length) {
388
- throw new Error("Invalid password");
389
- }
390
- try {
391
- 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, {
392
408
  type: "HD Key Tree" /* hd */,
393
409
  opts: {
394
410
  mnemonic: seed,
395
411
  numberOfAccounts: 1
396
412
  }
397
413
  });
398
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
399
- } finally {
400
- releaseLock();
401
- }
414
+ });
402
415
  }
403
416
  /**
404
417
  * Create a new primary keychain and wipe any previous keychains.
405
418
  *
406
419
  * @param password - Password to unlock the new vault.
407
- * @returns Newly-created keychain object.
420
+ * @returns Promise resolving when the operation ends successfully.
408
421
  */
409
422
  async createNewVaultAndKeychain(password) {
410
- const releaseLock = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _initVaultMutex).acquire();
411
- try {
412
- const accounts = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
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);
413
425
  if (!accounts.length) {
414
- 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, {
415
427
  type: "HD Key Tree" /* hd */
416
428
  });
417
429
  }
418
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
419
- } finally {
420
- releaseLock();
421
- }
430
+ });
422
431
  }
423
432
  /**
424
433
  * Adds a new keyring of the given `type`.
@@ -432,7 +441,7 @@ var KeyringController = class extends _basecontroller.BaseController {
432
441
  if (type === "QR Hardware Wallet Device" /* qr */) {
433
442
  return this.getOrAddQRKeyring();
434
443
  }
435
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts, true);
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));
436
445
  }
437
446
  /**
438
447
  * Method to verify a given password validity. Throws an
@@ -444,7 +453,7 @@ var KeyringController = class extends _basecontroller.BaseController {
444
453
  if (!this.state.vault) {
445
454
  throw new Error("KeyringController - Cannot unlock without a previous vault." /* VaultError */);
446
455
  }
447
- 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);
448
457
  }
449
458
  /**
450
459
  * Returns the status of the vault.
@@ -462,8 +471,8 @@ var KeyringController = class extends _basecontroller.BaseController {
462
471
  */
463
472
  async exportSeedPhrase(password) {
464
473
  await this.verifyPassword(password);
465
- assertHasUint8ArrayMnemonic(_chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings)[0]);
466
- 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;
467
476
  }
468
477
  /**
469
478
  * Gets the private key from the keyring controlling an address.
@@ -488,7 +497,10 @@ var KeyringController = class extends _basecontroller.BaseController {
488
497
  * @returns A promise resolving to an array of addresses.
489
498
  */
490
499
  async getAccounts() {
491
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
500
+ return this.state.keyrings.reduce(
501
+ (accounts, keyring) => accounts.concat(keyring.accounts),
502
+ []
503
+ );
492
504
  }
493
505
  /**
494
506
  * Get encryption public key.
@@ -539,7 +551,7 @@ var KeyringController = class extends _basecontroller.BaseController {
539
551
  async getKeyringForAccount(account) {
540
552
  const hexed = _ethsigutil.normalize.call(void 0, account);
541
553
  const candidates = await Promise.all(
542
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
554
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
543
555
  return Promise.all([keyring, keyring.getAccounts()]);
544
556
  })
545
557
  );
@@ -572,7 +584,7 @@ var KeyringController = class extends _basecontroller.BaseController {
572
584
  * @returns An array of keyrings of the given type.
573
585
  */
574
586
  getKeyringsByType(type) {
575
- 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);
576
588
  }
577
589
  /**
578
590
  * Persist all serialized keyrings in the vault.
@@ -581,7 +593,7 @@ var KeyringController = class extends _basecontroller.BaseController {
581
593
  * operation completes.
582
594
  */
583
595
  async persistAllKeyrings() {
584
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
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));
585
597
  }
586
598
  /**
587
599
  * Imports an account with the specified import strategy.
@@ -589,88 +601,88 @@ var KeyringController = class extends _basecontroller.BaseController {
589
601
  * @param strategy - Import strategy name.
590
602
  * @param args - Array of arguments to pass to the underlying stategy.
591
603
  * @throws Will throw when passed an unrecognized strategy.
592
- * @returns Promise resolving to keyring current state and imported account
593
- * address.
604
+ * @returns Promise resolving to the imported account address.
594
605
  */
595
606
  async importAccountWithStrategy(strategy, args) {
596
- let privateKey;
597
- switch (strategy) {
598
- case "privateKey":
599
- const [importedKey] = args;
600
- if (!importedKey) {
601
- throw new Error("Cannot import an empty key.");
602
- }
603
- const prefixed = _utils.add0x.call(void 0, importedKey);
604
- let bufferedPrivateKey;
605
- try {
606
- bufferedPrivateKey = _util.toBuffer.call(void 0, prefixed);
607
- } catch {
608
- throw new Error("Cannot import invalid private key.");
609
- }
610
- if (!_util.isValidPrivate.call(void 0, bufferedPrivateKey) || // ensures that the key is 64 bytes long
611
- _util.getBinarySize.call(void 0, prefixed) !== 64 + "0x".length) {
612
- throw new Error("Cannot import invalid private key.");
613
- }
614
- privateKey = _utils.remove0x.call(void 0, prefixed);
615
- break;
616
- case "json":
617
- let wallet;
618
- const [input, password] = args;
619
- try {
620
- wallet = _ethereumjswallet.thirdparty.fromEtherWallet(input, password);
621
- } catch (e) {
622
- wallet = wallet || await _ethereumjswallet2.default.fromV3(input, password, true);
623
- }
624
- privateKey = _utils.bytesToHex.call(void 0, wallet.getPrivateKey());
625
- break;
626
- default:
627
- throw new Error(`Unexpected import strategy: '${strategy}'`);
628
- }
629
- const newKeyring = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, "Simple Key Pair" /* simple */, [privateKey], true);
630
- const accounts = await newKeyring.getAccounts();
631
- return {
632
- keyringState: _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this),
633
- importedAccountAddress: accounts[0]
634
- };
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
+ });
635
645
  }
636
646
  /**
637
647
  * Removes an account from keyring state.
638
648
  *
639
649
  * @param address - Address of the account to remove.
640
650
  * @fires KeyringController:accountRemoved
641
- * @returns Promise resolving current state when this account removal completes.
651
+ * @returns Promise resolving when the account is removed.
642
652
  */
643
653
  async removeAccount(address) {
644
- const keyring = await this.getKeyringForAccount(
645
- address
646
- );
647
- if (!keyring.removeAccount) {
648
- throw new Error("`KeyringController - The keyring for the current address does not support the method removeAccount" /* UnsupportedRemoveAccount */);
649
- }
650
- await keyring.removeAccount(address);
651
- const accounts = await keyring.getAccounts();
652
- if (accounts.length === 0) {
653
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _removeEmptyKeyrings, removeEmptyKeyrings_fn).call(this);
654
- }
655
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
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
+ });
656
668
  this.messagingSystem.publish(`${name}:accountRemoved`, address);
657
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
658
669
  }
659
670
  /**
660
671
  * Deallocates all secrets and locks the wallet.
661
672
  *
662
- * @returns Promise resolving to current state.
673
+ * @returns Promise resolving when the operation completes.
663
674
  */
664
675
  async setLocked() {
665
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn).call(this);
666
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, void 0);
667
- this.update((state) => {
668
- state.isUnlocked = false;
669
- 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`);
670
685
  });
671
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
672
- this.messagingSystem.publish(`${name}:lock`);
673
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
674
686
  }
675
687
  /**
676
688
  * Signs message by calling down into a specific keyring.
@@ -824,32 +836,34 @@ var KeyringController = class extends _basecontroller.BaseController {
824
836
  *
825
837
  * @param encryptionKey - Key to unlock the keychain.
826
838
  * @param encryptionSalt - Salt to unlock the keychain.
827
- * @returns Promise resolving to the current state.
839
+ * @returns Promise resolving when the operation completes.
828
840
  */
829
841
  async submitEncryptionKey(encryptionKey, encryptionSalt) {
830
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _unlockKeyrings, unlockKeyrings_fn).call(this, void 0, encryptionKey, encryptionSalt));
831
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
832
- const qrKeyring = this.getQRKeyring();
833
- if (qrKeyring) {
834
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
835
- }
836
- 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
+ });
837
850
  }
838
851
  /**
839
852
  * Attempts to decrypt the current vault and load its keyrings,
840
853
  * using the given password.
841
854
  *
842
855
  * @param password - Password to unlock the keychain.
843
- * @returns Promise resolving to the current state.
856
+ * @returns Promise resolving when the operation completes.
844
857
  */
845
858
  async submitPassword(password) {
846
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _unlockKeyrings, unlockKeyrings_fn).call(this, password));
847
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
848
- const qrKeyring = this.getQRKeyring();
849
- if (qrKeyring) {
850
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
851
- }
852
- 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
+ });
853
867
  }
854
868
  /**
855
869
  * Verifies the that the seed phrase restores the current keychain's accounts.
@@ -867,7 +881,7 @@ var KeyringController = class extends _basecontroller.BaseController {
867
881
  if (accounts.length === 0) {
868
882
  throw new Error("Cannot verify an empty keyring.");
869
883
  }
870
- 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 */);
871
885
  const hdKeyring = hdKeyringBuilder();
872
886
  await hdKeyring.deserialize({
873
887
  mnemonic: seedWords,
@@ -899,14 +913,16 @@ var KeyringController = class extends _basecontroller.BaseController {
899
913
  * @returns The added keyring
900
914
  */
901
915
  async getOrAddQRKeyring() {
902
- 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));
903
917
  }
904
918
  // TODO: Replace `any` with type
905
919
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
906
920
  async restoreQRKeyring(serialized) {
907
- const keyring = this.getQRKeyring() || await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
908
- keyring.deserialize(serialized);
909
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
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
+ });
910
926
  }
911
927
  async resetQRKeyringState() {
912
928
  (await this.getOrAddQRKeyring()).resetStore();
@@ -933,34 +949,38 @@ var KeyringController = class extends _basecontroller.BaseController {
933
949
  (await this.getOrAddQRKeyring()).cancelSync();
934
950
  }
935
951
  async connectQRHardware(page) {
936
- try {
937
- const keyring = this.getQRKeyring() || await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
938
- let accounts;
939
- switch (page) {
940
- case -1:
941
- accounts = await keyring.getPreviousPage();
942
- break;
943
- case 1:
944
- accounts = await keyring.getNextPage();
945
- break;
946
- default:
947
- 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}`);
948
974
  }
949
- return accounts.map((account) => {
950
- return {
951
- ...account,
952
- balance: "0x0"
953
- };
954
- });
955
- } catch (e) {
956
- throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
957
- }
975
+ });
958
976
  }
959
977
  async unlockQRHardwareWalletAccount(index) {
960
- const keyring = this.getQRKeyring() || await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
961
- keyring.setAccountToUnlock(index);
962
- await keyring.addAccounts(1);
963
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
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
+ });
964
984
  }
965
985
  async getAccountKeyringType(account) {
966
986
  const keyring = await this.getKeyringForAccount(
@@ -969,21 +989,23 @@ var KeyringController = class extends _basecontroller.BaseController {
969
989
  return keyring.type;
970
990
  }
971
991
  async forgetQRDevice() {
972
- const keyring = this.getQRKeyring();
973
- if (!keyring) {
974
- return { removedAccounts: [], remainingAccounts: [] };
975
- }
976
- const allAccounts = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
977
- keyring.forgetDevice();
978
- const remainingAccounts = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
979
- const removedAccounts = allAccounts.filter(
980
- (address) => !remainingAccounts.includes(address)
981
- );
982
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
983
- 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
+ });
984
1006
  }
985
1007
  };
986
- _initVaultMutex = new WeakMap();
1008
+ _controllerOperationMutex = new WeakMap();
987
1009
  _vaultOperationMutex = new WeakMap();
988
1010
  _keyringBuilders = new WeakMap();
989
1011
  _keyrings = new WeakMap();
@@ -1045,28 +1067,29 @@ registerMessageHandlers_fn = function() {
1045
1067
  };
1046
1068
  _getKeyringBuilderForType = new WeakSet();
1047
1069
  getKeyringBuilderForType_fn = function(type) {
1048
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyringBuilders).find(
1070
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyringBuilders).find(
1049
1071
  (keyringBuilder) => keyringBuilder.type === type
1050
1072
  );
1051
1073
  };
1052
1074
  _addQRKeyring = new WeakSet();
1053
1075
  addQRKeyring_fn = async function() {
1054
- 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 */, {
1055
1078
  accounts: []
1056
1079
  });
1057
1080
  const accounts = await qrKeyring.getAccounts();
1058
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, "QR Hardware Wallet Device" /* qr */, accounts);
1059
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).push(qrKeyring);
1060
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1061
- _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);
1062
1085
  return qrKeyring;
1063
1086
  };
1064
1087
  _subscribeToQRKeyringEvents = new WeakSet();
1065
1088
  subscribeToQRKeyringEvents_fn = function(qrKeyring) {
1066
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _qrKeyringStateListener, (state) => {
1089
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _qrKeyringStateListener, (state) => {
1067
1090
  this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
1068
1091
  });
1069
- qrKeyring.getMemStore().subscribe(_chunkDNQK26H6js.__privateGet.call(void 0, this, _qrKeyringStateListener));
1092
+ qrKeyring.getMemStore().subscribe(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _qrKeyringStateListener));
1070
1093
  };
1071
1094
  _unsubscribeFromQRKeyringsEvents = new WeakSet();
1072
1095
  unsubscribeFromQRKeyringsEvents_fn = function() {
@@ -1074,45 +1097,45 @@ unsubscribeFromQRKeyringsEvents_fn = function() {
1074
1097
  "QR Hardware Wallet Device" /* qr */
1075
1098
  );
1076
1099
  qrKeyrings.forEach((qrKeyring) => {
1077
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _qrKeyringStateListener)) {
1078
- 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));
1079
1102
  }
1080
1103
  });
1081
1104
  };
1082
1105
  _createNewVaultWithKeyring = new WeakSet();
1083
1106
  createNewVaultWithKeyring_fn = async function(password, keyring) {
1107
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1084
1108
  if (typeof password !== "string") {
1085
1109
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1086
1110
  }
1087
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, password);
1088
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
1089
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn).call(this, keyring.type, keyring.opts);
1090
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
1091
- 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);
1092
1115
  };
1093
1116
  _getUpdatedKeyrings = new WeakSet();
1094
1117
  getUpdatedKeyrings_fn = async function() {
1095
- 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));
1096
1119
  };
1097
1120
  _unlockKeyrings = new WeakSet();
1098
1121
  unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1099
- 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 }) => {
1100
1123
  const encryptedVault = this.state.vault;
1101
1124
  if (!encryptedVault) {
1102
1125
  throw new Error("KeyringController - Cannot unlock without a previous vault." /* VaultError */);
1103
1126
  }
1104
- 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 });
1105
1128
  let vault;
1106
1129
  const updatedState = {};
1107
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1108
- 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));
1109
1132
  if (password) {
1110
- const result = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decryptWithDetail(
1133
+ const result = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decryptWithDetail(
1111
1134
  password,
1112
1135
  encryptedVault
1113
1136
  );
1114
1137
  vault = result.vault;
1115
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, password);
1138
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, password);
1116
1139
  updatedState.encryptionKey = result.exportedKeyString;
1117
1140
  updatedState.encryptionSalt = result.salt;
1118
1141
  } else {
@@ -1123,8 +1146,8 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1123
1146
  if (typeof encryptionKey !== "string") {
1124
1147
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1125
1148
  }
1126
- const key = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
1127
- 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(
1128
1151
  key,
1129
1152
  parsedEncryptedVault
1130
1153
  );
@@ -1135,14 +1158,14 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1135
1158
  if (typeof password !== "string") {
1136
1159
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1137
1160
  }
1138
- vault = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decrypt(password, encryptedVault);
1139
- _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);
1140
1163
  }
1141
1164
  if (!isSerializedKeyringsArray(vault)) {
1142
1165
  throw new Error("KeyringController - The decrypted vault has an unexpected shape." /* VaultDataError */);
1143
1166
  }
1144
- await Promise.all(vault.map(_chunkDNQK26H6js.__privateMethod.call(void 0, this, _restoreKeyring, restoreKeyring_fn).bind(this)));
1145
- 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);
1146
1169
  this.update((state) => {
1147
1170
  state.keyrings = updatedKeyrings;
1148
1171
  if (updatedState.encryptionKey || updatedState.encryptionSalt) {
@@ -1150,22 +1173,22 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1150
1173
  state.encryptionSalt = updatedState.encryptionSalt;
1151
1174
  }
1152
1175
  });
1153
- 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)) {
1154
1177
  releaseLock();
1155
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1178
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1156
1179
  }
1157
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings);
1180
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings);
1158
1181
  });
1159
1182
  };
1160
1183
  _updateVault = new WeakSet();
1161
1184
  updateVault_fn = function() {
1162
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async () => {
1185
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async () => {
1163
1186
  const { encryptionKey, encryptionSalt } = this.state;
1164
- if (!_chunkDNQK26H6js.__privateGet.call(void 0, this, _password) && !encryptionKey) {
1187
+ if (!_chunkQDPHKQONjs.__privateGet.call(void 0, this, _password) && !encryptionKey) {
1165
1188
  throw new Error("KeyringController - Cannot persist vault without password and encryption key" /* MissingCredentials */);
1166
1189
  }
1167
1190
  const serializedKeyrings = await Promise.all(
1168
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1191
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1169
1192
  const [type, data] = await Promise.all([
1170
1193
  keyring.type,
1171
1194
  keyring.serialize()
@@ -1173,42 +1196,42 @@ updateVault_fn = function() {
1173
1196
  return { type, data };
1174
1197
  })
1175
1198
  );
1176
- serializedKeyrings.push(..._chunkDNQK26H6js.__privateGet.call(void 0, this, _unsupportedKeyrings));
1199
+ serializedKeyrings.push(..._chunkQDPHKQONjs.__privateGet.call(void 0, this, _unsupportedKeyrings));
1177
1200
  if (!serializedKeyrings.some((keyring) => keyring.type === "HD Key Tree" /* hd */)) {
1178
1201
  throw new Error("KeyringController - No HD Keyring found" /* NoHdKeyring */);
1179
1202
  }
1180
1203
  const updatedState = {};
1181
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1182
- assertIsExportableKeyEncryptor(_chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor));
1204
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1205
+ assertIsExportableKeyEncryptor(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor));
1183
1206
  if (encryptionKey) {
1184
- const key = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
1185
- const vaultJSON = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).encryptWithKey(
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(
1186
1209
  key,
1187
1210
  serializedKeyrings
1188
1211
  );
1189
1212
  vaultJSON.salt = encryptionSalt;
1190
1213
  updatedState.vault = JSON.stringify(vaultJSON);
1191
- } else if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _password)) {
1192
- const { vault: newVault, exportedKeyString } = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).encryptWithDetail(
1193
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _password),
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),
1194
1217
  serializedKeyrings
1195
1218
  );
1196
1219
  updatedState.vault = newVault;
1197
1220
  updatedState.encryptionKey = exportedKeyString;
1198
1221
  }
1199
1222
  } else {
1200
- if (typeof _chunkDNQK26H6js.__privateGet.call(void 0, this, _password) !== "string") {
1223
+ if (typeof _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password) !== "string") {
1201
1224
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1202
1225
  }
1203
- updatedState.vault = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).encrypt(
1204
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _password),
1226
+ updatedState.vault = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).encrypt(
1227
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password),
1205
1228
  serializedKeyrings
1206
1229
  );
1207
1230
  }
1208
1231
  if (!updatedState.vault) {
1209
1232
  throw new Error("KeyringController - Cannot persist vault without vault information" /* MissingVaultData */);
1210
1233
  }
1211
- const updatedKeyrings = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1234
+ const updatedKeyrings = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1212
1235
  this.update((state) => {
1213
1236
  state.vault = updatedState.vault;
1214
1237
  state.keyrings = updatedKeyrings;
@@ -1222,7 +1245,7 @@ updateVault_fn = function() {
1222
1245
  };
1223
1246
  _getAccountsFromKeyrings = new WeakSet();
1224
1247
  getAccountsFromKeyrings_fn = async function() {
1225
- const keyrings = _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings);
1248
+ const keyrings = _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings);
1226
1249
  const keyringArrays = await Promise.all(
1227
1250
  keyrings.map(async (keyring) => keyring.getAccounts())
1228
1251
  );
@@ -1233,7 +1256,8 @@ getAccountsFromKeyrings_fn = async function() {
1233
1256
  };
1234
1257
  _createKeyringWithFirstAccount = new WeakSet();
1235
1258
  createKeyringWithFirstAccount_fn = async function(type, opts) {
1236
- const keyring = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts, true);
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);
1237
1261
  const [firstAccount] = await keyring.getAccounts();
1238
1262
  if (!firstAccount) {
1239
1263
  throw new Error("KeyringController - First Account not found." /* NoFirstAccount */);
@@ -1241,7 +1265,8 @@ createKeyringWithFirstAccount_fn = async function(type, opts) {
1241
1265
  };
1242
1266
  _newKeyring = new WeakSet();
1243
1267
  newKeyring_fn = async function(type, data, persist = false) {
1244
- const keyringBuilder = _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, type);
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);
1245
1270
  if (!keyringBuilder) {
1246
1271
  throw new Error(
1247
1272
  `${"KeyringController - No keyringBuilder found for keyring" /* NoKeyringBuilder */}. Keyring type: ${type}`
@@ -1261,19 +1286,20 @@ newKeyring_fn = async function(type, data, persist = false) {
1261
1286
  keyring.generateRandomMnemonic();
1262
1287
  await keyring.addAccounts(1);
1263
1288
  }
1264
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, await keyring.getAccounts());
1289
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, await keyring.getAccounts());
1265
1290
  if (persist) {
1266
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).push(keyring);
1267
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1291
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).push(keyring);
1292
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1268
1293
  }
1269
1294
  return keyring;
1270
1295
  };
1271
1296
  _clearKeyrings = new WeakSet();
1272
1297
  clearKeyrings_fn = async function(options = { skipStateUpdate: false }) {
1273
- for (const keyring of _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings)) {
1274
- 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);
1275
1301
  }
1276
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, []);
1302
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, []);
1277
1303
  if (!options.skipStateUpdate) {
1278
1304
  this.update((state) => {
1279
1305
  state.keyrings = [];
@@ -1282,14 +1308,15 @@ clearKeyrings_fn = async function(options = { skipStateUpdate: false }) {
1282
1308
  };
1283
1309
  _restoreKeyring = new WeakSet();
1284
1310
  restoreKeyring_fn = async function(serialized) {
1311
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1285
1312
  try {
1286
1313
  const { type, data } = serialized;
1287
- 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);
1288
1315
  await keyring.getAccounts();
1289
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).push(keyring);
1316
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).push(keyring);
1290
1317
  return keyring;
1291
1318
  } catch (_) {
1292
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _unsupportedKeyrings).push(serialized);
1319
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _unsupportedKeyrings).push(serialized);
1293
1320
  return void 0;
1294
1321
  }
1295
1322
  };
@@ -1299,22 +1326,23 @@ destroyKeyring_fn = async function(keyring) {
1299
1326
  };
1300
1327
  _removeEmptyKeyrings = new WeakSet();
1301
1328
  removeEmptyKeyrings_fn = async function() {
1329
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1302
1330
  const validKeyrings = [];
1303
1331
  await Promise.all(
1304
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1332
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1305
1333
  const accounts = await keyring.getAccounts();
1306
1334
  if (accounts.length > 0) {
1307
1335
  validKeyrings.push(keyring);
1308
1336
  } else {
1309
- 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);
1310
1338
  }
1311
1339
  })
1312
1340
  );
1313
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, validKeyrings);
1341
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, validKeyrings);
1314
1342
  };
1315
1343
  _checkForDuplicate = new WeakSet();
1316
1344
  checkForDuplicate_fn = async function(type, newAccountArray) {
1317
- const accounts = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
1345
+ const accounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
1318
1346
  switch (type) {
1319
1347
  case "Simple Key Pair" /* simple */: {
1320
1348
  const isIncluded = Boolean(
@@ -1334,27 +1362,35 @@ checkForDuplicate_fn = async function(type, newAccountArray) {
1334
1362
  };
1335
1363
  _setUnlocked = new WeakSet();
1336
1364
  setUnlocked_fn = function() {
1365
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1337
1366
  this.update((state) => {
1338
1367
  state.isUnlocked = true;
1339
1368
  });
1340
1369
  this.messagingSystem.publish(`${name}:unlock`);
1341
1370
  };
1342
- _getMemState = new WeakSet();
1343
- getMemState_fn = function() {
1344
- return {
1345
- isUnlocked: this.state.isUnlocked,
1346
- keyrings: this.state.keyrings
1347
- };
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);
1348
1380
  };
1349
1381
  _withVaultLock = new WeakSet();
1350
1382
  withVaultLock_fn = async function(fn) {
1351
- 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();
1352
1388
  try {
1353
1389
  return await fn({ releaseLock });
1354
1390
  } finally {
1355
1391
  releaseLock();
1356
1392
  }
1357
- };
1393
+ }
1358
1394
  var KeyringController_default = KeyringController;
1359
1395
 
1360
1396
 
@@ -1367,4 +1403,4 @@ var KeyringController_default = KeyringController;
1367
1403
 
1368
1404
 
1369
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;
1370
- //# sourceMappingURL=chunk-STOYI7AT.js.map
1406
+ //# sourceMappingURL=chunk-ISACMNF3.js.map