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

Commit 8699ccd

Browse files
authored
Allow non-object keyring serialized state (#253)
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 1941d3c commit 8699ccd

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
@@ -457,7 +457,7 @@ describe('KeyringController', () => {
457457
const previousAccounts = await keyringController.getAccounts();
458458
const keyring = await keyringController.addNewKeyring(
459459
KeyringType.Simple,
460-
{ privateKeys: [privateKey] },
460+
[privateKey],
461461
);
462462

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

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

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

671662
// remove that account that we just added
@@ -688,9 +679,9 @@ describe('KeyringController', () => {
688679
};
689680

690681
// Add a new keyring with one account
691-
await keyringController.addNewKeyring(KeyringType.Simple, {
692-
privateKeys: [account.privateKey],
693-
});
682+
await keyringController.addNewKeyring(KeyringType.Simple, [
683+
account.privateKey,
684+
]);
694685

695686
// We should have 2 keyrings
696687
expect(keyringController.keyrings).toHaveLength(2);
@@ -823,7 +814,7 @@ describe('KeyringController', () => {
823814

824815
const keyring = await keyringController.addNewKeyring(
825816
KeyringType.Simple,
826-
{ privateKeys: [privateKey] },
817+
[privateKey],
827818
);
828819

829820
const getAppKeyAddressSpy = sinon.spy(
@@ -857,9 +848,7 @@ describe('KeyringController', () => {
857848
const address = '0x01560cd3bac62cc6d7e6380600d9317363400896';
858849
const privateKey =
859850
'0xb8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952';
860-
await keyringController.addNewKeyring(KeyringType.Simple, {
861-
privateKeys: [privateKey],
862-
});
851+
await keyringController.addNewKeyring(KeyringType.Simple, [privateKey]);
863852
const appKeyAddress = await keyringController.getAppKeyAddress(
864853
address,
865854
'someapp.origin.io',
@@ -1051,9 +1040,9 @@ describe('KeyringController', () => {
10511040
};
10521041

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

10591048
// remove that account that we just added

src/KeyringController.ts

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

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

0 commit comments

Comments
 (0)