aboutsummaryrefslogtreecommitdiff
path: root/extension/lib/wallet/wxmessaging.js
diff options
context:
space:
mode:
authorFlorian Dold <florian.dold@gmail.com>2016-02-17 15:56:48 +0100
committerFlorian Dold <florian.dold@gmail.com>2016-02-17 15:56:48 +0100
commit874d083ec371441d2f2b31281652fd8f82cc5489 (patch)
tree27747e9a675247a75d3f29c89516be717163d463 /extension/lib/wallet/wxmessaging.js
parent0c760bc2a1459cc7ec7fd22ae39ca0a8201df9b5 (diff)
simplify wallet message dispatching and error handling
Diffstat (limited to 'extension/lib/wallet/wxmessaging.js')
-rw-r--r--extension/lib/wallet/wxmessaging.js171
1 files changed, 77 insertions, 94 deletions
diff --git a/extension/lib/wallet/wxmessaging.js b/extension/lib/wallet/wxmessaging.js
index c70bfb5a0..6310d6cd4 100644
--- a/extension/lib/wallet/wxmessaging.js
+++ b/extension/lib/wallet/wxmessaging.js
@@ -13,34 +13,20 @@
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/>
*/
-System.register(["./wallet", "./db", "./http"], function(exports_1, context_1) {
+System.register(["./wallet", "./db", "./http", "./checkable"], function(exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
- var wallet_1, db_1, http_1;
+ var wallet_1, db_1, http_1, checkable_1;
var ChromeBadge;
- /**
- * Messaging for the WebExtensions wallet. Should contain
- * parts that are specific for WebExtensions, but as little business
- * logic as possible.
- *
- * @author Florian Dold
- */
- function makeHandlers(wallet) {
+ function makeHandlers(db, wallet) {
return (_a = {},
- _a["balances"] = function (db, detail, sendResponse) {
- wallet.getBalances()
- .then(sendResponse)
- .catch(function (e) {
- console.log("exception during 'balances'");
- console.error(e.stack);
- });
- return true;
+ _a["balances"] = function (detail) {
+ return wallet.getBalances();
},
- _a["dump-db"] = function (db, detail, sendResponse) {
- db_1.exportDb(db).then(sendResponse);
- return true;
+ _a["dump-db"] = function (detail) {
+ return db_1.exportDb(db);
},
- _a["reset"] = function (db, detail, sendResponse) {
+ _a["reset"] = function (detail) {
var tx = db.transaction(db.objectStoreNames, 'readwrite');
for (var i = 0; i < db.objectStoreNames.length; i++) {
tx.objectStore(db.objectStoreNames[i]).clear();
@@ -49,108 +35,102 @@ System.register(["./wallet", "./db", "./http"], function(exports_1, context_1) {
chrome.browserAction.setBadgeText({ text: "" });
console.log("reset done");
// Response is synchronous
- return false;
+ return Promise.resolve({});
},
- _a["create-reserve"] = function (db, detail, sendResponse) {
+ _a["create-reserve"] = function (detail) {
var d = {
mint: detail.mint,
amount: detail.amount,
};
var req = wallet_1.CreateReserveRequest.checked(d);
- wallet.createReserve(req)
- .then(function (resp) {
- sendResponse(resp);
- })
- .catch(function (e) {
- sendResponse({ error: "exception" });
- console.error("exception during 'create-reserve'");
- console.error(e.stack);
- });
- return true;
+ return wallet.createReserve(req);
},
- _a["confirm-reserve"] = function (db, detail, sendResponse) {
+ _a["confirm-reserve"] = function (detail) {
// TODO: make it a checkable
var d = {
reservePub: detail.reservePub
};
var req = wallet_1.ConfirmReserveRequest.checked(d);
- wallet.confirmReserve(req)
- .then(function (resp) {
- sendResponse(resp);
- })
- .catch(function (e) {
- sendResponse({ error: "exception" });
- console.error("exception during 'confirm-reserve'");
- console.error(e.stack);
- });
- return true;
+ return wallet.confirmReserve(req);
},
- _a["confirm-pay"] = function (db, detail, sendResponse) {
- console.log("in confirm-pay handler");
- var offer = wallet_1.Offer.checked(detail.offer);
- wallet.confirmPay(offer)
- .then(function (r) {
- sendResponse(r);
- })
- .catch(function (e) {
- console.error("exception during 'confirm-pay'");
- console.error(e.stack);
- sendResponse({ error: e.message });
- });
- return true;
+ _a["confirm-pay"] = function (detail) {
+ var offer;
+ try {
+ offer = wallet_1.Offer.checked(detail.offer);
+ }
+ catch (e) {
+ if (e instanceof checkable_1.Checkable.SchemaError) {
+ console.error("schema error:", e.message);
+ return Promise.resolve({ error: "invalid contract", hint: e.message });
+ }
+ else {
+ throw e;
+ }
+ }
+ return wallet.confirmPay(offer);
},
- _a["execute-payment"] = function (db, detail, sendResponse) {
- wallet.executePayment(detail.H_contract)
- .then(function (r) {
- sendResponse(r);
- })
- .catch(function (e) {
- console.error("exception during 'execute-payment'");
- console.error(e.stack);
- sendResponse({ error: e.message });
- });
- // async sendResponse
- return true;
+ _a["execute-payment"] = function (detail) {
+ return wallet.executePayment(detail.H_contract);
},
- _a["get-history"] = function (db, detail, sendResponse) {
+ _a["get-history"] = function (detail) {
// TODO: limit history length
- wallet.getHistory()
- .then(function (h) {
- sendResponse(h);
- })
- .catch(function (e) {
- console.error("exception during 'get-history'");
- console.error(e.stack);
- });
- return true;
- },
- _a["error-fatal"] = function (db, detail, sendResponse) {
- console.log("fatal error from page", detail.url);
+ return wallet.getHistory();
},
_a
);
var _a;
}
+ function dispatch(handlers, db, req, sendResponse) {
+ if (req.type in handlers) {
+ Promise
+ .resolve()
+ .then(function () {
+ var p = handlers[req.type](db, req.detail);
+ return p.then(function (r) {
+ sendResponse(r);
+ });
+ })
+ .catch(function (e) {
+ console.log("exception during wallet handler'");
+ console.error(e.stack);
+ sendResponse({
+ error: "exception",
+ hint: e.message,
+ stack: e.stack.toString()
+ });
+ });
+ // The sendResponse call is async
+ return true;
+ }
+ else {
+ console.error("Request type " + JSON.stringify(req) + " unknown, req " + req.type);
+ sendResponse({ error: "request unknown" });
+ // The sendResponse call is sync
+ return false;
+ }
+ }
function wxMain() {
chrome.browserAction.setBadgeText({ text: "" });
- db_1.openTalerDb()
+ Promise.resolve()
+ .then(function () {
+ return db_1.openTalerDb();
+ })
+ .catch(function (e) {
+ console.error("could not open database");
+ console.error(e);
+ })
.then(function (db) {
var http = new http_1.BrowserHttpLib();
var badge = new ChromeBadge();
var wallet = new wallet_1.Wallet(db, http, badge);
- var handlers = makeHandlers(wallet);
- chrome.runtime.onMessage.addListener(function (req, sender, onresponse) {
- if (req.type in handlers) {
- return handlers[req.type](db, req.detail, onresponse);
- }
- console.error("Request type " + JSON.stringify(req) + " unknown, req " + req.type);
- onresponse({ error: "request unknown" });
- return false;
+ var handlers = makeHandlers(db, wallet);
+ chrome.runtime.onMessage.addListener(function (req, sender, sendResponse) {
+ return dispatch(handlers, db, req, sendResponse);
});
})
.catch(function (e) {
- console.error("could not open database:");
- console.error(e.stack);
+ console.error("could not initialize wallet messaging");
+ console.error(e);
});
}
exports_1("wxMain", wxMain);
@@ -164,6 +144,9 @@ System.register(["./wallet", "./db", "./http"], function(exports_1, context_1) {
},
function (http_1_1) {
http_1 = http_1_1;
+ },
+ function (checkable_1_1) {
+ checkable_1 = checkable_1_1;
}],
execute: function() {
"use strict";