@metamask-previews/keyring-controller 15.0.0-preview-61ace68c → 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.
@@ -7,7 +7,7 @@
7
7
 
8
8
 
9
9
 
10
- var _chunkISACMNF3js = require('./chunk-ISACMNF3.js');
10
+ var _chunkH4OUA6YGjs = require('./chunk-H4OUA6YG.js');
11
11
  require('./chunk-QDPHKQON.js');
12
12
 
13
13
 
@@ -18,5 +18,5 @@ require('./chunk-QDPHKQON.js');
18
18
 
19
19
 
20
20
 
21
- exports.AccountImportStrategy = _chunkISACMNF3js.AccountImportStrategy; exports.KeyringController = _chunkISACMNF3js.KeyringController; exports.KeyringTypes = _chunkISACMNF3js.KeyringTypes; exports.SignTypedDataVersion = _chunkISACMNF3js.SignTypedDataVersion; exports.default = _chunkISACMNF3js.KeyringController_default; exports.getDefaultKeyringState = _chunkISACMNF3js.getDefaultKeyringState; exports.isCustodyKeyring = _chunkISACMNF3js.isCustodyKeyring; exports.keyringBuilderFactory = _chunkISACMNF3js.keyringBuilderFactory;
21
+ exports.AccountImportStrategy = _chunkH4OUA6YGjs.AccountImportStrategy; exports.KeyringController = _chunkH4OUA6YGjs.KeyringController; exports.KeyringTypes = _chunkH4OUA6YGjs.KeyringTypes; exports.SignTypedDataVersion = _chunkH4OUA6YGjs.SignTypedDataVersion; exports.default = _chunkH4OUA6YGjs.KeyringController_default; exports.getDefaultKeyringState = _chunkH4OUA6YGjs.getDefaultKeyringState; exports.isCustodyKeyring = _chunkH4OUA6YGjs.isCustodyKeyring; exports.keyringBuilderFactory = _chunkH4OUA6YGjs.keyringBuilderFactory;
22
22
  //# sourceMappingURL=KeyringController.js.map
@@ -7,7 +7,7 @@ import {
7
7
  getDefaultKeyringState,
8
8
  isCustodyKeyring,
9
9
  keyringBuilderFactory
10
- } from "./chunk-RVCG63UG.mjs";
10
+ } from "./chunk-5VBIIMC6.mjs";
11
11
  import "./chunk-HT7WOORD.mjs";
