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
|
/*
This file is part of TALER
(C) 2016 Inria
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/>
*/
/**
* Show contents of the wallet as a tree.
*
* @author Florian Dold
*/
import { IExchangeInfo } from "../lib/wallet/types";
import { ReserveRecord, Coin, PreCoin, Denomination } from "../lib/wallet/types";
import { ImplicitStateComponent, StateHolder } from "../lib/components";
import {
getReserves, getExchanges, getCoins, getPreCoins,
refresh
} from "../lib/wallet/wxApi";
import { prettyAmount, abbrev } from "../lib/wallet/renderHtml";
import { getTalerStampDate } from "../lib/wallet/helpers";
interface ReserveViewProps {
reserve: ReserveRecord;
}
class ReserveView extends React.Component<ReserveViewProps, void> {
render(): JSX.Element {
let r: ReserveRecord = this.props.reserve;
return (
<div className="tree-item">
<ul>
<li>Key: {r.reserve_pub}</li>
<li>Created: {(new Date(r.created * 1000).toString())}</li>
<li>Current: {r.current_amount ? prettyAmount(r.current_amount!) : "null"}</li>
<li>Requested: {prettyAmount(r.requested_amount)}</li>
<li>Confirmed: {r.confirmed}</li>
</ul>
</div>
);
}
}
interface ReserveListProps {
exchangeBaseUrl: string;
}
interface ToggleProps {
expanded: StateHolder<boolean>;
}
class Toggle extends ImplicitStateComponent<ToggleProps> {
renderButton() {
let show = () => {
this.props.expanded(true);
this.setState({});
};
let hide = () => {
this.props.expanded(false);
this.setState({});
};
if (this.props.expanded()) {
return <button onClick={hide}>hide</button>;
}
return <button onClick={show}>show</button>;
}
render() {
return (
<div style={{display: "inline"}}>
{this.renderButton()}
{this.props.expanded() ? this.props.children : []}
</div>);
}
}
interface CoinViewProps {
coin: Coin;
}
interface RefreshDialogProps {
coin: Coin;
}
class RefreshDialog extends ImplicitStateComponent<RefreshDialogProps> {
refreshRequested = this.makeState<boolean>(false);
render(): JSX.Element {
if (!this.refreshRequested()) {
return (
<div style={{display: "inline"}}>
<button onClick={() => this.refreshRequested(true)}>refresh</button>
</div>
);
}
return (
<div>
Refresh amount: <input type="text" size={10} />
<button onClick={() => refresh(this.props.coin.coinPub)}>ok</button>
<button onClick={() => this.refreshRequested(false)}>cancel</button>
</div>
);
}
}
class CoinView extends React.Component<CoinViewProps, void> {
render() {
let c = this.props.coin;
return (
<div className="tree-item">
<ul>
<li>Key: {c.coinPub}</li>
<li>Current amount: {prettyAmount(c.currentAmount)}</li>
<li>Denomination: {abbrev(c.denomPub, 20)}</li>
<li>Suspended: {(c.suspended || false).toString()}</li>
<li><RefreshDialog coin={c} /></li>
</ul>
</div>
);
}
}
interface PreCoinViewProps {
precoin: PreCoin;
}
class PreCoinView extends React.Component<PreCoinViewProps, void> {
render() {
let c = this.props.precoin;
return (
<div className="tree-item">
<ul>
<li>Key: {c.coinPub}</li>
</ul>
</div>
);
}
}
interface CoinListProps {
exchangeBaseUrl: string;
}
class CoinList extends ImplicitStateComponent<CoinListProps> {
coins = this.makeState<Coin[] | null>(null);
expanded = this.makeState<boolean>(false);
constructor(props: CoinListProps) {
super(props);
this.update(props);
}
async update(props: CoinListProps) {
let coins = await getCoins(props.exchangeBaseUrl);
this.coins(coins);
}
componentWillReceiveProps(newProps: CoinListProps) {
this.update(newProps);
}
render(): JSX.Element {
if (!this.coins()) {
return <div>...</div>;
}
return (
<div className="tree-item">
Coins ({this.coins() !.length.toString()})
{" "}
<Toggle expanded={this.expanded}>
{this.coins() !.map((c) => <CoinView coin={c} />)}
</Toggle>
</div>
);
}
}
interface PreCoinListProps {
exchangeBaseUrl: string;
}
class PreCoinList extends ImplicitStateComponent<PreCoinListProps> {
precoins = this.makeState<PreCoin[] | null>(null);
expanded = this.makeState<boolean>(false);
constructor(props: PreCoinListProps) {
super(props);
this.update();
}
async update() {
let precoins = await getPreCoins(this.props.exchangeBaseUrl);
this.precoins(precoins);
}
render(): JSX.Element {
if (!this.precoins()) {
return <div>...</div>;
}
return (
<div className="tree-item">
Pre-Coins ({this.precoins() !.length.toString()})
{" "}
<Toggle expanded={this.expanded}>
{this.precoins() !.map((c) => <PreCoinView precoin={c} />)}
</Toggle>
</div>
);
}
}
interface DenominationListProps {
exchange: IExchangeInfo;
}
interface ExpanderTextProps {
text: string;
}
class ExpanderText extends ImplicitStateComponent<ExpanderTextProps> {
expanded = this.makeState<boolean>(false);
textArea: any = undefined;
componentDidUpdate() {
if (this.expanded() && this.textArea) {
this.textArea.focus();
this.textArea.scrollTop = 0;
}
}
render(): JSX.Element {
if (!this.expanded()) {
return (
<span onClick={() => { this.expanded(true); }}>
{(this.props.text.length <= 10)
? this.props.text
: (
<span>
{this.props.text.substring(0,10)}
<span style={{textDecoration: "underline"}}>...</span>
</span>
)
}
</span>
);
}
return (
<textarea
readOnly
style={{display: "block"}}
onBlur={() => this.expanded(false)}
ref={(e) => this.textArea = e}>
{this.props.text}
</textarea>
);
}
}
class DenominationList extends ImplicitStateComponent<DenominationListProps> {
expanded = this.makeState<boolean>(false);
renderDenom(d: Denomination) {
return (
<div className="tree-item">
<ul>
<li>Value: {prettyAmount(d.value)}</li>
<li>Withdraw fee: {prettyAmount(d.fee_withdraw)}</li>
<li>Refresh fee: {prettyAmount(d.fee_refresh)}</li>
<li>Deposit fee: {prettyAmount(d.fee_deposit)}</li>
<li>Refund fee: {prettyAmount(d.fee_refund)}</li>
<li>Start: {getTalerStampDate(d.stamp_start)!.toString()}</li>
<li>Withdraw expiration: {getTalerStampDate(d.stamp_expire_withdraw)!.toString()}</li>
<li>Legal expiration: {getTalerStampDate(d.stamp_expire_legal)!.toString()}</li>
<li>Deposit expiration: {getTalerStampDate(d.stamp_expire_deposit)!.toString()}</li>
<li>Denom pub: <ExpanderText text={d.denom_pub} /></li>
</ul>
</div>
);
}
render(): JSX.Element {
return (
<div className="tree-item">
Denominations ({this.props.exchange.active_denoms.length.toString()})
{" "}
<Toggle expanded={this.expanded}>
{this.props.exchange.active_denoms.map((d) => this.renderDenom(d))}
</Toggle>
</div>
);
}
}
class ReserveList extends ImplicitStateComponent<ReserveListProps> {
reserves = this.makeState<ReserveRecord[] | null>(null);
expanded = this.makeState<boolean>(false);
constructor(props: ReserveListProps) {
super(props);
this.update();
}
async update() {
let reserves = await getReserves(this.props.exchangeBaseUrl);
this.reserves(reserves);
}
render(): JSX.Element {
if (!this.reserves()) {
return <div>...</div>;
}
return (
<div className="tree-item">
Reserves ({this.reserves() !.length.toString()})
{" "}
<Toggle expanded={this.expanded}>
{this.reserves() !.map((r) => <ReserveView reserve={r} />)}
</Toggle>
</div>
);
}
}
interface ExchangeProps {
exchange: IExchangeInfo;
}
class ExchangeView extends React.Component<ExchangeProps, void> {
render(): JSX.Element {
let e = this.props.exchange;
return (
<div className="tree-item">
<ul>
<li>Exchange Base Url: {this.props.exchange.baseUrl}</li>
<li>Master public key: <ExpanderText text={this.props.exchange.masterPublicKey} /></li>
</ul>
<DenominationList exchange={e} />
<ReserveList exchangeBaseUrl={this.props.exchange.baseUrl} />
<CoinList exchangeBaseUrl={this.props.exchange.baseUrl} />
<PreCoinList exchangeBaseUrl={this.props.exchange.baseUrl} />
</div>
);
}
}
interface ExchangesListState {
exchanges?: IExchangeInfo[];
}
class ExchangesList extends React.Component<any, ExchangesListState> {
constructor() {
super();
let port = chrome.runtime.connect();
port.onMessage.addListener((msg: any) => {
if (msg.notify) {
console.log("got notified");
this.update();
}
});
this.update();
this.state = {} as any;
}
async update() {
let exchanges = await getExchanges();
console.log("exchanges: ", exchanges);
this.setState({ exchanges });
}
render(): JSX.Element {
let exchanges = this.state.exchanges;
if (!exchanges) {
return <span>...</span>;
}
return (
<div className="tree-item">
Exchanges ({exchanges.length.toString()}):
{exchanges.map(e => <ExchangeView exchange={e} />)}
</div>
);
}
}
export function main() {
ReactDOM.render(<ExchangesList />, document.getElementById("container")!);
}
|