signal-desktop/ts/main/challengeMain.ts
2021-05-05 17:09:29 -07:00

50 lines
1.2 KiB
TypeScript

// Copyright 2021 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only
/* eslint-disable no-restricted-syntax, no-console */
import { ipcMain as ipc, IpcMainEvent } from 'electron';
import { IPCRequest, IPCResponse, ChallengeResponse } from '../challenge';
export class ChallengeMainHandler {
private handlers: Array<(response: ChallengeResponse) => void> = [];
constructor() {
this.initialize();
}
public handleCaptcha(captcha: string): void {
const response: ChallengeResponse = { captcha };
const { handlers } = this;
this.handlers = [];
for (const resolve of handlers) {
resolve(response);
}
}
private async onRequest(
event: IpcMainEvent,
request: IPCRequest
): Promise<void> {
console.log('Received challenge request, waiting for response');
const data = await new Promise<ChallengeResponse>(resolve => {
this.handlers.push(resolve);
});
console.log('Sending challenge response', data);
const ipcResponse: IPCResponse = {
seq: request.seq,
data,
};
event.sender.send('challenge:response', ipcResponse);
}
private initialize(): void {
ipc.on('challenge:request', (event, request) => {
this.onRequest(event, request);
});
}
}