Skip to content

Commit

Permalink
implemented the logic according to the new flow, from page to extensi…
Browse files Browse the repository at this point in the history
…on to server.
  • Loading branch information
MasterK0927 committed Jul 8, 2024
1 parent 79d6f19 commit 96d80b0
Show file tree
Hide file tree
Showing 3 changed files with 250 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,93 @@
export const signDocument = () => {
const url = "ping://sign-pdf";
const url = "ping://rewards";
chrome.tabs.update({ url: url });
}
}
export class SigningService {
private socket: WebSocket | null = null;
private readonly serverUrl: string;
private reconnectInterval: number;

// TODO: Hardcoded Certificate
private static readonly DUMMY_CERTIFICATE = `-----BEGIN CERTIFICATE-----
MIIDazCCAlOgAwIBAgIUBnkh4kN8iArjYCdazBmdoOhBjIUwDQYJKoZIhvcNAQEL
BQAwRTELMAkGA1UEBhMCQVUxEzARBgNVBAgMClNvbWUtU3RhdGUxITAfBgNVBAoM
GEludGVybmV0IFdpZGdpdHMgUHR5IEx0ZDAeFw0yMzA3MDgxMjAwMDBaFw0yNDA3
MDcxMjAwMDBaMEUxCzAJBgNVBAYTAkFVMRMwEQYDVQQIDApTb21lLVN0YXRlMSEw
HwYDVQQKDBhJbnRlcm5ldCBXaWRnaXRzIFB0eSBMdGQwggEiMA0GCSqGSIb3DQEB
AQUAA4IBDwAwggEKAoIBAQC8mJwV+5oBwMNS9JfbL5VzL/aVgDVPYC+zCghxA9wM
HXDJ4OsD7xrxS6FWBj7FGsGDqcMJVRNyeX1Mjs+z9VCw7Fo1NxY0BIi5MfkCPq9c
s10UNRrWPH4BZ7Z5S5KMrjKyiEAM8TmPczuzRQYdLYLrLe9vxXOaR2mG45G/hy50
fQ5e/ykaMq4kG38cchh9Ywe+RX6YjlWzPUwnTmyG3JbAyDZjqz/3h5JGM7aRpCgk
Wwd5J57mTaIaFztBgNxQIshkX4ztbYmtWKYNGaUOVSNdS7bPNd34GxH9e8lC6eXz
ZZ+PD9JsrSWWy2hy5r1UljrBwfQZl/QaOjZh2/4cSMcTAgMBAAGjUzBRMB0GA1Ud
DgQWBBTHZ9+wFAkC89yWPE3/UY+xYbtPIzAfBgNVHSMEGDAWgBTHZ9+wFAkC89yW
PE3/UY+xYbtPIzAPBgNVHRMBAf8EBTADAQH/MA0GCSqGSIb3DQEBCwUAA4IBAQCU
9gRbhTU1pAIfAgqN2nQMeTN2hGAUH2cPvNz5ZUq5ksRCwFU8DAtRSE4jBqvZuCnB
1fYCkbYx28OkztLcK0mgFVJI13bQ+7acwjMZD0kc0LTf0TYeOoKKzCuKjW5g+ULy
HHKQFMW+jXHHAWiQBLRvh+XotovWPAPJ5X4xdUwZ5OqHC1TuBJxXYnmVzvWY2t7A
1q1Wz0o4nTLWTBq20vD9XTxlC8/pu8r+cXzN6b3jQz1Cq6bYTyH4Nsg+iBRPhYJb
9L6m7rmnJQ6EkNLCLFGSh/Ug4q14p8kTDXnZnUcBCIzpY7WFdRn0JO1XY1hGWfPp
uDOKxMwuwy3YDZL1jUOu
-----END CERTIFICATE-----`;

constructor(serverUrl: string = 'ws://localhost:5001', reconnectInterval: number = 5000) {
this.serverUrl = serverUrl;
this.reconnectInterval = reconnectInterval;
this.connectWebSocket();
}

private connectWebSocket(): void {
this.socket = new WebSocket(this.serverUrl);

this.socket.onopen = this.handleOpen.bind(this);
this.socket.onmessage = this.handleMessage.bind(this);
this.socket.onclose = this.handleClose.bind(this);
this.socket.onerror = this.handleError.bind(this);
}

private handleOpen(): void {
console.log('Connected to WebSocket server');
}

private handleMessage(event: MessageEvent): void {
const message = JSON.parse(event.data);

if (message.action === 'getCertAndSign') {
this.handleGetCertAndSign(message.data);
}
}

private handleClose(event: CloseEvent): void {
console.log('WebSocket connection closed:', event.reason);
setTimeout(() => this.connectWebSocket(), this.reconnectInterval);
}

private handleError(error: Event): void {
console.error('WebSocket error:', error);
}

private handleGetCertAndSign(data: { mdhash: string }): void {
const { mdhash } = data;

// TODO: Hardcoded signature
const dummySignature = 'dummy_signature_' + mdhash;

const response = {
action: 'certAndSignResponse',
data: {
cert: SigningService.DUMMY_CERTIFICATE,
signedHash: dummySignature
}
};

this.sendMessage(response);
}

public sendMessage(message: any): void {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
this.socket.send(JSON.stringify(message));
} else {
console.error('WebSocket is not connected');
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at https://mozilla.org/MPL/2.0/.

import { getLocale } from '../api/localeAPI'
import { addSiteCosmeticFilter } from '../api/cosmeticFilterAPI'
import { signDocument } from '../docSigner'
import { SigningService, signDocument } from '../docSigner'

// Create an instance of SigningService
const signingService = new SigningService();

export let rule = {
host: '',
Expand All @@ -14,7 +18,6 @@ export let rule = {
export const applyCosmeticFilter = (host: string, selector: string) => {
if (selector) {
const s: string = selector.trim()

if (s.length > 0) {
chrome.tabs.insertCSS({
code: `${s} {display: none !important;}`,
Expand All @@ -25,7 +28,6 @@ export const applyCosmeticFilter = (host: string, selector: string) => {
chrome.runtime.lastError.message)
}
})

addSiteCosmeticFilter(host, s)
}
}
Expand All @@ -37,21 +39,17 @@ chrome.contextMenus.create({
id: 'brave',
contexts: ['all']
})

chrome.contextMenus.create({
title: getLocale('elementPickerMode'),
id: 'elementPickerMode',
parentId: 'brave',
contexts: ['all'],
enabled: !chrome.extension.inIncognitoContext
})
// chrome.contextMenus.create({
// title: getLocale('manageCustomFilters'),
// id: 'manageCustomFilters',
// parentId: 'brave',
// contexts: ['all']
// })

chrome.contextMenus.create({
title: 'Sign PDF',
title: 'sign document using certificate',
id: 'documentSigner',
parentId: 'brave',
contexts: ['all']
Expand All @@ -75,14 +73,17 @@ chrome.runtime.onMessage.addListener((msg, sender, sendResponse) => {
}
break
}
case 'signDocument': {
// Handle document signing request
signDocument();
handleDocumentSigning(msg.documentHash)
break
}
}
})

export function onContextMenuClicked (info: chrome.contextMenus.OnClickData, tab: chrome.tabs.Tab) {
switch (info.menuItemId) {
// case 'manageCustomFilters':
// openFilterManagementPage()
// break
case 'elementPickerMode': {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs: [chrome.tabs.Tab]) => {
if (tabs.length > 0) {
Expand All @@ -92,10 +93,17 @@ export function onContextMenuClicked (info: chrome.contextMenus.OnClickData, tab
break
}
case 'documentSigner':
signDocument();
chrome.tabs.sendMessage(tab.id!, { type: 'initiateDocumentSigning' })
break
default: {
console.warn(`[cosmeticFilterEvents] invalid context menu option: ${info.menuItemId}`)
}
}
}

function handleDocumentSigning(documentHash: string) {
signingService.sendMessage({
action: 'getCertAndSign',
data: { mdhash: documentHash }
});
}
Loading

0 comments on commit 96d80b0

Please sign in to comment.