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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
|
/*
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, see <http://www.gnu.org/licenses/>
*/
/**
* Popup shown to the user when they click
* the Taler browser action button.
*
* @author Florian Dold
*/
"use strict";
import {substituteFulfillmentUrl} from "../lib/wallet/helpers";
import BrowserClickedEvent = chrome.browserAction.BrowserClickedEvent;
import {HistoryRecord, HistoryLevel} from "../lib/wallet/wallet";
import {
AmountJson, WalletBalance, Amounts,
WalletBalanceEntry
} from "../lib/wallet/types";
import {abbrev, prettyAmount} from "../lib/wallet/renderHtml";
declare var i18n: any;
function onUpdateNotification(f: () => void) {
let port = chrome.runtime.connect({name: "notifications"});
port.onMessage.addListener((msg, port) => {
f();
});
}
class Router extends preact.Component<any,any> {
static setRoute(s: string): void {
window.location.hash = s;
}
static getRoute(): string {
// Omit the '#' at the beginning
return window.location.hash.substring(1);
}
static onRoute(f: any): () => void {
Router.routeHandlers.push(f);
return () => {
let i = Router.routeHandlers.indexOf(f);
this.routeHandlers = this.routeHandlers.splice(i, 1);
}
}
static routeHandlers: any[] = [];
componentWillMount() {
console.log("router mounted");
window.onhashchange = () => {
this.setState({});
for (let f of Router.routeHandlers) {
f();
}
}
}
componentWillUnmount() {
console.log("router unmounted");
}
render(props: any, state: any): JSX.Element {
let route = window.location.hash.substring(1);
console.log("rendering route", route);
let defaultChild: JSX.Element|null = null;
for (let child of props.children) {
if (child.attributes["default"]) {
defaultChild = child;
}
if (child.attributes["route"] == route) {
return <div>{child}</div>;
}
}
if (defaultChild == null) {
throw Error("unknown route");
}
console.log("rendering default route");
Router.setRoute(defaultChild.attributes["route"]);
return <div>{defaultChild}</div>;
}
}
export function main() {
console.log("popup main");
let el = (
<div>
<WalletNavBar />
<div style="margin:1em">
<Router>
<WalletBalanceView route="/balance" default/>
<WalletHistory route="/history"/>
<WalletDebug route="/debug"/>
</Router>
</div>
</div>
);
preact.render(el, document.getElementById("content")!);
}
interface TabProps extends preact.ComponentProps {
target: string;
}
function Tab(props: TabProps) {
let cssClass = "";
if (props.target == Router.getRoute()) {
cssClass = "active";
}
let onClick = (e: Event) => {
Router.setRoute(props.target);
e.preventDefault();
};
return (
<a onClick={onClick} href={props.target} className={cssClass}>
{props.children}
</a>
);
}
class WalletNavBar extends preact.Component<any,any> {
cancelSubscription: any;
componentWillMount() {
this.cancelSubscription = Router.onRoute(() => {
this.setState({});
});
}
componentWillUnmount() {
if (this.cancelSubscription) {
this.cancelSubscription();
}
}
render() {
console.log("rendering nav bar");
return (
<div class="nav" id="header">
<Tab target="/balance">
Balance
</Tab>
<Tab target="/history">
History
</Tab>
<Tab target="/debug">
Debug
</Tab>
</div>);
}
}
function ExtensionLink(props: any) {
let onClick = (e: Event) => {
chrome.tabs.create({
"url": chrome.extension.getURL(props.target)
});
e.preventDefault();
};
return (
<a onClick={onClick} href={props.target}>
{props.children}
</a>)
}
class WalletBalanceView extends preact.Component<any, any> {
balance: WalletBalance;
gotError = false;
componentWillMount() {
this.updateBalance();
onUpdateNotification(() => this.updateBalance());
}
updateBalance() {
chrome.runtime.sendMessage({type: "balances"}, (resp) => {
if (resp.error) {
this.gotError = true;
console.error("could not retrieve balances", resp);
this.setState({});
return;
}
this.gotError = false;
console.log("got wallet", resp);
this.balance = resp;
this.setState({});
});
}
renderEmpty(): JSX.Element {
let helpLink = (
<ExtensionLink target="pages/help/empty-wallet.html">
help
</ExtensionLink>
);
return <div>You have no balance to show. Need some
{" "}{helpLink}{" "}
getting started?</div>;
}
formatPending(entry: WalletBalanceEntry): JSX.Element {
let incoming: JSX.Element | undefined;
let payment: JSX.Element | undefined;
console.log("available: ", entry.pendingIncoming ? prettyAmount(entry.available) : null);
console.log("incoming: ", entry.pendingIncoming ? prettyAmount(entry.pendingIncoming) : null);
if (Amounts.isNonZero(entry.pendingIncoming)) {
incoming = (
<span>
<span style="color: darkgreen">
{"+"}
{prettyAmount(entry.pendingIncoming)}
</span>
{" "}
incoming
</span>);
}
if (Amounts.isNonZero(entry.pendingPayment)) {
payment = (
<span>
<span style="color: darkblue">
{prettyAmount(entry.pendingPayment)}
</span>
{" "}
being spent
</span>);
}
let l = [incoming, payment].filter((x) => x !== undefined);
if (l.length == 0) {
return <span />;
}
if (l.length == 1) {
return <span>({l})</span>
}
return <span>({l[0]}, {l[1]})</span>;
}
render(): JSX.Element {
let wallet = this.balance;
if (this.gotError) {
return i18n`Error: could not retrieve balance information.`;
}
if (!wallet) {
return this.renderEmpty();
}
console.log(wallet);
let listing = Object.keys(wallet).map((key) => {
let entry: WalletBalanceEntry = wallet[key];
return (
<p>
{prettyAmount(entry.available)}
{" "}
{this.formatPending(entry)}
</p>
);
});
if (listing.length > 0) {
return <div>{listing}</div>;
}
return this.renderEmpty();
}
}
function formatHistoryItem(historyItem: HistoryRecord) {
const d = historyItem.detail;
const t = historyItem.timestamp;
console.log("hist item", historyItem);
switch (historyItem.type) {
case "create-reserve":
return (
<p>
{i18n.parts`Bank requested reserve (${abbrev(d.reservePub)}) for ${prettyAmount(
d.requestedAmount)}.`}
</p>
);
case "confirm-reserve": {
// FIXME: eventually remove compat fix
let exchange = d.exchangeBaseUrl ? URI(d.exchangeBaseUrl).host() : "??";
let amount = prettyAmount(d.requestedAmount);
let pub = abbrev(d.reservePub);
return (
<p>
{i18n.parts`Started to withdraw ${amount} from ${exchange} (${pub}).`}
</p>
);
}
case "offer-contract": {
let link = chrome.extension.getURL("view-contract.html");
let linkElem = <a href={link}>{abbrev(d.contractHash)}</a>;
let merchantElem = <em>{abbrev(d.merchantName, 15)}</em>;
return (
<p>
{i18n.parts`Merchant ${merchantElem} offered contract ${linkElem}.`}
</p>
);
}
case "depleted-reserve": {
let exchange = d.exchangeBaseUrl ? URI(d.exchangeBaseUrl).host() : "??";
let amount = prettyAmount(d.requestedAmount);
let pub = abbrev(d.reservePub);
return (<p>
{i18n.parts`Withdrew ${amount} from ${exchange} (${pub}).`}
</p>);
}
case "pay": {
let url = substituteFulfillmentUrl(d.fulfillmentUrl,
{H_contract: d.contractHash});
let merchantElem = <em>{abbrev(d.merchantName, 15)}</em>;
let fulfillmentLinkElem = <a href={url} onClick={openTab(url)}>view product</a>;
return (
<p>
{i18n.parts`Paid ${prettyAmount(d.amount)} to merchant ${merchantElem}. (${fulfillmentLinkElem})`}
</p>);
}
default:
return (<p>i18n`Unknown event (${historyItem.type})`</p>);
}
}
class WalletHistory extends preact.Component<any, any> {
myHistory: any[];
gotError = false;
componentWillMount() {
this.update();
onUpdateNotification(() => this.update());
}
update() {
chrome.runtime.sendMessage({type: "get-history"}, (resp) => {
console.log("got history response");
if (resp.error) {
this.gotError = true;
console.error("could not retrieve history", resp);
this.setState({});
return;
}
this.gotError = false;
console.log("got history", resp.history);
this.myHistory = resp.history;
this.setState({});
});
}
render(): JSX.Element {
console.log("rendering history");
let history: HistoryRecord[] = this.myHistory;
if (this.gotError) {
return i18n`Error: could not retrieve event history`;
}
if (!history) {
// We're not ready yet
return <span />;
}
let subjectMemo: {[s: string]: boolean} = {};
let listing: any[] = [];
for (let record of history.reverse()) {
if (record.subjectId && subjectMemo[record.subjectId]) {
continue;
}
if (record.level != undefined && record.level < HistoryLevel.User) {
continue;
}
subjectMemo[record.subjectId as string] = true;
let item = (
<div className="historyItem">
<div className="historyDate">
{(new Date(record.timestamp)).toString()}
</div>
{formatHistoryItem(record)}
</div>
);
listing.push(item);
}
if (listing.length > 0) {
return <div className="container">{listing}</div>;
}
return <p>{i18n`Your wallet has no events recorded.`}</p>
}
}
function reload() {
try {
chrome.runtime.reload();
window.close();
} catch (e) {
// Functionality missing in firefox, ignore!
}
}
function confirmReset() {
if (confirm("Do you want to IRREVOCABLY DESTROY everything inside your" +
" wallet and LOSE ALL YOUR COINS?")) {
chrome.runtime.sendMessage({type: "reset"});
window.close();
}
}
function WalletDebug(props: any) {
return (<div>
<p>Debug tools:</p>
<button onClick={openExtensionPage("popup/popup.html")}>
wallet tab
</button>
<button onClick={openExtensionPage("pages/show-db.html")}>
show db
</button>
<button onClick={openExtensionPage("pages/tree.html")}>
show tree
</button>
<br />
<button onClick={confirmReset}>
reset
</button>
<button onClick={reload}>
reload chrome extension
</button>
</div>);
}
function openExtensionPage(page: string) {
return function() {
chrome.tabs.create({
"url": chrome.extension.getURL(page)
});
}
}
function openTab(page: string) {
return function() {
chrome.tabs.create({
"url": page
});
}
}
|