@metamask-previews/keyring-controller 15.0.0-preview-aedc5103 → 15.0.0-preview-cb17e99

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, _getSerializedKeyrings, getSerializedKeyrings_fn, _restoreSerializedKeyrings, restoreSerializedKeyrings_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, _persistOrRollback, persistOrRollback_fn, _withRollback, withRollback_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,29 @@ 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
+ /**
174
+ * Serialize the current array of keyring instances,
175
+ * including unsupported keyrings by default.
176
+ *
177
+ * @param options - Method options.
178
+ * @param options.includeUnsupported - Whether to include unsupported keyrings.
179
+ * @returns The serialized keyrings.
180
+ */
181
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _getSerializedKeyrings);
182
+ /**
183
+ * Restore a serialized keyrings array.
184
+ *
185
+ * @param serializedKeyrings - The serialized keyrings array.
186
+ */
187
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _restoreSerializedKeyrings);
173
188
  /**
174
189
  * Unlock Keyrings, decrypting the vault and deserializing all
175
190
  * keyrings contained in it, using a password or an encryption key with salt.
@@ -179,20 +194,20 @@ var KeyringController = class extends _basecontroller.BaseController {
179
194
  * @param encryptionSalt - The salt used to encrypt the vault.
180
195
  * @returns A promise resolving to the deserialized keyrings array.
181
196
  */
182
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _unlockKeyrings);
197
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _unlockKeyrings);
183
198
  /**
184
199
  * Update the vault with the current keyrings.
185
200
  *
186
201
  * @returns A promise resolving to `true` if the operation is successful.
187
202
  */
188
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _updateVault);
203
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _updateVault);
189
204
  /**
190
205
  * Retrieves all the accounts from keyrings instances
191
206
  * that are currently in memory.
192
207
  *
193
208
  * @returns A promise resolving to an array of accounts.
194
209
  */
195
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _getAccountsFromKeyrings);
210
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _getAccountsFromKeyrings);
196
211
  /**
197
212
  * Create a new keyring, ensuring that the first account is
198
213
  * also created.
@@ -201,7 +216,7 @@ var KeyringController = class extends _basecontroller.BaseController {
201
216
  * @param opts - Optional parameters required to instantiate the keyring.
202
217
  * @returns A promise that resolves if the operation is successful.
203
218
  */
204
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _createKeyringWithFirstAccount);
219
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _createKeyringWithFirstAccount);
205
220
  /**
206
221
  * Instantiate, initialize and return a new keyring of the given `type`,
207
222
  * using the given `opts`. The keyring is built using the keyring builder
@@ -210,19 +225,15 @@ var KeyringController = class extends _basecontroller.BaseController {
210
225
  *
211
226
  * @param type - The type of keyring to add.
212
227
  * @param data - The data to restore a previously serialized keyring.
213
- * @param persist - Whether to persist the keyring to the vault.
214
228
  * @returns The new keyring.
215
229
  * @throws If the keyring includes duplicated accounts.
216
230
  */
217
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _newKeyring);
231
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _newKeyring);
218
232
  /**
219
233
  * Remove all managed keyrings, destroying all their
220
234
  * instances in memory.
221
- *
222
- * @param options - Operations options.
223
- * @param options.skipStateUpdate - Whether to skip updating the controller state.
224
235
  */
225
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _clearKeyrings);
236
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _clearKeyrings);
226
237
  /**
227
238
  * Restore a Keyring from a provided serialized payload.
228
239
  * On success, returns the resulting keyring instance.
@@ -230,7 +241,7 @@ var KeyringController = class extends _basecontroller.BaseController {
230
241
  * @param serialized - The serialized keyring.
231
242
  * @returns The deserialized keyring or undefined if the keyring type is unsupported.
232
243
  */
233
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _restoreKeyring);
244
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _restoreKeyring);
234
245
  /**
235
246
  * Destroy Keyring
236
247
  *
@@ -240,14 +251,14 @@ var KeyringController = class extends _basecontroller.BaseController {
240
251
  *
241
252
  * @param keyring - The keyring to destroy.
242
253
  */
243
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _destroyKeyring);
254
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _destroyKeyring);
244
255
  /**
245
256
  * Remove empty keyrings.
246
257
  *
247
258
  * Loops through the keyrings and removes the ones with empty accounts
248
259
  * (usually after removing the last / only account) from a keyring.
249
260
  */
250
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _removeEmptyKeyrings);
261
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _removeEmptyKeyrings);
251
262
  /**
252
263
  * Checks for duplicate keypairs, using the the first account in the given
253
264
  * array. Rejects if a duplicate is found.
@@ -258,15 +269,50 @@ var KeyringController = class extends _basecontroller.BaseController {
258
269
  * @param newAccountArray - Array of new accounts.
259
270
  * @returns The account, if no duplicate is found.
260
271
  */
261
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _checkForDuplicate);
272
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _checkForDuplicate);
262
273
  /**
263
274
  * Set the `isUnlocked` to true and notify listeners
264
275
  * through the messenger.
265
276
  *
266
277
  * @fires KeyringController:unlock
267
278
  */
