aboutsummaryrefslogtreecommitdiff
path: root/extension/lib/wallet/wxmessaging.js
blob: 4218d6aa1461786e1d73d5db0c14b477f63e978c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 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/>
 */
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, checkable_1;
    var ChromeBadge;
    function makeHandlers(db, wallet) {
        return (_a = {},
            _a["balances"] = function (detail) {
                return wallet.getBalances();
            },
            _a["dump-db"] = function (detail) {
                return db_1.exportDb(db);
            },
            _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();
                }
                db_1.deleteDb();
                chrome.browserAction.setBadgeText({ text: "" });
                console.log("reset done");
                // Response is synchronous
                return Promise.resolve({});
            },
            _a["create-reserve"] = function (detail) {
                var d = {
                    mint: detail.mint,
                    amount: detail.amount,
                };
                var req = wallet_1.CreateReserveRequest.checked(d);
                return wallet.createReserve(req);
            },
            _a["confirm-reserve"] = function (detail) {
                // TODO: make it a checkable
                var d = {
                    reservePub: detail.reservePub
                };
                var req = wallet_1.ConfirmReserveRequest.checked(d);
                return wallet.confirmReserve(req);
            },
            _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, detail: detail });
                    }
                    else {
                        throw e;
                    }
                }
                return wallet.confirmPay(offer);
            },
            _a["execute-payment"] = function (detail) {
                return wallet.executePayment(detail.H_contract);
            },
            _a["get-history"] = function (detail) {
                // TODO: limit history length
                return wallet.getHistory();
            },
            _a
        );
        var _a;
    }
    function dispatch(handlers, req, sendResponse) {
        if (req.type in handlers) {
            Promise
                .resolve()
                .then(function () {
                var p = handlers[req.type](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: "" });
        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(db, wallet);
            chrome.runtime.onMessage.addListener(function (req, sender, sendResponse) {
                return dispatch(handlers, req, sendResponse);
            });
        })
            .catch(function (e) {
            console.error("could not initialize wallet messaging");
            console.error(e);
        });
    }
    exports_1("wxMain", wxMain);
    return {
        setters:[
            function (wallet_1_1) {
                wallet_1 = wallet_1_1;
            },
            function (db_1_1) {
                db_1 = db_1_1;
            },
            function (http_1_1) {
                http_1 = http_1_1;
            },
            function (checkable_1_1) {
                checkable_1 = checkable_1_1;
            }],
        execute: function() {
            "use strict";
            ChromeBadge = (function () {
                function ChromeBadge() {
                }
                ChromeBadge.prototype.setText = function (s) {
                    chrome.browserAction.setBadgeText({ text: s });
                };
                ChromeBadge.prototype.setColor = function (c) {
                    chrome.browserAction.setBadgeBackgroundColor({ color: c });
                };
                return ChromeBadge;
            }());
        }
    }
});
//# sourceMappingURL=wxmessaging.js.map