Skip to content

Commit 46d8e17

Browse files
authored
Fix: saving serialized keyring for which corresponding keyring class is not present (MetaMask#169)
1 parent 2b2c71f commit 46d8e17

2 files changed

Lines changed: 41 additions & 2 deletions

File tree

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class KeyringController extends EventEmitter {
4848

4949
this.encryptor = opts.encryptor || encryptor;
5050
this.keyrings = [];
51+
this._unsupportedKeyrings = [];
5152

5253
// This option allows the controller to cache an exported key
5354
// for use in decrypting and encrypting data without password
@@ -562,6 +563,8 @@ class KeyringController extends EventEmitter {
562563
}),
563564
);
564565

566+
serializedKeyrings.push(...this._unsupportedKeyrings);
567+
565568
let vault;
566569
let newEncryptionKey;
567570

@@ -679,7 +682,9 @@ class KeyringController extends EventEmitter {
679682
*/
680683
async restoreKeyring(serialized) {
681684
const keyring = await this._restoreKeyring(serialized);
682-
await this._updateMemStoreKeyrings();
685+
if (keyring) {
686+
await this._updateMemStoreKeyrings();
687+
}
683688
return keyring;
684689
}
685690

@@ -690,12 +695,16 @@ class KeyringController extends EventEmitter {
690695
* On success, returns the resulting keyring instance.
691696
*
692697
* @param {Object} serialized - The serialized keyring.
693-
* @returns {Promise<Keyring>} The deserialized keyring.
698+
* @returns {Promise<Keyring|undefined>} The deserialized keyring or undefined if the keyring type is unsupported.
694699
*/
695700
async _restoreKeyring(serialized) {
696701
const { type, data } = serialized;
697702

698703
const Keyring = this.getKeyringClassForType(type);
704+
if (!Keyring) {
705+
this._unsupportedKeyrings.push(serialized);
706+
return undefined;
707+
}
699708
const keyring = new Keyring();
700709
await keyring.deserialize(data);
701710
// getAccounts also validates the accounts for some keyrings

test/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,19 @@ describe('KeyringController', function () {
9494
});
9595
});
9696

97+
describe('persistAllKeyrings', function () {
98+
it('should persist keyrings in _unsupportedKeyrings array', async function () {
99+
const unsupportedKeyring = 'DUMMY_KEYRING';
100+
keyringController._unsupportedKeyrings = [unsupportedKeyring];
101+
await keyringController.persistAllKeyrings();
102+
103+
const { vault } = keyringController.store.getState();
104+
const keyrings = await mockEncryptor.decrypt(password, vault);
105+
expect(keyrings.indexOf(unsupportedKeyring) > -1).toBe(true);
106+
expect(keyrings).toHaveLength(2);
107+
});
108+
});
109+
97110
describe('createNewVaultAndKeychain', function () {
98111
it('should create a new vault', async function () {
99112
keyringController.store.updateState({ vault: null });
@@ -245,6 +258,13 @@ describe('KeyringController', function () {
245258
const accounts = await keyring.getAccounts();
246259
expect(accounts[0]).toBe(walletOneAddresses[0]);
247260
});
261+
it('should return undefined if keyring type is not supported.', async function () {
262+
const unsupportedKeyring = { type: 'Ledger Keyring', data: 'DUMMY' };
263+
const keyring = await keyringController.restoreKeyring(
264+
unsupportedKeyring,
265+
);
266+
expect(keyring).toBeUndefined();
267+
});
248268
});
249269

250270
describe('getAccounts', function () {
@@ -330,6 +350,16 @@ describe('KeyringController', function () {
330350
expect(keyring.wallets).toHaveLength(1);
331351
});
332352
});
353+
it('add serialized keyring to _unsupportedKeyrings array if keyring type is not known', async function () {
354+
const _unsupportedKeyrings = [{ type: 'Ledger Keyring', data: 'DUMMY' }];
355+
mockEncryptor.encrypt(password, _unsupportedKeyrings);
356+
await keyringController.setLocked();
357+
const keyrings = await keyringController.unlockKeyrings(password);
358+
expect(keyrings).toHaveLength(0);
359+
expect(keyringController._unsupportedKeyrings).toStrictEqual(
360+
_unsupportedKeyrings,
361+
);
362+
});
333363
});
334364

335365
describe('verifyPassword', function () {

0 commit comments

Comments
 (0)