2016-02-19 04:23:00 +01:00
|
|
|
/*
|
|
|
|
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
|
2016-07-07 17:59:29 +02:00
|
|
|
TALER; see the file COPYING. If not, see <http://www.gnu.org/licenses/>
|
2016-02-19 04:23:00 +01:00
|
|
|
*/
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
/**
|
|
|
|
* Interface to the wallet through WebExtension messaging.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
2016-10-12 02:55:53 +02:00
|
|
|
import {
|
|
|
|
AmountJson,
|
2016-11-15 15:07:17 +01:00
|
|
|
CoinRecord,
|
2017-05-28 01:10:54 +02:00
|
|
|
CurrencyRecord,
|
|
|
|
DenominationRecord,
|
|
|
|
ExchangeRecord,
|
2016-11-15 15:07:17 +01:00
|
|
|
PreCoinRecord,
|
2016-10-12 02:55:53 +02:00
|
|
|
ReserveCreationInfo,
|
2017-05-28 01:10:54 +02:00
|
|
|
ReserveRecord,
|
2017-05-28 23:15:41 +02:00
|
|
|
} from "../types";
|
2016-02-19 04:23:00 +01:00
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
2016-02-19 04:23:00 +01:00
|
|
|
/**
|
2017-05-28 16:27:34 +02:00
|
|
|
* Query the wallet for the coins that would be used to withdraw
|
|
|
|
* from a given reserve.
|
2016-02-19 04:23:00 +01:00
|
|
|
*/
|
|
|
|
export function getReserveCreationInfo(baseUrl: string,
|
2017-05-28 01:10:54 +02:00
|
|
|
amount: AmountJson): Promise<ReserveCreationInfo> {
|
|
|
|
const m = { type: "reserve-creation-info", detail: { baseUrl, amount } };
|
2017-04-20 03:09:25 +02:00
|
|
|
return new Promise<ReserveCreationInfo>((resolve, reject) => {
|
2016-02-19 04:23:00 +01:00
|
|
|
chrome.runtime.sendMessage(m, (resp) => {
|
|
|
|
if (resp.error) {
|
|
|
|
console.error("error response", resp);
|
2017-05-28 01:10:54 +02:00
|
|
|
const e = Error("call to reserve-creation-info failed");
|
2016-02-19 04:23:00 +01:00
|
|
|
(e as any).errorResponse = resp;
|
|
|
|
reject(e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
resolve(resp);
|
|
|
|
});
|
|
|
|
});
|
2016-03-01 19:46:20 +01:00
|
|
|
}
|
2016-10-12 02:55:53 +02:00
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
async function callBackend(type: string, detail?: any): Promise<any> {
|
2017-04-28 23:28:27 +02:00
|
|
|
return new Promise<any>((resolve, reject) => {
|
2016-10-12 02:55:53 +02:00
|
|
|
chrome.runtime.sendMessage({ type, detail }, (resp) => {
|
2017-05-23 13:41:52 +02:00
|
|
|
if (resp && resp.error) {
|
2017-04-28 23:28:27 +02:00
|
|
|
reject(resp);
|
|
|
|
} else {
|
|
|
|
resolve(resp);
|
|
|
|
}
|
2016-10-12 02:55:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all exchanges the wallet knows about.
|
|
|
|
*/
|
2016-11-15 15:07:17 +01:00
|
|
|
export async function getExchanges(): Promise<ExchangeRecord[]> {
|
2016-10-12 02:55:53 +02:00
|
|
|
return await callBackend("get-exchanges");
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all currencies the exchange knows about.
|
|
|
|
*/
|
2017-03-24 17:54:22 +01:00
|
|
|
export async function getCurrencies(): Promise<CurrencyRecord[]> {
|
|
|
|
return await callBackend("get-currencies");
|
|
|
|
}
|
|
|
|
|
2017-04-28 23:28:27 +02:00
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
/**
|
|
|
|
* Get information about a specific currency.
|
|
|
|
*/
|
2017-04-28 23:28:27 +02:00
|
|
|
export async function getCurrency(name: string): Promise<CurrencyRecord|null> {
|
|
|
|
return await callBackend("currency-info", {name});
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information about a specific exchange.
|
|
|
|
*/
|
2017-04-28 23:28:27 +02:00
|
|
|
export async function getExchangeInfo(baseUrl: string): Promise<ExchangeRecord> {
|
|
|
|
return await callBackend("exchange-info", {baseUrl});
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Replace an existing currency record with the one given. The currency to
|
|
|
|
* replace is specified inside the currency record.
|
|
|
|
*/
|
2017-03-24 17:54:22 +01:00
|
|
|
export async function updateCurrency(currencyRecord: CurrencyRecord): Promise<void> {
|
|
|
|
return await callBackend("update-currency", { currencyRecord });
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all reserves the wallet has at an exchange.
|
|
|
|
*/
|
2016-10-13 02:23:24 +02:00
|
|
|
export async function getReserves(exchangeBaseUrl: string): Promise<ReserveRecord[]> {
|
2016-10-12 02:55:53 +02:00
|
|
|
return await callBackend("get-reserves", { exchangeBaseUrl });
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all reserves for which a payback is available.
|
|
|
|
*/
|
2017-05-01 04:05:16 +02:00
|
|
|
export async function getPaybackReserves(): Promise<ReserveRecord[]> {
|
|
|
|
return await callBackend("get-payback-reserves");
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Withdraw the payback that is available for a reserve.
|
|
|
|
*/
|
2017-05-01 04:05:16 +02:00
|
|
|
export async function withdrawPaybackReserve(reservePub: string): Promise<ReserveRecord[]> {
|
|
|
|
return await callBackend("withdraw-payback-reserve", { reservePub });
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all coins withdrawn from the given exchange.
|
|
|
|
*/
|
2016-11-15 15:07:17 +01:00
|
|
|
export async function getCoins(exchangeBaseUrl: string): Promise<CoinRecord[]> {
|
2016-10-12 02:55:53 +02:00
|
|
|
return await callBackend("get-coins", { exchangeBaseUrl });
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all precoins withdrawn from the given exchange.
|
|
|
|
*/
|
2016-11-15 15:07:17 +01:00
|
|
|
export async function getPreCoins(exchangeBaseUrl: string): Promise<PreCoinRecord[]> {
|
2016-10-12 02:55:53 +02:00
|
|
|
return await callBackend("get-precoins", { exchangeBaseUrl });
|
2016-10-13 02:36:33 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all denoms offered by the given exchange.
|
|
|
|
*/
|
2016-11-16 01:59:39 +01:00
|
|
|
export async function getDenoms(exchangeBaseUrl: string): Promise<DenominationRecord[]> {
|
|
|
|
return await callBackend("get-denoms", { exchangeBaseUrl });
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start refreshing a coin.
|
|
|
|
*/
|
2016-10-13 02:36:33 +02:00
|
|
|
export async function refresh(coinPub: string): Promise<void> {
|
|
|
|
return await callBackend("refresh-coin", { coinPub });
|
2016-11-13 08:16:12 +01:00
|
|
|
}
|
2017-05-01 04:33:47 +02:00
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Request payback for a coin. Only works for non-refreshed coins.
|
|
|
|
*/
|
2017-05-01 04:33:47 +02:00
|
|
|
export async function payback(coinPub: string): Promise<void> {
|
|
|
|
return await callBackend("payback-coin", { coinPub });
|
|
|
|
}
|