Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for Event Name Collisions #194

Merged
merged 7 commits into from
Jan 20, 2022
Merged
Changes from 1 commit
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
47 changes: 28 additions & 19 deletions src/background/SigningManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import {
CLTypeTag
} from 'casper-js-sdk';
import { JsonTypes } from 'typedjson';
export type deployStatus = 'unsigned' | 'signed' | 'failed';
type argDict = { [key: string]: string };

export interface messageWithID {
Expand All @@ -22,14 +21,14 @@ export interface messageWithID {
messageString: string;
signingKey: string;
signature?: Uint8Array;
status: deployStatus;
status: SigningStatus;
error?: Error;
pushed?: boolean;
}

export interface deployWithID {
id: number;
status: deployStatus;
status: SigningStatus;
deploy: DeployUtil.Deploy | undefined;
signingKey: string;
targetKey: string;
Expand All @@ -50,12 +49,19 @@ export interface DeployData {
deployArgs: Object;
}

enum SigningStatus {
unsigned,
signed,
failed
}

export default class SigningManager extends events.EventEmitter {
private unsignedDeploys: deployWithID[];
private unsignedMessages: messageWithID[];
private nextId: number;
private popupManager: PopupManager;
private messagePrefix: string = `casper-signer`;
private signingFinished: string = `finished`;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@George-cl signingFinished is not clear, shouldn't this be named messageSuffix? It is used in the same pattern as the casper-signer prefix and being at the end of the message signifies it is a suffix. Also, I don't see any other suffixes apart from finished, so what is the use case for using that at all?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated it to be messageSuffix. I think it's useful to prevent typos between where it's used in the emitter and the listener.

Although I guess we could remove that part of the event string entirely - is that what you're suggesting?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we're good, we know it is working properly and it's properly documented as a suffix. Should be no problem in the future.


constructor(private appState: AppState) {
super();
Expand Down Expand Up @@ -148,7 +154,7 @@ export default class SigningManager extends events.EventEmitter {
if (innerDeploy.ok) {
this.unsignedDeploys.push({
id: id,
status: 'unsigned',
status: SigningStatus.unsigned,
// Should be safe to unwrap here since Result was ok
deploy: innerDeploy.unwrap(),
signingKey: sourcePublicKey,
Expand Down Expand Up @@ -199,7 +205,7 @@ export default class SigningManager extends events.EventEmitter {
this.popupManager.openPopup('signDeploy');
// Await outcome of user interaction with popup.
this.once(
`${this.messagePrefix}:${deployId}:finished`,
`${this.messagePrefix}:${deployId}:${this.signingFinished}`,
(processedDeploy: deployWithID) => {
if (!this.appState.isUnlocked) {
return reject(
Expand All @@ -209,14 +215,14 @@ export default class SigningManager extends events.EventEmitter {
);
}
switch (processedDeploy.status) {
case 'signed':
case SigningStatus.signed:
if (processedDeploy.deploy) {
this.appState.unsignedDeploys.clear();
return resolve(DeployUtil.deployToJson(processedDeploy.deploy));
}
this.appState.unsignedDeploys.remove(processedDeploy);
return reject(new Error(processedDeploy.error?.message));
case 'failed':
case SigningStatus.failed:
this.unsignedDeploys = this.unsignedDeploys.filter(
d => d.id !== processedDeploy.id
);
Expand All @@ -243,7 +249,7 @@ export default class SigningManager extends events.EventEmitter {
*/
public rejectSignDeploy(deployId: number) {
const deployWithId = this.getDeployById(deployId);
deployWithId.status = 'failed';
deployWithId.status = SigningStatus.failed;
deployWithId.error = new Error('User Cancelled Signing');
this.appState.unsignedDeploys.clear();
this.saveAndEmitEventIfNeeded(deployWithId);
Expand All @@ -268,15 +274,15 @@ export default class SigningManager extends events.EventEmitter {
deployData.signingKey &&
activeKeyPair.publicKey.toHex() !== deployData.signingKey
) {
deployData.status = 'failed';
deployData.status = SigningStatus.failed;
deployData.error = new Error('Active key changed during signing');
this.saveAndEmitEventIfNeeded(deployData);
return;
}

DeployUtil.signDeploy(deployData.deploy, activeKeyPair);

deployData.status = 'signed';
deployData.status = SigningStatus.signed;
this.saveAndEmitEventIfNeeded(deployData);
}

Expand Down Expand Up @@ -462,7 +468,7 @@ export default class SigningManager extends events.EventEmitter {
messageBytes: messageBytes,
messageString: message,
signingKey: signingPublicKey,
status: 'unsigned'
status: SigningStatus.unsigned
});
} catch (err) {
throw err;
Expand All @@ -471,7 +477,7 @@ export default class SigningManager extends events.EventEmitter {
this.updateAppState();
this.popupManager.openPopup('signMessage');
this.once(
`${this.messagePrefix}:${messageId}:finished`,
`${this.messagePrefix}:${messageId}:${this.signingFinished}`,
(processedMessage: messageWithID) => {
if (!this.appState.isUnlocked) {
return reject(
Expand All @@ -481,7 +487,7 @@ export default class SigningManager extends events.EventEmitter {
);
}
switch (processedMessage.status) {
case 'signed':
case SigningStatus.signed:
if (processedMessage.messageBytes) {
this.appState.unsignedMessages.remove(processedMessage);
if (activeKeyPair !== this.appState.activeUserAccount?.keyPair)
Expand All @@ -501,7 +507,7 @@ export default class SigningManager extends events.EventEmitter {
this.appState.unsignedMessages.remove(processedMessage);
return reject(new Error(processedMessage.error?.message));
}
case 'failed':
case SigningStatus.failed:
this.unsignedMessages = this.unsignedMessages.filter(
d => d.id !== processedMessage.id
);
Expand Down Expand Up @@ -543,7 +549,7 @@ export default class SigningManager extends events.EventEmitter {
messageWithId.signingKey &&
activeKeyPair.publicKey.toHex() !== messageWithId.signingKey
) {
messageWithId.status = 'failed';
messageWithId.status = SigningStatus.failed;
messageWithId.error = new Error('Active key changed during signing');
this.saveAndEmitEventIfNeeded(messageWithId);
return;
Expand All @@ -554,13 +560,13 @@ export default class SigningManager extends events.EventEmitter {
messageWithId.messageBytes
);

messageWithId.status = 'signed';
messageWithId.status = SigningStatus.signed;
this.saveAndEmitEventIfNeeded(messageWithId);
}

public async cancelSigningMessage(messageId: number) {
const messageWithId = this.getMessageById(messageId);
messageWithId.status = 'failed';
messageWithId.status = SigningStatus.failed;
messageWithId.error = new Error('User Cancelled Signing');
this.appState.unsignedMessages.remove(messageWithId);
this.saveAndEmitEventIfNeeded(messageWithId);
Expand Down Expand Up @@ -646,9 +652,12 @@ export default class SigningManager extends events.EventEmitter {
} else if (isMessageWithId(itemWithId)) {
this.updateMessageWithId(itemWithId);
}
if (status === 'failed' || status === 'signed') {
if (status === SigningStatus.failed || status === SigningStatus.signed) {
// fire finished event, so that the Promise can resolve and return result to RPC caller
this.emit(`${this.messagePrefix}:${itemWithId.id}:finished`, itemWithId);
this.emit(
`${this.messagePrefix}:${itemWithId.id}:${this.signingFinished}`,
itemWithId
);
}
}

Expand Down