268
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _setUnlocked);
269
- _chunkDNQK26H6js.__privateAdd.call(void 0, this, _getMemState);
279
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _setUnlocked);
280
+ /**
281
+ * Execute the given function after acquiring the controller lock
282
+ * and save the keyrings to state after it, or rollback to their
283
+ * previous state in case of error.
284
+ *
285
+ * @param fn - The function to execute.
286
+ * @returns The result of the function.
287
+ */
288
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _persistOrRollback);
289
+ /**
290
+ * Execute the given function after acquiring the controller lock
291
+ * and rollback keyrings and password states in case of error.
292
+ *
293
+ * @param fn - The function to execute atomically.
294
+ * @returns The result of the function.
295
+ */
296
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _withRollback);
297
+ /**
298
+ * Assert that the controller mutex is locked.
299
+ *
300
+ * @throws If the controller mutex is not locked.
301
+ */
302
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _assertControllerMutexIsLocked);
303
+ /**
304
+ * Lock the controller mutex before executing the given function,
305
+ * and release it after the function is resolved or after an
306
+ * error is thrown.
307
+ *
308
+ * This wrapper ensures that each mutable operation that interacts with the
309
+ * controller and that changes its state is executed in a mutually exclusive way,
310
+ * preventing unsafe concurrent access that could lead to unpredictable behavior.
311
+ *
312
+ * @param fn - The function to execute while the controller mutex is locked.
313
+ * @returns The result of the function.
314
+ */
315
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _withControllerLock);
270
316
  /**
271
317
  * Lock the vault mutex before executing the given function,
272
318
  * and release it after the function is resolved or after an
@@ -278,100 +324,96 @@ var KeyringController = class extends _basecontroller.BaseController {
278
324
  * @param fn - The function to execute while the vault mutex is locked.
279
325
  * @returns The result of the function.
280
326
  */
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)) {
327
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _withVaultLock);
328
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _controllerOperationMutex, new (0, _asyncmutex.Mutex)());
329
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _vaultOperationMutex, new (0, _asyncmutex.Mutex)());
330
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _keyringBuilders, void 0);
331
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _keyrings, void 0);
332
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _unsupportedKeyrings, void 0);
333
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _password, void 0);
334
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _encryptor, void 0);
335
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _cacheEncryptionKey, void 0);
336
+ _chunkQDPHKQONjs.__privateAdd.call(void 0, this, _qrKeyringStateListener, void 0);
337
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyringBuilders, keyringBuilders ? defaultKeyringBuilders.concat(keyringBuilders) : defaultKeyringBuilders);
338
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _encryptor, encryptor);
339
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, []);
340
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _unsupportedKeyrings, []);
341
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _cacheEncryptionKey, Boolean(options.cacheEncryptionKey));
342
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
297
343
  assertIsExportableKeyEncryptor(encryptor);
298
344
  }
299
- _chunkDNQK26H6js.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
345
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _registerMessageHandlers, registerMessageHandlers_fn).call(this);
300
346
  }
301
347
  /**
302
348
  * Adds a new account to the default (first) HD seed phrase keyring.
303
349
  *
304
350
  * @param accountCount - Number of accounts before adding a new one, used to
305
351
  * make the method idempotent.
306
- * @returns Promise resolving to keyring current state and added account
307
- * address.
352
+ * @returns Promise resolving to the added account address.
308
353
  */
309
354
  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");
355
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
356
+ const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
357
+ if (!primaryKeyring) {
358
+ throw new Error("No HD keyring found");
318
359
  }
319
- const existingAccount = oldAccounts[accountCount];
320
- if (!existingAccount) {
321
- throw new Error(`Can't find account at index ${accountCount}`);
360
+ const oldAccounts = await primaryKeyring.getAccounts();
361
+ if (accountCount && oldAccounts.length !== accountCount) {
362
+ if (accountCount > oldAccounts.length) {
363
+ throw new Error("Account out of sequence");
364
+ }
365
+ const existingAccount = oldAccounts[accountCount];
366
+ if (!existingAccount) {
367
+ throw new Error(`Can't find account at index ${accountCount}`);
368
+ }
369
+ return existingAccount;
322
370
  }
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
- };
371
+ const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
372
+ await this.verifySeedPhrase();
373
+ return addedAccountAddress;
374
+ });
335
375
  }
336
376
  /**
337
377
  * Adds a new account to the specified keyring.
338
378
  *
339
379
  * @param keyring - Keyring to add the account to.
340
380
  * @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
381
+ * @returns Promise resolving to the added account address
342
382
  */
343
383
  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");
384
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
385
+ const oldAccounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
386
+ if (accountCount && oldAccounts.length !== accountCount) {
387
+ if (accountCount > oldAccounts.length) {
388
+ throw new Error("Account out of sequence");
389
+ }
390
+ const existingAccount = oldAccounts[accountCount];
391
+ _utils.assertIsStrictHexString.call(void 0, existingAccount);
392
+ return existingAccount;
348
393
  }
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;
394
+ await keyring.addAccounts(1);
395
+ const addedAccountAddress = (await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this)).find(
396
+ (selectedAddress) => !oldAccounts.includes(selectedAddress)
397
+ );
398
+ _utils.assertIsStrictHexString.call(void 0, addedAccountAddress);
399
+ return addedAccountAddress;
400
+ });
360
401
  }
361
402
  /**
362
403
  * Adds a new account to the default (first) HD seed phrase keyring without updating identities in preferences.
363
404
  *
364
- * @returns Promise resolving to current state when the account is added.
405
+ * @returns Promise resolving to the added account address.
365
406
  */
366
407
  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);
