Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

Commit cf828cd

Browse files
committed
Allow non-object keyring serialized state
The `addNewKeyring` method has been updated to support non-object keyring serialized state, which had been supported already on versions prior to v11. This allows us to remove a special-case for the simple keyring, which requires a JSON array as its serialized keyring state. When constructing a simple keyring, the method `addNewKeying` now expects the second parameter (`opts`) to be an array of private keys rather than an object with a `privateKeys` property. This is how it worked prior to v11.
1 parent 9ebdc0f commit cf828cd

File tree

3 files changed

+17
-42
lines changed

3 files changed

+17
-42
lines changed

jest.config.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ module.exports = {
4141
// An object that configures minimum threshold enforcement for coverage results
4242
coverageThreshold: {
4343
global: {
44-
branches: 72.41,
44+
branches: 71.42,
4545
functions: 92.85,
46-
lines: 90.87,
47-
statements: 91.08,
46+
lines: 90.68,
47+
statements: 90.9,
4848
},
4949
},
5050
preset: 'ts-jest',

src/KeyringController.test.ts

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ describe('KeyringController', () => {
456456
const previousAccounts = await keyringController.getAccounts();
457457
const keyring = await keyringController.addNewKeyring(
458458
KeyringType.Simple,
459-
{ privateKeys: [privateKey] },
459+
[privateKey],
460460
);
461461

462462
const keyringAccounts = await keyring?.getAccounts();
@@ -472,15 +472,6 @@ describe('KeyringController', () => {
472472
expect(allAccounts).toStrictEqual(expectedAllAccounts);
473473
});
474474

475-
it('should throw an error when attempting to add simple key pair without private keys', async () => {
476-
const keyringController = await initializeKeyringController({
477-
password: PASSWORD,
478-
});
479-
await expect(async () =>
480-
keyringController.addNewKeyring(KeyringType.Simple),
481-
).rejects.toThrow('Private keys missing');
482-
});
483-
484475
it('should add HD Key Tree without mnemonic passed as an argument', async () => {
485476
const keyringController = await initializeKeyringController({
486477
password: PASSWORD,
@@ -662,9 +653,9 @@ describe('KeyringController', () => {
662653
const accountsBeforeAdding = await keyringController.getAccounts();
663654

664655
// Add a new keyring with one account
665-
await keyringController.addNewKeyring(KeyringType.Simple, {
666-
privateKeys: [account.privateKey],
667-
});
656+
await keyringController.addNewKeyring(KeyringType.Simple, [
657+
account.privateKey,
658+
]);
668659
expect(keyringController.keyrings).toHaveLength(2);
669660

670661
// remove that account that we just added
@@ -687,9 +678,9 @@ describe('KeyringController', () => {
687678
};
688679

689680
// Add a new keyring with one account
690-
await keyringController.addNewKeyring(KeyringType.Simple, {
691-
privateKeys: [account.privateKey],
692-
});
681+
await keyringController.addNewKeyring(KeyringType.Simple, [
682+
account.privateKey,
683+
]);
693684

694685
// We should have 2 keyrings
695686
expect(keyringController.keyrings).toHaveLength(2);
@@ -822,7 +813,7 @@ describe('KeyringController', () => {
822813

823814
const keyring = await keyringController.addNewKeyring(
824815
KeyringType.Simple,
825-
{ privateKeys: [privateKey] },
816+
[privateKey],
826817
);
827818

828819
const getAppKeyAddressSpy = sinon.spy(
@@ -856,9 +847,7 @@ describe('KeyringController', () => {
856847
const address = '0x01560cd3bac62cc6d7e6380600d9317363400896';
857848
const privateKey =
858849
'0xb8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952';
859-
await keyringController.addNewKeyring(KeyringType.Simple, {
860-
privateKeys: [privateKey],
861-
});
850+
await keyringController.addNewKeyring(KeyringType.Simple, [privateKey]);
862851
const appKeyAddress = await keyringController.getAppKeyAddress(
863852
address,
864853
'someapp.origin.io',
@@ -1050,9 +1039,9 @@ describe('KeyringController', () => {
10501039
};
10511040

10521041
// Add a new keyring with one account
1053-
await keyringController.addNewKeyring(KeyringType.Simple, {
1054-
privateKeys: [account.privateKey],
1055-
});
1042+
await keyringController.addNewKeyring(KeyringType.Simple, [
1043+
account.privateKey,
1044+
]);
10561045
expect(await keyringController.getAccounts()).toHaveLength(4);
10571046

10581047
// remove that account that we just added

src/KeyringController.ts

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -585,22 +585,8 @@ class KeyringController extends EventEmitter {
585585
* @param opts - The constructor options for the keyring.
586586
* @returns The new keyring.
587587
*/
588-
async addNewKeyring(
589-
type: string,
590-
opts?: Record<string, unknown>,
591-
): Promise<Keyring<Json>> {
592-
let keyring: Keyring<Json>;
593-
switch (type) {
594-
case KeyringType.Simple:
595-
if (!isObject(opts)) {
596-
throw new Error('Private keys missing');
597-
}
598-
keyring = await this.#newKeyring(type, opts.privateKeys);
599-
break;
600-
default:
601-
keyring = await this.#newKeyring(type, opts);
602-
break;
603-
}
588+
async addNewKeyring(type: string, opts?: unknown): Promise<Keyring<Json>> {
589+
const keyring = await this.#newKeyring(type, opts);
604590

605591
if (!keyring) {
606592
throw new Error(KeyringControllerError.NoKeyring);

0 commit comments

Comments
 (0)