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

Restore compatibility with QR Keyring #252

Merged
merged 1 commit into from
Jul 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ module.exports = {
// An object that configures minimum threshold enforcement for coverage results
coverageThreshold: {
global: {
branches: 72.09,
branches: 72.41,
functions: 92.85,
lines: 90.81,
statements: 91.02,
lines: 90.87,
statements: 91.08,
},
},
preset: 'ts-jest',
Expand Down
29 changes: 29 additions & 0 deletions src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,15 @@ describe('KeyringController', () => {
expect(allAccounts).toStrictEqual(expectedAllAccounts);
});

it('should throw an error when attempting to add simple key pair without private keys', async () => {
const keyringController = await initializeKeyringController({
password: PASSWORD,
});
await expect(async () =>
keyringController.addNewKeyring(KeyringType.Simple),
).rejects.toThrow('Private keys missing');
});

it('should add HD Key Tree without mnemonic passed as an argument', async () => {
const keyringController = await initializeKeyringController({
password: PASSWORD,
Expand Down Expand Up @@ -504,6 +513,26 @@ describe('KeyringController', () => {
expect(allAccounts).toHaveLength(3);
});

it('should add keyring that expects undefined serialized state', async () => {
let deserializedSpy = sinon.spy();
const mockKeyringBuilder = () => {
const keyring = new KeyringMockWithInit();
deserializedSpy = sinon.spy(keyring, 'deserialize');
return keyring;
};
mockKeyringBuilder.type = 'Mock Keyring';
const keyringController = await initializeKeyringController({
constructorOptions: {
keyringBuilders: [mockKeyringBuilder],
},
password: PASSWORD,
});
await keyringController.addNewKeyring('Mock Keyring');

expect(deserializedSpy.callCount).toBe(1);
expect(deserializedSpy.calledWith(undefined)).toBe(true);
});

it('should call init method if available', async () => {
const keyringController = await initializeKeyringController({
password: PASSWORD,
Expand Down
9 changes: 6 additions & 3 deletions src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import HDKeyring from '@metamask/eth-hd-keyring';
import { normalize as normalizeToHex } from '@metamask/eth-sig-util';
import SimpleKeyring from '@metamask/eth-simple-keyring';
import { ObservableStore } from '@metamask/obs-store';
import { remove0x, isValidHexAddress } from '@metamask/utils';
import { remove0x, isValidHexAddress, isObject } from '@metamask/utils';
import type {
Hex,
Json,
Expand Down Expand Up @@ -586,11 +586,14 @@ class KeyringController extends EventEmitter {
*/
async addNewKeyring(
type: string,
opts: Record<string, unknown> = {},
opts?: Record<string, unknown>,
): Promise<Keyring<Json>> {
let keyring: Keyring<Json>;
switch (type) {
case KeyringType.Simple:
if (!isObject(opts)) {
throw new Error('Private keys missing');
}
keyring = await this.#newKeyring(type, opts.privateKeys);
break;
default:
Expand All @@ -602,7 +605,7 @@ class KeyringController extends EventEmitter {
throw new Error(KeyringControllerError.NoKeyring);
}

if (!opts.mnemonic && type === KeyringType.HD) {
if (type === KeyringType.HD && (!isObject(opts) || !opts.mnemonic)) {
if (!keyring.generateRandomMnemonic) {
throw new Error(
KeyringControllerError.UnsupportedGenerateRandomMnemonic,
Expand Down