408
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
409
+ const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
410
+ if (!primaryKeyring) {
411
+ throw new Error("No HD keyring found");
412
+ }
413
+ const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
414
+ await this.verifySeedPhrase();
415
+ return addedAccountAddress;
416
+ });
375
417
  }
376
418
  /**
377
419
  * Effectively the same as creating a new keychain then populating it
@@ -380,45 +422,37 @@ var KeyringController = class extends _basecontroller.BaseController {
380
422
  * @param password - Password to unlock keychain.
381
423
  * @param seed - A BIP39-compliant seed phrase as Uint8Array,
382
424
  * either as a string or an array of UTF-8 bytes that represent the string.
383
- * @returns Promise resolving to the restored keychain object.
425
+ * @returns Promise resolving when the operation ends successfully.
384
426
  */
385
427
  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, {
428
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
429
+ if (!password || !password.length) {
430
+ throw new Error("Invalid password");
431
+ }
432
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
392
433
  type: "HD Key Tree" /* hd */,
393
434
  opts: {
394
435
  mnemonic: seed,
395
436
  numberOfAccounts: 1
396
437
  }
397
438
  });
398
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
399
- } finally {
400
- releaseLock();
401
- }
439
+ });
402
440
  }
403
441
  /**
404
442
  * Create a new primary keychain and wipe any previous keychains.
405
443
  *
406
444
  * @param password - Password to unlock the new vault.
407
- * @returns Newly-created keychain object.
445
+ * @returns Promise resolving when the operation ends successfully.
408
446
  */
409
447
  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);
448
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
449
+ const accounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
413
450
  if (!accounts.length) {
414
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
451
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
415
452
  type: "HD Key Tree" /* hd */
416
453
  });
417
454
  }
418
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
419
- } finally {
420
- releaseLock();
421
- }
455
+ });
422
456
  }
423
457
  /**
424
458
  * Adds a new keyring of the given `type`.
@@ -432,7 +466,7 @@ var KeyringController = class extends _basecontroller.BaseController {
432
466
  if (type === "QR Hardware Wallet Device" /* qr */) {
433
467
  return this.getOrAddQRKeyring();
434
468
  }
435
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts, true);
469
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts));
436
470
  }
437
471
  /**
438
472
  * Method to verify a given password validity. Throws an
@@ -444,7 +478,7 @@ var KeyringController = class extends _basecontroller.BaseController {
444
478
  if (!this.state.vault) {
445
479
  throw new Error("KeyringController - Cannot unlock without a previous vault." /* VaultError */);
446
480
  }
447
- await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decrypt(password, this.state.vault);
481
+ await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decrypt(password, this.state.vault);
448
482
  }
449
483
  /**
450
484
  * Returns the status of the vault.
@@ -462,8 +496,8 @@ var KeyringController = class extends _basecontroller.BaseController {
462
496
  */
463
497
  async exportSeedPhrase(password) {
464
498
  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;
499
+ assertHasUint8ArrayMnemonic(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings)[0]);
500
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings)[0].mnemonic;
467
501
  }
468
502
  /**
469
503
  * Gets the private key from the keyring controlling an address.
@@ -488,7 +522,10 @@ var KeyringController = class extends _basecontroller.BaseController {
488
522
  * @returns A promise resolving to an array of addresses.
489
523
  */
490
524
  async getAccounts() {
491
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
525
+ return this.state.keyrings.reduce(
526
+ (accounts, keyring) => accounts.concat(keyring.accounts),
527
+ []
528
+ );
492
529
  }
493
530
  /**
494
531
  * Get encryption public key.
@@ -539,7 +576,7 @@ var KeyringController = class extends _basecontroller.BaseController {
539
576
  async getKeyringForAccount(account) {
540
577
  const hexed = _ethsigutil.normalize.call(void 0, account);
541
578
  const candidates = await Promise.all(
542
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
579
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
543
580
  return Promise.all([keyring, keyring.getAccounts()]);
544
581
  })
545
582
  );
@@ -572,7 +609,7 @@ var KeyringController = class extends _basecontroller.BaseController {
572
609
  * @returns An array of keyrings of the given type.
573
610
  */
574
611
  getKeyringsByType(type) {
575
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).filter((keyring) => keyring.type === type);
612
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).filter((keyring) => keyring.type === type);
576
613
  }
577
614
  /**
578
615
  * Persist all serialized keyrings in the vault.
@@ -581,7 +618,7 @@ var KeyringController = class extends _basecontroller.BaseController {
581
618
  * operation completes.
582
619
  */
583
620
  async persistAllKeyrings() {
584
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
621
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => true);
585
622
  }
586
623
  /**
587
624
  * Imports an account with the specified import strategy.
@@ -589,88 +626,89 @@ var KeyringController = class extends _basecontroller.BaseController {
589
626
  * @param strategy - Import strategy name.
590
627
  * @param args - Array of arguments to pass to the underlying stategy.
591
628
  * @throws Will throw when passed an unrecognized strategy.
592
- * @returns Promise resolving to keyring current state and imported account
593
- * address.
629
+ * @returns Promise resolving to the imported account address.
594
630
  */
595
631
  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
