From 3e961d5bcd9036de7b987956807d8a9cf58e2bed Mon Sep 17 00:00:00 2001 From: Soralit Date: Sun, 26 Sep 2021 16:12:54 +0800 Subject: [PATCH] add forget Keyring method --- index.js | 19 +++++++++++++++++++ test/index.js | 23 +++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/index.js b/index.js index e5a046ad..b82f7b4e 100644 --- a/index.js +++ b/index.js @@ -725,6 +725,7 @@ class KeyringController extends EventEmitter { * Deallocates all currently managed keyrings and accounts. * Used before initializing a new vault. */ + /* eslint-disable require-await */ async clearKeyrings() { // clear keyrings from memory @@ -757,6 +758,24 @@ class KeyringController extends EventEmitter { this.memStore.updateState({ isUnlocked: true }); this.emit('unlock'); } + + /** + * Forget hardware keyring + * + * Forget hardware and update memorized state. + * @param {Keyring} keyring + */ + forgetKeyring(keyring) { + if (keyring.forgetDevice) { + keyring.forgetDevice(); + this.persistAllKeyrings.bind(this)(); + this._updateMemStoreKeyrings.bind(this)(); + } else { + throw new Error( + `KeyringController - keyring does not have method "forgetDevice", keyring type: ${keyring.type}`, + ); + } + } } module.exports = KeyringController; diff --git a/test/index.js b/test/index.js index 72ecaaa0..636ded54 100644 --- a/test/index.js +++ b/test/index.js @@ -280,4 +280,27 @@ describe('KeyringController', function () { expect(privateAppKey).not.toBe(privateKey); }); }); + + describe('forgetHardwareDevice', function () { + it('throw when keyring is not hardware device', async function () { + const privateKey = + '0xb8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952'; + const keyring = await keyringController.addNewKeyring('Simple Key Pair', [ + privateKey, + ]); + expect(keyringController.keyrings).toHaveLength(2); + expect(() => keyringController.forgetKeyring(keyring)).toThrow( + new Error( + 'KeyringController - keyring does not have method "forgetDevice", keyring type: Simple Key Pair', + ), + ); + }); + + it('forget hardware device', async function () { + const hdKeyring = keyringController.getKeyringsByType('HD Key Tree'); + hdKeyring.forgetDevice = sinon.spy(); + keyringController.forgetKeyring(hdKeyring); + expect(hdKeyring.forgetDevice.calledOnce).toBe(true); + }); + }); });