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

refactor: offscreen and ws event handlers #115

Merged
merged 2 commits into from
Dec 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
1 change: 0 additions & 1 deletion src/components/PluginList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
PluginInfoModalHeader,
} from '../PluginInfo';
import { getPluginConfigByHash } from '../../entries/Background/db';
import { OffscreenActionTypes } from '../../entries/Offscreen/types';
import { SidePanelActionTypes } from '../../entries/SidePanel/types';
import { openSidePanel } from '../../entries/utils';

Expand Down Expand Up @@ -101,7 +100,7 @@
await runPlugin(hash, 'start');

window.close();
} catch (e: any) {

Check warning on line 103 in src/components/PluginList/index.tsx

View workflow job for this annotation

GitHub Actions / build

Unexpected any. Specify a different type
showError(e.message);
}
}, [hash, config, remove, onClick]);
Expand Down
56 changes: 31 additions & 25 deletions src/entries/Background/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,24 +52,14 @@ import { subtractRanges } from '../Offscreen/utils';
import { mapSecretsToRange } from './plugins/utils';
import { pushToRedux } from '../utils';
import {
acceptPairRequest,
cancelPairRequest,
connectSession,
disconnectSession,
getP2PState,
rejectPairRequest,
requestProofByHash,
requestProof,
sendPairRequest,
cancelProofRequest,
acceptProofRequest,
rejectProofRequest,
startProofRequest,
startedVerifier,
startedProver,
endProofRequest,
setupProver,
onProverInstantiated,
sendMessage,
sendPairedMessage,
} from './ws';