- };
632
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
633
+ let privateKey;
634
+ switch (strategy) {
635
+ case "privateKey":
636
+ const [importedKey] = args;
637
+ if (!importedKey) {
638
+ throw new Error("Cannot import an empty key.");
639
+ }
640
+ const prefixed = _utils.add0x.call(void 0, importedKey);
641
+ let bufferedPrivateKey;
642
+ try {
643
+ bufferedPrivateKey = _util.toBuffer.call(void 0, prefixed);
644
+ } catch {
645
+ throw new Error("Cannot import invalid private key.");
646
+ }
647
+ if (!_util.isValidPrivate.call(void 0, bufferedPrivateKey) || // ensures that the key is 64 bytes long
648
+ _util.getBinarySize.call(void 0, prefixed) !== 64 + "0x".length) {
649
+ throw new Error("Cannot import invalid private key.");
650
+ }
651
+ privateKey = _utils.remove0x.call(void 0, prefixed);
652
+ break;
653
+ case "json":
654
+ let wallet;
655
+ const [input, password] = args;
656
+ try {
657
+ wallet = _ethereumjswallet.thirdparty.fromEtherWallet(input, password);
658
+ } catch (e) {
659
+ wallet = wallet || await _ethereumjswallet2.default.fromV3(input, password, true);
660
+ }
661
+ privateKey = _utils.bytesToHex.call(void 0, wallet.getPrivateKey());
662
+ break;
663
+ default:
664
+ throw new Error(`Unexpected import strategy: '${strategy}'`);
665
+ }
666
+ const newKeyring = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, "Simple Key Pair" /* simple */, [
667
+ privateKey
668
+ ]);
669
+ const accounts = await newKeyring.getAccounts();
670
+ return accounts[0];
671
+ });
635
672
  }
636
673
  /**
637
674
  * Removes an account from keyring state.
638
675
  *
639
676
  * @param address - Address of the account to remove.
640
677
  * @fires KeyringController:accountRemoved
641
- * @returns Promise resolving current state when this account removal completes.
678
+ * @returns Promise resolving when the account is removed.
642
679
  */
643
680
  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);
681
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
682
+ const keyring = await this.getKeyringForAccount(
683
+ address
684
+ );
685
+ if (!keyring.removeAccount) {
686
+ throw new Error("`KeyringController - The keyring for the current address does not support the method removeAccount" /* UnsupportedRemoveAccount */);
687
+ }
688
+ await keyring.removeAccount(address);
689
+ const accounts = await keyring.getAccounts();
690
+ if (accounts.length === 0) {
691
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _removeEmptyKeyrings, removeEmptyKeyrings_fn).call(this);
692
+ }
693
+ });
656
694
  this.messagingSystem.publish(`${name}:accountRemoved`, address);
657
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getMemState, getMemState_fn).call(this);
658
695
  }
659
696
  /**
660
697
  * Deallocates all secrets and locks the wallet.
661
698
  *
662
- * @returns Promise resolving to current state.
699
+ * @returns Promise resolving when the operation completes.
663
700
  */
664
701
  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 = [];
702
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withRollback, withRollback_fn).call(this, async () => {
703
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn).call(this);
704
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, void 0);
705
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
706
+ this.update((state) => {
707
+ state.isUnlocked = false;
708
+ state.keyrings = [];
709
+ });
710
+ this.messagingSystem.publish(`${name}:lock`);
670
711
  });
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
712
  }
675
713
  /**
676
714
  * Signs message by calling down into a specific keyring.
@@ -824,32 +862,26 @@ var KeyringController = class extends _basecontroller.BaseController {
824
862
  *
825
863
  * @param encryptionKey - Key to unlock the keychain.
826
864
  * @param encryptionSalt - Salt to unlock the keychain.
827
- * @returns Promise resolving to the current state.
865
+ * @returns Promise resolving when the operation completes.
828
866
  */
829
867
  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);
868
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withRollback, withRollback_fn).call(this, async () => {
869
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _unlockKeyrings, unlockKeyrings_fn).call(this, void 0, encryptionKey, encryptionSalt));
870
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
871
+ });
837
872
  }
838
873
  /**
839
874
  * Attempts to decrypt the current vault and load its keyrings,
840
875
  * using the given password.
841
876
  *
842
877
  * @param password - Password to unlock the keychain.
843
- * @returns Promise resolving to the current state.
878
+ * @returns Promise resolving when the operation completes.
844
879
  */
845
880
  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);
881
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withRollback, withRollback_fn).call(this, async () => {
882
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _unlockKeyrings, unlockKeyrings_fn).call(this, password));
883
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
884
+ });
853
885
  }
854
886
  /**
855
887
  * Verifies the that the seed phrase restores the current keychain's accounts.
@@ -867,7 +899,7 @@ var KeyringController = class extends _basecontroller.BaseController {
867
899
  if (accounts.length === 0) {
868
900
  throw new Error("Cannot verify an empty keyring.");
869
901
  }
870
- const hdKeyringBuilder = _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, "HD Key Tree" /* hd */);
902
+ const hdKeyringBuilder = _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, "HD Key Tree" /* hd */);
871
903
  const hdKeyring = hdKeyringBuilder();
872
904
  await hdKeyring.deserialize({
873
905
  mnemonic: seedWords,
@@ -899,14 +931,15 @@ var KeyringController = class extends _basecontroller.BaseController {
899
931
  * @returns The added keyring
900
932
  */
901
933
  async getOrAddQRKeyring() {
902
- return this.getQRKeyring() || await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
934
+ return this.getQRKeyring() || await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this));
903
935
  }
904
936
  // TODO: Replace `any` with type
905
937
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
906
938
  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);
