@metamask-previews/keyring-controller 15.0.0-preview-aedc5103 → 15.0.0-preview-02d970ca
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.
- package/CHANGELOG.md +13 -0
- package/dist/KeyringController.js +3 -3
- package/dist/KeyringController.mjs +2 -2
- package/dist/{chunk-STOYI7AT.js → chunk-2GJQ6KDW.js} +434 -336
- package/dist/chunk-2GJQ6KDW.js.map +1 -0
- package/dist/{chunk-XPARO3LL.mjs → chunk-4OE2G6WW.mjs} +4 -1
- package/dist/chunk-4OE2G6WW.mjs.map +1 -0
- package/dist/{chunk-5QTEIEPG.mjs → chunk-USAGXPFN.mjs} +344 -246
- package/dist/chunk-USAGXPFN.mjs.map +1 -0
- package/dist/{chunk-DNQK26H6.js → chunk-ZGV2QNCG.js} +4 -1
- package/dist/chunk-ZGV2QNCG.js.map +1 -0
- package/dist/constants.js +2 -2
- package/dist/constants.mjs +1 -1
- package/dist/index.js +3 -3
- package/dist/index.mjs +2 -2
- package/dist/tsconfig.build.tsbuildinfo +1 -1
- package/dist/types/KeyringController.d.ts +69 -29
- package/dist/types/KeyringController.d.ts.map +1 -1
- package/dist/types/constants.d.ts +4 -1
- package/dist/types/constants.d.ts.map +1 -1
- package/package.json +2 -2
- package/dist/chunk-5QTEIEPG.mjs.map +0 -1
- package/dist/chunk-DNQK26H6.js.map +0 -1
- package/dist/chunk-STOYI7AT.js.map +0 -1
- package/dist/chunk-XPARO3LL.mjs.map +0 -1
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
__privateGet,
|
|
4
4
|
__privateMethod,
|
|
5
5
|
__privateSet
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-4OE2G6WW.mjs";
|
|
7
7
|
|
|
8
8
|
// src/KeyringController.ts
|
|
9
9
|
import { isValidPrivate, toBuffer, getBinarySize } from "@ethereumjs/util";
|
|
@@ -86,7 +86,7 @@ async function displayForKeyring(keyring) {
|
|
|
86
86
|
accounts: accounts.map(normalize)
|
|
87
87
|
};
|
|
88
88
|
}
|
|
89
|
-
var
|
|
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,7 +277,42 @@ var KeyringController = class extends BaseController {
|
|
|
266
277
|
* @fires KeyringController:unlock
|
|
267
278
|
*/
|
|
268
279
|
__privateAdd(this, _setUnlocked);
|
|
269
|
-
|
|
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);
|
|
297
|
+
/**
|
|
298
|
+
* Assert that the controller mutex is locked.
|
|
299
|
+
*
|
|
300
|
+
* @throws If the controller mutex is not locked.
|
|
301
|
+
*/
|
|
302
|
+
__privateAdd(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
|
+
__privateAdd(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
|
|
@@ -279,7 +325,7 @@ var KeyringController = class extends BaseController {
|
|
|
279
325
|
* @returns The result of the function.
|
|
280
326
|
*/
|
|
281
327
|
__privateAdd(this, _withVaultLock);
|
|
282
|
-
__privateAdd(this,
|
|
328
|
+
__privateAdd(this, _controllerOperationMutex, new Mutex());
|
|
283
329
|
__privateAdd(this, _vaultOperationMutex, new Mutex());
|
|
284
330
|
__privateAdd(this, _keyringBuilders, void 0);
|
|
285
331
|
__privateAdd(this, _keyrings, void 0);
|
|
@@ -303,75 +349,71 @@ var KeyringController = class extends BaseController {
|
|
|
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
|
|
307
|
-
* address.
|
|
352
|
+
* @returns Promise resolving to the added account address.
|
|
308
353
|
*/
|
|
309
354
|
async addNewAccount(accountCount) {
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
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 __privateMethod(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
|
|
320
|
-
if (
|
|
321
|
-
|
|
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
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
}
|
|
328
|
-
const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
|
|
329
|
-
await this.verifySeedPhrase();
|
|
330
|
-
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
331
|
-
return {
|
|
332
|
-
keyringState: __privateMethod(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
|
|
381
|
+
* @returns Promise resolving to the added account address
|
|
342
382
|
*/
|
|
343
383
|
async addNewAccountForKeyring(keyring, accountCount) {
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
if (accountCount
|
|
347
|
-
|
|
384
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
385
|
+
const oldAccounts = await __privateMethod(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
|
+
assertIsStrictHexString(existingAccount);
|
|
392
|
+
return existingAccount;
|
|
348
393
|
}
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
(selectedAddress) => !oldAccounts.includes(selectedAddress)
|
|
357
|
-
);
|
|
358
|
-
assertIsStrictHexString(addedAccountAddress);
|
|
359
|
-
return addedAccountAddress;
|
|
394
|
+
await keyring.addAccounts(1);
|
|
395
|
+
const addedAccountAddress = (await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this)).find(
|
|
396
|
+
(selectedAddress) => !oldAccounts.includes(selectedAddress)
|
|
397
|
+
);
|
|
398
|
+
assertIsStrictHexString(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
|
|
405
|
+
* @returns Promise resolving to the added account address.
|
|
365
406
|
*/
|
|
366
407
|
async addNewAccountWithoutUpdate() {
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
408
|
+
return __privateMethod(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,14 +422,13 @@ var KeyringController = class extends 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
|
|
425
|
+
* @returns Promise resolving when the operation ends successfully.
|
|
384
426
|
*/
|
|
385
427
|
async createNewVaultAndRestore(password, seed) {
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
try {
|
|
428
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
429
|
+
if (!password || !password.length) {
|
|
430
|
+
throw new Error("Invalid password");
|
|
431
|
+
}
|
|
391
432
|
await __privateMethod(this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
|
|
392
433
|
type: "HD Key Tree" /* hd */,
|
|
393
434
|
opts: {
|
|
@@ -395,30 +436,23 @@ var KeyringController = class extends BaseController {
|
|
|
395
436
|
numberOfAccounts: 1
|
|
396
437
|
}
|
|
397
438
|
});
|
|
398
|
-
|
|
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
|
|
445
|
+
* @returns Promise resolving when the operation ends successfully.
|
|
408
446
|
*/
|
|
409
447
|
async createNewVaultAndKeychain(password) {
|
|
410
|
-
|
|
411
|
-
try {
|
|
448
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
412
449
|
const accounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
|
|
413
450
|
if (!accounts.length) {
|
|
414
451
|
await __privateMethod(this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
|
|
415
452
|
type: "HD Key Tree" /* hd */
|
|
416
453
|
});
|
|
417
454
|
}
|
|
418
|
-
|
|
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 {
|
|
|
432
466
|
if (type === "QR Hardware Wallet Device" /* qr */) {
|
|
433
467
|
return this.getOrAddQRKeyring();
|
|
434
468
|
}
|
|
435
|
-
return __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts
|
|
469
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts));
|
|
436
470
|
}
|
|
437
471
|
/**
|
|
438
472
|
* Method to verify a given password validity. Throws an
|
|
@@ -488,7 +522,10 @@ var KeyringController = class extends BaseController {
|
|
|
488
522
|
* @returns A promise resolving to an array of addresses.
|
|
489
523
|
*/
|
|
490
524
|
async getAccounts() {
|
|
491
|
-
return
|
|
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.
|
|
@@ -532,7 +569,7 @@ var KeyringController = class extends BaseController {
|
|
|
532
569
|
*
|
|
533
570
|
* @deprecated Use of this method is discouraged as actions executed directly on
|
|
534
571
|
* keyrings are not being reflected in the KeyringController state and not
|
|
535
|
-
* persisted in the vault.
|
|
572
|
+
* persisted in the vault. Use `withKeyring` instead.
|
|
536
573
|
* @param account - An account address.
|
|
537
574
|
* @returns Promise resolving to keyring of the `account` if one exists.
|
|
538
575
|
*/
|
|
@@ -567,7 +604,7 @@ var KeyringController = class extends BaseController {
|
|
|
567
604
|
*
|
|
568
605
|
* @deprecated Use of this method is discouraged as actions executed directly on
|
|
569
606
|
* keyrings are not being reflected in the KeyringController state and not
|
|
570
|
-
* persisted in the vault.
|
|
607
|
+
* persisted in the vault. Use `withKeyring` instead.
|
|
571
608
|
* @param type - Keyring type name.
|
|
572
609
|
* @returns An array of keyrings of the given type.
|
|
573
610
|
*/
|
|
@@ -577,11 +614,12 @@ var KeyringController = class extends BaseController {
|
|
|
577
614
|
/**
|
|
578
615
|
* Persist all serialized keyrings in the vault.
|
|
579
616
|
*
|
|
617
|
+
* @deprecated This method is being phased out in favor of `withKeyring`.
|
|
580
618
|
* @returns Promise resolving with `true` value when the
|
|
581
619
|
* operation completes.
|
|
582
620
|
*/
|
|
583
621
|
async persistAllKeyrings() {
|
|
584
|
-
return __privateMethod(this,
|
|
622
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => true);
|
|
585
623
|
}
|
|
586
624
|
/**
|
|
587
625
|
* Imports an account with the specified import strategy.
|
|
@@ -589,88 +627,89 @@ var KeyringController = class extends BaseController {
|
|
|
589
627
|
* @param strategy - Import strategy name.
|
|
590
628
|
* @param args - Array of arguments to pass to the underlying stategy.
|
|
591
629
|
* @throws Will throw when passed an unrecognized strategy.
|
|
592
|
-
* @returns Promise resolving to
|
|
593
|
-
* address.
|
|
630
|
+
* @returns Promise resolving to the imported account address.
|
|
594
631
|
*/
|
|
595
632
|
async importAccountWithStrategy(strategy, args) {
|
|
596
|
-
|
|
597
|
-
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
626
|
-
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
|
|
634
|
-
|
|
633
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
634
|
+
let privateKey;
|
|
635
|
+
switch (strategy) {
|
|
636
|
+
case "privateKey":
|
|
637
|
+
const [importedKey] = args;
|
|
638
|
+
if (!importedKey) {
|
|
639
|
+
throw new Error("Cannot import an empty key.");
|
|
640
|
+
}
|
|
641
|
+
const prefixed = add0x(importedKey);
|
|
642
|
+
let bufferedPrivateKey;
|
|
643
|
+
try {
|
|
644
|
+
bufferedPrivateKey = toBuffer(prefixed);
|
|
645
|
+
} catch {
|
|
646
|
+
throw new Error("Cannot import invalid private key.");
|
|
647
|
+
}
|
|
648
|
+
if (!isValidPrivate(bufferedPrivateKey) || // ensures that the key is 64 bytes long
|
|
649
|
+
getBinarySize(prefixed) !== 64 + "0x".length) {
|
|
650
|
+
throw new Error("Cannot import invalid private key.");
|
|
651
|
+
}
|
|
652
|
+
privateKey = remove0x(prefixed);
|
|
653
|
+
break;
|
|
654
|
+
case "json":
|
|
655
|
+
let wallet;
|
|
656
|
+
const [input, password] = args;
|
|
657
|
+
try {
|
|
658
|
+
wallet = importers.fromEtherWallet(input, password);
|
|
659
|
+
} catch (e) {
|
|
660
|
+
wallet = wallet || await Wallet.fromV3(input, password, true);
|
|
661
|
+
}
|
|
662
|
+
privateKey = bytesToHex(wallet.getPrivateKey());
|
|
663
|
+
break;
|
|
664
|
+
default:
|
|
665
|
+
throw new Error(`Unexpected import strategy: '${strategy}'`);
|
|
666
|
+
}
|
|
667
|
+
const newKeyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, "Simple Key Pair" /* simple */, [
|
|
668
|
+
privateKey
|
|
669
|
+
]);
|
|
670
|
+
const accounts = await newKeyring.getAccounts();
|
|
671
|
+
return accounts[0];
|
|
672
|
+
});
|
|
635
673
|
}
|
|
636
674
|
/**
|
|
637
675
|
* Removes an account from keyring state.
|
|
638
676
|
*
|
|
639
677
|
* @param address - Address of the account to remove.
|
|
640
678
|
* @fires KeyringController:accountRemoved
|
|
641
|
-
* @returns Promise resolving
|
|
679
|
+
* @returns Promise resolving when the account is removed.
|
|
642
680
|
*/
|
|
643
681
|
async removeAccount(address) {
|
|
644
|
-
|
|
645
|
-
|
|
646
|
-
|
|
647
|
-
|
|
648
|
-
|
|
649
|
-
|
|
650
|
-
|
|
651
|
-
|
|
652
|
-
|
|
653
|
-
|
|
654
|
-
|
|
655
|
-
|
|
682
|
+
await __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
683
|
+
const keyring = await this.getKeyringForAccount(
|
|
684
|
+
address
|
|
685
|
+
);
|
|
686
|
+
if (!keyring.removeAccount) {
|
|
687
|
+
throw new Error("`KeyringController - The keyring for the current address does not support the method removeAccount" /* UnsupportedRemoveAccount */);
|
|
688
|
+
}
|
|
689
|
+
await keyring.removeAccount(address);
|
|
690
|
+
const accounts = await keyring.getAccounts();
|
|
691
|
+
if (accounts.length === 0) {
|
|
692
|
+
await __privateMethod(this, _removeEmptyKeyrings, removeEmptyKeyrings_fn).call(this);
|
|
693
|
+
}
|
|
694
|
+
});
|
|
656
695
|
this.messagingSystem.publish(`${name}:accountRemoved`, address);
|
|
657
|
-
return __privateMethod(this, _getMemState, getMemState_fn).call(this);
|
|
658
696
|
}
|
|
659
697
|
/**
|
|
660
698
|
* Deallocates all secrets and locks the wallet.
|
|
661
699
|
*
|
|
662
|
-
* @returns Promise resolving
|
|
700
|
+
* @returns Promise resolving when the operation completes.
|
|
663
701
|
*/
|
|
664
702
|
async setLocked() {
|
|
665
|
-
__privateMethod(this,
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
state
|
|
703
|
+
return __privateMethod(this, _withRollback, withRollback_fn).call(this, async () => {
|
|
704
|
+
__privateMethod(this, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn).call(this);
|
|
705
|
+
__privateSet(this, _password, void 0);
|
|
706
|
+
await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
|
|
707
|
+
this.update((state) => {
|
|
708
|
+
state.isUnlocked = false;
|
|
709
|
+
state.keyrings = [];
|
|
710
|
+
});
|
|
711
|
+
this.messagingSystem.publish(`${name}:lock`);
|
|
670
712
|
});
|
|
671
|
-
await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
|
|
672
|
-
this.messagingSystem.publish(`${name}:lock`);
|
|
673
|
-
return __privateMethod(this, _getMemState, getMemState_fn).call(this);
|
|
674
713
|
}
|
|
675
714
|
/**
|
|
676
715
|
* Signs message by calling down into a specific keyring.
|
|
@@ -824,32 +863,26 @@ var KeyringController = class extends BaseController {
|
|
|
824
863
|
*
|
|
825
864
|
* @param encryptionKey - Key to unlock the keychain.
|
|
826
865
|
* @param encryptionSalt - Salt to unlock the keychain.
|
|
827
|
-
* @returns Promise resolving
|
|
866
|
+
* @returns Promise resolving when the operation completes.
|
|
828
867
|
*/
|
|
829
868
|
async submitEncryptionKey(encryptionKey, encryptionSalt) {
|
|
830
|
-
|
|
831
|
-
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
__privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
|
|
835
|
-
}
|
|
836
|
-
return __privateMethod(this, _getMemState, getMemState_fn).call(this);
|
|
869
|
+
return __privateMethod(this, _withRollback, withRollback_fn).call(this, async () => {
|
|
870
|
+
__privateSet(this, _keyrings, await __privateMethod(this, _unlockKeyrings, unlockKeyrings_fn).call(this, void 0, encryptionKey, encryptionSalt));
|
|
871
|
+
__privateMethod(this, _setUnlocked, setUnlocked_fn).call(this);
|
|
872
|
+
});
|
|
837
873
|
}
|
|
838
874
|
/**
|
|
839
875
|
* Attempts to decrypt the current vault and load its keyrings,
|
|
840
876
|
* using the given password.
|
|
841
877
|
*
|
|
842
878
|
* @param password - Password to unlock the keychain.
|
|
843
|
-
* @returns Promise resolving
|
|
879
|
+
* @returns Promise resolving when the operation completes.
|
|
844
880
|
*/
|
|
845
881
|
async submitPassword(password) {
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
__privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
|
|
851
|
-
}
|
|
852
|
-
return __privateMethod(this, _getMemState, getMemState_fn).call(this);
|
|
882
|
+
return __privateMethod(this, _withRollback, withRollback_fn).call(this, async () => {
|
|
883
|
+
__privateSet(this, _keyrings, await __privateMethod(this, _unlockKeyrings, unlockKeyrings_fn).call(this, password));
|
|
884
|
+
__privateMethod(this, _setUnlocked, setUnlocked_fn).call(this);
|
|
885
|
+
});
|
|
853
886
|
}
|
|
854
887
|
/**
|
|
855
888
|
* Verifies the that the seed phrase restores the current keychain's accounts.
|
|
@@ -884,6 +917,29 @@ var KeyringController = class extends BaseController {
|
|
|
884
917
|
});
|
|
885
918
|
return seedWords;
|
|
886
919
|
}
|
|
920
|
+
async withKeyring(selector, operation, options = {
|
|
921
|
+
createIfMissing: false
|
|
922
|
+
}) {
|
|
923
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
924
|
+
let keyring;
|
|
925
|
+
if ("address" in selector) {
|
|
926
|
+
keyring = await this.getKeyringForAccount(selector.address);
|
|
927
|
+
} else {
|
|
928
|
+
keyring = this.getKeyringsByType(selector.type)[selector.index || 0];
|
|
929
|
+
if (!keyring && options.createIfMissing) {
|
|
930
|
+
keyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, selector.type, options.createWithData);
|
|
931
|
+
}
|
|
932
|
+
}
|
|
933
|
+
if (!keyring) {
|
|
934
|
+
throw new Error("KeyringController - Keyring not found." /* KeyringNotFound */);
|
|
935
|
+
}
|
|
936
|
+
const result = await operation(keyring);
|
|
937
|
+
if (Object.is(result, keyring)) {
|
|
938
|
+
throw new Error("KeyringController - Returning keyring instances is unsafe" /* UnsafeDirectKeyringAccess */);
|
|
939
|
+
}
|
|
940
|
+
return result;
|
|
941
|
+
});
|
|
942
|
+
}
|
|
887
943
|
// QR Hardware related methods
|
|
888
944
|
/**
|
|
889
945
|
* Get QR Hardware keyring.
|
|
@@ -899,14 +955,15 @@ var KeyringController = class extends BaseController {
|
|
|
899
955
|
* @returns The added keyring
|
|
900
956
|
*/
|
|
901
957
|
async getOrAddQRKeyring() {
|
|
902
|
-
return this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
|
|
958
|
+
return this.getQRKeyring() || await __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this));
|
|
903
959
|
}
|
|
904
960
|
// TODO: Replace `any` with type
|
|
905
961
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
906
962
|
async restoreQRKeyring(serialized) {
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
963
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
964
|
+
const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
|
|
965
|
+
keyring.deserialize(serialized);
|
|
966
|
+
});
|
|
910
967
|
}
|
|
911
968
|
async resetQRKeyringState() {
|
|
912
969
|
(await this.getOrAddQRKeyring()).resetStore();
|
|
@@ -933,34 +990,37 @@ var KeyringController = class extends BaseController {
|
|
|
933
990
|
(await this.getOrAddQRKeyring()).cancelSync();
|
|
934
991
|
}
|
|
935
992
|
async connectQRHardware(page) {
|
|
936
|
-
|
|
937
|
-
|
|
938
|
-
|
|
939
|
-
|
|
940
|
-
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
993
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
994
|
+
try {
|
|
995
|
+
const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
|
|
996
|
+
let accounts;
|
|
997
|
+
switch (page) {
|
|
998
|
+
case -1:
|
|
999
|
+
accounts = await keyring.getPreviousPage();
|
|
1000
|
+
break;
|
|
1001
|
+
case 1:
|
|
1002
|
+
accounts = await keyring.getNextPage();
|
|
1003
|
+
break;
|
|
1004
|
+
default:
|
|
1005
|
+
accounts = await keyring.getFirstPage();
|
|
1006
|
+
}
|
|
1007
|
+
return accounts.map((account) => {
|
|
1008
|
+
return {
|
|
1009
|
+
...account,
|
|
1010
|
+
balance: "0x0"
|
|
1011
|
+
};
|
|
1012
|
+
});
|
|
1013
|
+
} catch (e) {
|
|
1014
|
+
throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
|
|
948
1015
|
}
|
|
949
|
-
|
|
950
|
-
return {
|
|
951
|
-
...account,
|
|
952
|
-
balance: "0x0"
|
|
953
|
-
};
|
|
954
|
-
});
|
|
955
|
-
} catch (e) {
|
|
956
|
-
throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
|
|
957
|
-
}
|
|
1016
|
+
});
|
|
958
1017
|
}
|
|
959
1018
|
async unlockQRHardwareWalletAccount(index) {
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
1019
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
1020
|
+
const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
|
|
1021
|
+
keyring.setAccountToUnlock(index);
|
|
1022
|
+
await keyring.addAccounts(1);
|
|
1023
|
+
});
|
|
964
1024
|
}
|
|
965
1025
|
async getAccountKeyringType(account) {
|
|
966
1026
|
const keyring = await this.getKeyringForAccount(
|
|
@@ -969,21 +1029,22 @@ var KeyringController = class extends BaseController {
|
|
|
969
1029
|
return keyring.type;
|
|
970
1030
|
}
|
|
971
1031
|
async forgetQRDevice() {
|
|
972
|
-
|
|
973
|
-
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
1032
|
+
return __privateMethod(this, _persistOrRollback, persistOrRollback_fn).call(this, async () => {
|
|
1033
|
+
const keyring = this.getQRKeyring();
|
|
1034
|
+
if (!keyring) {
|
|
1035
|
+
return { removedAccounts: [], remainingAccounts: [] };
|
|
1036
|
+
}
|
|
1037
|
+
const allAccounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
|
|
1038
|
+
keyring.forgetDevice();
|
|
1039
|
+
const remainingAccounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
|
|
1040
|
+
const removedAccounts = allAccounts.filter(
|
|
1041
|
+
(address) => !remainingAccounts.includes(address)
|
|
1042
|
+
);
|
|
1043
|
+
return { removedAccounts, remainingAccounts };
|
|
1044
|
+
});
|
|
984
1045
|
}
|
|
985
1046
|
};
|
|
986
|
-
|
|
1047
|
+
_controllerOperationMutex = new WeakMap();
|
|
987
1048
|
_vaultOperationMutex = new WeakMap();
|
|
988
1049
|
_keyringBuilders = new WeakMap();
|
|
989
1050
|
_keyrings = new WeakMap();
|
|
@@ -1051,15 +1112,10 @@ getKeyringBuilderForType_fn = function(type) {
|
|
|
1051
1112
|
};
|
|
1052
1113
|
_addQRKeyring = new WeakSet();
|
|
1053
1114
|
addQRKeyring_fn = async function() {
|
|
1054
|
-
|
|
1115
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1116
|
+
return await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, "QR Hardware Wallet Device" /* qr */, {
|
|
1055
1117
|
accounts: []
|
|
1056
1118
|
});
|
|
1057
|
-
const accounts = await qrKeyring.getAccounts();
|
|
1058
|
-
await __privateMethod(this, _checkForDuplicate, checkForDuplicate_fn).call(this, "QR Hardware Wallet Device" /* qr */, accounts);
|
|
1059
|
-
__privateGet(this, _keyrings).push(qrKeyring);
|
|
1060
|
-
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
1061
|
-
__privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
|
|
1062
|
-
return qrKeyring;
|
|
1063
1119
|
};
|
|
1064
1120
|
_subscribeToQRKeyringEvents = new WeakSet();
|
|
1065
1121
|
subscribeToQRKeyringEvents_fn = function(qrKeyring) {
|
|
@@ -1081,6 +1137,7 @@ unsubscribeFromQRKeyringsEvents_fn = function() {
|
|
|
1081
1137
|
};
|
|
1082
1138
|
_createNewVaultWithKeyring = new WeakSet();
|
|
1083
1139
|
createNewVaultWithKeyring_fn = async function(password, keyring) {
|
|
1140
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1084
1141
|
if (typeof password !== "string") {
|
|
1085
1142
|
throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
|
|
1086
1143
|
}
|
|
@@ -1088,12 +1145,36 @@ createNewVaultWithKeyring_fn = async function(password, keyring) {
|
|
|
1088
1145
|
await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
|
|
1089
1146
|
await __privateMethod(this, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn).call(this, keyring.type, keyring.opts);
|
|
1090
1147
|
__privateMethod(this, _setUnlocked, setUnlocked_fn).call(this);
|
|
1091
|
-
return __privateMethod(this, _getMemState, getMemState_fn).call(this);
|
|
1092
1148
|
};
|
|
1093
1149
|
_getUpdatedKeyrings = new WeakSet();
|
|
1094
1150
|
getUpdatedKeyrings_fn = async function() {
|
|
1095
1151
|
return Promise.all(__privateGet(this, _keyrings).map(displayForKeyring));
|
|
1096
1152
|
};
|
|
1153
|
+
_getSerializedKeyrings = new WeakSet();
|
|
1154
|
+
getSerializedKeyrings_fn = async function({ includeUnsupported } = {
|
|
1155
|
+
includeUnsupported: true
|
|
1156
|
+
}) {
|
|
1157
|
+
const serializedKeyrings = await Promise.all(
|
|
1158
|
+
__privateGet(this, _keyrings).map(async (keyring) => {
|
|
1159
|
+
const [type, data] = await Promise.all([
|
|
1160
|
+
keyring.type,
|
|
1161
|
+
keyring.serialize()
|
|
1162
|
+
]);
|
|
1163
|
+
return { type, data };
|
|
1164
|
+
})
|
|
1165
|
+
);
|
|
1166
|
+
if (includeUnsupported) {
|
|
1167
|
+
serializedKeyrings.push(...__privateGet(this, _unsupportedKeyrings));
|
|
1168
|
+
}
|
|
1169
|
+
return serializedKeyrings;
|
|
1170
|
+
};
|
|
1171
|
+
_restoreSerializedKeyrings = new WeakSet();
|
|
1172
|
+
restoreSerializedKeyrings_fn = async function(serializedKeyrings) {
|
|
1173
|
+
await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
|
|
1174
|
+
for (const serializedKeyring of serializedKeyrings) {
|
|
1175
|
+
await __privateMethod(this, _restoreKeyring, restoreKeyring_fn).call(this, serializedKeyring);
|
|
1176
|
+
}
|
|
1177
|
+
};
|
|
1097
1178
|
_unlockKeyrings = new WeakSet();
|
|
1098
1179
|
unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
|
|
1099
1180
|
return __privateMethod(this, _withVaultLock, withVaultLock_fn).call(this, async ({ releaseLock }) => {
|
|
@@ -1101,7 +1182,6 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
|
|
|
1101
1182
|
if (!encryptedVault) {
|
|
1102
1183
|
throw new Error("KeyringController - Cannot unlock without a previous vault." /* VaultError */);
|
|
1103
1184
|
}
|
|
1104
|
-
await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this, { skipStateUpdate: true });
|
|
1105
1185
|
let vault;
|
|
1106
1186
|
const updatedState = {};
|
|
1107
1187
|
if (__privateGet(this, _cacheEncryptionKey)) {
|
|
@@ -1141,7 +1221,7 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
|
|
|
1141
1221
|
if (!isSerializedKeyringsArray(vault)) {
|
|
1142
1222
|
throw new Error("KeyringController - The decrypted vault has an unexpected shape." /* VaultDataError */);
|
|
1143
1223
|
}
|
|
1144
|
-
await
|
|
1224
|
+
await __privateMethod(this, _restoreSerializedKeyrings, restoreSerializedKeyrings_fn).call(this, vault);
|
|
1145
1225
|
const updatedKeyrings = await __privateMethod(this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
|
|
1146
1226
|
this.update((state) => {
|
|
1147
1227
|
state.keyrings = updatedKeyrings;
|
|
@@ -1164,16 +1244,7 @@ updateVault_fn = function() {
|
|
|
1164
1244
|
if (!__privateGet(this, _password) && !encryptionKey) {
|
|
1165
1245
|
throw new Error("KeyringController - Cannot persist vault without password and encryption key" /* MissingCredentials */);
|
|
1166
1246
|
}
|
|
1167
|
-
const serializedKeyrings = await
|
|
1168
|
-
__privateGet(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(...__privateGet(this, _unsupportedKeyrings));
|
|
1247
|
+
const serializedKeyrings = await __privateMethod(this, _getSerializedKeyrings, getSerializedKeyrings_fn).call(this);
|
|
1177
1248
|
if (!serializedKeyrings.some((keyring) => keyring.type === "HD Key Tree" /* hd */)) {
|
|
1178
1249
|
throw new Error("KeyringController - No HD Keyring found" /* NoHdKeyring */);
|
|
1179
1250
|
}
|
|
@@ -1233,14 +1304,16 @@ getAccountsFromKeyrings_fn = async function() {
|
|
|
1233
1304
|
};
|
|
1234
1305
|
_createKeyringWithFirstAccount = new WeakSet();
|
|
1235
1306
|
createKeyringWithFirstAccount_fn = async function(type, opts) {
|
|
1236
|
-
|
|
1307
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1308
|
+
const keyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts);
|
|
1237
1309
|
const [firstAccount] = await keyring.getAccounts();
|
|
1238
1310
|
if (!firstAccount) {
|
|
1239
1311
|
throw new Error("KeyringController - First Account not found." /* NoFirstAccount */);
|
|
1240
1312
|
}
|
|
1241
1313
|
};
|
|
1242
1314
|
_newKeyring = new WeakSet();
|
|
1243
|
-
newKeyring_fn = async function(type, data
|
|
1315
|
+
newKeyring_fn = async function(type, data) {
|
|
1316
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1244
1317
|
const keyringBuilder = __privateMethod(this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, type);
|
|
1245
1318
|
if (!keyringBuilder) {
|
|
1246
1319
|
throw new Error(
|
|
@@ -1262,32 +1335,26 @@ newKeyring_fn = async function(type, data, persist = false) {
|
|
|
1262
1335
|
await keyring.addAccounts(1);
|
|
1263
1336
|
}
|
|
1264
1337
|
await __privateMethod(this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, await keyring.getAccounts());
|
|
1265
|
-
if (
|
|
1266
|
-
|
|
1267
|
-
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
1338
|
+
if (type === "QR Hardware Wallet Device" /* qr */) {
|
|
1339
|
+
__privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, keyring);
|
|
1268
1340
|
}
|
|
1341
|
+
__privateGet(this, _keyrings).push(keyring);
|
|
1269
1342
|
return keyring;
|
|
1270
1343
|
};
|
|
1271
1344
|
_clearKeyrings = new WeakSet();
|
|
1272
|
-
clearKeyrings_fn = async function(
|
|
1345
|
+
clearKeyrings_fn = async function() {
|
|
1346
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1273
1347
|
for (const keyring of __privateGet(this, _keyrings)) {
|
|
1274
1348
|
await __privateMethod(this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
|
|
1275
1349
|
}
|
|
1276
1350
|
__privateSet(this, _keyrings, []);
|
|
1277
|
-
if (!options.skipStateUpdate) {
|
|
1278
|
-
this.update((state) => {
|
|
1279
|
-
state.keyrings = [];
|
|
1280
|
-
});
|
|
1281
|
-
}
|
|
1282
1351
|
};
|
|
1283
1352
|
_restoreKeyring = new WeakSet();
|
|
1284
1353
|
restoreKeyring_fn = async function(serialized) {
|
|
1354
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1285
1355
|
try {
|
|
1286
1356
|
const { type, data } = serialized;
|
|
1287
|
-
|
|
1288
|
-
await keyring.getAccounts();
|
|
1289
|
-
__privateGet(this, _keyrings).push(keyring);
|
|
1290
|
-
return keyring;
|
|
1357
|
+
return await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, data);
|
|
1291
1358
|
} catch (_) {
|
|
1292
1359
|
__privateGet(this, _unsupportedKeyrings).push(serialized);
|
|
1293
1360
|
return void 0;
|
|
@@ -1299,6 +1366,7 @@ destroyKeyring_fn = async function(keyring) {
|
|
|
1299
1366
|
};
|
|
1300
1367
|
_removeEmptyKeyrings = new WeakSet();
|
|
1301
1368
|
removeEmptyKeyrings_fn = async function() {
|
|
1369
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1302
1370
|
const validKeyrings = [];
|
|
1303
1371
|
await Promise.all(
|
|
1304
1372
|
__privateGet(this, _keyrings).map(async (keyring) => {
|
|
@@ -1334,27 +1402,57 @@ checkForDuplicate_fn = async function(type, newAccountArray) {
|
|
|
1334
1402
|
};
|
|
1335
1403
|
_setUnlocked = new WeakSet();
|
|
1336
1404
|
setUnlocked_fn = function() {
|
|
1405
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1337
1406
|
this.update((state) => {
|
|
1338
1407
|
state.isUnlocked = true;
|
|
1339
1408
|
});
|
|
1340
1409
|
this.messagingSystem.publish(`${name}:unlock`);
|
|
1341
1410
|
};
|
|
1342
|
-
|
|
1343
|
-
|
|
1344
|
-
return {
|
|
1345
|
-
|
|
1346
|
-
|
|
1347
|
-
|
|
1411
|
+
_persistOrRollback = new WeakSet();
|
|
1412
|
+
persistOrRollback_fn = async function(fn) {
|
|
1413
|
+
return __privateMethod(this, _withRollback, withRollback_fn).call(this, async ({ releaseLock }) => {
|
|
1414
|
+
const callbackResult = await fn({ releaseLock });
|
|
1415
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
1416
|
+
return callbackResult;
|
|
1417
|
+
});
|
|
1418
|
+
};
|
|
1419
|
+
_withRollback = new WeakSet();
|
|
1420
|
+
withRollback_fn = async function(fn) {
|
|
1421
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async ({ releaseLock }) => {
|
|
1422
|
+
const currentSerializedKeyrings = await __privateMethod(this, _getSerializedKeyrings, getSerializedKeyrings_fn).call(this);
|
|
1423
|
+
const currentPassword = __privateGet(this, _password);
|
|
1424
|
+
try {
|
|
1425
|
+
return await fn({ releaseLock });
|
|
1426
|
+
} catch (e) {
|
|
1427
|
+
await __privateMethod(this, _restoreSerializedKeyrings, restoreSerializedKeyrings_fn).call(this, currentSerializedKeyrings);
|
|
1428
|
+
__privateSet(this, _password, currentPassword);
|
|
1429
|
+
throw e;
|
|
1430
|
+
}
|
|
1431
|
+
});
|
|
1432
|
+
};
|
|
1433
|
+
_assertControllerMutexIsLocked = new WeakSet();
|
|
1434
|
+
assertControllerMutexIsLocked_fn = function() {
|
|
1435
|
+
if (!__privateGet(this, _controllerOperationMutex).isLocked()) {
|
|
1436
|
+
throw new Error("KeyringController - attempt to update vault during a non mutually exclusive operation" /* ControllerLockRequired */);
|
|
1437
|
+
}
|
|
1438
|
+
};
|
|
1439
|
+
_withControllerLock = new WeakSet();
|
|
1440
|
+
withControllerLock_fn = async function(fn) {
|
|
1441
|
+
return withLock(__privateGet(this, _controllerOperationMutex), fn);
|
|
1348
1442
|
};
|
|
1349
1443
|
_withVaultLock = new WeakSet();
|
|
1350
1444
|
withVaultLock_fn = async function(fn) {
|
|
1351
|
-
|
|
1445
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1446
|
+
return withLock(__privateGet(this, _vaultOperationMutex), fn);
|
|
1447
|
+
};
|
|
1448
|
+
async function withLock(mutex, fn) {
|
|
1449
|
+
const releaseLock = await mutex.acquire();
|
|
1352
1450
|
try {
|
|
1353
1451
|
return await fn({ releaseLock });
|
|
1354
1452
|
} finally {
|
|
1355
1453
|
releaseLock();
|
|
1356
1454
|
}
|
|
1357
|
-
}
|
|
1455
|
+
}
|
|
1358
1456
|
var KeyringController_default = KeyringController;
|
|
1359
1457
|
|
|
1360
1458
|
export {
|
|
@@ -1367,4 +1465,4 @@ export {
|
|
|
1367
1465
|
KeyringController,
|
|
1368
1466
|
KeyringController_default
|
|
1369
1467
|
};
|
|
1370
|
-
//# sourceMappingURL=chunk-
|
|
1468
|
+
//# sourceMappingURL=chunk-USAGXPFN.mjs.map
|