2016-02-22 19:21:06 +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-22 19:21:06 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-03-01 19:46:20 +01:00
|
|
|
/**
|
|
|
|
* API to access the Taler crypto worker thread.
|
|
|
|
* @author Florian Dold
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-10-13 02:23:24 +02:00
|
|
|
import {PreCoin, Coin, ReserveRecord, AmountJson} from "./types";
|
2016-02-22 19:21:06 +01:00
|
|
|
import {Denomination} from "./types";
|
2016-02-22 21:52:53 +01:00
|
|
|
import {Offer} from "./wallet";
|
|
|
|
import {CoinWithDenom} from "./wallet";
|
|
|
|
import {PayCoinInfo} from "./types";
|
2016-10-13 02:23:24 +02:00
|
|
|
import {RefreshSession} from "./types";
|
2016-09-26 14:22:06 +02:00
|
|
|
|
|
|
|
|
|
|
|
interface WorkerState {
|
|
|
|
/**
|
|
|
|
* The actual worker thread.
|
|
|
|
*/
|
2016-10-11 14:08:41 +02:00
|
|
|
w: Worker|null;
|
|
|
|
|
2016-09-26 14:22:06 +02:00
|
|
|
/**
|
2016-10-13 20:02:42 +02:00
|
|
|
* Work we're currently executing or null if not busy.
|
2016-09-26 14:22:06 +02:00
|
|
|
*/
|
2016-10-13 20:02:42 +02:00
|
|
|
currentWorkItem: WorkItem|null;
|
2016-10-11 14:08:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer to terminate the worker if it's not busy enough.
|
|
|
|
*/
|
|
|
|
terminationTimerHandle: number|null;
|
2016-09-26 14:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface WorkItem {
|
|
|
|
operation: string;
|
|
|
|
args: any[];
|
|
|
|
resolve: any;
|
|
|
|
reject: any;
|
2016-10-13 20:02:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Serial id to identify a matching response.
|
|
|
|
*/
|
|
|
|
rpcId: number;
|
2016-09-26 14:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Number of different priorities. Each priority p
|
|
|
|
* must be 0 <= p < NUM_PRIO.
|
|
|
|
*/
|
|
|
|
const NUM_PRIO = 5;
|
|
|
|
|
2016-02-22 19:21:06 +01:00
|
|
|
export class CryptoApi {
|
|
|
|
private nextRpcId: number = 1;
|
2016-09-26 14:22:06 +02:00
|
|
|
private workers: WorkerState[];
|
|
|
|
private workQueues: WorkItem[][];
|
|
|
|
/**
|
|
|
|
* Number of busy workers.
|
|
|
|
*/
|
|
|
|
private numBusy: number = 0;
|
2016-02-22 19:21:06 +01:00
|
|
|
|
2016-10-11 14:08:41 +02:00
|
|
|
/**
|
|
|
|
* Start a worker (if not started) and set as busy.
|
|
|
|
*/
|
2016-10-13 20:02:42 +02:00
|
|
|
wake<T>(ws: WorkerState, work: WorkItem): void {
|
|
|
|
if (ws.currentWorkItem != null) {
|
2016-10-11 14:08:41 +02:00
|
|
|
throw Error("assertion failed");
|
|
|
|
}
|
2016-10-13 20:02:42 +02:00
|
|
|
ws.currentWorkItem = work;
|
2016-10-11 14:08:41 +02:00
|
|
|
this.numBusy++;
|
|
|
|
if (!ws.w) {
|
|
|
|
let w = new Worker("/lib/wallet/cryptoWorker.js");
|
|
|
|
w.onmessage = (m: MessageEvent) => this.handleWorkerMessage(ws, m);
|
|
|
|
w.onerror = (e: ErrorEvent) => this.handleWorkerError(ws, e);
|
|
|
|
ws.w = w;
|
|
|
|
}
|
2016-10-13 20:02:42 +02:00
|
|
|
|
|
|
|
let msg: any = {
|
|
|
|
operation: work.operation, args: work.args,
|
|
|
|
id: work.rpcId
|
|
|
|
};
|
|
|
|
this.resetWorkerTimeout(ws);
|
|
|
|
ws.w!.postMessage(msg);
|
|
|
|
}
|
|
|
|
|
|
|
|
resetWorkerTimeout(ws: WorkerState) {
|
2016-10-11 14:08:41 +02:00
|
|
|
if (ws.terminationTimerHandle != null) {
|
|
|
|
clearTimeout(ws.terminationTimerHandle);
|
|
|
|
}
|
|
|
|
let destroy = () => {
|
2016-10-13 20:02:42 +02:00
|
|
|
// terminate worker if it's idle
|
|
|
|
if (ws.w && ws.currentWorkItem == null) {
|
2016-10-11 14:08:41 +02:00
|
|
|
ws.w!.terminate();
|
|
|
|
ws.w = null;
|
2016-09-28 17:45:13 +02:00
|
|
|
}
|
2016-10-13 20:02:42 +02:00
|
|
|
};
|
2016-10-11 14:08:41 +02:00
|
|
|
ws.terminationTimerHandle = setTimeout(destroy, 20 * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWorkerError(ws: WorkerState, e: ErrorEvent) {
|
2016-10-13 20:02:42 +02:00
|
|
|
if (ws.currentWorkItem) {
|
2016-10-14 02:13:06 +02:00
|
|
|
console.error(`error in worker during ${ws.currentWorkItem!.operation}`,
|
|
|
|
e);
|
2016-10-13 20:02:42 +02:00
|
|
|
} else {
|
|
|
|
console.error("error in worker", e);
|
|
|
|
}
|
|
|
|
console.error(e.message);
|
2016-10-11 14:08:41 +02:00
|
|
|
try {
|
|
|
|
ws.w!.terminate();
|
2016-10-13 20:02:42 +02:00
|
|
|
ws.w = null;
|
2016-10-11 14:08:41 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
2016-10-13 20:02:42 +02:00
|
|
|
if (ws.currentWorkItem != null) {
|
|
|
|
ws.currentWorkItem.reject(e);
|
|
|
|
ws.currentWorkItem = null;
|
2016-09-26 14:22:06 +02:00
|
|
|
this.numBusy--;
|
2016-10-11 14:08:41 +02:00
|
|
|
}
|
|
|
|
this.findWork(ws);
|
|
|
|
}
|
|
|
|
|
|
|
|
findWork(ws: WorkerState) {
|
|
|
|
// try to find more work for this worker
|
|
|
|
for (let i = 0; i < NUM_PRIO; i++) {
|
|
|
|
let q = this.workQueues[NUM_PRIO - i - 1];
|
|
|
|
if (q.length != 0) {
|
|
|
|
let work: WorkItem = q.shift()!;
|
2016-10-13 20:02:42 +02:00
|
|
|
this.wake(ws, work);
|
2016-10-11 14:08:41 +02:00
|
|
|
return;
|
2016-09-26 14:22:06 +02:00
|
|
|
}
|
2016-10-11 14:08:41 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWorkerMessage(ws: WorkerState, msg: MessageEvent) {
|
|
|
|
let id = msg.data.id;
|
|
|
|
if (typeof id !== "number") {
|
|
|
|
console.error("rpc id must be number");
|
|
|
|
return;
|
|
|
|
}
|
2016-10-13 20:02:42 +02:00
|
|
|
let currentWorkItem = ws.currentWorkItem;
|
|
|
|
ws.currentWorkItem = null;
|
|
|
|
this.numBusy--;
|
|
|
|
this.findWork(ws);
|
|
|
|
if (!currentWorkItem) {
|
|
|
|
console.error("unsolicited response from worker");
|
2016-10-11 14:08:41 +02:00
|
|
|
return;
|
|
|
|
}
|
2016-10-13 20:02:42 +02:00
|
|
|
if (id != currentWorkItem.rpcId) {
|
|
|
|
console.error(`RPC with id ${id} has no registry entry`);
|
|
|
|
return;
|
2016-10-11 14:08:41 +02:00
|
|
|
}
|
2016-10-13 20:02:42 +02:00
|
|
|
currentWorkItem.resolve(msg.data.result);
|
2016-10-11 14:08:41 +02:00
|
|
|
}
|
2016-09-26 14:22:06 +02:00
|
|
|
|
2016-10-11 14:08:41 +02:00
|
|
|
constructor() {
|
2016-09-26 14:22:06 +02:00
|
|
|
this.workers = new Array<WorkerState>((navigator as any)["hardwareConcurrency"] || 2);
|
|
|
|
|
|
|
|
for (let i = 0; i < this.workers.length; i++) {
|
|
|
|
this.workers[i] = {
|
2016-10-11 14:08:41 +02:00
|
|
|
w: null,
|
|
|
|
terminationTimerHandle: null,
|
2016-10-13 20:02:42 +02:00
|
|
|
currentWorkItem: null,
|
2016-09-26 14:22:06 +02:00
|
|
|
};
|
|
|
|
}
|
|
|
|
this.workQueues = [];
|
|
|
|
for (let i = 0; i < NUM_PRIO; i++) {
|
|
|
|
this.workQueues.push([]);
|
2016-02-22 19:21:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-26 14:22:06 +02:00
|
|
|
private doRpc<T>(operation: string, priority: number,
|
|
|
|
...args: any[]): Promise<T> {
|
|
|
|
|
2016-10-13 20:02:42 +02:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
let rpcId = this.nextRpcId++;
|
|
|
|
let workItem: WorkItem = {operation, args, resolve, reject, rpcId};
|
|
|
|
|
|
|
|
if (this.numBusy == this.workers.length) {
|
|
|
|
let q = this.workQueues[priority];
|
|
|
|
if (!q) {
|
|
|
|
throw Error("assertion failed");
|
|
|
|
}
|
|
|
|
this.workQueues[priority].push(workItem);
|
|
|
|
return;
|
2016-09-26 14:22:06 +02:00
|
|
|
}
|
|
|
|
|
2016-10-13 20:02:42 +02:00
|
|
|
for (let i = 0; i < this.workers.length; i++) {
|
|
|
|
let ws = this.workers[i];
|
|
|
|
if (ws.currentWorkItem != null) {
|
|
|
|
continue;
|
|
|
|
}
|
2016-09-28 17:45:13 +02:00
|
|
|
|
2016-10-13 20:02:42 +02:00
|
|
|
this.wake<T>(ws, workItem);
|
|
|
|
return;
|
|
|
|
}
|
2016-09-26 14:22:06 +02:00
|
|
|
|
2016-10-13 20:02:42 +02:00
|
|
|
throw Error("assertion failed");
|
|
|
|
});
|
2016-02-22 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-13 02:23:24 +02:00
|
|
|
createPreCoin(denom: Denomination, reserve: ReserveRecord): Promise<PreCoin> {
|
2016-09-26 14:22:06 +02:00
|
|
|
return this.doRpc("createPreCoin", 1, denom, reserve);
|
2016-02-22 19:21:06 +01:00
|
|
|
}
|
|
|
|
|
2016-09-28 23:41:34 +02:00
|
|
|
hashString(str: string): Promise<string> {
|
|
|
|
return this.doRpc("hashString", 1, str);
|
|
|
|
}
|
|
|
|
|
2016-02-22 19:21:06 +01:00
|
|
|
isValidDenom(denom: Denomination,
|
|
|
|
masterPub: string): Promise<boolean> {
|
2016-09-26 14:22:06 +02:00
|
|
|
return this.doRpc("isValidDenom", 2, denom, masterPub);
|
2016-02-22 19:21:06 +01:00
|
|
|
}
|
2016-02-22 21:52:53 +01:00
|
|
|
|
|
|
|
signDeposit(offer: Offer,
|
|
|
|
cds: CoinWithDenom[]): Promise<PayCoinInfo> {
|
2016-09-26 14:22:06 +02:00
|
|
|
return this.doRpc("signDeposit", 3, offer, cds);
|
2016-02-22 21:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
createEddsaKeypair(): Promise<{priv: string, pub: string}> {
|
2016-09-26 14:22:06 +02:00
|
|
|
return this.doRpc("createEddsaKeypair", 1);
|
2016-02-22 21:52:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
rsaUnblind(sig: string, bk: string, pk: string): Promise<string> {
|
2016-09-26 14:22:06 +02:00
|
|
|
return this.doRpc("rsaUnblind", 4, sig, bk, pk);
|
2016-02-22 21:52:53 +01:00
|
|
|
}
|
2016-10-13 02:23:24 +02:00
|
|
|
|
2016-10-14 02:13:06 +02:00
|
|
|
createRefreshSession(exchangeBaseUrl: string,
|
|
|
|
kappa: number,
|
|
|
|
meltCoin: Coin,
|
|
|
|
newCoinDenoms: Denomination[],
|
|
|
|
meltFee: AmountJson): Promise<RefreshSession> {
|
|
|
|
return this.doRpc("createRefreshSession",
|
2016-10-13 02:23:24 +02:00
|
|
|
4,
|
2016-10-14 02:13:06 +02:00
|
|
|
exchangeBaseUrl,
|
2016-10-13 02:23:24 +02:00
|
|
|
kappa,
|
|
|
|
meltCoin,
|
|
|
|
newCoinDenoms,
|
|
|
|
meltFee);
|
|
|
|
}
|
2016-11-02 21:13:56 +01:00
|
|
|
}
|