@metamask-previews/keyring-controller 15.0.0-preview-11ae513 → 15.0.0-preview-fb52b9eb
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-XPARO3LL.mjs → chunk-HT7WOORD.mjs} +2 -1
- package/dist/{chunk-XPARO3LL.mjs.map → chunk-HT7WOORD.mjs.map} +1 -1
- package/dist/{chunk-LKJCRVNR.js → chunk-ISACMNF3.js} +429 -368
- package/dist/chunk-ISACMNF3.js.map +1 -0
- package/dist/{chunk-DNQK26H6.js → chunk-QDPHKQON.js} +2 -1
- package/dist/chunk-QDPHKQON.js.map +1 -0
- package/dist/{chunk-SFPZOKV2.mjs → chunk-RVCG63UG.mjs} +351 -290
- package/dist/chunk-RVCG63UG.mjs.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 +19 -27
- package/dist/types/KeyringController.d.ts.map +1 -1
- package/dist/types/constants.d.ts +2 -1
- package/dist/types/constants.d.ts.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-DNQK26H6.js.map +0 -1
- package/dist/chunk-LKJCRVNR.js.map +0 -1
- package/dist/chunk-SFPZOKV2.mjs.map +0 -1
|
@@ -3,7 +3,7 @@ import {
|
|
|
3
3
|
__privateGet,
|
|
4
4
|
__privateMethod,
|
|
5
5
|
__privateSet
|
|
6
|
-
} from "./chunk-
|
|
6
|
+
} from "./chunk-HT7WOORD.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, _unlockKeyrings, unlockKeyrings_fn, _updateVault, updateVault_fn, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn, _newKeyring, newKeyring_fn, _clearKeyrings, clearKeyrings_fn, _restoreKeyring, restoreKeyring_fn, _destroyKeyring, destroyKeyring_fn, _removeEmptyKeyrings, removeEmptyKeyrings_fn, _checkForDuplicate, checkForDuplicate_fn, _setUnlocked, setUnlocked_fn, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn, _withControllerLock, withControllerLock_fn, _withVaultLock, withVaultLock_fn;
|
|
90
90
|
var KeyringController = class extends BaseController {
|
|
91
91
|
/**
|
|
92
92
|
* Creates a KeyringController instance.
|
|
@@ -180,6 +180,19 @@ var KeyringController = class extends BaseController {
|
|
|
180
180
|
* @returns A promise resolving to the deserialized keyrings array.
|
|
181
181
|
*/
|
|
182
182
|
__privateAdd(this, _unlockKeyrings);
|
|
183
|
+
/**
|
|
184
|
+
* Update the vault with the current keyrings.
|
|
185
|
+
*
|
|
186
|
+
* @returns A promise resolving to `true` if the operation is successful.
|
|
187
|
+
*/
|
|
188
|
+
__privateAdd(this, _updateVault);
|
|
189
|
+
/**
|
|
190
|
+
* Retrieves all the accounts from keyrings instances
|
|
191
|
+
* that are currently in memory.
|
|
192
|
+
*
|
|
193
|
+
* @returns A promise resolving to an array of accounts.
|
|
194
|
+
*/
|
|
195
|
+
__privateAdd(this, _getAccountsFromKeyrings);
|
|
183
196
|
/**
|
|
184
197
|
* Create a new keyring, ensuring that the first account is
|
|
185
198
|
* also created.
|
|
@@ -194,9 +207,12 @@ var KeyringController = class extends BaseController {
|
|
|
194
207
|
* using the given `opts`. The keyring is built using the keyring builder
|
|
195
208
|
* registered for the given `type`.
|
|
196
209
|
*
|
|
210
|
+
*
|
|
197
211
|
* @param type - The type of keyring to add.
|
|
198
212
|
* @param data - The data to restore a previously serialized keyring.
|
|
213
|
+
* @param persist - Whether to persist the keyring to the vault.
|
|
199
214
|
* @returns The new keyring.
|
|
215
|
+
* @throws If the keyring includes duplicated accounts.
|
|
200
216
|
*/
|
|
201
217
|
__privateAdd(this, _newKeyring);
|
|
202
218
|
/**
|
|
@@ -250,7 +266,25 @@ var KeyringController = class extends BaseController {
|
|
|
250
266
|
* @fires KeyringController:unlock
|
|
251
267
|
*/
|
|
252
268
|
__privateAdd(this, _setUnlocked);
|
|
253
|
-
|
|
269
|
+
/**
|
|
270
|
+
* Assert that the controller mutex is locked.
|
|
271
|
+
*
|
|
272
|
+
* @throws If the controller mutex is not locked.
|
|
273
|
+
*/
|
|
274
|
+
__privateAdd(this, _assertControllerMutexIsLocked);
|
|
275
|
+
/**
|
|
276
|
+
* Lock the controller mutex before executing the given function,
|
|
277
|
+
* and release it after the function is resolved or after an
|
|
278
|
+
* error is thrown.
|
|
279
|
+
*
|
|
280
|
+
* This wrapper ensures that each mutable operation that interacts with the
|
|
281
|
+
* controller and that changes its state is executed in a mutually exclusive way,
|
|
282
|
+
* preventing unsafe concurrent access that could lead to unpredictable behavior.
|
|
283
|
+
*
|
|
284
|
+
* @param fn - The function to execute while the controller mutex is locked.
|
|
285
|
+
* @returns The result of the function.
|
|
286
|
+
*/
|
|
287
|
+
__privateAdd(this, _withControllerLock);
|
|
254
288
|
/**
|
|
255
289
|
* Lock the vault mutex before executing the given function,
|
|
256
290
|
* and release it after the function is resolved or after an
|
|
@@ -263,7 +297,7 @@ var KeyringController = class extends BaseController {
|
|
|
263
297
|
* @returns The result of the function.
|
|
264
298
|
*/
|
|
265
299
|
__privateAdd(this, _withVaultLock);
|
|
266
|
-
__privateAdd(this,
|
|
300
|
+
__privateAdd(this, _controllerOperationMutex, new Mutex());
|
|
267
301
|
__privateAdd(this, _vaultOperationMutex, new Mutex());
|
|
268
302
|
__privateAdd(this, _keyringBuilders, void 0);
|
|
269
303
|
__privateAdd(this, _keyrings, void 0);
|
|
@@ -287,76 +321,74 @@ var KeyringController = class extends BaseController {
|
|
|
287
321
|
*
|
|
288
322
|
* @param accountCount - Number of accounts before adding a new one, used to
|
|
289
323
|
* make the method idempotent.
|
|
290
|
-
* @returns Promise resolving to
|
|
291
|
-
* address.
|
|
324
|
+
* @returns Promise resolving to the added account address.
|
|
292
325
|
*/
|
|
293
326
|
async addNewAccount(accountCount) {
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
const oldAccounts = await primaryKeyring.getAccounts();
|
|
299
|
-
if (accountCount && oldAccounts.length !== accountCount) {
|
|
300
|
-
if (accountCount > oldAccounts.length) {
|
|
301
|
-
throw new Error("Account out of sequence");
|
|
327
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
328
|
+
const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
|
|
329
|
+
if (!primaryKeyring) {
|
|
330
|
+
throw new Error("No HD keyring found");
|
|
302
331
|
}
|
|
303
|
-
const
|
|
304
|
-
if (
|
|
305
|
-
|
|
332
|
+
const oldAccounts = await primaryKeyring.getAccounts();
|
|
333
|
+
if (accountCount && oldAccounts.length !== accountCount) {
|
|
334
|
+
if (accountCount > oldAccounts.length) {
|
|
335
|
+
throw new Error("Account out of sequence");
|
|
336
|
+
}
|
|
337
|
+
const existingAccount = oldAccounts[accountCount];
|
|
338
|
+
if (!existingAccount) {
|
|
339
|
+
throw new Error(`Can't find account at index ${accountCount}`);
|
|
340
|
+
}
|
|
341
|
+
return existingAccount;
|
|
306
342
|
}
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
}
|
|
312
|
-
const addedAccountAddress = await this.addNewAccountForKeyring(
|
|
313
|
-
primaryKeyring
|
|
314
|
-
);
|
|
315
|
-
await this.verifySeedPhrase();
|
|
316
|
-
return {
|
|
317
|
-
keyringState: __privateMethod(this, _getMemState, getMemState_fn).call(this),
|
|
318
|
-
addedAccountAddress
|
|
319
|
-
};
|
|
343
|
+
const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
|
|
344
|
+
await this.verifySeedPhrase();
|
|
345
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
346
|
+
return addedAccountAddress;
|
|
347
|
+
});
|
|
320
348
|
}
|
|
321
349
|
/**
|
|
322
350
|
* Adds a new account to the specified keyring.
|
|
323
351
|
*
|
|
324
352
|
* @param keyring - Keyring to add the account to.
|
|
325
353
|
* @param accountCount - Number of accounts before adding a new one, used to make the method idempotent.
|
|
326
|
-
* @returns Promise resolving to
|
|
354
|
+
* @returns Promise resolving to the added account address
|
|
327
355
|
*/
|
|
328
356
|
async addNewAccountForKeyring(keyring, accountCount) {
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
if (accountCount
|
|
332
|
-
|
|
357
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
358
|
+
const oldAccounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
|
|
359
|
+
if (accountCount && oldAccounts.length !== accountCount) {
|
|
360
|
+
if (accountCount > oldAccounts.length) {
|
|
361
|
+
throw new Error("Account out of sequence");
|
|
362
|
+
}
|
|
363
|
+
const existingAccount = oldAccounts[accountCount];
|
|
364
|
+
assertIsStrictHexString(existingAccount);
|
|
365
|
+
return existingAccount;
|
|
333
366
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
);
|
|
343
|
-
assertIsStrictHexString(addedAccountAddress);
|
|
344
|
-
return addedAccountAddress;
|
|
367
|
+
await keyring.addAccounts(1);
|
|
368
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
369
|
+
const addedAccountAddress = (await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this)).find(
|
|
370
|
+
(selectedAddress) => !oldAccounts.includes(selectedAddress)
|
|
371
|
+
);
|
|
372
|
+
assertIsStrictHexString(addedAccountAddress);
|
|
373
|
+
return addedAccountAddress;
|
|
374
|
+
});
|
|
345
375
|
}
|
|
346
376
|
/**
|
|
347
377
|
* Adds a new account to the default (first) HD seed phrase keyring without updating identities in preferences.
|
|
348
378
|
*
|
|
349
|
-
* @returns Promise resolving to
|
|
379
|
+
* @returns Promise resolving to the added account address.
|
|
350
380
|
*/
|
|
351
381
|
async addNewAccountWithoutUpdate() {
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
382
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
383
|
+
const primaryKeyring = this.getKeyringsByType("HD Key Tree")[0];
|
|
384
|
+
if (!primaryKeyring) {
|
|
385
|
+
throw new Error("No HD keyring found");
|
|
386
|
+
}
|
|
387
|
+
const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
|
|
388
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
389
|
+
await this.verifySeedPhrase();
|
|
390
|
+
return addedAccountAddress;
|
|
391
|
+
});
|
|
360
392
|
}
|
|
361
393
|
/**
|
|
362
394
|
* Effectively the same as creating a new keychain then populating it
|
|
@@ -365,14 +397,13 @@ var KeyringController = class extends BaseController {
|
|
|
365
397
|
* @param password - Password to unlock keychain.
|
|
366
398
|
* @param seed - A BIP39-compliant seed phrase as Uint8Array,
|
|
367
399
|
* either as a string or an array of UTF-8 bytes that represent the string.
|
|
368
|
-
* @returns Promise resolving
|
|
400
|
+
* @returns Promise resolving when the operation ends successfully.
|
|
369
401
|
*/
|
|
370
402
|
async createNewVaultAndRestore(password, seed) {
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
try {
|
|
403
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
404
|
+
if (!password || !password.length) {
|
|
405
|
+
throw new Error("Invalid password");
|
|
406
|
+
}
|
|
376
407
|
await __privateMethod(this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
|
|
377
408
|
type: "HD Key Tree" /* hd */,
|
|
378
409
|
opts: {
|
|
@@ -380,30 +411,23 @@ var KeyringController = class extends BaseController {
|
|
|
380
411
|
numberOfAccounts: 1
|
|
381
412
|
}
|
|
382
413
|
});
|
|
383
|
-
|
|
384
|
-
} finally {
|
|
385
|
-
releaseLock();
|
|
386
|
-
}
|
|
414
|
+
});
|
|
387
415
|
}
|
|
388
416
|
/**
|
|
389
417
|
* Create a new primary keychain and wipe any previous keychains.
|
|
390
418
|
*
|
|
391
419
|
* @param password - Password to unlock the new vault.
|
|
392
|
-
* @returns
|
|
420
|
+
* @returns Promise resolving when the operation ends successfully.
|
|
393
421
|
*/
|
|
394
422
|
async createNewVaultAndKeychain(password) {
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
const accounts = await this.getAccounts();
|
|
423
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
424
|
+
const accounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
|
|
398
425
|
if (!accounts.length) {
|
|
399
426
|
await __privateMethod(this, _createNewVaultWithKeyring, createNewVaultWithKeyring_fn).call(this, password, {
|
|
400
427
|
type: "HD Key Tree" /* hd */
|
|
401
428
|
});
|
|
402
429
|
}
|
|
403
|
-
|
|
404
|
-
} finally {
|
|
405
|
-
releaseLock();
|
|
406
|
-
}
|
|
430
|
+
});
|
|
407
431
|
}
|
|
408
432
|
/**
|
|
409
433
|
* Adds a new keyring of the given `type`.
|
|
@@ -417,21 +441,7 @@ var KeyringController = class extends BaseController {
|
|
|
417
441
|
if (type === "QR Hardware Wallet Device" /* qr */) {
|
|
418
442
|
return this.getOrAddQRKeyring();
|
|
419
443
|
}
|
|
420
|
-
|
|
421
|
-
if (type === "HD Key Tree" /* hd */ && (!isObject(opts) || !opts.mnemonic)) {
|
|
422
|
-
if (!keyring.generateRandomMnemonic) {
|
|
423
|
-
throw new Error(
|
|
424
|
-
"KeyringController - The current keyring does not support the method generateRandomMnemonic." /* UnsupportedGenerateRandomMnemonic */
|
|
425
|
-
);
|
|
426
|
-
}
|
|
427
|
-
keyring.generateRandomMnemonic();
|
|
428
|
-
await keyring.addAccounts(1);
|
|
429
|
-
}
|
|
430
|
-
const accounts = await keyring.getAccounts();
|
|
431
|
-
await __privateMethod(this, _checkForDuplicate, checkForDuplicate_fn).call(this, type, accounts);
|
|
432
|
-
__privateGet(this, _keyrings).push(keyring);
|
|
433
|
-
await this.persistAllKeyrings();
|
|
434
|
-
return keyring;
|
|
444
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts, true));
|
|
435
445
|
}
|
|
436
446
|
/**
|
|
437
447
|
* Method to verify a given password validity. Throws an
|
|
@@ -487,14 +497,10 @@ var KeyringController = class extends BaseController {
|
|
|
487
497
|
* @returns A promise resolving to an array of addresses.
|
|
488
498
|
*/
|
|
489
499
|
async getAccounts() {
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
500
|
+
return this.state.keyrings.reduce(
|
|
501
|
+
(accounts, keyring) => accounts.concat(keyring.accounts),
|
|
502
|
+
[]
|
|
493
503
|
);
|
|
494
|
-
const addresses = keyringArrays.reduce((res, arr) => {
|
|
495
|
-
return res.concat(arr);
|
|
496
|
-
}, []);
|
|
497
|
-
return addresses.map(normalize);
|
|
498
504
|
}
|
|
499
505
|
/**
|
|
500
506
|
* Get encryption public key.
|
|
@@ -587,66 +593,7 @@ var KeyringController = class extends BaseController {
|
|
|
587
593
|
* operation completes.
|
|
588
594
|
*/
|
|
589
595
|
async persistAllKeyrings() {
|
|
590
|
-
return __privateMethod(this,
|
|
591
|
-
const { encryptionKey, encryptionSalt } = this.state;
|
|
592
|
-
if (!__privateGet(this, _password) && !encryptionKey) {
|
|
593
|
-
throw new Error("KeyringController - Cannot persist vault without password and encryption key" /* MissingCredentials */);
|
|
594
|
-
}
|
|
595
|
-
const serializedKeyrings = await Promise.all(
|
|
596
|
-
__privateGet(this, _keyrings).map(async (keyring) => {
|
|
597
|
-
const [type, data] = await Promise.all([
|
|
598
|
-
keyring.type,
|
|
599
|
-
keyring.serialize()
|
|
600
|
-
]);
|
|
601
|
-
return { type, data };
|
|
602
|
-
})
|
|
603
|
-
);
|
|
604
|
-
serializedKeyrings.push(...__privateGet(this, _unsupportedKeyrings));
|
|
605
|
-
if (!serializedKeyrings.some((keyring) => keyring.type === "HD Key Tree" /* hd */)) {
|
|
606
|
-
throw new Error("KeyringController - No HD Keyring found" /* NoHdKeyring */);
|
|
607
|
-
}
|
|
608
|
-
const updatedState = {};
|
|
609
|
-
if (__privateGet(this, _cacheEncryptionKey)) {
|
|
610
|
-
assertIsExportableKeyEncryptor(__privateGet(this, _encryptor));
|
|
611
|
-
if (encryptionKey) {
|
|
612
|
-
const key = await __privateGet(this, _encryptor).importKey(encryptionKey);
|
|
613
|
-
const vaultJSON = await __privateGet(this, _encryptor).encryptWithKey(
|
|
614
|
-
key,
|
|
615
|
-
serializedKeyrings
|
|
616
|
-
);
|
|
617
|
-
vaultJSON.salt = encryptionSalt;
|
|
618
|
-
updatedState.vault = JSON.stringify(vaultJSON);
|
|
619
|
-
} else if (__privateGet(this, _password)) {
|
|
620
|
-
const { vault: newVault, exportedKeyString } = await __privateGet(this, _encryptor).encryptWithDetail(
|
|
621
|
-
__privateGet(this, _password),
|
|
622
|
-
serializedKeyrings
|
|
623
|
-
);
|
|
624
|
-
updatedState.vault = newVault;
|
|
625
|
-
updatedState.encryptionKey = exportedKeyString;
|
|
626
|
-
}
|
|
627
|
-
} else {
|
|
628
|
-
if (typeof __privateGet(this, _password) !== "string") {
|
|
629
|
-
throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
|
|
630
|
-
}
|
|
631
|
-
updatedState.vault = await __privateGet(this, _encryptor).encrypt(
|
|
632
|
-
__privateGet(this, _password),
|
|
633
|
-
serializedKeyrings
|
|
634
|
-
);
|
|
635
|
-
}
|
|
636
|
-
if (!updatedState.vault) {
|
|
637
|
-
throw new Error("KeyringController - Cannot persist vault without vault information" /* MissingVaultData */);
|
|
638
|
-
}
|
|
639
|
-
const updatedKeyrings = await __privateMethod(this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
|
|
640
|
-
this.update((state) => {
|
|
641
|
-
state.vault = updatedState.vault;
|
|
642
|
-
state.keyrings = updatedKeyrings;
|
|
643
|
-
if (updatedState.encryptionKey) {
|
|
644
|
-
state.encryptionKey = updatedState.encryptionKey;
|
|
645
|
-
state.encryptionSalt = JSON.parse(updatedState.vault).salt;
|
|
646
|
-
}
|
|
647
|
-
});
|
|
648
|
-
return true;
|
|
649
|
-
});
|
|
596
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => __privateMethod(this, _updateVault, updateVault_fn).call(this));
|
|
650
597
|
}
|
|
651
598
|
/**
|
|
652
599
|
* Imports an account with the specified import strategy.
|
|
@@ -654,90 +601,88 @@ var KeyringController = class extends BaseController {
|
|
|
654
601
|
* @param strategy - Import strategy name.
|
|
655
602
|
* @param args - Array of arguments to pass to the underlying stategy.
|
|
656
603
|
* @throws Will throw when passed an unrecognized strategy.
|
|
657
|
-
* @returns Promise resolving to
|
|
658
|
-
* address.
|
|
604
|
+
* @returns Promise resolving to the imported account address.
|
|
659
605
|
*/
|
|
660
606
|
async importAccountWithStrategy(strategy, args) {
|
|
661
|
-
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
|
|
671
|
-
|
|
672
|
-
|
|
673
|
-
|
|
674
|
-
|
|
675
|
-
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
686
|
-
|
|
687
|
-
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
privateKey
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
keyringState: __privateMethod(this, _getMemState, getMemState_fn).call(this),
|
|
700
|
-
importedAccountAddress: accounts[0]
|
|
701
|
-
};
|
|
607
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
608
|
+
let privateKey;
|
|
609
|
+
switch (strategy) {
|
|
610
|
+
case "privateKey":
|
|
611
|
+
const [importedKey] = args;
|
|
612
|
+
if (!importedKey) {
|
|
613
|
+
throw new Error("Cannot import an empty key.");
|
|
614
|
+
}
|
|
615
|
+
const prefixed = add0x(importedKey);
|
|
616
|
+
let bufferedPrivateKey;
|
|
617
|
+
try {
|
|
618
|
+
bufferedPrivateKey = toBuffer(prefixed);
|
|
619
|
+
} catch {
|
|
620
|
+
throw new Error("Cannot import invalid private key.");
|
|
621
|
+
}
|
|
622
|
+
if (!isValidPrivate(bufferedPrivateKey) || // ensures that the key is 64 bytes long
|
|
623
|
+
getBinarySize(prefixed) !== 64 + "0x".length) {
|
|
624
|
+
throw new Error("Cannot import invalid private key.");
|
|
625
|
+
}
|
|
626
|
+
privateKey = remove0x(prefixed);
|
|
627
|
+
break;
|
|
628
|
+
case "json":
|
|
629
|
+
let wallet;
|
|
630
|
+
const [input, password] = args;
|
|
631
|
+
try {
|
|
632
|
+
wallet = importers.fromEtherWallet(input, password);
|
|
633
|
+
} catch (e) {
|
|
634
|
+
wallet = wallet || await Wallet.fromV3(input, password, true);
|
|
635
|
+
}
|
|
636
|
+
privateKey = bytesToHex(wallet.getPrivateKey());
|
|
637
|
+
break;
|
|
638
|
+
default:
|
|
639
|
+
throw new Error(`Unexpected import strategy: '${strategy}'`);
|
|
640
|
+
}
|
|
641
|
+
const newKeyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, "Simple Key Pair" /* simple */, [privateKey], true);
|
|
642
|
+
const accounts = await newKeyring.getAccounts();
|
|
643
|
+
return accounts[0];
|
|
644
|
+
});
|
|
702
645
|
}
|
|
703
646
|
/**
|
|
704
647
|
* Removes an account from keyring state.
|
|
705
648
|
*
|
|
706
649
|
* @param address - Address of the account to remove.
|
|
707
650
|
* @fires KeyringController:accountRemoved
|
|
708
|
-
* @returns Promise resolving
|
|
651
|
+
* @returns Promise resolving when the account is removed.
|
|
709
652
|
*/
|
|
710
653
|
async removeAccount(address) {
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
721
|
-
|
|
722
|
-
|
|
654
|
+
await __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
655
|
+
const keyring = await this.getKeyringForAccount(
|
|
656
|
+
address
|
|
657
|
+
);
|
|
658
|
+
if (!keyring.removeAccount) {
|
|
659
|
+
throw new Error("`KeyringController - The keyring for the current address does not support the method removeAccount" /* UnsupportedRemoveAccount */);
|
|
660
|
+
}
|
|
661
|
+
await keyring.removeAccount(address);
|
|
662
|
+
const accounts = await keyring.getAccounts();
|
|
663
|
+
if (accounts.length === 0) {
|
|
664
|
+
await __privateMethod(this, _removeEmptyKeyrings, removeEmptyKeyrings_fn).call(this);
|
|
665
|
+
}
|
|
666
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
667
|
+
});
|
|
723
668
|
this.messagingSystem.publish(`${name}:accountRemoved`, address);
|
|
724
|
-
return __privateMethod(this, _getMemState, getMemState_fn).call(this);
|
|
725
669
|
}
|
|
726
670
|
/**
|
|
727
671
|
* Deallocates all secrets and locks the wallet.
|
|
728
672
|
*
|
|
729
|
-
* @returns Promise resolving
|
|
673
|
+
* @returns Promise resolving when the operation completes.
|
|
730
674
|
*/
|
|
731
675
|
async setLocked() {
|
|
732
|
-
__privateMethod(this,
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
state
|
|
736
|
-
|
|
676
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
677
|
+
__privateMethod(this, _unsubscribeFromQRKeyringsEvents, unsubscribeFromQRKeyringsEvents_fn).call(this);
|
|
678
|
+
__privateSet(this, _password, void 0);
|
|
679
|
+
this.update((state) => {
|
|
680
|
+
state.isUnlocked = false;
|
|
681
|
+
state.keyrings = [];
|
|
682
|
+
});
|
|
683
|
+
await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
|
|
684
|
+
this.messagingSystem.publish(`${name}:lock`);
|
|
737
685
|
});
|
|
738
|
-
await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
|
|
739
|
-
this.messagingSystem.publish(`${name}:lock`);
|
|
740
|
-
return __privateMethod(this, _getMemState, getMemState_fn).call(this);
|
|
741
686
|
}
|
|
742
687
|
/**
|
|
743
688
|
* Signs message by calling down into a specific keyring.
|
|
@@ -891,32 +836,34 @@ var KeyringController = class extends BaseController {
|
|
|
891
836
|
*
|
|
892
837
|
* @param encryptionKey - Key to unlock the keychain.
|
|
893
838
|
* @param encryptionSalt - Salt to unlock the keychain.
|
|
894
|
-
* @returns Promise resolving
|
|
839
|
+
* @returns Promise resolving when the operation completes.
|
|
895
840
|
*/
|
|
896
841
|
async submitEncryptionKey(encryptionKey, encryptionSalt) {
|
|
897
|
-
|
|
898
|
-
|
|
899
|
-
|
|
900
|
-
|
|
901
|
-
|
|
902
|
-
|
|
903
|
-
|
|
842
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
843
|
+
__privateSet(this, _keyrings, await __privateMethod(this, _unlockKeyrings, unlockKeyrings_fn).call(this, void 0, encryptionKey, encryptionSalt));
|
|
844
|
+
__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
|
+
});
|
|
904
850
|
}
|
|
905
851
|
/**
|
|
906
852
|
* Attempts to decrypt the current vault and load its keyrings,
|
|
907
853
|
* using the given password.
|
|
908
854
|
*
|
|
909
855
|
* @param password - Password to unlock the keychain.
|
|
910
|
-
* @returns Promise resolving
|
|
856
|
+
* @returns Promise resolving when the operation completes.
|
|
911
857
|
*/
|
|
912
858
|
async submitPassword(password) {
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
859
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
860
|
+
__privateSet(this, _keyrings, await __privateMethod(this, _unlockKeyrings, unlockKeyrings_fn).call(this, password));
|
|
861
|
+
__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
|
+
});
|
|
920
867
|
}
|
|
921
868
|
/**
|
|
922
869
|
* Verifies the that the seed phrase restores the current keychain's accounts.
|
|
@@ -966,13 +913,16 @@ var KeyringController = class extends BaseController {
|
|
|
966
913
|
* @returns The added keyring
|
|
967
914
|
*/
|
|
968
915
|
async getOrAddQRKeyring() {
|
|
969
|
-
return this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
|
|
916
|
+
return this.getQRKeyring() || await __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this));
|
|
970
917
|
}
|
|
971
918
|
// TODO: Replace `any` with type
|
|
972
919
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
973
920
|
async restoreQRKeyring(serialized) {
|
|
974
|
-
(
|
|
975
|
-
|
|
921
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
922
|
+
const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
|
|
923
|
+
keyring.deserialize(serialized);
|
|
924
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
925
|
+
});
|
|
976
926
|
}
|
|
977
927
|
async resetQRKeyringState() {
|
|
978
928
|
(await this.getOrAddQRKeyring()).resetStore();
|
|
@@ -999,34 +949,38 @@ var KeyringController = class extends BaseController {
|
|
|
999
949
|
(await this.getOrAddQRKeyring()).cancelSync();
|
|
1000
950
|
}
|
|
1001
951
|
async connectQRHardware(page) {
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
952
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
953
|
+
try {
|
|
954
|
+
const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
|
|
955
|
+
let accounts;
|
|
956
|
+
switch (page) {
|
|
957
|
+
case -1:
|
|
958
|
+
accounts = await keyring.getPreviousPage();
|
|
959
|
+
break;
|
|
960
|
+
case 1:
|
|
961
|
+
accounts = await keyring.getNextPage();
|
|
962
|
+
break;
|
|
963
|
+
default:
|
|
964
|
+
accounts = await keyring.getFirstPage();
|
|
965
|
+
}
|
|
966
|
+
return accounts.map((account) => {
|
|
967
|
+
return {
|
|
968
|
+
...account,
|
|
969
|
+
balance: "0x0"
|
|
970
|
+
};
|
|
971
|
+
});
|
|
972
|
+
} catch (e) {
|
|
973
|
+
throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
|
|
1014
974
|
}
|
|
1015
|
-
|
|
1016
|
-
return {
|
|
1017
|
-
...account,
|
|
1018
|
-
balance: "0x0"
|
|
1019
|
-
};
|
|
1020
|
-
});
|
|
1021
|
-
} catch (e) {
|
|
1022
|
-
throw new Error(`Unspecified error when connect QR Hardware, ${e}`);
|
|
1023
|
-
}
|
|
975
|
+
});
|
|
1024
976
|
}
|
|
1025
977
|
async unlockQRHardwareWalletAccount(index) {
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
|
|
1029
|
-
|
|
978
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
979
|
+
const keyring = this.getQRKeyring() || await __privateMethod(this, _addQRKeyring, addQRKeyring_fn).call(this);
|
|
980
|
+
keyring.setAccountToUnlock(index);
|
|
981
|
+
await keyring.addAccounts(1);
|
|
982
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
983
|
+
});
|
|
1030
984
|
}
|
|
1031
985
|
async getAccountKeyringType(account) {
|
|
1032
986
|
const keyring = await this.getKeyringForAccount(
|
|
@@ -1035,18 +989,23 @@ var KeyringController = class extends BaseController {
|
|
|
1035
989
|
return keyring.type;
|
|
1036
990
|
}
|
|
1037
991
|
async forgetQRDevice() {
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
1042
|
-
|
|
1043
|
-
(
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
992
|
+
return __privateMethod(this, _withControllerLock, withControllerLock_fn).call(this, async () => {
|
|
993
|
+
const keyring = this.getQRKeyring();
|
|
994
|
+
if (!keyring) {
|
|
995
|
+
return { removedAccounts: [], remainingAccounts: [] };
|
|
996
|
+
}
|
|
997
|
+
const allAccounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
|
|
998
|
+
keyring.forgetDevice();
|
|
999
|
+
const remainingAccounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
|
|
1000
|
+
const removedAccounts = allAccounts.filter(
|
|
1001
|
+
(address) => !remainingAccounts.includes(address)
|
|
1002
|
+
);
|
|
1003
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
1004
|
+
return { removedAccounts, remainingAccounts };
|
|
1005
|
+
});
|
|
1047
1006
|
}
|
|
1048
1007
|
};
|
|
1049
|
-
|
|
1008
|
+
_controllerOperationMutex = new WeakMap();
|
|
1050
1009
|
_vaultOperationMutex = new WeakMap();
|
|
1051
1010
|
_keyringBuilders = new WeakMap();
|
|
1052
1011
|
_keyrings = new WeakMap();
|
|
@@ -1114,13 +1073,14 @@ getKeyringBuilderForType_fn = function(type) {
|
|
|
1114
1073
|
};
|
|
1115
1074
|
_addQRKeyring = new WeakSet();
|
|
1116
1075
|
addQRKeyring_fn = async function() {
|
|
1076
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1117
1077
|
const qrKeyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, "QR Hardware Wallet Device" /* qr */, {
|
|
1118
1078
|
accounts: []
|
|
1119
1079
|
});
|
|
1120
1080
|
const accounts = await qrKeyring.getAccounts();
|
|
1121
1081
|
await __privateMethod(this, _checkForDuplicate, checkForDuplicate_fn).call(this, "QR Hardware Wallet Device" /* qr */, accounts);
|
|
1122
1082
|
__privateGet(this, _keyrings).push(qrKeyring);
|
|
1123
|
-
await this.
|
|
1083
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
1124
1084
|
__privateMethod(this, _subscribeToQRKeyringEvents, subscribeToQRKeyringEvents_fn).call(this, qrKeyring);
|
|
1125
1085
|
return qrKeyring;
|
|
1126
1086
|
};
|
|
@@ -1144,6 +1104,7 @@ unsubscribeFromQRKeyringsEvents_fn = function() {
|
|
|
1144
1104
|
};
|
|
1145
1105
|
_createNewVaultWithKeyring = new WeakSet();
|
|
1146
1106
|
createNewVaultWithKeyring_fn = async function(password, keyring) {
|
|
1107
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1147
1108
|
if (typeof password !== "string") {
|
|
1148
1109
|
throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
|
|
1149
1110
|
}
|
|
@@ -1151,7 +1112,6 @@ createNewVaultWithKeyring_fn = async function(password, keyring) {
|
|
|
1151
1112
|
await __privateMethod(this, _clearKeyrings, clearKeyrings_fn).call(this);
|
|
1152
1113
|
await __privateMethod(this, _createKeyringWithFirstAccount, createKeyringWithFirstAccount_fn).call(this, keyring.type, keyring.opts);
|
|
1153
1114
|
__privateMethod(this, _setUnlocked, setUnlocked_fn).call(this);
|
|
1154
|
-
return __privateMethod(this, _getMemState, getMemState_fn).call(this);
|
|
1155
1115
|
};
|
|
1156
1116
|
_getUpdatedKeyrings = new WeakSet();
|
|
1157
1117
|
getUpdatedKeyrings_fn = async function() {
|
|
@@ -1215,21 +1175,97 @@ unlockKeyrings_fn = async function(password, encryptionKey, encryptionSalt) {
|
|
|
1215
1175
|
});
|
|
1216
1176
|
if (__privateGet(this, _password) && (!__privateGet(this, _cacheEncryptionKey) || !encryptionKey) && __privateGet(this, _encryptor).isVaultUpdated && !__privateGet(this, _encryptor).isVaultUpdated(encryptedVault)) {
|
|
1217
1177
|
releaseLock();
|
|
1218
|
-
await this.
|
|
1178
|
+
await __privateMethod(this, _updateVault, updateVault_fn).call(this);
|
|
1219
1179
|
}
|
|
1220
1180
|
return __privateGet(this, _keyrings);
|
|
1221
1181
|
});
|
|
1222
1182
|
};
|
|
1183
|
+
_updateVault = new WeakSet();
|
|
1184
|
+
updateVault_fn = function() {
|
|
1185
|
+
return __privateMethod(this, _withVaultLock, withVaultLock_fn).call(this, async () => {
|
|
1186
|
+
const { encryptionKey, encryptionSalt } = this.state;
|
|
1187
|
+
if (!__privateGet(this, _password) && !encryptionKey) {
|
|
1188
|
+
throw new Error("KeyringController - Cannot persist vault without password and encryption key" /* MissingCredentials */);
|
|
1189
|
+
}
|
|
1190
|
+
const serializedKeyrings = await Promise.all(
|
|
1191
|
+
__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));
|
|
1200
|
+
if (!serializedKeyrings.some((keyring) => keyring.type === "HD Key Tree" /* hd */)) {
|
|
1201
|
+
throw new Error("KeyringController - No HD Keyring found" /* NoHdKeyring */);
|
|
1202
|
+
}
|
|
1203
|
+
const updatedState = {};
|
|
1204
|
+
if (__privateGet(this, _cacheEncryptionKey)) {
|
|
1205
|
+
assertIsExportableKeyEncryptor(__privateGet(this, _encryptor));
|
|
1206
|
+
if (encryptionKey) {
|
|
1207
|
+
const key = await __privateGet(this, _encryptor).importKey(encryptionKey);
|
|
1208
|
+
const vaultJSON = await __privateGet(this, _encryptor).encryptWithKey(
|
|
1209
|
+
key,
|
|
1210
|
+
serializedKeyrings
|
|
1211
|
+
);
|
|
1212
|
+
vaultJSON.salt = encryptionSalt;
|
|
1213
|
+
updatedState.vault = JSON.stringify(vaultJSON);
|
|
1214
|
+
} else if (__privateGet(this, _password)) {
|
|
1215
|
+
const { vault: newVault, exportedKeyString } = await __privateGet(this, _encryptor).encryptWithDetail(
|
|
1216
|
+
__privateGet(this, _password),
|
|
1217
|
+
serializedKeyrings
|
|
1218
|
+
);
|
|
1219
|
+
updatedState.vault = newVault;
|
|
1220
|
+
updatedState.encryptionKey = exportedKeyString;
|
|
1221
|
+
}
|
|
1222
|
+
} else {
|
|
1223
|
+
if (typeof __privateGet(this, _password) !== "string") {
|
|
1224
|
+
throw new TypeError("KeyringController - Password must be of type string." /* WrongPasswordType */);
|
|
1225
|
+
}
|
|
1226
|
+
updatedState.vault = await __privateGet(this, _encryptor).encrypt(
|
|
1227
|
+
__privateGet(this, _password),
|
|
1228
|
+
serializedKeyrings
|
|
1229
|
+
);
|
|
1230
|
+
}
|
|
1231
|
+
if (!updatedState.vault) {
|
|
1232
|
+
throw new Error("KeyringController - Cannot persist vault without vault information" /* MissingVaultData */);
|
|
1233
|
+
}
|
|
1234
|
+
const updatedKeyrings = await __privateMethod(this, _getUpdatedKeyrings, getUpdatedKeyrings_fn).call(this);
|
|
1235
|
+
this.update((state) => {
|
|
1236
|
+
state.vault = updatedState.vault;
|
|
1237
|
+
state.keyrings = updatedKeyrings;
|
|
1238
|
+
if (updatedState.encryptionKey) {
|
|
1239
|
+
state.encryptionKey = updatedState.encryptionKey;
|
|
1240
|
+
state.encryptionSalt = JSON.parse(updatedState.vault).salt;
|
|
1241
|
+
}
|
|
1242
|
+
});
|
|
1243
|
+
return true;
|
|
1244
|
+
});
|
|
1245
|
+
};
|
|
1246
|
+
_getAccountsFromKeyrings = new WeakSet();
|
|
1247
|
+
getAccountsFromKeyrings_fn = async function() {
|
|
1248
|
+
const keyrings = __privateGet(this, _keyrings);
|
|
1249
|
+
const keyringArrays = await Promise.all(
|
|
1250
|
+
keyrings.map(async (keyring) => keyring.getAccounts())
|
|
1251
|
+
);
|
|
1252
|
+
const addresses = keyringArrays.reduce((res, arr) => {
|
|
1253
|
+
return res.concat(arr);
|
|
1254
|
+
}, []);
|
|
1255
|
+
return addresses.map(normalize);
|
|
1256
|
+
};
|
|
1223
1257
|
_createKeyringWithFirstAccount = new WeakSet();
|
|
1224
1258
|
createKeyringWithFirstAccount_fn = async function(type, opts) {
|
|
1225
|
-
|
|
1259
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1260
|
+
const keyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, opts, true);
|
|
1226
1261
|
const [firstAccount] = await keyring.getAccounts();
|
|
1227
1262
|
if (!firstAccount) {
|
|
1228
1263
|
throw new Error("KeyringController - First Account not found." /* NoFirstAccount */);
|
|
1229
1264
|
}
|
|
1230
1265
|
};
|
|
1231
1266
|
_newKeyring = new WeakSet();
|
|
1232
|
-
newKeyring_fn = async function(type, data) {
|
|
1267
|
+
newKeyring_fn = async function(type, data, persist = false) {
|
|
1268
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1233
1269
|
const keyringBuilder = __privateMethod(this, _getKeyringBuilderForType, getKeyringBuilderForType_fn).call(this, type);
|
|
1234
1270
|
if (!keyringBuilder) {
|
|
1235
1271
|
throw new Error(
|
|
@@ -1241,10 +1277,25 @@ newKeyring_fn = async function(type, data) {
|
|
|
1241
1277
|
if (keyring.init) {
|
|
1242
1278
|
await keyring.init();
|
|
1243
1279
|
}
|
|
1280
|
+
if (type === "HD Key Tree" /* hd */ && (!isObject(data) || !data.mnemonic)) {
|
|
1281
|
+
if (!keyring.generateRandomMnemonic) {
|
|
1282
|
+
throw new Error(
|
|
1283
|
+
"KeyringController - The current keyring does not support the method generateRandomMnemonic." /* UnsupportedGenerateRandomMnemonic */
|
|
1284
|
+
);
|
|
1285
|
+
}
|
|
1286
|
+
keyring.generateRandomMnemonic();
|
|
1287
|
+
await keyring.addAccounts(1);
|
|
1288
|
+
}
|
|
1289
|
+
await __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);
|
|
1293
|
+
}
|
|
1244
1294
|
return keyring;
|
|
1245
1295
|
};
|
|
1246
1296
|
_clearKeyrings = new WeakSet();
|
|
1247
1297
|
clearKeyrings_fn = async function(options = { skipStateUpdate: false }) {
|
|
1298
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1248
1299
|
for (const keyring of __privateGet(this, _keyrings)) {
|
|
1249
1300
|
await __privateMethod(this, _destroyKeyring, destroyKeyring_fn).call(this, keyring);
|
|
1250
1301
|
}
|
|
@@ -1257,6 +1308,7 @@ clearKeyrings_fn = async function(options = { skipStateUpdate: false }) {
|
|
|
1257
1308
|
};
|
|
1258
1309
|
_restoreKeyring = new WeakSet();
|
|
1259
1310
|
restoreKeyring_fn = async function(serialized) {
|
|
1311
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1260
1312
|
try {
|
|
1261
1313
|
const { type, data } = serialized;
|
|
1262
1314
|
const keyring = await __privateMethod(this, _newKeyring, newKeyring_fn).call(this, type, data);
|
|
@@ -1274,6 +1326,7 @@ destroyKeyring_fn = async function(keyring) {
|
|
|
1274
1326
|
};
|
|
1275
1327
|
_removeEmptyKeyrings = new WeakSet();
|
|
1276
1328
|
removeEmptyKeyrings_fn = async function() {
|
|
1329
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1277
1330
|
const validKeyrings = [];
|
|
1278
1331
|
await Promise.all(
|
|
1279
1332
|
__privateGet(this, _keyrings).map(async (keyring) => {
|
|
@@ -1289,7 +1342,7 @@ removeEmptyKeyrings_fn = async function() {
|
|
|
1289
1342
|
};
|
|
1290
1343
|
_checkForDuplicate = new WeakSet();
|
|
1291
1344
|
checkForDuplicate_fn = async function(type, newAccountArray) {
|
|
1292
|
-
const accounts = await this.
|
|
1345
|
+
const accounts = await __privateMethod(this, _getAccountsFromKeyrings, getAccountsFromKeyrings_fn).call(this);
|
|
1293
1346
|
switch (type) {
|
|
1294
1347
|
case "Simple Key Pair" /* simple */: {
|
|
1295
1348
|
const isIncluded = Boolean(
|
|
@@ -1309,27 +1362,35 @@ checkForDuplicate_fn = async function(type, newAccountArray) {
|
|
|
1309
1362
|
};
|
|
1310
1363
|
_setUnlocked = new WeakSet();
|
|
1311
1364
|
setUnlocked_fn = function() {
|
|
1365
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1312
1366
|
this.update((state) => {
|
|
1313
1367
|
state.isUnlocked = true;
|
|
1314
1368
|
});
|
|
1315
1369
|
this.messagingSystem.publish(`${name}:unlock`);
|
|
1316
1370
|
};
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1371
|
+
_assertControllerMutexIsLocked = new WeakSet();
|
|
1372
|
+
assertControllerMutexIsLocked_fn = function() {
|
|
1373
|
+
if (!__privateGet(this, _controllerOperationMutex).isLocked()) {
|
|
1374
|
+
throw new Error("KeyringController - attempt to update vault during a non mutually exclusive operation" /* ControllerLockRequired */);
|
|
1375
|
+
}
|
|
1376
|
+
};
|
|
1377
|
+
_withControllerLock = new WeakSet();
|
|
1378
|
+
withControllerLock_fn = async function(fn) {
|
|
1379
|
+
return withLock(__privateGet(this, _controllerOperationMutex), fn);
|
|
1323
1380
|
};
|
|
1324
1381
|
_withVaultLock = new WeakSet();
|
|
1325
1382
|
withVaultLock_fn = async function(fn) {
|
|
1326
|
-
|
|
1383
|
+
__privateMethod(this, _assertControllerMutexIsLocked, assertControllerMutexIsLocked_fn).call(this);
|
|
1384
|
+
return withLock(__privateGet(this, _vaultOperationMutex), fn);
|
|
1385
|
+
};
|
|
1386
|
+
async function withLock(mutex, fn) {
|
|
1387
|
+
const releaseLock = await mutex.acquire();
|
|
1327
1388
|
try {
|
|
1328
1389
|
return await fn({ releaseLock });
|
|
1329
1390
|
} finally {
|
|
1330
1391
|
releaseLock();
|
|
1331
1392
|
}
|
|
1332
|
-
}
|
|
1393
|
+
}
|
|
1333
1394
|
var KeyringController_default = KeyringController;
|
|
1334
1395
|
|
|
1335
1396
|
export {
|
|
@@ -1342,4 +1403,4 @@ export {
|
|
|
1342
1403
|
KeyringController,
|
|
1343
1404
|
KeyringController_default
|
|
1344
1405
|
};
|
|
1345
|
-
//# sourceMappingURL=chunk-
|
|
1406
|
+
//# sourceMappingURL=chunk-RVCG63UG.mjs.map
|