939
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
940
+ const keyring = this.getQRKeyring() || await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
941
+ keyring.deserialize(serialized);
942
+ });
910
943
  }
911
944
  async resetQRKeyringState() {
912
945
  (await this.getOrAddQRKeyring()).resetStore();
@@ -933,34 +966,37 @@ var KeyringController = class extends _basecontroller.BaseController {
933
966
  (await this.getOrAddQRKeyring()).cancelSync();
934
967
  }
935
968
  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();
969
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
970
+ try {
971
+ const keyring = this.getQRKeyring() || await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
972
+ let accounts;
973
+ switch (page) {
974
+ case -1:
975
+ accounts = await keyring.getPreviousPage();
976
+ break;
977
+ case 1:
978
+ accounts = await keyring.getNextPage();
979
+ break;
980
+ default:
981
+ accounts = await keyring.getFirstPage();
982
+ }
983
+ return accounts.map((account) => {
984
+ return {
985
+ ...account,
986
+ balance: "0x0"
987
+ };
988
+ });
989
+ } catch (e) {
990
+ throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
948
991
  }
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
- }
992
+ });
958
993
  }
959
994
  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);
995
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
996
+ const keyring = this.getQRKeyring() || await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _addQRKeyring, addQRKeyring_fn).call(this);
997
+ keyring.setAccountToUnlock(index);
998
+ await keyring.addAccounts(1);
999
+ });
964
1000
  }
965
1001
  async getAccountKeyringType(account) {
966
1002
  const keyring = await this.getKeyringForAccount(
@@ -969,21 +1005,22 @@ var KeyringController = class extends _basecontroller.BaseController {
969
1005
  return keyring.type;
970
1006
  }
971
1007
  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 };
1008
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
1009
+ const keyring = this.getQRKeyring();
1010
+ if (!keyring) {
1011
+ return { removedAccounts: [], remainingAccounts: [] };
1012
+ }
1013
+ const allAccounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
1014
+ keyring.forgetDevice();
1015
+ const remainingAccounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
1016
+ const removedAccounts = allAccounts.filter(
1017
+ (address) => !remainingAccounts.includes(address)
1018
+ );
1019
+ return { removedAccounts, remainingAccounts };
1020
+ });
984
1021
  }
985
1022
  };
986
- _initVaultMutex = new WeakMap();
1023
+ _controllerOperationMutex = new WeakMap();
987
1024
  _vaultOperationMutex = new WeakMap();
988
1025
  _keyringBuilders = new WeakMap();
989
1026
  _keyrings = new WeakMap();
@@ -1045,28 +1082,23 @@ registerMessageHandlers_fn = function() {
1045
1082
  };
1046
1083
  _getKeyringBuilderForType = new WeakSet();
1047
1084
  getKeyringBuilderForType_fn = function(type) {
1048
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyringBuilders).find(
1085
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyringBuilders).find(
1049
1086
  (keyringBuilder) => keyringBuilder.type === type
1050
1087
  );
1051
1088
  };
1052
1089
  _addQRKeyring = new WeakSet();
1053
1090
  addQRKeyring_fn = async function() {
1054
- const qrKeyring = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, "QR Hardware Wallet Device" /* qr */, {
1091
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1092
+ return await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, "QR Hardware Wallet Device" /* qr */, {
1055
1093
  accounts: []
1056
1094
  });
1057
- 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);
1062
- return qrKeyring;
1063
1095
  };
1064
1096
  _subscribeToQRKeyringEvents = new WeakSet();
1065
1097
  subscribeToQRKeyringEvents_fn = function(qrKeyring) {
1066
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _qrKeyringStateListener, (state) => {
1098
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _qrKeyringStateListener, (state) => {
1067
1099
  this.messagingSystem.publish(`${name}:qrKeyringStateChange`, state);
1068
1100
  });
1069
- qrKeyring.getMemStore().subscribe(_chunkDNQK26H6js.__privateGet.call(void 0, this, _qrKeyringStateListener));
1101
+ qrKeyring.getMemStore().subscribe(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _qrKeyringStateListener));
1070
1102
  };
1071
1103
  _unsubscribeFromQRKeyringsEvents = new WeakSet();
1072
1104
  unsubscribeFromQRKeyringsEvents_fn = function() {
@@ -1074,45 +1106,69 @@ unsubscribeFromQRKeyringsEvents_fn = function() {
1074
1106
  "QR Hardware Wallet Device" /* qr */
1075
1107
  );
1076
1108
  qrKeyrings.forEach((qrKeyring) => {
1077
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _qrKeyringStateListener)) {
1078
- qrKeyring.getMemStore().unsubscribe(_chunkDNQK26H6js.__privateGet.call(void 0, this, _qrKeyringStateListener));
1109
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _qrKeyringStateListener)) {
1110
+ qrKeyring.getMemStore().unsubscribe(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _qrKeyringStateListener));
1079
1111
  }
1080
1112
  });
1081
1113
  };
1082
1114
  _createNewVaultWithKeyring = new WeakSet();
1083
1115
  createNewVaultWithKeyring_fn = async function(password, keyring) {
1116
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1084
1117
  if (typeof password !== "string") {
1085
1118
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1086
1119
  }
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);
1120
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, password);
1121
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
1122
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn).call(this, keyring.type, keyring.opts);
1123
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _setUnlocked, setUnlocked_fn).call(this);
1092
1124
  };
