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 RegistryEntry {
|
|
|
|
resolve: any;
|
|
|
|
reject: any;
|
|
|
|
workerIndex: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Are we currently running a task on this worker?
|
|
|
|
*/
|
|
|
|
busy: boolean;
|
2016-10-11 14:08:41 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Timer to terminate the worker if it's not busy enough.
|
|
|
|
*/
|
|
|
|
terminationTimerHandle: number|null;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Index of this worker in the list of workers.
|
|
|
|
*/
|
|
|
|
workerIndex: number;
|
2016-09-26 14:22:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
interface WorkItem {
|
|
|
|
operation: string;
|
|
|
|
args: any[];
|
|
|
|
resolve: any;
|
|
|
|
reject: any;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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-12 20:25:56 +02:00
|
|
|
private rpcRegistry: {[n: number]: RegistryEntry} = {};
|
2016-09-26 14:22:06 +02:00
|
|
|
private workers: WorkerState[];
|
|
|
|
private workQueues: WorkItem[][];
|
|
|
|
/**
|
|
|
|
* Number of busy workers.
|
|
|
|
*/
|
|
|
|
private numBusy: number = 0;
|
|
|
|
/**
|
|
|
|
* Number if pending work items.
|
|
|
|
*/
|
|
|
|
private numWaiting: 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.
|
|
|
|
*/
|
|
|
|
wake(ws: WorkerState) {
|
|
|
|
if (ws.busy) {
|
|
|
|
throw Error("assertion failed");
|
|
|
|
}
|
|
|
|
ws.busy = true;
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (ws.terminationTimerHandle != null) {
|
|
|
|
clearTimeout(ws.terminationTimerHandle);
|
|
|
|
}
|
|
|
|
let destroy = () => {
|
|
|
|
if (ws.w && !ws.busy) {
|
|
|
|
ws.w!.terminate();
|
|
|
|
ws.w = null;
|
2016-09-28 17:45:13 +02:00
|
|
|
}
|
2016-10-11 14:08:41 +02:00
|
|
|
}
|
|
|
|
ws.terminationTimerHandle = setTimeout(destroy, 20 * 1000);
|
|
|
|
}
|
|
|
|
|
|
|
|
handleWorkerError(ws: WorkerState, e: ErrorEvent) {
|
|
|
|
console.error("error in worker", e);
|
|
|
|
try {
|
|
|
|
ws.w!.terminate();
|
|
|
|
} catch (e) {
|
|
|
|
console.error(e);
|
|
|
|
}
|
|
|
|
if (ws.busy) {
|
2016-09-26 14:22:06 +02:00
|
|
|
ws.busy = false;
|
|
|
|
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()!;
|
|
|
|
let msg: any = {
|
|
|
|
operation: work.operation,
|
|
|
|
args: work.args,
|
|
|
|
id: this.registerRpcId(work.resolve, work.reject, ws.workerIndex),
|
|
|
|
};
|
|
|
|
|
|
|
|
this.wake(ws);
|
|
|
|
ws.w!.postMessage(msg);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
if (!this.rpcRegistry[id]) {
|
|
|
|
console.error(`RPC with id ${id} has no registry entry`);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let {resolve, workerIndex} = this.rpcRegistry[id];
|
|
|
|
delete this.rpcRegistry[id];
|
|
|
|
if (workerIndex != ws.workerIndex) {
|
|
|
|
throw Error("assertion failed");
|
|
|
|
}
|
|
|
|
if (!ws.busy) {
|
|
|
|
throw Error("assertion failed");
|
|
|
|
}
|
|
|
|
ws.busy = false;
|
|
|
|
this.numBusy--;
|
|
|
|
resolve(msg.data.result);
|
|
|
|
this.findWork(ws);
|
|
|
|
}
|
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,
|
2016-09-26 14:22:06 +02:00
|
|
|
busy: false,
|
2016-10-11 14:08:41 +02:00
|
|
|
terminationTimerHandle: null,
|
|
|
|
workerIndex: i,
|
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 registerRpcId(resolve: any, reject: any,
|
|
|
|
workerIndex: number): number {
|
2016-02-22 19:21:06 +01:00
|
|
|
let id = this.nextRpcId++;
|
2016-09-26 14:22:06 +02:00
|
|
|
this.rpcRegistry[id] = {resolve, reject, workerIndex};
|
2016-02-22 19:21:06 +01:00
|
|
|
return id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-09-26 14:22:06 +02:00
|
|
|
private doRpc<T>(operation: string, priority: number,
|
|
|
|
...args: any[]): Promise<T> {
|
|
|
|
if (this.numBusy == this.workers.length) {
|
|
|
|
let q = this.workQueues[priority];
|
|
|
|
if (!q) {
|
|
|
|
throw Error("assertion failed");
|
|
|
|
}
|
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
this.workQueues[priority].push({operation, args, resolve, reject});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (let i = 0; i < this.workers.length; i++) {
|
|
|
|
let ws = this.workers[i];
|
|
|
|
if (ws.busy) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-10-11 14:08:41 +02:00
|
|
|
this.wake(ws);
|
2016-09-28 17:45:13 +02:00
|
|
|
|
2016-09-26 14:22:06 +02:00
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
let msg: any = {
|
|
|
|
operation, args,
|
|
|
|
id: this.registerRpcId(resolve, reject, i),
|
|
|
|
};
|
2016-10-11 14:08:41 +02:00
|
|
|
ws.w!.postMessage(msg);
|
2016-09-26 14:22:06 +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 21:52:53 +01:00
|
|
|
hashRsaPub(rsaPub: string): Promise<string> {
|
2016-09-26 14:22:06 +02:00
|
|
|
return this.doRpc("hashRsaPub", 2, rsaPub);
|
2016-02-22 21:52:53 +01:00
|
|
|
}
|
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
|
|
|
|
|
|
|
createWithdrawSession(kappa: number, meltCoin: Coin,
|
|
|
|
newCoinDenoms: Denomination[],
|
|
|
|
meltAmount: AmountJson,
|
|
|
|
meltFee: AmountJson): Promise<RefreshSession> {
|
|
|
|
return this.doRpc("createWithdrawSession",
|
|
|
|
4,
|
|
|
|
kappa,
|
|
|
|
meltCoin,
|
|
|
|
newCoinDenoms,
|
|
|
|
meltAmount,
|
|
|
|
meltFee);
|
|
|
|
}
|
2016-03-01 19:46:20 +01:00
|
|
|
}
|