@metamask-previews/keyring-controller 19.0.1-preview-470d2e31 → 19.0.1-preview-953c11ad
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 +75 -13
- package/dist/KeyringController.cjs.map +1 -1
- package/dist/KeyringController.d.cts +21 -5
- package/dist/KeyringController.d.cts.map +1 -1
- package/dist/KeyringController.d.mts +21 -5
- package/dist/KeyringController.d.mts.map +1 -1
- package/dist/KeyringController.mjs +75 -13
- package/dist/KeyringController.mjs.map +1 -1
- package/package.json +16 -6
|
@@ -48,6 +48,7 @@ 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");
|
|
51
52
|
const constants_1 = require("./constants.cjs");
|
|
52
53
|
const name = 'KeyringController';
|
|
53
54
|
/**
|
|
@@ -199,11 +200,14 @@ function isSerializedKeyringsArray(array) {
|
|
|
199
200
|
*/
|
|
200
201
|
async function displayForKeyring(keyring) {
|
|
201
202
|
const accounts = await keyring.getAccounts();
|
|
203
|
+
const { opts } = keyring;
|
|
202
204
|
return {
|
|
203
205
|
type: keyring.type,
|
|
204
206
|
// Cast to `string[]` here is safe here because `accounts` has no nullish
|
|
205
207
|
// values, and `normalize` returns `string` unless given a nullish value
|
|
206
208
|
accounts: accounts.map(normalize),
|
|
209
|
+
typeIndex: opts?.typeIndex,
|
|
210
|
+
id: opts?.id,
|
|
207
211
|
};
|
|
208
212
|
}
|
|
209
213
|
/**
|
|
@@ -300,16 +304,23 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
300
304
|
* Adds a new account to the default (first) HD seed phrase keyring.
|
|
301
305
|
*
|
|
302
306
|
* @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.
|
|
303
308
|
* make the method idempotent.
|
|
304
309
|
* @returns Promise resolving to the added account address.
|
|
305
310
|
*/
|
|
306
|
-
async addNewAccount(accountCount) {
|
|
311
|
+
async addNewAccount(accountCount, keyringId) {
|
|
307
312
|
return __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_persistOrRollback).call(this, async () => {
|
|
308
|
-
|
|
309
|
-
if (
|
|
313
|
+
let selectedKeyring;
|
|
314
|
+
if (keyringId) {
|
|
315
|
+
selectedKeyring = this.getKeyringById(keyringId);
|
|
316
|
+
}
|
|
317
|
+
else {
|
|
318
|
+
selectedKeyring = this.getKeyringsByType(KeyringTypes.hd)[0];
|
|
319
|
+
}
|
|
320
|
+
if (!selectedKeyring) {
|
|
310
321
|
throw new Error('No HD keyring found');
|
|
311
322
|
}
|
|
312
|
-
const oldAccounts = await
|
|
323
|
+
const oldAccounts = await selectedKeyring.getAccounts();
|
|
313
324
|
if (accountCount && oldAccounts.length !== accountCount) {
|
|
314
325
|
if (accountCount > oldAccounts.length) {
|
|
315
326
|
throw new Error('Account out of sequence');
|
|
@@ -321,7 +332,7 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
321
332
|
}
|
|
322
333
|
return existingAccount;
|
|
323
334
|
}
|
|
324
|
-
const [addedAccountAddress] = await
|
|
335
|
+
const [addedAccountAddress] = await selectedKeyring.addAccounts(1);
|
|
325
336
|
await this.verifySeedPhrase();
|
|
326
337
|
return addedAccountAddress;
|
|
327
338
|
});
|
|
@@ -375,6 +386,14 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
375
386
|
});
|
|
376
387
|
});
|
|
377
388
|
}
|
|
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
|
+
}
|
|
378
397
|
/**
|
|
379
398
|
* Create a new vault and primary keyring.
|
|
380
399
|
*
|
|
@@ -431,12 +450,18 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
431
450
|
* Gets the seed phrase of the HD keyring.
|
|
432
451
|
*
|
|
433
452
|
* @param password - Password of the keyring.
|
|
453
|
+
* @param typeIndex - Hd keyring identifier
|
|
434
454
|
* @returns Promise resolving to the seed phrase.
|
|
435
455
|
*/
|
|
436
|
-
async exportSeedPhrase(password) {
|
|
456
|
+
async exportSeedPhrase(password, typeIndex) {
|
|
437
457
|
await this.verifyPassword(password);
|
|
438
|
-
|
|
439
|
-
|
|
458
|
+
const keyring = this.getKeyringsByType(KeyringTypes.hd).find((innerKeyring) => innerKeyring
|
|
459
|
+
.opts.typeIndex === typeIndex);
|
|
460
|
+
if (!keyring) {
|
|
461
|
+
throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
|
|
462
|
+
}
|
|
463
|
+
assertHasUint8ArrayMnemonic(keyring);
|
|
464
|
+
return keyring.mnemonic;
|
|
440
465
|
}
|
|
441
466
|
/**
|
|
442
467
|
* Gets the private key from the keyring controlling an address.
|
|
@@ -456,10 +481,13 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
456
481
|
/**
|
|
457
482
|
* Returns the public addresses of all accounts from every keyring.
|
|
458
483
|
*
|
|
484
|
+
* @param keyringId - The id of the keyring to get the accounts from.
|
|
459
485
|
* @returns A promise resolving to an array of addresses.
|
|
460
486
|
*/
|
|
461
|
-
async getAccounts() {
|
|
462
|
-
return this.state.keyrings
|
|
487
|
+
async getAccounts(keyringId) {
|
|
488
|
+
return this.state.keyrings
|
|
489
|
+
.filter((keyring) => (keyringId ? keyring.id === keyringId : true))
|
|
490
|
+
.reduce((accounts, keyring) => accounts.concat(keyring.accounts), []);
|
|
463
491
|
}
|
|
464
492
|
/**
|
|
465
493
|
* Get encryption public key.
|
|
@@ -493,6 +521,19 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
493
521
|
}
|
|
494
522
|
return keyring.decryptMessage(address, messageParams.data);
|
|
495
523
|
}
|
|
524
|
+
/**
|
|
525
|
+
* Returns the keyring with the given id.
|
|
526
|
+
*
|
|
527
|
+
* @param id - The id of the keyring to return.
|
|
528
|
+
* @returns The keyring with the given id.
|
|
529
|
+
*/
|
|
530
|
+
getKeyringById(id) {
|
|
531
|
+
const keyring = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").find((item) => item.opts.id === id);
|
|
532
|
+
if (!keyring) {
|
|
533
|
+
throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
|
|
534
|
+
}
|
|
535
|
+
return keyring;
|
|
536
|
+
}
|
|
496
537
|
/**
|
|
497
538
|
* Returns the currently initialized keyring that manages
|
|
498
539
|
* the specified `address` if one exists.
|
|
@@ -882,12 +923,15 @@ class KeyringController extends base_controller_1.BaseController {
|
|
|
882
923
|
if ('address' in selector) {
|
|
883
924
|
keyring = (await this.getKeyringForAccount(selector.address));
|
|
884
925
|
}
|
|
885
|
-
else {
|
|
926
|
+
else if ('type' in selector) {
|
|
886
927
|
keyring = this.getKeyringsByType(selector.type)[selector.index || 0];
|
|
887
928
|
if (!keyring && options.createIfMissing) {
|
|
888
929
|
keyring = (await __classPrivateFieldGet(this, _KeyringController_instances, "m", _KeyringController_newKeyring).call(this, selector.type, options.createWithData));
|
|
889
930
|
}
|
|
890
931
|
}
|
|
932
|
+
else if ('id' in selector) {
|
|
933
|
+
keyring = this.getKeyringById(selector.id);
|
|
934
|
+
}
|
|
891
935
|
if (!keyring) {
|
|
892
936
|
throw new Error(constants_1.KeyringControllerError.KeyringNotFound);
|
|
893
937
|
}
|
|
@@ -1351,6 +1395,7 @@ async function _KeyringController_createKeyringWithFirstAccount(type, opts) {
|
|
|
1351
1395
|
if (!firstAccount) {
|
|
1352
1396
|
throw new Error(constants_1.KeyringControllerError.NoFirstAccount);
|
|
1353
1397
|
}
|
|
1398
|
+
return firstAccount;
|
|
1354
1399
|
}, _KeyringController_newKeyring =
|
|
1355
1400
|
/**
|
|
1356
1401
|
* Instantiate, initialize and return a new keyring of the given `type`,
|
|
@@ -1370,8 +1415,25 @@ async function _KeyringController_newKeyring(type, data) {
|
|
|
1370
1415
|
throw new Error(`${constants_1.KeyringControllerError.NoKeyringBuilder}. Keyring type: ${type}`);
|
|
1371
1416
|
}
|
|
1372
1417
|
const keyring = keyringBuilder();
|
|
1373
|
-
//
|
|
1374
|
-
|
|
1418
|
+
// find the last index of the type
|
|
1419
|
+
const lastIndexOfType = __classPrivateFieldGet(this, _KeyringController_keyrings, "f").reduce((maxIndex, item) => {
|
|
1420
|
+
if (item.type === type) {
|
|
1421
|
+
return Math.max(maxIndex, item.opts
|
|
1422
|
+
?.typeIndex ?? 0);
|
|
1423
|
+
}
|
|
1424
|
+
return maxIndex;
|
|
1425
|
+
}, 0);
|
|
1426
|
+
if (type === KeyringTypes.hd) {
|
|
1427
|
+
await keyring.deserialize({
|
|
1428
|
+
...(data ?? {}),
|
|
1429
|
+
typeIndex: lastIndexOfType + 1,
|
|
1430
|
+
id: (0, ulid_1.ulid)(),
|
|
1431
|
+
});
|
|
1432
|
+
}
|
|
1433
|
+
else {
|
|
1434
|
+
// @ts-expect-error Enforce data type after updating clients
|
|
1435
|
+
await keyring.deserialize(data);
|
|
1436
|
+
}
|
|
1375
1437
|
if (keyring.init) {
|
|
1376
1438
|
await keyring.init();
|
|
1377
1439
|
}
|