1093
1125
  _getUpdatedKeyrings = new WeakSet();
1094
1126
  getUpdatedKeyrings_fn = async function() {
1095
- return Promise.all(_chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(displayForKeyring));
1127
+ return Promise.all(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(displayForKeyring));
1128
+ };
1129
+ _getSerializedKeyrings = new WeakSet();
1130
+ getSerializedKeyrings_fn = async function({ includeUnsupported } = {
1131
+ includeUnsupported: true
1132
+ }) {
1133
+ const serializedKeyrings = await Promise.all(
1134
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1135
+ const [type, data] = await Promise.all([
1136
+ keyring.type,
1137
+ keyring.serialize()
1138
+ ]);
1139
+ return { type, data };
1140
+ })
1141
+ );
1142
+ if (includeUnsupported) {
1143
+ serializedKeyrings.push(..._chunkQDPHKQONjs.__privateGet.call(void 0, this, _unsupportedKeyrings));
1144
+ }
1145
+ return serializedKeyrings;
1146
+ };
1147
+ _restoreSerializedKeyrings = new WeakSet();
1148
+ restoreSerializedKeyrings_fn = async function(serializedKeyrings) {
1149
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this);
1150
+ for (const serializedKeyring of serializedKeyrings) {
1151
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _restoreKeyring, restoreKeyring_fn).call(this, serializedKeyring);
1152
+ }
1096
1153
  };
1097
1154
  _unlockKeyrings = new WeakSet();
1098
1155
  unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1099
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async ({ releaseLock }) => {
1156
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async ({ releaseLock }) => {
1100
1157
  const encryptedVault = this.state.vault;
1101
1158
  if (!encryptedVault) {
1102
1159
  throw new Error("KeyringController - Cannot unlock without a previous vault." /* VaultError */);
1103
1160
  }
1104
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _clearKeyrings, clearKeyrings_fn).call(this, { skipStateUpdate: true });
1105
1161
  let vault;
1106
1162
  const updatedState = {};
1107
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1108
- assertIsExportableKeyEncryptor(_chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor));
1163
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1164
+ assertIsExportableKeyEncryptor(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor));
1109
1165
  if (password) {
1110
- const result = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decryptWithDetail(
1166
+ const result = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decryptWithDetail(
1111
1167
  password,
1112
1168
  encryptedVault
1113
1169
  );
1114
1170
  vault = result.vault;
1115
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, password);
1171
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, password);
1116
1172
  updatedState.encryptionKey = result.exportedKeyString;
1117
1173
  updatedState.encryptionSalt = result.salt;
1118
1174
  } else {
@@ -1123,8 +1179,8 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1123
1179
  if (typeof encryptionKey !== "string") {
1124
1180
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1125
1181
  }
1126
- const key = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
1127
- vault = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decryptWithKey(
1182
+ const key = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
1183
+ vault = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decryptWithKey(
1128
1184
  key,
1129
1185
  parsedEncryptedVault
1130
1186
  );
@@ -1135,14 +1191,14 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1135
1191
  if (typeof password !== "string") {
1136
1192
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1137
1193
  }
1138
- vault = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).decrypt(password, encryptedVault);
1139
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _password, password);
1194
+ vault = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).decrypt(password, encryptedVault);
1195
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, password);
1140
1196
  }
1141
1197
  if (!isSerializedKeyringsArray(vault)) {
1142
1198
  throw new Error("KeyringController - The decrypted vault has an unexpected shape." /* VaultDataError */);
1143
1199
  }
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);
1200
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _restoreSerializedKeyrings, restoreSerializedKeyrings_fn).call(this, vault);
1201
+ const updatedKeyrings = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1146
1202
  this.update((state) => {
1147
1203
  state.keyrings = updatedKeyrings;
1148
1204
  if (updatedState.encryptionKey || updatedState.encryptionSalt) {
@@ -1150,65 +1206,56 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1150
1206
  state.encryptionSalt = updatedState.encryptionSalt;
1151
1207
  }
1152
1208
  });
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)) {
1209
+ 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
1210
  releaseLock();
1155
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1211
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1156
1212
  }
1157
- return _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings);
1213
+ return _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings);
1158
1214
  });
1159
1215
  };
1160
1216
  _updateVault = new WeakSet();
1161
1217
  updateVault_fn = function() {
1162
- return _chunkDNQK26H6js.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async () => {
1218
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withVaultLock, withVaultLock_fn).call(this, async () => {
1163
1219
  const { encryptionKey, encryptionSalt } = this.state;
1164
- if (!_chunkDNQK26H6js.__privateGet.call(void 0, this, _password) && !encryptionKey) {
1220
+ if (!_chunkQDPHKQONjs.__privateGet.call(void 0, this, _password) && !encryptionKey) {
1165
1221
  throw new Error("KeyringController - Cannot persist vault without password and encryption key" /* MissingCredentials */);
1166
1222
  }
1167
- const serializedKeyrings = await Promise.all(
1168
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1169
- const [type, data] = await Promise.all([
1170
- keyring.type,
1171
- keyring.serialize()
1172
- ]);
1173
- return { type, data };
1174
- })
1175
- );
1176
- serializedKeyrings.push(..._chunkDNQK26H6js.__privateGet.call(void 0, this, _unsupportedKeyrings));
1223
+ const serializedKeyrings = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getSerializedKeyrings, getSerializedKeyrings_fn).call(this);
1177
1224
  if (!serializedKeyrings.some((keyring) => keyring.type === "HD Key Tree" /* hd */)) {
1178
1225
  throw new Error("KeyringController - No HD Keyring found" /* NoHdKeyring */);
1179
1226
  }
1180
1227
  const updatedState = {};
1181
- if (_chunkDNQK26H6js.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1182
- assertIsExportableKeyEncryptor(_chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor));
1228
+ if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _cacheEncryptionKey)) {
1229
+ assertIsExportableKeyEncryptor(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor));
1183
1230
  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(
1231
+ const key = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).importKey(encryptionKey);
1232
+ const vaultJSON = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).encryptWithKey(
1186
1233
  key,
1187
1234
  serializedKeyrings
1188
1235
  );
