Skip to content

Commit

Permalink
refactor: offscreen and ws event handlers (#115)
Browse files Browse the repository at this point in the history
* refactor: offscreen rpc code

* refactor: ws message sender
  • Loading branch information
0xtsukino authored Dec 11, 2024
1 parent ca382f3 commit e3166d6
Show file tree
Hide file tree
Showing 5 changed files with 543 additions and 634 deletions.
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 @@ import {
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
56 changes: 31 additions & 25 deletions src/entries/Background/rpc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,24 +54,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 @@ -274,49 +264,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

0 comments on commit e3166d6

Please sign in to comment.