const charwise = require('charwise');
Expand Down Expand Up @@ -265,49 +255,65 @@ export const initRPC = () => {
disconnectSession().then(sendResponse);
return;
case BackgroundActiontype.send_pair_request:
sendPairRequest(request.data).then(sendResponse);
sendMessage(request.data, 'pair_request').then(sendResponse);
return;
case BackgroundActiontype.cancel_pair_request:
cancelPairRequest(request.data).then(sendResponse);
sendMessage(request.data, 'pair_request_cancel').then(sendResponse);
return;
case BackgroundActiontype.accept_pair_request:
acceptPairRequest(request.data).then(sendResponse);
sendMessage(request.data, 'pair_request_accept').then(sendResponse);
return;
case BackgroundActiontype.reject_pair_request:
rejectPairRequest(request.data).then(sendResponse);
sendMessage(request.data, 'pair_request_reject').then(sendResponse);
return;
case BackgroundActiontype.cancel_proof_request:
cancelProofRequest(request.data).then(sendResponse);
sendPairedMessage('proof_request_cancel', {
pluginHash: request.data,
}).then(sendResponse);
return;
case BackgroundActiontype.accept_proof_request:
acceptProofRequest(request.data).then(sendResponse);
sendPairedMessage('proof_request_accept', {
plugfinHash: request.data,
}).then(sendResponse);
return;
case BackgroundActiontype.reject_proof_request:
rejectProofRequest(request.data).then(sendResponse);
sendPairedMessage('proof_request_reject', {
pluginHash: request.data,
}).then(sendResponse);
return;
case BackgroundActiontype.start_proof_request:
startProofRequest(request.data.pluginHash).then(sendResponse);
sendPairedMessage('proof_request_start', {
pluginHash: request.data.pluginHash,
}).then(sendResponse);
return;
case BackgroundActiontype.proof_request_end:
endProofRequest(request.data).then(sendResponse);
return;
case BackgroundActiontype.verifier_started:
startedVerifier(request.data.pluginHash).then(sendResponse);
sendPairedMessage('verifier_started', {
pluginHash: request.data.pluginHash,
}).then(sendResponse);
return;
case BackgroundActiontype.prover_started:
startedProver(request.data.pluginHash).then(sendResponse);
sendPairedMessage('prover_started', {
pluginHash: request.data.pluginHash,
}).then(sendResponse);
return;
case BackgroundActiontype.prover_instantiated:
onProverInstantiated(request.data.pluginHash);
onProverInstantiated();
return;
case BackgroundActiontype.prover_setup:
setupProver(request.data.pluginHash).then(sendResponse);
sendPairedMessage('prover_setup', {
pluginHash: request.data.pluginHash,
}).then(sendResponse);
return;
case BackgroundActiontype.request_p2p_proof:
requestProof(request.data).then(sendResponse);
return;
case BackgroundActiontype.request_p2p_proof_by_hash:
requestProofByHash(request.data).then(sendResponse);
sendPairedMessage('request_proof_by_hash', {
pluginHash: request.data,
}).then(sendResponse);
return;
case BackgroundActiontype.get_p2p_state:
getP2PState();
Expand Down
76 changes: 8 additions & 68 deletions src/entries/Background/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import {
} from '../../reducers/p2p';
import { pushToRedux } from '../utils';
import { getPluginByHash } from './db';
import browser, { storage } from 'webextension-polyfill';
import browser from 'webextension-polyfill';
import { OffscreenActionTypes } from '../Offscreen/types';
import { getMaxRecv, getMaxSent, getRendezvousApi } from '../../utils/storage';
import { SidePanelActionTypes } from '../SidePanel/types';
Expand Down Expand Up @@ -378,7 +378,11 @@ export const disconnectSession = async () => {
await socket.close();
};

function sendMessage(target: string, method: string, params?: any) {
export async function sendMessage(
target: string,
method: string,
params?: any,
) {
const { socket, clientId } = state;

if (clientId === target) {
Expand Down Expand Up @@ -409,7 +413,7 @@ function sendMessage(target: string, method: string, params?: any) {
);
}

function sendPairedMessage(method: string, params?: any) {
export async function sendPairedMessage(method: string, params?: any) {
const { pairing } = state;

if (!pairing) {
Expand All @@ -420,22 +424,6 @@ function sendPairedMessage(method: string, params?: any) {
sendMessage(pairing, method, params);
}

export const sendPairRequest = async (target: string) => {
sendMessage(target, 'pair_request');
};

export const cancelPairRequest = async (target: string) => {
sendMessage(target, 'pair_request_cancel');
};

export const acceptPairRequest = async (target: string) => {
sendMessage(target, 'pair_request_accept');
};

export const rejectPairRequest = async (target: string) => {
sendMessage(target, 'pair_request_reject');
};

export const requestProof = async (pluginHash: string) => {
const pluginHex = await getPluginByHash(pluginHash);
sendPairedMessage('request_proof', {
Expand All @@ -444,30 +432,6 @@ export const requestProof = async (pluginHash: string) => {
});
};

export const requestProofByHash = async (pluginHash: string) => {
sendPairedMessage('request_proof_by_hash', {
pluginHash,
});
};

export const cancelProofRequest = async (pluginHash: string) => {
sendPairedMessage('proof_request_cancel', {
pluginHash,
});
};

export const acceptProofRequest = async (pluginHash: string) => {
sendPairedMessage('proof_request_accept', {
pluginHash,
});
};

export const startProofRequest = async (pluginHash: string) => {
sendPairedMessage('proof_request_start', {
pluginHash,
});
};

export const endProofRequest = async (data: {
pluginHash: string;
proof: VerifierOutput;
Expand All @@ -490,35 +454,11 @@ export const endProofRequest = async (data: {
});
};

export const rejectProofRequest = async (pluginHash: string) => {
sendPairedMessage('proof_request_reject', {
pluginHash,
});
};

export const startedVerifier = async (pluginHash: string) => {
sendPairedMessage('verifier_started', {
pluginHash,
});
};

export const startedProver = async (pluginHash: string) => {
sendPairedMessage('prover_started', {
pluginHash,
});
};

export const onProverInstantiated = async (pluginHash: string) => {
export const onProverInstantiated = async () => {
state.isProving = true;
pushToRedux(setIsProving(true));
};

export const setupProver = async (pluginHash: string) => {
sendPairedMessage('prover_setup', {
pluginHash,
});
};

function bufferify(data: any): Buffer {
return Buffer.from(JSON.stringify(data));
}
Loading
Loading