aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-01-06 15:58:18 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-01-06 15:58:18 +0100
commit5bb1c95ec531c4cf7848466e4ff5c3408f2dca7f (patch)
tree366c7fce1e6fcec0196f301b4cf2539f32733fb8
parentabf15268acafe588191fffb7ca6ddb963244bb0f (diff)
downloadwallet-core-5bb1c95ec531c4cf7848466e4ff5c3408f2dca7f.tar.xz
make wallet independent of chrome/WX api
-rw-r--r--extension/background/http.ts71
-rw-r--r--extension/background/messaging.ts59
-rw-r--r--extension/background/wallet.js13
-rw-r--r--extension/background/wallet.ts25
4 files changed, 95 insertions, 73 deletions
diff --git a/extension/background/http.ts b/extension/background/http.ts
index 9355add6f..fdbf8425c 100644
--- a/extension/background/http.ts
+++ b/extension/background/http.ts
@@ -28,50 +28,51 @@ interface HttpResponse {
}
-function httpReq(method: string,
- url: string|uri.URI,
- options?: any): Promise<HttpResponse> {
- let urlString: string;
- if (url instanceof URI) {
- urlString = url.href();
- } else if (typeof url === "string") {
- urlString = url;
- }
-
- return new Promise((resolve, reject) => {
- let myRequest = new XMLHttpRequest();
- myRequest.open(method, urlString);
- if (options && options.req) {
- myRequest.send(options.req);
- } else {
- myRequest.send();
+class BrowserHttpLib {
+ req(method: string,
+ url: string|uri.URI,
+ options?: any): Promise<HttpResponse> {
+ let urlString: string;
+ if (url instanceof URI) {
+ urlString = url.href();
+ } else if (typeof url === "string") {
+ urlString = url;
}
- myRequest.addEventListener("readystatechange", (e) => {
- if (myRequest.readyState == XMLHttpRequest.DONE) {
- let resp = {
- status: myRequest.status,
- responseText: myRequest.responseText
- };
- resolve(resp);
+
+ return new Promise((resolve, reject) => {
+ let myRequest = new XMLHttpRequest();
+ myRequest.open(method, urlString);
+ if (options && options.req) {
+ myRequest.send(options.req);
+ } else {
+ myRequest.send();
}
+ myRequest.addEventListener("readystatechange", (e) => {
+ if (myRequest.readyState == XMLHttpRequest.DONE) {
+ let resp = {
+ status: myRequest.status,
+ responseText: myRequest.responseText
+ };
+ resolve(resp);
+ }
+ });
});
- });
-}
-
+ }
-function httpGet(url: string|uri.URI) {
- return httpReq("get", url);
-}
+ get(url: string|uri.URI) {
+ return this.req("get", url);
+ }
-function httpPostJson(url: string|uri.URI, body) {
- return httpReq("post", url, {req: JSON.stringify(body)});
-}
+ postJson(url: string|uri.URI, body) {
+ return this.req("post", url, {req: JSON.stringify(body)});
+ }
-function httpPostForm(url: string|uri.URI, form) {
- return httpReq("post", url, {req: form});
+ postForm(url: string|uri.URI, form) {
+ return this.req("post", url, {req: form});
+ }
}
diff --git a/extension/background/messaging.ts b/extension/background/messaging.ts
index 8cde06262..d95c4bfb5 100644
--- a/extension/background/messaging.ts
+++ b/extension/background/messaging.ts
@@ -57,48 +57,61 @@ function makeHandlers(wallet) {
amount_str: detail.amount_str
};
wallet.confirmReserve(req)
- .then((resp) => {
- if (resp.success) {
- resp.backlink = chrome.extension.getURL("pages/reserve-success.html");
- }
- sendResponse(resp);
- });
+ .then((resp) => {
+ if (resp.success) {
+ resp.backlink = chrome.extension.getURL(
+ "pages/reserve-success.html");
+ }
+ sendResponse(resp);
+ });
return true;
},
["confirm-pay"]: function(db, detail, sendResponse) {
wallet.confirmPay(detail.offer, detail.merchantPageUrl)
- .then(() => {
- sendResponse({success: true})
- })
- .catch((e) => {
- sendResponse({error: e.message});
- });
+ .then(() => {
+ sendResponse({success: true})
+ })
+ .catch((e) => {
+ sendResponse({error: e.message});
+ });
return true;
},
["execute-payment"]: function(db, detail, sendResponse) {
wallet.doPayment(detail.H_contract)
- .then((r) => {
- sendResponse({
- success: true,
- payUrl: r.payUrl,
- payReq: r.payReq
- });
- })
- .catch((e) => {
- sendResponse({success: false, error: e.message});
- });
+ .then((r) => {
+ sendResponse({
+ success: true,
+ payUrl: r.payUrl,
+ payReq: r.payReq
+ });
+ })
+ .catch((e) => {
+ sendResponse({success: false, error: e.message});
+ });
// async sendResponse
return true;
}
};
}
+class ChromeBadge {
+ setText(s: string) {
+ chrome.browserAction.setBadgeText({text: s});
+ }
+
+ setColor(c: string) {
+ chrome.browserAction.setBadgeBackgroundColor({color: c});
+ }
+}
+
function wxMain() {
chrome.browserAction.setBadgeText({text: ""});
openTalerDb().then((db) => {
- let wallet = new Wallet(db, undefined, undefined);
+ let http = new BrowserHttpLib();
+ let badge = new ChromeBadge();
+ let wallet = new Wallet(db, http, badge);
let handlers = makeHandlers(wallet);
wallet.updateBadge();
chrome.runtime.onMessage.addListener(
diff --git a/extension/background/wallet.js b/extension/background/wallet.js
index f0337818c..cc2154a8a 100644
--- a/extension/background/wallet.js
+++ b/extension/background/wallet.js
@@ -20,7 +20,6 @@
* @author Florian Dold
*/
/// <reference path="../decl/urijs/URIjs.d.ts" />
-/// <reference path="../decl/chrome/chrome.d.ts" />
"use strict";
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
@@ -264,7 +263,7 @@ class Wallet {
form.append(req.field_mint, req.mint);
// TODO: set bank-specified fields.
let mintBaseUrl = canonicalizeBaseUrl(req.mint);
- return httpPostForm(req.post_url, form)
+ return this.http.postForm(req.post_url, form)
.then((hresp) => {
let resp = {
status: hresp.status,
@@ -351,7 +350,7 @@ class Wallet {
wd.reserve_sig = pc.withdrawSig;
wd.coin_ev = pc.coinEv;
let reqUrl = URI("reserve/withdraw").absoluteTo(r.mint_base_url);
- return httpPostJson(reqUrl, wd);
+ return this.http.postJson(reqUrl, wd);
})
.then(resp => {
if (resp.status != 200) {
@@ -381,8 +380,8 @@ class Wallet {
return n;
}
function doBadge(n) {
- chrome.browserAction.setBadgeText({ text: "" + n });
- chrome.browserAction.setBadgeBackgroundColor({ color: "#0F0" });
+ this.badge.setText(n.toString());
+ this.badge.setColor("#0F0");
}
Query(this.db)
.iter("coins")
@@ -447,7 +446,7 @@ class Wallet {
.then((reserve) => {
let reqUrl = URI("reserve/status").absoluteTo(mint.baseUrl);
reqUrl.query({ 'reserve_pub': reservePubStr });
- return httpGet(reqUrl).then(resp => {
+ return this.http.get(reqUrl).then(resp => {
if (resp.status != 200) {
throw Error();
}
@@ -470,7 +469,7 @@ class Wallet {
*/
updateMintFromUrl(baseUrl) {
let reqUrl = URI("keys").absoluteTo(baseUrl);
- return httpGet(reqUrl).then((resp) => {
+ return this.http.get(reqUrl).then((resp) => {
if (resp.status != 200) {
throw Error("/keys request failed");
}
diff --git a/extension/background/wallet.ts b/extension/background/wallet.ts
index 6479b961a..87e41e854 100644
--- a/extension/background/wallet.ts
+++ b/extension/background/wallet.ts
@@ -23,7 +23,6 @@
/// <reference path="../decl/urijs/URIjs.d.ts" />
-/// <reference path="../decl/chrome/chrome.d.ts" />
"use strict";
@Checkable.Class
@@ -196,12 +195,22 @@ function canonicalizeBaseUrl(url) {
return x.href()
}
+
interface HttpRequestLibrary {
+ req(method: string,
+ url: string|uri.URI,
+ options?: any): Promise<HttpResponse>;
+
+ get(url: string|uri.URI): Promise<HttpResponse>;
+ postJson(url: string|uri.URI, body): Promise<HttpResponse>;
+
+ postForm(url: string|uri.URI, form): Promise<HttpResponse>;
}
interface Badge {
-
+ setText(s: string): void;
+ setColor(c: string): void;
}
@@ -435,7 +444,7 @@ class Wallet {
// TODO: set bank-specified fields.
let mintBaseUrl = canonicalizeBaseUrl(req.mint);
- return httpPostForm(req.post_url, form)
+ return this.http.postForm(req.post_url, form)
.then((hresp) => {
let resp: ConfirmReserveResponse = {
status: hresp.status,
@@ -537,7 +546,7 @@ class Wallet {
wd.reserve_sig = pc.withdrawSig;
wd.coin_ev = pc.coinEv;
let reqUrl = URI("reserve/withdraw").absoluteTo(r.mint_base_url);
- return httpPostJson(reqUrl, wd);
+ return this.http.postJson(reqUrl, wd);
})
.then(resp => {
if (resp.status != 200) {
@@ -572,8 +581,8 @@ class Wallet {
}
function doBadge(n) {
- chrome.browserAction.setBadgeText({text: "" + n});
- chrome.browserAction.setBadgeBackgroundColor({color: "#0F0"});
+ this.badge.setText(n.toString());
+ this.badge.setColor("#0F0");
}
Query(this.db)
@@ -647,7 +656,7 @@ class Wallet {
.then((reserve) => {
let reqUrl = URI("reserve/status").absoluteTo(mint.baseUrl);
reqUrl.query({'reserve_pub': reservePubStr});
- return httpGet(reqUrl).then(resp => {
+ return this.http.get(reqUrl).then(resp => {
if (resp.status != 200) {
throw Error();
}
@@ -671,7 +680,7 @@ class Wallet {
*/
updateMintFromUrl(baseUrl) {
let reqUrl = URI("keys").absoluteTo(baseUrl);
- return httpGet(reqUrl).then((resp) => {
+ return this.http.get(reqUrl).then((resp) => {
if (resp.status != 200) {
throw Error("/keys request failed");
}