2015-12-25 22:42:14 +01:00
|
|
|
/*
|
|
|
|
This file is part of TALER
|
2016-01-26 17:21:17 +01:00
|
|
|
(C) 2015-2016 GNUnet e.V.
|
2015-12-25 22:42:14 +01:00
|
|
|
|
|
|
|
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
|
2016-07-07 17:59:29 +02:00
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2015-12-25 22:42:14 +01:00
|
|
|
*/
|
|
|
|
|
2016-03-01 19:46:20 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Page shown to the user to confirm creation
|
|
|
|
* of a reserve, usually requested by the bank.
|
|
|
|
*
|
|
|
|
* @author Florian Dold
|
|
|
|
*/
|
|
|
|
|
2016-02-11 11:29:57 +01:00
|
|
|
import {amountToPretty, canonicalizeBaseUrl} from "../lib/wallet/helpers";
|
|
|
|
import {AmountJson, CreateReserveResponse} from "../lib/wallet/types";
|
2016-02-22 23:13:28 +01:00
|
|
|
import {ReserveCreationInfo, Amounts} from "../lib/wallet/types";
|
2016-02-18 22:50:17 +01:00
|
|
|
import {Denomination} from "../lib/wallet/types";
|
2016-02-19 04:23:00 +01:00
|
|
|
import {getReserveCreationInfo} from "../lib/wallet/wxApi";
|
2016-10-12 02:55:53 +02:00
|
|
|
import {ImplicitStateComponent, StateHolder} from "../lib/components";
|
2016-02-11 11:29:57 +01:00
|
|
|
|
2015-12-20 20:34:20 +01:00
|
|
|
"use strict";
|
|
|
|
|
2016-10-07 14:34:31 +02:00
|
|
|
let h = preact.h;
|
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
function delay<T>(delayMs: number, value: T): Promise<T> {
|
|
|
|
return new Promise<T>((resolve, reject) => {
|
|
|
|
setTimeout(() => resolve(value), delayMs);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
class EventTrigger {
|
|
|
|
triggerResolve: any;
|
|
|
|
triggerPromise: Promise<boolean>;
|
|
|
|
|
|
|
|
constructor() {
|
|
|
|
this.reset();
|
2016-02-11 11:29:57 +01:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
private reset() {
|
|
|
|
this.triggerPromise = new Promise<boolean>((resolve, reject) => {
|
|
|
|
this.triggerResolve = resolve;
|
|
|
|
});
|
2015-12-20 20:34:20 +01:00
|
|
|
}
|
2016-02-15 11:29:58 +01:00
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
trigger() {
|
|
|
|
this.triggerResolve(false);
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
async wait(delayMs: number): Promise<boolean> {
|
|
|
|
return await Promise.race([this.triggerPromise, delay(delayMs, true)]);
|
2016-02-15 11:29:58 +01:00
|
|
|
}
|
2016-02-11 11:29:57 +01:00
|
|
|
}
|
2015-12-20 20:34:20 +01:00
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
|
|
|
|
function renderReserveCreationDetails(rci: ReserveCreationInfo|null) {
|
|
|
|
if (!rci) {
|
2016-10-10 03:32:18 +02:00
|
|
|
return <p>
|
|
|
|
Details will be displayed when a valid exchange provider URL is entered.</p>
|
2016-10-07 17:10:22 +02:00
|
|
|
}
|
|
|
|
|
2016-10-07 14:34:31 +02:00
|
|
|
let denoms = rci.selectedDenoms;
|
|
|
|
|
|
|
|
let countByPub: {[s: string]: number} = {};
|
|
|
|
let uniq: Denomination[] = [];
|
|
|
|
|
|
|
|
denoms.forEach((x: Denomination) => {
|
|
|
|
let c = countByPub[x.denom_pub] || 0;
|
|
|
|
if (c == 0) {
|
|
|
|
uniq.push(x);
|
|
|
|
}
|
|
|
|
c += 1;
|
|
|
|
countByPub[x.denom_pub] = c;
|
|
|
|
});
|
|
|
|
|
|
|
|
function row(denom: Denomination) {
|
|
|
|
return (
|
|
|
|
<tr>
|
|
|
|
<td>{countByPub[denom.denom_pub] + "x"}</td>
|
|
|
|
<td>{amountToPretty(denom.value)}</td>
|
|
|
|
<td>{amountToPretty(denom.fee_withdraw)}</td>
|
|
|
|
<td>{amountToPretty(denom.fee_refresh)}</td>
|
|
|
|
<td>{amountToPretty(denom.fee_deposit)}</td>
|
|
|
|
</tr>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
let withdrawFeeStr = amountToPretty(rci.withdrawFee);
|
|
|
|
let overheadStr = amountToPretty(rci.overhead);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p>{`Withdrawal fees: ${withdrawFeeStr}`}</p>
|
|
|
|
<p>{`Rounding loss: ${overheadStr}`}</p>
|
|
|
|
<table>
|
|
|
|
<thead>
|
|
|
|
<th># Coins</th>
|
|
|
|
<th>Value</th>
|
|
|
|
<th>Withdraw Fee</th>
|
|
|
|
<th>Refresh Fee</th>
|
|
|
|
<th>Deposit fee</th>
|
|
|
|
</thead>
|
|
|
|
<tbody>
|
|
|
|
{uniq.map(row)}
|
|
|
|
</tbody>
|
|
|
|
</table>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function getSuggestedExchange(currency: string): Promise<string> {
|
|
|
|
// TODO: make this request go to the wallet backend
|
|
|
|
// Right now, this is a stub.
|
|
|
|
const defaultExchange: {[s: string]: string} = {
|
|
|
|
"KUDOS": "https://exchange.demo.taler.net",
|
|
|
|
"PUDOS": "https://exchange.test.taler.net",
|
|
|
|
};
|
|
|
|
|
|
|
|
let exchange = defaultExchange[currency];
|
|
|
|
|
|
|
|
if (!exchange) {
|
|
|
|
exchange = ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return Promise.resolve(exchange);
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
|
|
|
|
function WithdrawFee(props: {reserveCreationInfo: ReserveCreationInfo|null}): JSX.Element {
|
|
|
|
if (props.reserveCreationInfo) {
|
|
|
|
let {overhead, withdrawFee} = props.reserveCreationInfo;
|
|
|
|
let totalCost = Amounts.add(overhead, withdrawFee).amount;
|
|
|
|
return <p>Withdraw fees: {amountToPretty(totalCost)}</p>;
|
|
|
|
}
|
|
|
|
return <p />;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-10-07 14:34:31 +02:00
|
|
|
interface ExchangeSelectionProps {
|
2016-04-27 06:50:38 +02:00
|
|
|
suggestedExchangeUrl: string;
|
2016-10-07 14:34:31 +02:00
|
|
|
amount: AmountJson;
|
|
|
|
callback_url: string;
|
|
|
|
wt_types: string[];
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
class ExchangeSelection extends ImplicitStateComponent<ExchangeSelectionProps> {
|
|
|
|
statusString: StateHolder<string|null> = this.makeState(null);
|
|
|
|
reserveCreationInfo: StateHolder<ReserveCreationInfo|null> = this.makeState(
|
|
|
|
null);
|
|
|
|
url: StateHolder<string|null> = this.makeState(null);
|
|
|
|
detailCollapsed: StateHolder<boolean> = this.makeState(true);
|
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
updateEvent = new EventTrigger();
|
2016-10-07 14:34:31 +02:00
|
|
|
|
|
|
|
constructor(props: ExchangeSelectionProps) {
|
|
|
|
super(props);
|
2016-10-07 17:10:22 +02:00
|
|
|
this.onUrlChanged(props.suggestedExchangeUrl || null);
|
2016-02-11 11:29:57 +01:00
|
|
|
}
|
|
|
|
|
2016-10-07 14:34:31 +02:00
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
renderAdvanced(): JSX.Element {
|
2016-10-10 03:32:18 +02:00
|
|
|
if (this.detailCollapsed() && this.url() !== null) {
|
2016-10-07 14:34:31 +02:00
|
|
|
return (
|
2016-10-07 17:10:22 +02:00
|
|
|
<button className="linky"
|
|
|
|
onClick={() => this.detailCollapsed(false)}>
|
|
|
|
view fee structure / select different exchange provider
|
|
|
|
</button>
|
|
|
|
);
|
2016-10-07 14:34:31 +02:00
|
|
|
}
|
|
|
|
return (
|
|
|
|
<div>
|
2016-10-07 17:10:22 +02:00
|
|
|
<h2>Provider Selection</h2>
|
|
|
|
<label>URL: </label>
|
|
|
|
<input className="url" type="text" spellCheck={false}
|
|
|
|
value={this.url()!}
|
|
|
|
key="exchange-url-input"
|
|
|
|
onInput={(e) => this.onUrlChanged((e.target as HTMLInputElement).value)}/>
|
|
|
|
<br />
|
|
|
|
{this.renderStatus()}
|
|
|
|
<h2>Detailed Fee Structure</h2>
|
|
|
|
{renderReserveCreationDetails(this.reserveCreationInfo())}
|
|
|
|
</div>)
|
2016-10-07 14:34:31 +02:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
renderFee() {
|
|
|
|
if (!this.reserveCreationInfo()) {
|
|
|
|
return "??";
|
|
|
|
}
|
|
|
|
let rci = this.reserveCreationInfo()!;
|
|
|
|
let totalCost = Amounts.add(rci.overhead, rci.withdrawFee).amount;
|
|
|
|
return `${amountToPretty(totalCost)}`;
|
|
|
|
}
|
2016-10-07 14:34:31 +02:00
|
|
|
|
2016-10-10 03:32:18 +02:00
|
|
|
renderFeeStatus() {
|
|
|
|
if (this.reserveCreationInfo()) {
|
|
|
|
return (
|
|
|
|
<p>
|
|
|
|
The exchange provider will charge
|
|
|
|
{" "}
|
|
|
|
{this.renderFee()}
|
|
|
|
{" "}
|
|
|
|
in fees.
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (this.url() && !this.statusString()) {
|
|
|
|
let shortName = URI(this.url()!).host();
|
2016-10-10 04:01:14 +02:00
|
|
|
return <p>
|
|
|
|
Waiting for a response from
|
|
|
|
{" "}
|
2016-10-10 03:32:18 +02:00
|
|
|
<em>{shortName}</em>
|
|
|
|
</p>;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<p>
|
|
|
|
Information about fees will be available when an exchange provider is selected.
|
|
|
|
</p>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
render(props: ExchangeSelectionProps): JSX.Element {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<p>
|
|
|
|
{"You are about to withdraw "}
|
|
|
|
<strong>{amountToPretty(props.amount)}</strong>
|
|
|
|
{" from your bank account into your wallet."}
|
|
|
|
</p>
|
2016-10-10 03:32:18 +02:00
|
|
|
{this.renderFeeStatus()}
|
2016-10-07 17:10:22 +02:00
|
|
|
<button className="accept"
|
|
|
|
disabled={this.reserveCreationInfo() == null}
|
|
|
|
onClick={() => this.confirmReserve()}>
|
|
|
|
Accept fees and withdraw
|
|
|
|
</button>
|
|
|
|
<br/>
|
|
|
|
{this.renderAdvanced()}
|
|
|
|
</div>
|
2016-10-07 14:34:31 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
confirmReserve() {
|
|
|
|
this.confirmReserveImpl(this.reserveCreationInfo()!,
|
|
|
|
this.url()!,
|
|
|
|
this.props.amount,
|
|
|
|
this.props.callback_url);
|
|
|
|
}
|
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
/**
|
|
|
|
* Do an update of the reserve creation info, without any debouncing.
|
|
|
|
*/
|
|
|
|
async forceReserveUpdate() {
|
|
|
|
this.reserveCreationInfo(null);
|
|
|
|
if (!this.url()) {
|
|
|
|
this.statusString(i18n`Error: URL is empty`);
|
|
|
|
return;
|
|
|
|
}
|
2016-10-07 14:34:31 +02:00
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
this.statusString(null);
|
|
|
|
let parsedUrl = URI(this.url()!);
|
|
|
|
if (parsedUrl.is("relative")) {
|
|
|
|
this.statusString(i18n`Error: URL may not be relative`);
|
|
|
|
return;
|
|
|
|
}
|
2016-02-15 11:29:58 +01:00
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
try {
|
|
|
|
let r = await getReserveCreationInfo(this.url()!,
|
|
|
|
this.props.amount);
|
|
|
|
console.log("get exchange info resolved");
|
|
|
|
this.reserveCreationInfo(r);
|
|
|
|
console.dir(r);
|
|
|
|
} catch (e) {
|
|
|
|
console.log("get exchange info rejected");
|
|
|
|
if (e.hasOwnProperty("httpStatus")) {
|
|
|
|
this.statusString(`Error: request failed with status ${e.httpStatus}`);
|
|
|
|
} else if (e.hasOwnProperty("errorResponse")) {
|
|
|
|
let resp = e.errorResponse;
|
|
|
|
this.statusString(`Error: ${resp.error} (${resp.hint})`);
|
|
|
|
}
|
|
|
|
}
|
2016-02-11 11:29:57 +01:00
|
|
|
}
|
2015-12-20 20:34:20 +01:00
|
|
|
|
2016-02-11 11:29:57 +01:00
|
|
|
reset() {
|
2016-10-07 14:34:31 +02:00
|
|
|
this.statusString(null);
|
|
|
|
this.reserveCreationInfo(null);
|
2016-02-11 11:29:57 +01:00
|
|
|
}
|
2016-02-09 21:56:06 +01:00
|
|
|
|
2016-10-07 14:34:31 +02:00
|
|
|
confirmReserveImpl(rci: ReserveCreationInfo,
|
|
|
|
exchange: string,
|
|
|
|
amount: AmountJson,
|
|
|
|
callback_url: string) {
|
2016-03-01 19:39:17 +01:00
|
|
|
const d = {exchange, amount};
|
2016-09-12 20:25:56 +02:00
|
|
|
const cb = (rawResp: any) => {
|
2016-02-09 21:56:06 +01:00
|
|
|
if (!rawResp) {
|
|
|
|
throw Error("empty response");
|
|
|
|
}
|
2016-04-06 02:06:57 +02:00
|
|
|
// FIXME: filter out types that bank/exchange don't have in common
|
|
|
|
let wire_details = rci.wireInfo;
|
2016-02-09 21:56:06 +01:00
|
|
|
if (!rawResp.error) {
|
|
|
|
const resp = CreateReserveResponse.checked(rawResp);
|
2016-04-06 02:06:57 +02:00
|
|
|
let q: {[name: string]: string|number} = {
|
|
|
|
wire_details: JSON.stringify(wire_details),
|
2016-03-01 19:39:17 +01:00
|
|
|
exchange: resp.exchange,
|
2016-02-09 21:56:06 +01:00
|
|
|
reserve_pub: resp.reservePub,
|
2016-02-11 11:29:57 +01:00
|
|
|
amount_value: amount.value,
|
|
|
|
amount_fraction: amount.fraction,
|
|
|
|
amount_currency: amount.currency,
|
2016-02-09 21:56:06 +01:00
|
|
|
};
|
2016-02-11 11:29:57 +01:00
|
|
|
let url = URI(callback_url).addQuery(q);
|
2016-02-09 21:56:06 +01:00
|
|
|
if (!url.is("absolute")) {
|
|
|
|
throw Error("callback url is not absolute");
|
|
|
|
}
|
2016-02-11 11:29:57 +01:00
|
|
|
console.log("going to", url.href());
|
2016-02-09 21:56:06 +01:00
|
|
|
document.location.href = url.href();
|
2016-01-26 17:21:17 +01:00
|
|
|
} else {
|
2016-02-11 11:29:57 +01:00
|
|
|
this.reset();
|
2016-10-07 14:34:31 +02:00
|
|
|
this.statusString(
|
|
|
|
`Oops, something went wrong.` +
|
|
|
|
`The wallet responded with error status (${rawResp.error}).`);
|
2016-01-26 17:21:17 +01:00
|
|
|
}
|
|
|
|
};
|
2016-02-09 21:56:06 +01:00
|
|
|
chrome.runtime.sendMessage({type: 'create-reserve', detail: d}, cb);
|
2016-02-11 11:29:57 +01:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
async onUrlChanged(url: string|null) {
|
2016-02-11 11:29:57 +01:00
|
|
|
this.reset();
|
2016-02-15 11:29:58 +01:00
|
|
|
this.url(url);
|
2016-10-07 17:10:22 +02:00
|
|
|
if (url == undefined) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.updateEvent.trigger();
|
|
|
|
let waited = await this.updateEvent.wait(200);
|
|
|
|
if (waited) {
|
|
|
|
// Run the actual update if nobody else preempted us.
|
|
|
|
this.forceReserveUpdate();
|
|
|
|
this.forceUpdate();
|
|
|
|
}
|
2016-02-11 11:29:57 +01:00
|
|
|
}
|
2016-04-27 06:50:38 +02:00
|
|
|
|
2016-10-07 17:10:22 +02:00
|
|
|
renderStatus(): any {
|
|
|
|
if (this.statusString()) {
|
|
|
|
return <p><strong style="color: red;">{this.statusString()}</strong></p>;
|
|
|
|
} else if (!this.reserveCreationInfo()) {
|
|
|
|
return <p>Checking URL, please wait ...</p>;
|
2016-04-27 06:03:04 +02:00
|
|
|
}
|
2016-10-07 17:10:22 +02:00
|
|
|
return "";
|
2016-02-18 22:50:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-07 14:34:31 +02:00
|
|
|
export async function main() {
|
2016-02-15 11:29:58 +01:00
|
|
|
const url = URI(document.location.href);
|
|
|
|
const query: any = URI.parseQuery(url.query());
|
|
|
|
const amount = AmountJson.checked(JSON.parse(query.amount));
|
|
|
|
const callback_url = query.callback_url;
|
|
|
|
const bank_url = query.bank_url;
|
2016-04-06 02:06:57 +02:00
|
|
|
const wt_types = JSON.parse(query.wt_types);
|
2016-02-15 11:29:58 +01:00
|
|
|
|
2016-10-07 14:34:31 +02:00
|
|
|
try {
|
|
|
|
const suggestedExchangeUrl = await getSuggestedExchange(amount.currency);
|
|
|
|
let args = {
|
|
|
|
wt_types,
|
|
|
|
suggestedExchangeUrl,
|
|
|
|
callback_url,
|
|
|
|
amount
|
|
|
|
};
|
|
|
|
|
|
|
|
preact.render(<ExchangeSelection {...args} />, document.getElementById(
|
|
|
|
"exchange-selection")!);
|
|
|
|
|
|
|
|
} catch (e) {
|
|
|
|
// TODO: provide more context information, maybe factor it out into a
|
|
|
|
// TODO:generic error reporting function or component.
|
|
|
|
document.body.innerText = `Fatal error: "${e.message}".`;
|
|
|
|
console.error(`got error "${e.message}"`, e);
|
|
|
|
}
|
2016-10-10 04:01:14 +02:00
|
|
|
}
|