call proof generator from within a web worker
This commit is contained in:
parent
ab77eed91c
commit
0126dc0e3a
8 changed files with 109 additions and 46 deletions
|
@ -11,17 +11,12 @@
|
||||||
|
|
||||||
import genJsonPayload from "../utils/genJsonPayload";
|
import genJsonPayload from "../utils/genJsonPayload";
|
||||||
import * as CONST from "./const";
|
import * as CONST from "./const";
|
||||||
|
import { PoWConfig } from "./types";
|
||||||
|
|
||||||
type GetConfigPayload = {
|
type GetConfigPayload = {
|
||||||
key: string;
|
key: string;
|
||||||
};
|
};
|
||||||
|
|
||||||
export type PoWConfig = {
|
|
||||||
string: string;
|
|
||||||
difficulty_factor: number;
|
|
||||||
salt: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* fetch proof-of-work configuration
|
* fetch proof-of-work configuration
|
||||||
* @returns {PoWConfig} pow config
|
* @returns {PoWConfig} pow config
|
||||||
|
|
|
@ -9,7 +9,7 @@
|
||||||
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
|
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import prove from "./prove";
|
import { Work, ServiceWorkerWork } from "./types";
|
||||||
import fetchPoWConfig from "./fetchPoWConfig";
|
import fetchPoWConfig from "./fetchPoWConfig";
|
||||||
import sendWork from "./sendWork";
|
import sendWork from "./sendWork";
|
||||||
import sendToParent from "./sendToParent";
|
import sendToParent from "./sendToParent";
|
||||||
|
@ -18,6 +18,7 @@ import * as CONST from "./const";
|
||||||
import "./main.scss";
|
import "./main.scss";
|
||||||
|
|
||||||
let LOCK = false;
|
let LOCK = false;
|
||||||
|
const worker = new Worker("/bench.js");
|
||||||
|
|
||||||
/** add mcaptcha widget element to DOM */
|
/** add mcaptcha widget element to DOM */
|
||||||
export const registerVerificationEventHandler = (): void => {
|
export const registerVerificationEventHandler = (): void => {
|
||||||
|
@ -49,15 +50,30 @@ export const solveCaptchaRunner = async (e: Event): Promise<void> => {
|
||||||
// 1. get config
|
// 1. get config
|
||||||
const config = await fetchPoWConfig();
|
const config = await fetchPoWConfig();
|
||||||
// 2. prove work
|
// 2. prove work
|
||||||
const proof = await prove(config);
|
worker.postMessage(config);
|
||||||
// 3. submit work
|
|
||||||
const token = await sendWork(proof);
|
worker.onmessage = async (event: MessageEvent) => {
|
||||||
// 4. send token
|
const resp: ServiceWorkerWork = event.data;
|
||||||
sendToParent(token);
|
console.log(
|
||||||
// 5. mark checkbox checked
|
`Proof generated. Difficuly: ${config.difficulty_factor} Duration: ${resp.duration}`
|
||||||
CONST.btn().checked = true;
|
);
|
||||||
CONST.messageText().after();
|
|
||||||
LOCK = false;
|
const proof: Work = {
|
||||||
|
key: CONST.sitekey(),
|
||||||
|
string: config.string,
|
||||||
|
nonce: resp.work.nonce,
|
||||||
|
result: resp.work.result,
|
||||||
|
};
|
||||||
|
|
||||||
|
// 3. submit work
|
||||||
|
const token = await sendWork(proof);
|
||||||
|
// 4. send token
|
||||||
|
sendToParent(token);
|
||||||
|
// 5. mark checkbox checked
|
||||||
|
CONST.btn().checked = true;
|
||||||
|
CONST.messageText().after();
|
||||||
|
LOCK = false;
|
||||||
|
};
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
CONST.messageText().error();
|
CONST.messageText().error();
|
||||||
console.error(e);
|
console.error(e);
|
||||||
|
|
|
@ -10,27 +10,14 @@
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { gen_pow } from "@mcaptcha/pow-wasm";
|
import { gen_pow } from "@mcaptcha/pow-wasm";
|
||||||
import { PoWConfig } from "./fetchPoWConfig";
|
import { WasmWork, PoWConfig } from "./types";
|
||||||
import * as CONST from "./const";
|
|
||||||
|
|
||||||
export type Work = {
|
|
||||||
result: string;
|
|
||||||
nonce: number;
|
|
||||||
string: string;
|
|
||||||
key: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
type WasmWork = {
|
|
||||||
result: string;
|
|
||||||
nonce: number;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* proove work
|
* proove work
|
||||||
* @param {PoWConfig} config - the proof-of-work configuration using which
|
* @param {PoWConfig} config - the proof-of-work configuration using which
|
||||||
* work needs to be computed
|
* work needs to be computed
|
||||||
* */
|
* */
|
||||||
const prove = async (config: PoWConfig): Promise<Work> => {
|
const prove = (config: PoWConfig): WasmWork => {
|
||||||
const proofString = gen_pow(
|
const proofString = gen_pow(
|
||||||
config.salt,
|
config.salt,
|
||||||
config.string,
|
config.string,
|
||||||
|
@ -38,14 +25,7 @@ const prove = async (config: PoWConfig): Promise<Work> => {
|
||||||
);
|
);
|
||||||
const proof: WasmWork = JSON.parse(proofString);
|
const proof: WasmWork = JSON.parse(proofString);
|
||||||
|
|
||||||
const res: Work = {
|
return proof;
|
||||||
key: CONST.sitekey(),
|
|
||||||
string: config.string,
|
|
||||||
nonce: proof.nonce,
|
|
||||||
result: proof.result,
|
|
||||||
};
|
|
||||||
|
|
||||||
return res;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export default prove;
|
export default prove;
|
||||||
|
|
|
@ -8,7 +8,7 @@
|
||||||
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
|
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
|
||||||
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
|
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
|
||||||
*/
|
*/
|
||||||
import {Token} from "./sendWork";
|
import {Token} from "./types";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* send pow validation token as message to parant of the iframe
|
* send pow validation token as message to parant of the iframe
|
||||||
|
|
|
@ -11,11 +11,7 @@
|
||||||
|
|
||||||
import genJsonPayload from "../utils/genJsonPayload";
|
import genJsonPayload from "../utils/genJsonPayload";
|
||||||
import * as CONST from "./const";
|
import * as CONST from "./const";
|
||||||
import {Work} from "./prove";
|
import { Work, Token } from "./types";
|
||||||
|
|
||||||
export type Token = {
|
|
||||||
token: string;
|
|
||||||
};
|
|
||||||
|
|
||||||
export const sendWork = async (payload: Work): Promise<Token> => {
|
export const sendWork = async (payload: Work): Promise<Token> => {
|
||||||
try {
|
try {
|
||||||
|
@ -33,7 +29,7 @@ export const sendWork = async (payload: Work): Promise<Token> => {
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
CONST.messageText().error();
|
CONST.messageText().error();
|
||||||
console.error(err);
|
console.error(err);
|
||||||
await new Promise(r => setTimeout(r, 1000));
|
await new Promise((r) => setTimeout(r, 1000));
|
||||||
window.location.reload();
|
window.location.reload();
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
|
|
38
templates/widget/service-worker.ts
Normal file
38
templates/widget/service-worker.ts
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2021 Aravinth Manivannan <realaravinth@batsense.net>
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as
|
||||||
|
* published by the Free Software Foundation, either version 3 of the
|
||||||
|
* License, or (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
import prove from "./prove";
|
||||||
|
import { PoWConfig, ServiceWorkerWork } from "./types";
|
||||||
|
import log from "../logger";
|
||||||
|
|
||||||
|
log.log("worker registered");
|
||||||
|
onmessage = (e) => {
|
||||||
|
console.debug("message received at worker");
|
||||||
|
const config: PoWConfig = e.data;
|
||||||
|
|
||||||
|
const t0 = performance.now();
|
||||||
|
const work = prove(config);
|
||||||
|
|
||||||
|
const t1 = performance.now();
|
||||||
|
const duration = t1 - t0;
|
||||||
|
|
||||||
|
const res: ServiceWorkerWork = {
|
||||||
|
work,
|
||||||
|
duration,
|
||||||
|
};
|
||||||
|
|
||||||
|
postMessage(res);
|
||||||
|
};
|
37
templates/widget/types.ts
Normal file
37
templates/widget/types.ts
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* mCaptcha is a PoW based DoS protection software.
|
||||||
|
* This is the frontend web component of the mCaptcha system
|
||||||
|
* Copyright © 2021 Aravinth Manivnanan <realaravinth@batsense.net>.
|
||||||
|
*
|
||||||
|
* Use of this source code is governed by Apache 2.0 or MIT license.
|
||||||
|
* You shoud have received a copy of MIT and Apache 2.0 along with
|
||||||
|
* this program. If not, see <https://spdx.org/licenses/MIT.html> for
|
||||||
|
* MIT or <http://www.apache.org/licenses/LICENSE-2.0> for Apache.
|
||||||
|
*/
|
||||||
|
|
||||||
|
export type Work = {
|
||||||
|
result: string;
|
||||||
|
nonce: number;
|
||||||
|
string: string;
|
||||||
|
key: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type WasmWork = {
|
||||||
|
result: string;
|
||||||
|
nonce: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type ServiceWorkerWork = {
|
||||||
|
work: WasmWork;
|
||||||
|
duration: number;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type PoWConfig = {
|
||||||
|
string: string;
|
||||||
|
difficulty_factor: number;
|
||||||
|
salt: string;
|
||||||
|
};
|
||||||
|
|
||||||
|
export type Token = {
|
||||||
|
token: string;
|
||||||
|
};
|
|
@ -12,6 +12,7 @@ module.exports = {
|
||||||
bundle: "./templates/index.ts",
|
bundle: "./templates/index.ts",
|
||||||
mobile: "./templates/mobile.ts",
|
mobile: "./templates/mobile.ts",
|
||||||
verificationWidget: "./templates/widget/index.ts",
|
verificationWidget: "./templates/widget/index.ts",
|
||||||
|
bench: "./templates/widget/service-worker.ts",
|
||||||
},
|
},
|
||||||
output: {
|
output: {
|
||||||
filename: "[name].js",
|
filename: "[name].js",
|
||||||
|
|
Loading…
Reference in a new issue