@metamask-previews/keyring-controller 19.0.2-preview-e5e6db48 → 19.0.2-preview-e96ca40c
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 +0 -4
- package/dist/KeyringController.cjs +71 -55
- package/dist/KeyringController.cjs.map +1 -1
- package/dist/KeyringController.d.cts +9 -4
- package/dist/KeyringController.d.cts.map +1 -1
- package/dist/KeyringController.d.mts +9 -4
- package/dist/KeyringController.d.mts.map +1 -1
- package/dist/KeyringController.mjs +71 -55
- package/dist/KeyringController.mjs.map +1 -1
- package/package.json +8 -4
package/CHANGELOG.md
CHANGED
|
@@ -7,10 +7,6 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|
|
7
7
|
|
|
8
8
|
## [Unreleased]
|
|
9
9
|
|
|
10
|
-
### Changed
|
|
11
|
-
|
|
12
|
-
- Bump `@metamask/base-controller` from `^7.0.0` to `^7.1.0` ([#5079](https://github.com/MetaMask/core/pull/5079))
|
|
13
|
-
|
|
14
10
|
## [19.0.2]
|
|
15
11
|
|
|
16
12
|
### Changed
|
|
@@ -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,
|
|
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;
|
|
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");
|
|
@@ -300,16 +300,17 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
300
300
|
* Adds a new account to the default (first) HD seed phrase keyring.
|
|
301
301
|
*
|
|
302
302
|
* @param accountCount - Number of accounts before adding a new one, used to
|
|
303
|
+
* @param typeIndex - The id of the keyring to add the account to.
|
|
303
304
|
* make the method idempotent.
|
|
304
305
|
* @returns Promise resolving to the added account address.
|
|
305
306
|
*/
|
|
306
|
-
async addNewAccount(accountCount) {
|
|
307
|
+
async addNewAccount(accountCount, typeIndex) {
|
|
307
308
|
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
308
|
-
const
|
|
309
|
-
if (!
|
|
309
|
+
const selectedKeyring = this.getKeyringsByType('HD Key Tree')[typeIndex ?? 0];
|
|
310
|
+
if (!selectedKeyring) {
|
|
310
311
|
throw new Error('No HD keyring found');
|
|
311
312
|
}
|
|
312
|
-
const oldAccounts = await
|
|
313
|
+
const oldAccounts = await selectedKeyring.getAccounts();
|
|
313
314
|
if (accountCount && oldAccounts.length !== accountCount) {
|
|
314
315
|
if (accountCount > oldAccounts.length) {
|
|
315
316
|
throw new Error('Account out of sequence');
|
|
@@ -321,8 +322,8 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
321
322
|
}
|
|
322
323
|
return existingAccount;
|
|
323
324
|
}
|
|
324
|
-
const [addedAccountAddress] = await
|
|
325
|
-
await
|
|
325
|
+
const [addedAccountAddress] = await selectedKeyring.addAccounts(1);
|
|
326
|
+
await this.verifySeedPhrase(typeIndex ?? 0);
|
|
326
327
|
return addedAccountAddress;
|
|
327
328
|
});
|
|
328
329
|
}
|
|
@@ -375,6 +376,14 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
375
376
|
});
|
|
376
377
|
});
|
|
377
378
|
}
|
|
379
|
+
async createKeyringFromMnemonic(mnemonic) {
|
|
380
|
+
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
381
|
+
return await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, KeyringTypes.hd, {
|
|
382
|
+
mnemonic,
|
|
383
|
+
numberOfAccounts: 1,
|
|
384
|
+
});
|
|
385
|
+
});
|
|
386
|
+
}
|
|
378
387
|
/**
|
|
379
388
|
* Create a new vault and primary keyring.
|
|
380
389
|
*
|
|
@@ -431,12 +440,21 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
431
440
|
* Gets the seed phrase of the HD keyring.
|
|
432
441
|
*
|
|
433
442
|
* @param password - Password of the keyring.
|
|
443
|
+
* @param typeIndex - The index of the keyring type.
|
|
434
444
|
* @returns Promise resolving to the seed phrase.
|
|
435
445
|
*/
|
|
436
|
-
async exportSeedPhrase(password) {
|
|
446
|
+
async exportSeedPhrase(password, typeIndex) {
|
|
437
447
|
await this.verifyPassword(password);
|
|
438
|
-
|
|
439
|
-
|
|
448
|
+
if (!typeIndex) {
|
|
449
|
+
assertHasUint8ArrayMnemonic(__classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0]);
|
|
450
|
+
return __classPrivateFieldGet(this, _KeyringController_keyrings, "f")[0].mnemonic;
|
|
451
|
+
}
|
|
452
|
+
const selectedKeyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").filter((keyring) => keyring.type === KeyringTypes.hd)[typeIndex];
|
|
453
|
+
if (!selectedKeyring) {
|
|
454
|
+
throw new Error('Keyring not found');
|
|
455
|
+
}
|
|
456
|
+
assertHasUint8ArrayMnemonic(selectedKeyring);
|
|
457
|
+
return selectedKeyring.mnemonic;
|
|
440
458
|
}
|
|
441
459
|
/**
|
|
442
460
|
* Gets the private key from the keyring controlling an address.
|
|
@@ -456,10 +474,15 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
456
474
|
/**
|
|
457
475
|
* Returns the public addresses of all accounts from every keyring.
|
|
458
476
|
*
|
|
477
|
+
* @param keyringIndex - The index of the keyring to get the accounts from.
|
|
459
478
|
* @returns A promise resolving to an array of addresses.
|
|
460
479
|
*/
|
|
461
|
-
async getAccounts() {
|
|
462
|
-
|
|
480
|
+
async getAccounts(keyringIndex) {
|
|
481
|
+
const keyrings = this.state.keyrings.filter((keyring) => keyring.type === KeyringTypes.hd);
|
|
482
|
+
if (keyringIndex) {
|
|
483
|
+
return keyrings[keyringIndex].accounts;
|
|
484
|
+
}
|
|
485
|
+
return keyrings.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
|
|
463
486
|
}
|
|
464
487
|
/**
|
|
465
488
|
* Get encryption public key.
|
|
@@ -837,10 +860,43 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
837
860
|
/**
|
|
838
861
|
* Verifies the that the seed phrase restores the current keychain's accounts.
|
|
839
862
|
*
|
|
863
|
+
* @param typeIndex - The index of the keyring to verify.
|
|
840
864
|
* @returns Promise resolving to the seed phrase as Uint8Array.
|
|
841
865
|
*/
|
|
842
|
-
async verifySeedPhrase() {
|
|
843
|
-
|
|
866
|
+
async verifySeedPhrase(typeIndex) {
|
|
867
|
+
const primaryKeyring = this.getKeyringsByType(KeyringTypes.hd)[typeIndex ?? 0];
|
|
868
|
+
if (!primaryKeyring) {
|
|
869
|
+
throw new Error('No HD keyring found.');
|
|
870
|
+
}
|
|
871
|
+
assertHasUint8ArrayMnemonic(primaryKeyring);
|
|
872
|
+
const seedWords = primaryKeyring.mnemonic;
|
|
873
|
+
const accounts = await primaryKeyring.getAccounts();
|
|
874
|
+
/* istanbul ignore if */
|
|
875
|
+
if (accounts.length === 0) {
|
|
876
|
+
throw new Error('Cannot verify an empty keyring.');
|
|
877
|
+
}
|
|
878
|
+
// The HD Keyring Builder is a default keyring builder
|
|
879
|
+
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
880
|
+
const hdKeyringBuilder = __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_getKeyringBuilderForType).call(this, KeyringTypes.hd);
|
|
881
|
+
const hdKeyring = hdKeyringBuilder();
|
|
882
|
+
// @ts-expect-error @metamask/eth-hd-keyring correctly handles
|
|
883
|
+
// Uint8Array seed phrases in the `deserialize` method.
|
|
884
|
+
await hdKeyring.deserialize({
|
|
885
|
+
mnemonic: seedWords,
|
|
886
|
+
numberOfAccounts: accounts.length,
|
|
887
|
+
});
|
|
888
|
+
const testAccounts = await hdKeyring.getAccounts();
|
|
889
|
+
/* istanbul ignore if */
|
|
890
|
+
if (testAccounts.length !== accounts.length) {
|
|
891
|
+
throw new Error('Seed phrase imported incorrect number of accounts.');
|
|
892
|
+
}
|
|
893
|
+
testAccounts.forEach((account, i) => {
|
|
894
|
+
/* istanbul ignore if */
|
|
895
|
+
if (account.toLowerCase() !== accounts[i].toLowerCase()) {
|
|
896
|
+
throw new Error('Seed phrase imported different accounts.');
|
|
897
|
+
}
|
|
898
|
+
});
|
|
899
|
+
return seedWords;
|
|
844
900
|
}
|
|
845
901
|
async withKeyring(selector, operation, options = {
|
|
846
902
|
createIfMissing: false,
|
|
@@ -1124,47 +1180,6 @@ async function _KeyringController_createNewVaultWithKeyring(password, keyring) {
|
|
|
1124
1180
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_clearKeyrings).call(this);
|
|
1125
1181
|
await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_createKeyringWithFirstAccount).call(this, keyring.type, keyring.opts);
|
|
1126
1182
|
__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;
|
|
1168
1183
|
}, _KeyringController_getUpdatedKeyrings =
|
|
1169
1184
|
/**
|
|
1170
1185
|
* Get the updated array of each keyring's type and
|
|
@@ -1360,6 +1375,7 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
|
|
|
1360
1375
|
if (!firstAccount) {
|
|
1361
1376
|
throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
|
|
1362
1377
|
}
|
|
1378
|
+
return firstAccount;
|
|
1363
1379
|
}, _KeyringController_newKeyring =
|
|
1364
1380
|
/**
|
|
1365
1381
|
* Instantiate, initialize and return a new keyring of the given `type`,
|