wallet-core/src/crypto/cryptoWorker.ts

78 lines
2.0 KiB
TypeScript
Raw Normal View History

2016-02-19 00:49:22 +01:00
/*
This file is part of TALER
(C) 2016 GNUnet e.V.
TALER is free software; you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
TALER 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 General Public License for more details.
You should have received a copy of the GNU General Public License along with
2016-07-07 17:59:29 +02:00
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
2016-02-19 00:49:22 +01:00
*/
/**
* Web worker for crypto operations.
*/
2017-05-24 16:14:23 +02:00
/**
* Imports.
*/
2017-05-28 01:10:54 +02:00
2018-09-20 21:11:09 +02:00
import * as emscLoader from "./emscLoader";
2019-08-15 23:34:08 +02:00
import { CryptoImplementation } from "./cryptoImplementation";
import { EmscEnvironment } from "./emscInterface";
2019-08-15 23:34:08 +02:00
const worker: Worker = (self as any) as Worker;
2018-09-22 17:18:25 +02:00
2019-08-15 23:34:08 +02:00
let impl: CryptoImplementation | undefined;
worker.onmessage = (msg: MessageEvent) => {
2019-08-15 23:34:08 +02:00
const args = msg.data.args;
if (!Array.isArray(args)) {
console.error("args must be array");
return;
}
2019-08-15 23:34:08 +02:00
const id = msg.data.id;
if (typeof id !== "number") {
console.error("RPC id must be number");
2019-08-15 23:34:08 +02:00
return;
}
2019-08-15 23:34:08 +02:00
const operation = msg.data.operation;
if (typeof operation !== "string") {
console.error("RPC operation must be string");
return;
}
2018-09-20 21:11:09 +02:00
2019-08-15 23:34:08 +02:00
if (CryptoImplementation.enableTracing) {
console.log("onmessage with", operation);
2019-07-31 01:33:56 +02:00
}
2018-09-20 21:11:09 +02:00
2019-06-26 15:30:32 +02:00
emscLoader.getLib().then(p => {
2018-09-20 21:11:09 +02:00
const lib = p.lib;
2019-08-15 23:34:08 +02:00
const emsc = new EmscEnvironment(lib);
const impl = new CryptoImplementation(emsc);
if (!(operation in impl)) {
console.error(`unknown operation: '${operation}'`);
return;
2018-09-20 21:11:09 +02:00
}
2019-08-15 23:34:08 +02:00
if (CryptoImplementation.enableTracing) {
console.log("about to execute", operation);
2019-07-31 01:33:56 +02:00
}
2019-08-15 23:34:08 +02:00
const result = (impl as any)[operation](...args);
if (CryptoImplementation.enableTracing) {
console.log("finished executing", operation);
2019-07-31 01:33:56 +02:00
}
2019-08-15 23:34:08 +02:00
worker.postMessage({ result, id });
2018-09-20 21:11:09 +02:00
});
2017-05-28 01:10:54 +02:00
};