1189
1236
  vaultJSON.salt = encryptionSalt;
1190
1237
  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),
1238
+ } else if (_chunkQDPHKQONjs.__privateGet.call(void 0, this, _password)) {
1239
+ const { vault: newVault, exportedKeyString } = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).encryptWithDetail(
1240
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password),
1194
1241
  serializedKeyrings
1195
1242
  );
1196
1243
  updatedState.vault = newVault;
1197
1244
  updatedState.encryptionKey = exportedKeyString;
1198
1245
  }
1199
1246
  } else {
1200
- if (typeof _chunkDNQK26H6js.__privateGet.call(void 0, this, _password) !== "string") {
1247
+ if (typeof _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password) !== "string") {
1201
1248
  throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
1202
1249
  }
1203
- updatedState.vault = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _encryptor).encrypt(
1204
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _password),
1250
+ updatedState.vault = await _chunkQDPHKQONjs.__privateGet.call(void 0, this, _encryptor).encrypt(
1251
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password),
1205
1252
  serializedKeyrings
1206
1253
  );
1207
1254
  }
1208
1255
  if (!updatedState.vault) {
1209
1256
  throw new Error("KeyringController - Cannot persist vault without vault information" /* MissingVaultData */);
1210
1257
  }
1211
- const updatedKeyrings = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1258
+ const updatedKeyrings = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1212
1259
  this.update((state) => {
1213
1260
  state.vault = updatedState.vault;
1214
1261
  state.keyrings = updatedKeyrings;
@@ -1222,7 +1269,7 @@ updateVault_fn = function() {
1222
1269
  };
1223
1270
  _getAccountsFromKeyrings = new WeakSet();
1224
1271
  getAccountsFromKeyrings_fn = async function() {
1225
- const keyrings = _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings);
1272
+ const keyrings = _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings);
1226
1273
  const keyringArrays = await Promise.all(
1227
1274
  keyrings.map(async (keyring) => keyring.getAccounts())
1228
1275
  );
@@ -1233,15 +1280,17 @@ getAccountsFromKeyrings_fn = async function() {
1233
1280
  };
1234
1281
  _createKeyringWithFirstAccount = new WeakSet();
1235
1282
  createKeyringWithFirstAccount_fn = async function(type, opts) {
1236
- const keyring = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts, true);
1283
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1284
+ const keyring = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, opts);
1237
1285
  const [firstAccount] = await keyring.getAccounts();
1238
1286
  if (!firstAccount) {
1239
1287
  throw new Error("KeyringController - First Account not found." /* NoFirstAccount */);
1240
1288
  }
1241
1289
  };
1242
1290
  _newKeyring = new WeakSet();
1243
- newKeyring_fn = async function(type, data, persist = false) {
1244
- const keyringBuilder = _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, type);
1291
+ newKeyring_fn = async function(type, data) {
1292
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1293
+ const keyringBuilder = _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, type);
1245
1294
  if (!keyringBuilder) {
1246
1295
  throw new Error(
1247
1296
  `${"KeyringController - No keyringBuilder found for keyring" /* NoKeyringBuilder */}. Keyring type: ${type}`
@@ -1261,35 +1310,29 @@ newKeyring_fn = async function(type, data, persist = false) {
1261
1310
  keyring.generateRandomMnemonic();
1262
1311
  await keyring.addAccounts(1);
1263
1312
  }
1264
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, await keyring.getAccounts());
1265
- 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);
1313
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, await keyring.getAccounts());
1314
+ if (type === "QR Hardware Wallet Device" /* qr */) {
1315
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, keyring);
1268
1316
  }
1317
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).push(keyring);
1269
1318
  return keyring;
1270
1319
  };
1271
1320
  _clearKeyrings = new WeakSet();
1272
- 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);
1275
- }
1276
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, []);
1277
- if (!options.skipStateUpdate) {
1278
- this.update((state) => {
1279
- state.keyrings = [];
1280
- });
1321
+ clearKeyrings_fn = async function() {
1322
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1323
+ for (const keyring of _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings)) {
1324
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
1281
1325
  }
1326
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, []);
1282
1327
  };
1283
1328
  _restoreKeyring = new WeakSet();
1284
1329
  restoreKeyring_fn = async function(serialized) {
1330
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1285
1331
  try {
1286
1332
  const { type, data } = serialized;
1287
- const keyring = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, data);
1288
- await keyring.getAccounts();
1289
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).push(keyring);
1290
- return keyring;
1333
+ return await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _newKeyring, newKeyring_fn).call(this, type, data);
1291
1334
  } catch (_) {
1292
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _unsupportedKeyrings).push(serialized);
1335
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _unsupportedKeyrings).push(serialized);
1293
1336
  return void 0;
