2019-08-16 15:03:52 +02:00
|
|
|
/*
|
2022-06-06 17:05:26 +02:00
|
|
|
This file is part of GNU Taler
|
|
|
|
(C) 2022 Taler Systems S.A.
|
2019-08-16 15:03:52 +02:00
|
|
|
|
2022-06-06 17:05:26 +02:00
|
|
|
GNU Taler is free software; you can redistribute it and/or modify it under the
|
2019-08-16 15:03:52 +02:00
|
|
|
terms of the GNU General Public License as published by the Free Software
|
|
|
|
Foundation; either version 3, or (at your option) any later version.
|
|
|
|
|
2022-06-06 17:05:26 +02:00
|
|
|
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
|
2019-08-16 15:03:52 +02:00
|
|
|
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
|
|
|
|
A PARTICULAR PURPOSE. See the GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along with
|
2022-06-06 17:05:26 +02:00
|
|
|
GNU Taler; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*/
|
2019-08-16 15:03:52 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Web worker for crypto operations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
|
|
|
|
2021-06-08 20:58:13 +02:00
|
|
|
import { Logger } from "@gnu-taler/taler-util";
|
2022-03-23 21:24:23 +01:00
|
|
|
import { nativeCrypto } from "@gnu-taler/taler-wallet-core";
|
2020-08-14 12:23:50 +02:00
|
|
|
|
|
|
|
const logger = new Logger("browserWorkerEntry.ts");
|
2019-08-16 15:03:52 +02:00
|
|
|
|
2022-03-23 21:24:23 +01:00
|
|
|
const worker: Worker = self as any as Worker;
|
2019-08-16 15:03:52 +02:00
|
|
|
|
2020-04-07 10:28:55 +02:00
|
|
|
async function handleRequest(
|
|
|
|
operation: string,
|
|
|
|
id: number,
|
2022-03-25 20:57:27 +01:00
|
|
|
req: unknown,
|
2020-04-07 10:28:55 +02:00
|
|
|
): Promise<void> {
|
2022-03-23 21:24:23 +01:00
|
|
|
const impl = nativeCrypto;
|
2019-08-16 15:03:52 +02:00
|
|
|
|
|
|
|
if (!(operation in impl)) {
|
|
|
|
console.error(`crypto operation '${operation}' not found`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2022-03-25 20:57:27 +01:00
|
|
|
const result = await (impl as any)[operation](req);
|
2019-08-16 15:03:52 +02:00
|
|
|
worker.postMessage({ result, id });
|
|
|
|
} catch (e) {
|
2020-08-14 12:23:50 +02:00
|
|
|
logger.error("error during operation", e);
|
2019-08-16 15:03:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
worker.onmessage = (msg: MessageEvent) => {
|
2022-03-25 20:57:27 +01:00
|
|
|
const req = msg.data.req;
|
|
|
|
if (typeof req !== "object") {
|
|
|
|
console.error("request must be an object");
|
2019-08-16 15:03:52 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const id = msg.data.id;
|
|
|
|
if (typeof id !== "number") {
|
|
|
|
console.error("RPC id must be number");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const operation = msg.data.operation;
|
|
|
|
if (typeof operation !== "string") {
|
|
|
|
console.error("RPC operation must be string");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-25 20:57:27 +01:00
|
|
|
handleRequest(operation, id, req).catch((e) => {
|
2021-08-13 23:04:05 +02:00
|
|
|
console.error("error in browser worker", e);
|
2019-08-16 15:03:52 +02:00
|
|
|
});
|
|
|
|
};
|