12
12
  export {
13
13
  AccountImportStrategy,
@@ -86,7 +86,7 @@ async function displayForKeyring(keyring) {
86
86
  accounts: accounts.map(normalize)
87
87
  };
88
88
  }
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;
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 {
91
91
  /**
92
92
  * Creates a KeyringController instance.
@@ -170,6 +170,21 @@ var KeyringController = class extends BaseController {
170
170
  * @returns A promise resolving to the updated keyrings array.
171
171
  */
172
172
  __privateAdd(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
+ __privateAdd(this, _getSerializedKeyrings);
182
+ /**
183
+ * Restore a serialized keyrings array.
184
+ *
185
+ * @param serializedKeyrings - The serialized keyrings array.
186
+ */
187
+ __privateAdd(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.
@@ -210,7 +225,6 @@ var KeyringController = class extends 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
  */
@@ -218,9 +232,6 @@ var KeyringController = class extends BaseController {
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
236
  __privateAdd(this, _clearKeyrings);
226
237
  /**
@@ -266,6 +277,23 @@ var KeyringController = class extends BaseController {
266
277
  * @fires KeyringController:unlock
267
278
  */
268
279
  __privateAdd(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
+ __privateAdd(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
+ __privateAdd(this, _withRollback);
269
297
  /**
270
298
  * Assert that the controller mutex is locked.
271
299
  *
@@ -324,7 +352,7 @@ var KeyringController = class extends BaseController {
324
352
  * @returns Promise resolving to the added account address.
325
353
  */
326
354
  async addNewAccount(accountCount) {
327
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
355
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
328
356
  const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
329
357
  if (!primaryKeyring) {
330
358
  throw new Error("No HD keyring found");
@@ -342,7 +370,6 @@ var KeyringController = class extends BaseController {
342
370
  }
343
371
  const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
344
372
  await this.verifySeedPhrase();
345
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
346
373
  return addedAccountAddress;
347
374
  });
348
375
  }
@@ -354,7 +381,7 @@ var KeyringController = class extends BaseController {
354
381
  * @returns Promise resolving to the added account address
355
382
  */
356
383
  async addNewAccountForKeyring(keyring, accountCount) {
357
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
384
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
358
385
  const oldAccounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
359
386
  if (accountCount && oldAccounts.length !== accountCount) {
360
387
  if (accountCount > oldAccounts.length) {
@@ -365,7 +392,6 @@ var KeyringController = class extends BaseController {
365
392
  return existingAccount;
366
393
  }
367
394
  await keyring.addAccounts(1);
368
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
369
395
  const addedAccountAddress = (await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this)).find(
370
396
  (selectedAddress) => !oldAccounts.includes(selectedAddress)
371
397
  );
@@ -379,13 +405,12 @@ var KeyringController = class extends BaseController {
379
405
  * @returns Promise resolving to the added account address.
380
406
  */
381
407
  async addNewAccountWithoutUpdate() {
382
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
408
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
383
409
  const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
384
410
  if (!primaryKeyring) {
385
411
  throw new Error("No HD keyring found");
386
412
  }
387
413
  const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
388
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
389
414
  await this.verifySeedPhrase();
390
415
  return addedAccountAddress;
391
416
  });
@@ -400,7 +425,7 @@ var KeyringController = class extends BaseController {
400
425
  * @returns Promise resolving when the operation ends successfully.
401
426
  */
402
427
  async createNewVaultAndRestore(password, seed) {
403
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
428
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
404
429
  if (!password || !password.length) {
405
430
  throw new Error("Invalid password");
406
431
  }
@@ -420,7 +445,7 @@ var KeyringController = class extends BaseController {
420
445
  * @returns Promise resolving when the operation ends successfully.
421
446
  */
422
447
  async createNewVaultAndKeychain(password) {
423
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
448
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
424
449
  const accounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
425
450
  if (!accounts.length) {
426
451
  await __privateMethod(this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
@@ -441,7 +466,7 @@ var KeyringController = class extends BaseController {
441
466
  if (type === "QR Hardware Wallet Device" /* qr */) {
442
467
  return this.getOrAddQRKeyring();
443
468
  }
444
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts, true));
469
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts));
445
470
  }
446
471
  /**
447
472
  * Method to verify a given password validity. Throws an
@@ -593,7 +618,7 @@ var KeyringController = class extends BaseController {
593
618
  * operation completes.
594
619
  */
595
620
  async persistAllKeyrings() {
596
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => __privateMethod(this, _updateVault, updateVault_fn).call(this));
621
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => true);
597
622
  }
598
623
  /**
599
624
  * Imports an account with the specified import strategy.
@@ -604,7 +629,7 @@ var KeyringController = class extends BaseController {
604
629
  * @returns Promise resolving to the imported account address.
605
630
  */
606
631
  async importAccountWithStrategy(strategy, args) {
607
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
632
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
608
633
  let privateKey;
609
634
  switch (strategy) {
610
635
  case "privateKey":
@@ -638,7 +663,9 @@ var KeyringController = class extends BaseController {
638
663
  default:
639
664
  throw new Error(`Unexpected import strategy: '${strategy}'`);
640
665
  }
641
- const newKeyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, "Simple Key Pair" /* simple */, [privateKey], true);
666
+ const newKeyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, "Simple Key Pair" /* simple */, [
667
+ privateKey
668
+ ]);
642
669
  const accounts = await newKeyring.getAccounts();
643
670
  return accounts[0];
644
671
  });
@@ -651,7 +678,7 @@ var KeyringController = class extends BaseController {
651
678
  * @returns Promise resolving when the account is removed.
652
679
  */
653
680
  async removeAccount(address) {
654
- await __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
681
+ await __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
655
682
  const keyring = await this.getKeyringForAccount(
656
683
  address
657
684
  );
@@ -663,7 +690,6 @@ var KeyringController = class extends BaseController {
663
690
  if (accounts.length === 0) {
664
691
  await __privateMethod(this, _removeEmptyKeyrings, removeEmptyKeyrings_fn).call(this);
665
692
  }
666
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
667
693
  });
668
694
  this.messagingSystem.publish(`${name}:accountRemoved`, address);
669
695
  }
@@ -673,14 +699,14 @@ var KeyringController = class extends BaseController {
673
699
  * @returns Promise resolving when the operation completes.
674
700
  */
675
701
  async setLocked() {
676
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
702
+ return __privateMethod(this, _withRollback, withRollback_fn).call(this, async () => {
677
703
  __privateMethod(this, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn).call(this);
678
704
  __privateSet(this, _password, void 0);
705
+ await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
679
706
  this.update((state) => {
680
707
  state.isUnlocked = false;
681
708
  state.keyrings = [];
682
709
  });
683
- await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
684
710
  this.messagingSystem.publish(`${name}:lock`);
685
711
  });
686
712
  }
@@ -839,13 +865,9 @@ var KeyringController = class extends BaseController {
839
865
  * @returns Promise resolving when the operation completes.
840
866
  */
841
867
  async submitEncryptionKey(encryptionKey, encryptionSalt) {
842
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
868
+ return __privateMethod(this, _withRollback, withRollback_fn).call(this, async () => {
843
869
  __privateSet(this, _keyrings, await __privateMethod(this, _unlockKeyrings, unlockKeyrings_fn).call(this, void 0, encryptionKey, encryptionSalt));
844
870
  __privateMethod(this, _setUnlocked, setUnlocked_fn).call(this);
845
- const qrKeyring = this.getQRKeyring();
846
- if (qrKeyring) {
847
- __privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
848
- }
849
871
  });
850
872
  }
851
873
  /**
@@ -856,13 +878,9 @@ var KeyringController = class extends BaseController {
856
878
  * @returns Promise resolving when the operation completes.
857
879
  */
858
880
  async submitPassword(password) {
859
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
881
+ return __privateMethod(this, _withRollback, withRollback_fn).call(this, async () => {
860
882
  __privateSet(this, _keyrings, await __privateMethod(this, _unlockKeyrings, unlockKeyrings_fn).call(this, password));
861
883
  __privateMethod(this, _setUnlocked, setUnlocked_fn).call(this);
862
- const qrKeyring = this.getQRKeyring();
863
- if (qrKeyring) {
864
- __privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
865
- }
866
884
  });
867
885
  }
868
886
  /**
@@ -913,15 +931,14 @@ var KeyringController = class extends BaseController {
913
931
  * @returns The added keyring
914
932
  */
915
933
  async getOrAddQRKeyring() {
916
- return this.getQRKeyring() || await __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this));
934
+ return this.getQRKeyring() || await __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this));
917
935
  }
918
936
  // TODO: Replace `any` with type
919
937
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
920
938
  async restoreQRKeyring(serialized) {
921
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
939
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
922
940
  const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
923
941
  keyring.deserialize(serialized);
924
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
925
942
  });
926
943
  }
927
944
  async resetQRKeyringState() {
@@ -949,7 +966,7 @@ var KeyringController = class extends BaseController {
949
966
  (await this.getOrAddQRKeyring()).cancelSync();
950
967
  }
951
968
  async connectQRHardware(page) {
952
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
969
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
953
970
  try {
954
971
  const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
955
972
  let accounts;
@@ -975,11 +992,10 @@ var KeyringController = class extends BaseController {
975
992
  });
976
993
  }
977
994
  async unlockQRHardwareWalletAccount(index) {
978
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
995
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
979
996
  const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
980
997
  keyring.setAccountToUnlock(index);
981
998
  await keyring.addAccounts(1);
982
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
983
999
  });
984
1000
  }
985
1001
  async getAccountKeyringType(account) {
@@ -989,7 +1005,7 @@ var KeyringController = class extends BaseController {
989
1005
  return keyring.type;
990
1006
  }
991
1007
  async forgetQRDevice() {
992
- return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
1008
+ return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
993
1009
  const keyring = this.getQRKeyring();
994
1010
  if (!keyring) {
995
1011
  return { removedAccounts: [], remainingAccounts: [] };
@@ -1000,7 +1016,6 @@ var KeyringController = class extends BaseController {
1000
1016
  const removedAccounts = allAccounts.filter(
1001
1017
  (address) => !remainingAccounts.includes(address)
1002
1018
  );
1003
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
1004
1019
  return { removedAccounts, remainingAccounts };
1005
1020
  });
1006
1021
  }
@@ -1074,15 +1089,9 @@ getKeyringBuilderForType_fn = function(type) {
1074
1089
  _addQRKeyring = new WeakSet();
1075
1090
  addQRKeyring_fn = async function() {
1076
1091
  __privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1077
- const qrKeyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, "QR Hardware Wallet Device" /* qr */, {
1092
+ return await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, "QR Hardware Wallet Device" /* qr */, {
1078
1093
  accounts: []
1079
1094
  });
1080
- const accounts = await qrKeyring.getAccounts();
1081
- await __privateMethod(this, _checkForDuplicate, checkForDuplicate_fn).call(this, "QR Hardware Wallet Device" /* qr */, accounts);
1082
- __privateGet(this, _keyrings).push(qrKeyring);
1083
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
1084
- __privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
1085
- return qrKeyring;
1086
1095
  };
1087
1096
  _subscribeToQRKeyringEvents = new WeakSet();
1088
1097
  subscribeToQRKeyringEvents_fn = function(qrKeyring) {
@@ -1117,6 +1126,31 @@ _getUpdatedKeyrings = new WeakSet();
1117
1126
  getUpdatedKeyrings_fn = async function() {
1118
1127
  return Promise.all(__privateGet(this, _keyrings).map(displayForKeyring));
1119
1128
  };
1129
+ _getSerializedKeyrings = new WeakSet();
1130
+ getSerializedKeyrings_fn = async function({ includeUnsupported } = {
1131
+ includeUnsupported: true
1132
+ }) {
1133
+ const serializedKeyrings = await Promise.all(
1134
+ __privateGet(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(...__privateGet(this, _unsupportedKeyrings));
1144
+ }
1145
+ return serializedKeyrings;
1146
+ };
1147
+ _restoreSerializedKeyrings = new WeakSet();
1148
+ restoreSerializedKeyrings_fn = async function(serializedKeyrings) {
1149
+ await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
1150
+ for (const serializedKeyring of serializedKeyrings) {
1151
+ await __privateMethod(this, _restoreKeyring, restoreKeyring_fn).call(this, serializedKeyring);
1152
+ }
1153
+ };
1120
1154
  _unlockKeyrings = new WeakSet();
1121
1155
  unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1122
1156
  return __privateMethod(this, _withVaultLock, withVaultLock_fn).call(this, async ({ releaseLock }) => {
@@ -1124,7 +1158,6 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1124
1158
  if (!encryptedVault) {
1125
1159
  throw new Error("KeyringController - Cannot unlock without a previous vault." /* VaultError */);
1126
1160
  }
1127
- await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this, { skipStateUpdate: true });
1128
1161
  let vault;
1129
1162
  const updatedState = {};
1130
1163
  if (__privateGet(this, _cacheEncryptionKey)) {
@@ -1164,7 +1197,7 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
1164
1197
  if (!isSerializedKeyringsArray(vault)) {
1165
1198
  throw new Error("KeyringController - The decrypted vault has an unexpected shape." /* VaultDataError */);
1166
1199
  }
1167
- await Promise.all(vault.map(__privateMethod(this, _restoreKeyring, restoreKeyring_fn).bind(this)));
1200
+ await __privateMethod(this, _restoreSerializedKeyrings, restoreSerializedKeyrings_fn).call(this, vault);
1168
1201
  const updatedKeyrings = await __privateMethod(this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
1169
1202
  this.update((state) => {
1170
1203
  state.keyrings = updatedKeyrings;
@@ -1187,16 +1220,7 @@ updateVault_fn = function() {
1187
1220
  if (!__privateGet(this, _password) && !encryptionKey) {
1188
1221
  throw new Error("KeyringController - Cannot persist vault without password and encryption key" /* MissingCredentials */);
1189
1222
  }
1190
- const serializedKeyrings = await Promise.all(
1191
- __privateGet(this, _keyrings).map(async (keyring) => {
1192
- const [type, data] = await Promise.all([
1193
- keyring.type,
1194
- keyring.serialize()
1195
- ]);
1196
- return { type, data };
1197
- })
1198
- );
1199
- serializedKeyrings.push(...__privateGet(this, _unsupportedKeyrings));
1223
+ const serializedKeyrings = await __privateMethod(this, _getSerializedKeyrings, getSerializedKeyrings_fn).call(this);
1200
1224
  if (!serializedKeyrings.some((keyring) => keyring.type === "HD Key Tree" /* hd */)) {
1201
1225
  throw new Error("KeyringController - No HD Keyring found" /* NoHdKeyring */);
1202
1226
  }
@@ -1257,14 +1281,14 @@ getAccountsFromKeyrings_fn = async function() {
1257
1281
  _createKeyringWithFirstAccount = new WeakSet();
1258
1282
  createKeyringWithFirstAccount_fn = async function(type, opts) {
1259
1283
  __privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1260
- const keyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts, true);
1284
+ const keyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts);
1261
1285
  const [firstAccount] = await keyring.getAccounts();
1262
1286
  if (!firstAccount) {
1263
1287
  throw new Error("KeyringController - First Account not found." /* NoFirstAccount */);
1264
1288
  }
1265
1289
  };
1266
1290
  _newKeyring = new WeakSet();
1267
- newKeyring_fn = async function(type, data, persist = false) {
1291
+ newKeyring_fn = async function(type, data) {
1268
1292
  __privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1269
1293
  const keyringBuilder = __privateMethod(this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, type);
1270
1294
  if (!keyringBuilder) {
@@ -1287,34 +1311,26 @@ newKeyring_fn = async function(type, data, persist = false) {
1287
1311
  await keyring.addAccounts(1);
1288
1312
  }
1289
1313
  await __privateMethod(this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, await keyring.getAccounts());
1290
- if (persist) {
1291
- __privateGet(this, _keyrings).push(keyring);
1292
- await __privateMethod(this, _updateVault, updateVault_fn).call(this);
1314
+ if (type === "QR Hardware Wallet Device" /* qr */) {
1315
+ __privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, keyring);
1293
1316
  }
1317
+ __privateGet(this, _keyrings).push(keyring);
1294
1318
  return keyring;
1295
1319
  };
1296
1320
  _clearKeyrings = new WeakSet();
1297
- clearKeyrings_fn = async function(options = { skipStateUpdate: false }) {
1321
+ clearKeyrings_fn = async function() {
1298
1322
  __privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1299
1323
  for (const keyring of __privateGet(this, _keyrings)) {
1300
1324
  await __privateMethod(this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
1301
1325
  }
1302
1326
  __privateSet(this, _keyrings, []);
1303
- if (!options.skipStateUpdate) {
1304
- this.update((state) => {
1305
- state.keyrings = [];
1306
- });
1307
- }
1308
1327
  };
1309
1328
  _restoreKeyring = new WeakSet();
1310
1329
  restoreKeyring_fn = async function(serialized) {
1311
1330
  __privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
1312
1331
  try {
1313
1332
  const { type, data } = serialized;
1314
- const keyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, data);
1315
- await keyring.getAccounts();
1316
- __privateGet(this, _keyrings).push(keyring);
1317
- return keyring;
1333
+ return await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, data);
1318
1334
  } catch (_) {
1319
1335
  __privateGet(this, _unsupportedKeyrings).push(serialized);
1320
1336
  return void 0;
@@ -1368,6 +1384,28 @@ setUnlocked_fn = function() {
1368
1384
  });
1369
1385
  this.messagingSystem.publish(`${name}:unlock`);
1370
1386
  };
1387
+ _persistOrRollback = new WeakSet();
1388
+ persistOrRollback_fn = async function(fn) {
1389
+ return __privateMethod(this, _withRollback, withRollback_fn).call(this, async ({ releaseLock }) => {
1390
+ const callbackResult = await fn({ releaseLock });
1391
+ await __privateMethod(this, _updateVault, updateVault_fn).call(this);
1392
+ return callbackResult;
1393
+ });
1394
+ };
1395
+ _withRollback = new WeakSet();
1396
+ withRollback_fn = async function(fn) {
1397
+ return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async ({ releaseLock }) => {
1398
+ const currentSerializedKeyrings = await __privateMethod(this, _getSerializedKeyrings, getSerializedKeyrings_fn).call(this);
1399
+ const currentPassword = __privateGet(this, _password);
1400
+ try {
1401
+ return await fn({ releaseLock });
1402
+ } catch (e) {
1403
+ await __privateMethod(this, _restoreSerializedKeyrings, restoreSerializedKeyrings_fn).call(this, currentSerializedKeyrings);
1404
+ __privateSet(this, _password, currentPassword);
1405
+ throw e;
1406
+ }
1407
+ });
1408
+ };
1371
1409
  _assertControllerMutexIsLocked = new WeakSet();
1372
1410
  assertControllerMutexIsLocked_fn = function() {
1373
1411
  if (!__privateGet(this, _controllerOperationMutex).isLocked()) {
@@ -1403,4 +1441,4 @@ export {
1403
1441
  KeyringController,
1404
1442
  KeyringController_default
1405
1443
  };
1406
- //# sourceMappingURL=chunk-RVCG63UG.mjs.map
1444
+ //# sourceMappingURL=chunk-5VBIIMC6.mjs.map