1294
1337
  }
1295
1338
  };
@@ -1299,22 +1342,23 @@ destroyKeyring_fn = async function(keyring) {
1299
1342
  };
1300
1343
  _removeEmptyKeyrings = new WeakSet();
1301
1344
  removeEmptyKeyrings_fn = async function() {
1345
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1302
1346
  const validKeyrings = [];
1303
1347
  await Promise.all(
1304
- _chunkDNQK26H6js.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1348
+ _chunkQDPHKQONjs.__privateGet.call(void 0, this, _keyrings).map(async (keyring) => {
1305
1349
  const accounts = await keyring.getAccounts();
1306
1350
  if (accounts.length > 0) {
1307
1351
  validKeyrings.push(keyring);
1308
1352
  } else {
1309
- await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
1353
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
1310
1354
  }
1311
1355
  })
1312
1356
  );
1313
- _chunkDNQK26H6js.__privateSet.call(void 0, this, _keyrings, validKeyrings);
1357
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _keyrings, validKeyrings);
1314
1358
  };
1315
1359
  _checkForDuplicate = new WeakSet();
1316
1360
  checkForDuplicate_fn = async function(type, newAccountArray) {
1317
- const accounts = await _chunkDNQK26H6js.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
1361
+ const accounts = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
1318
1362
  switch (type) {
1319
1363
  case "Simple Key Pair" /* simple */: {
1320
1364
  const isIncluded = Boolean(
@@ -1334,27 +1378,57 @@ checkForDuplicate_fn = async function(type, newAccountArray) {
1334
1378
  };
1335
1379
  _setUnlocked = new WeakSet();
1336
1380
  setUnlocked_fn = function() {
1381
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1337
1382
  this.update((state) => {
1338
1383
  state.isUnlocked = true;
1339
1384
  });
1340
1385
  this.messagingSystem.publish(`${name}:unlock`);
1341
1386
  };
1342
- _getMemState = new WeakSet();
1343
- getMemState_fn = function() {
1344
- return {
1345
- isUnlocked: this.state.isUnlocked,
1346
- keyrings: this.state.keyrings
1347
- };
1387
+ _persistOrRollback = new WeakSet();
1388
+ persistOrRollback_fn = async function(fn) {
1389
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withRollback, withRollback_fn).call(this, async ({ releaseLock }) => {
1390
+ const callbackResult = await fn({ releaseLock });
1391
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _updateVault, updateVault_fn).call(this);
1392
+ return callbackResult;
1393
+ });
1394
+ };
1395
+ _withRollback = new WeakSet();
1396
+ withRollback_fn = async function(fn) {
1397
+ return _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _withControllerLock, withControllerLock_fn).call(this, async ({ releaseLock }) => {
1398
+ const currentSerializedKeyrings = await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _getSerializedKeyrings, getSerializedKeyrings_fn).call(this);
1399
+ const currentPassword = _chunkQDPHKQONjs.__privateGet.call(void 0, this, _password);
1400
+ try {
1401
+ return await fn({ releaseLock });
1402
+ } catch (e) {
1403
+ await _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _restoreSerializedKeyrings, restoreSerializedKeyrings_fn).call(this, currentSerializedKeyrings);
1404
+ _chunkQDPHKQONjs.__privateSet.call(void 0, this, _password, currentPassword);
1405
+ throw e;
1406
+ }
1407
+ });
1408
+ };
1409
+ _assertControllerMutexIsLocked = new WeakSet();
1410
+ assertControllerMutexIsLocked_fn = function() {
1411
+ if (!_chunkQDPHKQONjs.__privateGet.call(void 0, this, _controllerOperationMutex).isLocked()) {
1412
+ throw new Error("KeyringController - attempt to update vault during a non mutually exclusive operation" /* ControllerLockRequired */);
1413
+ }
1414
+ };
1415
+ _withControllerLock = new WeakSet();
1416
+ withControllerLock_fn = async function(fn) {
1417
+ return withLock(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _controllerOperationMutex), fn);
1348
1418
  };
1349
1419
  _withVaultLock = new WeakSet();
1350
1420
  withVaultLock_fn = async function(fn) {
1351
- const releaseLock = await _chunkDNQK26H6js.__privateGet.call(void 0, this, _vaultOperationMutex).acquire();
1421
+ _chunkQDPHKQONjs.__privateMethod.call(void 0, this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1422
+ return withLock(_chunkQDPHKQONjs.__privateGet.call(void 0, this, _vaultOperationMutex), fn);
1423
+ };
1424
+ async function withLock(mutex, fn) {
1425
+ const releaseLock = await mutex.acquire();
1352
1426
  try {
1353
1427
  return await fn({ releaseLock });
1354
1428
  } finally {
1355
1429
  releaseLock();
1356
1430
  }
1357
- };
1431
+ }
1358
1432
  var KeyringController_default = KeyringController;
1359
1433
 
1360
1434
 
@@ -1367,4 +1441,4 @@ var KeyringController_default = KeyringController;
1367
1441
 
1368
1442
 
1369
1443
  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
1444
+ //# sourceMappingURL=chunk-H4OUA6YG.js.map