@metamask-previews/keyring-controller 19.0.2-preview-579728ba → 19.0.2-preview-e218ba33
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/dist/KeyringController.cjs +54 -101
- package/dist/KeyringController.cjs.map +1 -1
- package/dist/KeyringController.d.cts +2 -18
- package/dist/KeyringController.d.cts.map +1 -1
- package/dist/KeyringController.d.mts +2 -18
- package/dist/KeyringController.d.mts.map +1 -1
- package/dist/KeyringController.mjs +54 -101
- package/dist/KeyringController.mjs.map +1 -1
- package/package.json +3 -8
|
@@ -36,7 +36,7 @@ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (
|
|
|
36
36
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
37
37
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
38
38
|
};
|
|
39
|
-
var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _KeyringController_setUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
|
|
39
|
+
var _KeyringController_instances, _KeyringController_controllerOperationMutex, _KeyringController_vaultOperationMutex, _KeyringController_keyringBuilders, _KeyringController_keyrings, _KeyringController_unsupportedKeyrings, _KeyringController_password, _KeyringController_encryptor, _KeyringController_cacheEncryptionKey, _KeyringController_qrKeyringStateListener, _KeyringController_registerMessageHandlers, _KeyringController_getKeyringBuilderForType, _KeyringController_addQRKeyring, _KeyringController_subscribeToQRKeyringEvents, _KeyringController_unsubscribeFromQRKeyringsEvents, _KeyringController_createNewVaultWithKeyring, _KeyringController_verifySeedPhrase, _KeyringController_getUpdatedKeyrings, _KeyringController_getSerializedKeyrings, _KeyringController_restoreSerializedKeyrings, _KeyringController_unlockKeyrings, _KeyringController_updateVault, _KeyringController_getAccountsFromKeyrings, _KeyringController_createKeyringWithFirstAccount, _KeyringController_newKeyring, _KeyringController_clearKeyrings, _KeyringController_restoreKeyring, _KeyringController_destroyKeyring, _KeyringController_removeEmptyKeyrings, _KeyringController_checkForDuplicate, _KeyringController_setUnlocked, _KeyringController_persistOrRollback, _KeyringController_withRollback, _KeyringController_assertControllerMutexIsLocked, _KeyringController_withControllerLock, _KeyringController_withVaultLock;
|
|
40
40
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
41
41
|
exports.KeyringController = exports.getDefaultKeyringState = exports.keyringBuilderFactory = exports.SignTypedDataVersion = exports.AccountImportStrategy = exports.isCustodyKeyring = exports.KeyringTypes = void 0;
|
|
42
42
|
const util_1 = require("@ethereumjs/util");
|
|
@@ -48,7 +48,6 @@ const eth_simple_keyring_1 = __importDefault(require("@metamask/eth-simple-keyri
|
|
|
48
48
|
const utils_1 = require("@metamask/utils");
|
|
49
49
|
const async_mutex_1 = require("async-mutex");
|
|
50
50
|
const ethereumjs_wallet_1 = __importStar(require("ethereumjs-wallet"));
|
|
51
|
-
const ulid_1 = require("ulid");
|
|
52
51
|
const constants_1 = require("./constants.cjs");
|
|
53
52
|
const name = 'KeyringController';
|
|
54
53
|
/**
|
|
@@ -200,14 +199,11 @@ function isSerializedKeyringsArray(array) {
|
|
|
200
199
|
*/
|
|
201
200
|
async function displayForKeyring(keyring) {
|
|
202
201
|
const accounts = await keyring.getAccounts();
|
|
203
|
-
const { opts } = keyring;
|
|
204
202
|
return {
|
|
205
203
|
type: keyring.type,
|
|
206
204
|
// Cast to `string[]` here is safe here because `accounts` has no nullish
|
|
207
205
|
// values, and `normalize` returns `string` unless given a nullish value
|
|
208
206
|
accounts: accounts.map(normalize),
|
|
209
|
-
typeIndex: opts?.typeIndex,
|
|
210
|
-
id: opts?.id,
|
|
211
207
|
};
|
|
212
208
|
}
|
|
213
209
|
/**
|
|
@@ -304,23 +300,16 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
304
300
|
* Adds a new account to the default (first) HD seed phrase keyring.
|
|
305
301
|
*
|
|
306
302
|
* @param accountCount - Number of accounts before adding a new one, used to
|
|
307
|
-
* @param keyringId - The id of the keyring to add the account to.
|
|
308
303
|
* make the method idempotent.
|
|
309
304
|
* @returns Promise resolving to the added account address.
|
|
310
305
|
*/
|
|
311
|
-
async addNewAccount(accountCount
|
|
306
|
+
async addNewAccount(accountCount) {
|
|
312
307
|
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
313
|
-
|
|
314
|
-
if (
|
|
315
|
-
selectedKeyring = this.getKeyringById(keyringId);
|
|
316
|
-
}
|
|
317
|
-
else {
|
|
318
|
-
selectedKeyring = this.getKeyringsByType('HD Key Tree')[0];
|
|
319
|
-
}
|
|
320
|
-
if (!selectedKeyring) {
|
|
308
|
+
const primaryKeyring = this.getKeyringsByType('HD Key Tree')[0];
|
|
309
|
+
if (!primaryKeyring) {
|
|
321
310
|
throw new Error('No HD keyring found');
|
|
322
311
|
}
|
|
323
|
-
const oldAccounts = await
|
|
312
|
+
const oldAccounts = await primaryKeyring.getAccounts();
|
|
324
313
|
if (accountCount && oldAccounts.length !== accountCount) {
|
|
325
314
|
if (accountCount > oldAccounts.length) {
|
|
326
315
|
throw new Error('Account out of sequence');
|
|
@@ -332,8 +321,8 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
332
321
|
}
|
|
333
322
|
return existingAccount;
|
|
334
323
|
}
|
|
335
|
-
const [addedAccountAddress] = await
|
|
336
|
-
await this.
|
|
324
|
+
const [addedAccountAddress] = await primaryKeyring.addAccounts(1);
|
|
325
|
+
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this);
|
|
337
326
|
return addedAccountAddress;
|
|
338
327
|
});
|
|
339
328
|
}
|
|
@@ -386,14 +375,6 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
386
375
|
});
|
|
387
376
|
});
|
|
388
377
|
}
|
|
389
|
-
async createKeyringFromMnemonic(mnemonic) {
|
|
390
|
-
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
391
|
-
return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, KeyringTypes.hd, {
|
|
392
|
-
mnemonic,
|
|
393
|
-
numberOfAccounts: 1,
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
378
|
/**
|
|
398
379
|
* Create a new vault and primary keyring.
|
|
399
380
|
*
|
|
@@ -475,13 +456,10 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
475
456
|
/**
|
|
476
457
|
* Returns the public addresses of all accounts from every keyring.
|
|
477
458
|
*
|
|
478
|
-
* @param keyringId - The id of the keyring to get the accounts from.
|
|
479
459
|
* @returns A promise resolving to an array of addresses.
|
|
480
460
|
*/
|
|
481
|
-
async getAccounts(
|
|
482
|
-
return this.state.keyrings
|
|
483
|
-
.filter((keyring) => (keyringId ? keyring.id === keyringId : true))
|
|
484
|
-
.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
|
|
461
|
+
async getAccounts() {
|
|
462
|
+
return this.state.keyrings.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
|
|
485
463
|
}
|
|
486
464
|
/**
|
|
487
465
|
* Get encryption public key.
|
|
@@ -515,19 +493,6 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
515
493
|
}
|
|
516
494
|
return keyring.decryptMessage(address, messageParams.data);
|
|
517
495
|
}
|
|
518
|
-
/**
|
|
519
|
-
* Returns the keyring with the given id.
|
|
520
|
-
*
|
|
521
|
-
* @param id - The id of the keyring to return.
|
|
522
|
-
* @returns The keyring with the given id.
|
|
523
|
-
*/
|
|
524
|
-
getKeyringById(id) {
|
|
525
|
-
const keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find((item) => item.opts.id === id);
|
|
526
|
-
if (!keyring) {
|
|
527
|
-
throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
|
|
528
|
-
}
|
|
529
|
-
return keyring;
|
|
530
|
-
}
|
|
531
496
|
/**
|
|
532
497
|
* Returns the currently initialized keyring that manages
|
|
533
498
|
* the specified `address` if one exists.
|
|
@@ -875,39 +840,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
875
840
|
* @returns Promise resolving to the seed phrase as Uint8Array.
|
|
876
841
|
*/
|
|
877
842
|
async verifySeedPhrase() {
|
|
878
|
-
|
|
879
|
-
if (!primaryKeyring) {
|
|
880
|
-
throw new Error('No HD keyring found.');
|
|
881
|
-
}
|
|
882
|
-
assertHasUint8ArrayMnemonic(primaryKeyring);
|
|
883
|
-
const seedWords = primaryKeyring.mnemonic;
|
|
884
|
-
const accounts = await primaryKeyring.getAccounts();
|
|
885
|
-
/* istanbul ignore if */
|
|
886
|
-
if (accounts.length === 0) {
|
|
887
|
-
throw new Error('Cannot verify an empty keyring.');
|
|
888
|
-
}
|
|
889
|
-
// The HD Keyring Builder is a default keyring builder
|
|
890
|
-
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
891
|
-
const hdKeyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, KeyringTypes.hd);
|
|
892
|
-
const hdKeyring = hdKeyringBuilder();
|
|
893
|
-
// @ts-expect-error @metamask/eth-hd-keyring correctly handles
|
|
894
|
-
// Uint8Array seed phrases in the `deserialize` method.
|
|
895
|
-
await hdKeyring.deserialize({
|
|
896
|
-
mnemonic: seedWords,
|
|
897
|
-
numberOfAccounts: accounts.length,
|
|
898
|
-
});
|
|
899
|
-
const testAccounts = await hdKeyring.getAccounts();
|
|
900
|
-
/* istanbul ignore if */
|
|
901
|
-
if (testAccounts.length !== accounts.length) {
|
|
902
|
-
throw new Error('Seed phrase imported incorrect number of accounts.');
|
|
903
|
-
}
|
|
904
|
-
testAccounts.forEach((account, i) => {
|
|
905
|
-
/* istanbul ignore if */
|
|
906
|
-
if (account.toLowerCase() !== accounts[i].toLowerCase()) {
|
|
907
|
-
throw new Error('Seed phrase imported different accounts.');
|
|
908
|
-
}
|
|
909
|
-
});
|
|
910
|
-
return seedWords;
|
|
843
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_withControllerLock).call(this, async () => __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_verifySeedPhrase).call(this));
|
|
911
844
|
}
|
|
912
845
|
async withKeyring(selector, operation, options = {
|
|
913
846
|
createIfMissing: false,
|
|
@@ -917,15 +850,12 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
917
850
|
if ('address' in selector) {
|
|
918
851
|
keyring = (await this.getKeyringForAccount(selector.address));
|
|
919
852
|
}
|
|
920
|
-
else
|
|
853
|
+
else {
|
|
921
854
|
keyring = this.getKeyringsByType(selector.type)[selector.index || 0];
|
|
922
855
|
if (!keyring && options.createIfMissing) {
|
|
923
856
|
keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
|
|
924
857
|
}
|
|
925
858
|
}
|
|
926
|
-
else if ('id' in selector) {
|
|
927
|
-
keyring = this.getKeyringById(selector.id);
|
|
928
|
-
}
|
|
929
859
|
if (!keyring) {
|
|
930
860
|
throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
|
|
931
861
|
}
|
|
@@ -1194,6 +1124,47 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
|
|
|
1194
1124
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
|
|
1195
1125
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, keyring.type, keyring.opts);
|
|
1196
1126
|
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_setUnlocked).call(this);
|
|
1127
|
+
}, _KeyringController_verifySeedPhrase =
|
|
1128
|
+
/**
|
|
1129
|
+
* Internal non-exclusive method to verify the seed phrase.
|
|
1130
|
+
*
|
|
1131
|
+
* @returns A promise resolving to the seed phrase as Uint8Array.
|
|
1132
|
+
*/
|
|
1133
|
+
async function _KeyringController_verifySeedPhrase() {
|
|
1134
|
+
__classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_assertControllerMutexIsLocked).call(this);
|
|
1135
|
+
const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[0];
|
|
1136
|
+
if (!primaryKeyring) {
|
|
1137
|
+
throw new Error('No HD keyring found.');
|
|
1138
|
+
}
|
|
1139
|
+
assertHasUint8ArrayMnemonic(primaryKeyring);
|
|
1140
|
+
const seedWords = primaryKeyring.mnemonic;
|
|
1141
|
+
const accounts = await primaryKeyring.getAccounts();
|
|
1142
|
+
/* istanbul ignore if */
|
|
1143
|
+
if (accounts.length === 0) {
|
|
1144
|
+
throw new Error('Cannot verify an empty keyring.');
|
|
1145
|
+
}
|
|
1146
|
+
// The HD Keyring Builder is a default keyring builder
|
|
1147
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
1148
|
+
const hdKeyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, KeyringTypes.hd);
|
|
1149
|
+
const hdKeyring = hdKeyringBuilder();
|
|
1150
|
+
// @ts-expect-error @metamask/eth-hd-keyring correctly handles
|
|
1151
|
+
// Uint8Array seed phrases in the `deserialize` method.
|
|
1152
|
+
await hdKeyring.deserialize({
|
|
1153
|
+
mnemonic: seedWords,
|
|
1154
|
+
numberOfAccounts: accounts.length,
|
|
1155
|
+
});
|
|
1156
|
+
const testAccounts = await hdKeyring.getAccounts();
|
|
1157
|
+
/* istanbul ignore if */
|
|
1158
|
+
if (testAccounts.length !== accounts.length) {
|
|
1159
|
+
throw new Error('Seed phrase imported incorrect number of accounts.');
|
|
1160
|
+
}
|
|
1161
|
+
testAccounts.forEach((account, i) => {
|
|
1162
|
+
/* istanbul ignore if */
|
|
1163
|
+
if (account.toLowerCase() !== accounts[i].toLowerCase()) {
|
|
1164
|
+
throw new Error('Seed phrase imported different accounts.');
|
|
1165
|
+
}
|
|
1166
|
+
});
|
|
1167
|
+
return seedWords;
|
|
1197
1168
|
}, _KeyringController_getUpdatedKeyrings =
|
|
1198
1169
|
/**
|
|
1199
1170
|
* Get the updated array of each keyring's type and
|
|
@@ -1389,7 +1360,6 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
|
|
|
1389
1360
|
if (!firstAccount) {
|
|
1390
1361
|
throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
|
|
1391
1362
|
}
|
|
1392
|
-
return firstAccount;
|
|
1393
1363
|
}, _KeyringController_newKeyring =
|
|
1394
1364
|
/**
|
|
1395
1365
|
* Instantiate, initialize and return a new keyring of the given `type`,
|
|
@@ -1409,25 +1379,8 @@ async function _KeyringController_newKeyring(type, data) {
|
|
|
1409
1379
|
throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
|
|
1410
1380
|
}
|
|
1411
1381
|
const keyring = keyringBuilder();
|
|
1412
|
-
//
|
|
1413
|
-
|
|
1414
|
-
if (item.type === type) {
|
|
1415
|
-
return Math.max(maxIndex, item.opts
|
|
1416
|
-
?.typeIndex ?? 0);
|
|
1417
|
-
}
|
|
1418
|
-
return maxIndex;
|
|
1419
|
-
}, 0);
|
|
1420
|
-
if (type === KeyringTypes.hd) {
|
|
1421
|
-
await keyring.deserialize({
|
|
1422
|
-
...(data ?? {}),
|
|
1423
|
-
typeIndex: lastIndexOfType + 1,
|
|
1424
|
-
id: (0, ulid_1.ulid)(),
|
|
1425
|
-
});
|
|
1426
|
-
}
|
|
1427
|
-
else {
|
|
1428
|
-
// @ts-expect-error Enforce data type after updating clients
|
|
1429
|
-
await keyring.deserialize(data);
|
|
1430
|
-
}
|
|
1382
|
+
// @ts-expect-error Enforce data type after updating clients
|
|
1383
|
+
await keyring.deserialize(data);
|
|
1431
1384
|
if (keyring.init) {
|
|
1432
1385
|
await keyring.init();
|
|
1433
1386
|
}
|