diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-02-22 19:21:06 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-02-22 21:53:05 +0100 |
commit | 180e122ee639fa41d4cfbca0f57117f5a12fa33a (patch) | |
tree | 9db6890fe6d7ba32b41012d1857f88079b494eac /extension | |
parent | 4c005a15960174ba96903f5e3882f56ab7485d81 (diff) |
separate crypto api
Diffstat (limited to 'extension')
-rw-r--r-- | extension/lib/wallet/cryptoApi.ts | 74 | ||||
-rw-r--r-- | extension/lib/wallet/wallet.ts | 78 | ||||
-rw-r--r-- | extension/tsconfig.json | 1 |
3 files changed, 94 insertions, 59 deletions
diff --git a/extension/lib/wallet/cryptoApi.ts b/extension/lib/wallet/cryptoApi.ts new file mode 100644 index 000000000..c29e9a45e --- /dev/null +++ b/extension/lib/wallet/cryptoApi.ts @@ -0,0 +1,74 @@ +/* + 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 + TALER; see the file COPYING. If not, If not, see <http://www.gnu.org/licenses/> + */ + + +import {PreCoin} from "./types"; +import {Reserve} from "./types"; +import {Denomination} from "./types"; +export class CryptoApi { + private nextRpcId: number = 1; + private rpcRegistry = {}; + private cryptoWorker: Worker; + + + constructor() { + this.cryptoWorker = new Worker("/lib/wallet/cryptoWorker.js"); + + this.cryptoWorker.onmessage = (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, reject} = this.rpcRegistry[id]; + resolve(msg.data.result); + } + } + + + private registerRpcId(resolve, reject): number { + let id = this.nextRpcId++; + this.rpcRegistry[id] = {resolve, reject}; + return id; + } + + + private doRpc<T>(methodName: string, ...args): Promise<T> { + return new Promise<T>((resolve, reject) => { + let msg = { + operation: methodName, + id: this.registerRpcId(resolve, reject), + args: args, + }; + this.cryptoWorker.postMessage(msg); + }); + } + + + createPreCoin(denom: Denomination, reserve: Reserve): Promise<PreCoin> { + return this.doRpc("createPreCoin", denom, reserve); + } + + + isValidDenom(denom: Denomination, + masterPub: string): Promise<boolean> { + return this.doRpc("isValidDenom", denom, masterPub); + } +}
\ No newline at end of file diff --git a/extension/lib/wallet/wallet.ts b/extension/lib/wallet/wallet.ts index 8e7f63b12..2bd2beee5 100644 --- a/extension/lib/wallet/wallet.ts +++ b/extension/lib/wallet/wallet.ts @@ -30,6 +30,7 @@ import {canonicalizeBaseUrl} from "./helpers"; import {ReserveCreationInfo} from "./types"; import {PreCoin} from "./types"; import {Reserve} from "./types"; +import {CryptoApi} from "./cryptoApi"; "use strict"; @@ -107,7 +108,7 @@ class MintInfo implements IMintInfo { * mint info is updated with the new information up until * the first error. */ - mergeKeys(newKeys: KeysJson, wallet: Wallet): Promise<void> { + mergeKeys(newKeys: KeysJson, cryptoApi: CryptoApi): Promise<void> { if (!this.masterPublicKey) { this.masterPublicKey = newKeys.master_public_key; } @@ -140,20 +141,21 @@ class MintInfo implements IMintInfo { return Promise.resolve(); } - return wallet.isValidDenom(newDenom, this.masterPublicKey) - .then((valid) => { - if (!valid) { - throw Error("signature on denomination invalid"); - } + return cryptoApi + .isValidDenom(newDenom, this.masterPublicKey) + .then((valid) => { + if (!valid) { + throw Error("signature on denomination invalid"); + } - let d: Denomination = Object.assign({}, newDenom); - d.pub_hash = native.RsaPublicKey.fromCrock(d.denom_pub) - .encode() - .hash() - .toCrock(); - this.denoms.push(d); + let d: Denomination = Object.assign({}, newDenom); + d.pub_hash = native.RsaPublicKey.fromCrock(d.denom_pub) + .encode() + .hash() + .toCrock(); + this.denoms.push(d); - }); + }); }); return Promise.all(ps).then(() => void 0); @@ -410,9 +412,7 @@ export class Wallet { private http: HttpRequestLibrary; private badge: Badge; private notifier: Notifier; - private cryptoWorker: Worker; - private nextRpcId: number = 1; - private rpcRegistry = {}; + public cryptoApi: CryptoApi; constructor(db: IDBDatabase, @@ -423,21 +423,7 @@ export class Wallet { this.http = http; this.badge = badge; this.notifier = notifier; - this.cryptoWorker = new Worker("/lib/wallet/cryptoWorker.js"); - - this.cryptoWorker.onmessage = (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, reject} = this.rpcRegistry[id]; - resolve(msg.data.result); - } + this.cryptoApi = new CryptoApi(); } @@ -859,7 +845,8 @@ export class Wallet { */ private withdraw(denom: Denomination, reserve: Reserve): Promise<void> { console.log("creating pre coin at", new Date()); - return this.createPreCoin(denom, reserve) + return this.cryptoApi + .createPreCoin(denom, reserve) .then((preCoin) => { return Query(this.db) .put("precoins", preCoin) @@ -1026,31 +1013,4 @@ export class Wallet { .iter("history", {indexName: "timestamp"}) .reduce(collect, []) } - - registerRpcId(resolve, reject): number { - let id = this.nextRpcId++; - this.rpcRegistry[id] = {resolve, reject}; - return id; - } - - private doRpc<T>(methodName: string, ...args): Promise<T> { - return new Promise<T>((resolve, reject) => { - let msg = { - operation: methodName, - id: this.registerRpcId(resolve, reject), - args: args, - }; - this.cryptoWorker.postMessage(msg); - }); - } - - - createPreCoin(denom: Denomination, reserve: Reserve): Promise<PreCoin> { - return this.doRpc("createPreCoin", denom, reserve); - } - - isValidDenom(denom: Denomination, - masterPub: string): Promise<boolean> { - return this.doRpc("isValidDenom", denom, masterPub); - } }
\ No newline at end of file diff --git a/extension/tsconfig.json b/extension/tsconfig.json index f11267a0c..0e6337d86 100644 --- a/extension/tsconfig.json +++ b/extension/tsconfig.json @@ -13,6 +13,7 @@ "lib/i18n.ts", "lib/refs.ts", "lib/wallet/checkable.ts", + "lib/wallet/cryptoApi.ts", "lib/wallet/cryptoLib.ts", "lib/wallet/cryptoWorker.ts", "lib/wallet/db.ts", |