Skip to content

Commit

Permalink
refactor: remove onLock and onUnlock in favor of events
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed May 19, 2023
1 parent 56b101d commit a0171f4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 54 deletions.
49 changes: 26 additions & 23 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ describe('KeyringController', () => {
expect(controller.state.isUnlocked).toBe(false);
});
});

it('should emit KeyringController:lock event', async () => {
await withController(async ({ controller, messenger }) => {
const listener = sinon.spy();
messenger.subscribe('KeyringController:lock', listener);
await controller.setLocked();
expect(listener.called).toBe(true);
});
});
});

describe('exportSeedPhrase', () => {
Expand Down Expand Up @@ -1012,6 +1021,18 @@ describe('KeyringController', () => {
);
});

it('should emit KeyringController:unlock event', async () => {
await withController(
{ cacheEncryptionKey },
async ({ controller, messenger }) => {
const listener = sinon.spy();
messenger.subscribe('KeyringController:unlock', listener);
await controller.submitPassword(password);
expect(listener.called).toBe(true);
},
);
});

cacheEncryptionKey &&
it('should set encryptionKey and encryptionSalt in state', async () => {
withController({ cacheEncryptionKey }, async ({ controller }) => {
Expand Down Expand Up @@ -1039,28 +1060,6 @@ describe('KeyringController', () => {
});
});

describe('onLock', () => {
it('should receive lock event', async () => {
await withController(async ({ controller }) => {
const listenerLock = sinon.stub();
controller.onLock(listenerLock);
await controller.setLocked();
expect(listenerLock.called).toBe(true);
});
});
});

describe('onUnlock', () => {
it('should receive unlock event', async () => {
await withController(async ({ controller }) => {
const listenerUnlock = sinon.stub();
controller.onUnlock(listenerUnlock);
await controller.submitPassword(password);
expect(listenerUnlock.called).toBe(true);
});
});
});

describe('verifySeedPhrase', () => {
it('should return current seedphrase', async () => {
await withController(async ({ controller }) => {
Expand Down Expand Up @@ -1600,7 +1599,11 @@ function buildKeyringControllerMessenger(messenger = buildMessenger()) {
return messenger.getRestricted({
name: 'KeyringController',
allowedActions: [],
allowedEvents: ['KeyringController:stateChange'],
allowedEvents: [
'KeyringController:stateChange',
'KeyringController:lock',
'KeyringController:unlock',
],
});
}

Expand Down
76 changes: 45 additions & 31 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,22 @@ export type KeyringControllerStateChangeEvent = {
payload: [KeyringControllerState, Patch[]];
};

export type KeyringControllerLockEvent = {
type: `${typeof name}:lock`;
payload: [];
};

export type KeyringControllerUnlockEvent = {
type: `${typeof name}:unlock`;
payload: [];
};

export type KeyringControllerActions = KeyringControllerGetStateAction;

export type KeyringControllerEvents = KeyringControllerStateChangeEvent;
export type KeyringControllerEvents =
| KeyringControllerStateChangeEvent
| KeyringControllerLockEvent
| KeyringControllerUnlockEvent;

export type KeyringControllerMessenger = RestrictedControllerMessenger<
typeof name,
Expand Down Expand Up @@ -223,6 +236,8 @@ export class KeyringController extends BaseControllerV2<
Object.assign({ initState: state }, config),
);
this.#keyring.memStore.subscribe(this.#fullUpdate.bind(this));
this.#keyring.on('lock', this.#handleLock.bind(this));
this.#keyring.on('unlock', this.#handleUnlock.bind(this));

this.removeIdentity = removeIdentity;
this.syncIdentities = syncIdentities;
Expand Down Expand Up @@ -612,26 +627,6 @@ export class KeyringController extends BaseControllerV2<
return this.state;
}

/**
* Adds new listener to be notified when the wallet is locked.
*
* @param listener - Callback triggered when wallet is locked.
* @returns EventEmitter if listener added.
*/
onLock(listener: () => void) {
return this.#keyring.on('lock', listener);
}

/**
* Adds new listener to be notified when the wallet is unlocked.
*
* @param listener - Callback triggered when wallet is unlocked.
* @returns EventEmitter if listener added.
*/
onUnlock(listener: () => void) {
return this.#keyring.on('unlock', listener);
}

/**
* Verifies the that the seed phrase restores the current keychain's accounts.
*
Expand Down Expand Up @@ -675,16 +670,6 @@ export class KeyringController extends BaseControllerV2<
return seedWords;
}

/**
* Sync controller state with current keyring memStore state.
*
*/
#fullUpdate() {
this.update(() => ({
...this.#keyring.memStore.getState(),
}));
}

// QR Hardware related methods

/**
Expand Down Expand Up @@ -800,6 +785,35 @@ export class KeyringController extends BaseControllerV2<
});
await this.#keyring.persistAllKeyrings();
}

/**
* Sync controller state with current keyring memStore state.
*
* @fires KeyringController:stateChange
*/
#fullUpdate() {
this.update(() => ({
...this.#keyring.memStore.getState(),
}));
}

/**
* Handle keyring lock event.
*
* @fires KeyringController:lock
*/
#handleLock() {
this.messagingSystem.publish(`${name}:lock`);
}

/**
* Handle keyring unlock event.
*
* @fires KeyringController:unlock
*/
#handleUnlock() {
this.messagingSystem.publish(`${name}:unlock`);
}
}

export default KeyringController;

0 comments on commit a0171f4

Please sign in to comment.