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.
|
|
|
|
*/
|
2018-01-03 14:42:06 +01:00
|
|
|
import { AmountJson } from "../amounts";
|
2016-10-12 02:55:53 +02:00
|
|
|
import {
|
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,
|
2018-01-03 14:42:06 +01:00
|
|
|
ReserveRecord,
|
|
|
|
} from "../dbTypes";
|
|
|
|
import {
|
2018-09-20 02:56:13 +02:00
|
|
|
BenchmarkResult,
|
2018-01-03 14:42:06 +01:00
|
|
|
ConfirmPayResult,
|
2016-10-12 02:55:53 +02:00
|
|
|
ReserveCreationInfo,
|
2017-08-14 04:16:12 +02:00
|
|
|
SenderWireInfos,
|
2017-11-30 04:07:36 +01:00
|
|
|
TipStatus,
|
2017-08-14 04:16:12 +02:00
|
|
|
WalletBalance,
|
2019-08-31 13:27:12 +02:00
|
|
|
PurchaseDetails,
|
2019-09-05 16:10:53 +02:00
|
|
|
WalletDiagnostics,
|
2018-01-03 14:42:06 +01:00
|
|
|
} from "../walletTypes";
|
|
|
|
|
2017-10-15 19:28:35 +02:00
|
|
|
import { MessageMap, MessageType } from "./messages";
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
|
2017-06-05 03:20:28 +02:00
|
|
|
/**
|
|
|
|
* Response with information about available version upgrades.
|
|
|
|
*/
|
|
|
|
export interface UpgradeResponse {
|
|
|
|
/**
|
|
|
|
* Is a reset required because of a new DB version
|
|
|
|
* that can't be atomatically upgraded?
|
|
|
|
*/
|
|
|
|
dbResetRequired: boolean;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Current database version.
|
|
|
|
*/
|
|
|
|
currentDbVersion: string;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Old db version (if applicable).
|
|
|
|
*/
|
|
|
|
oldDbVersion: string;
|
|
|
|
}
|
|
|
|
|
2017-05-31 16:04:14 +02:00
|
|
|
|
2018-04-09 00:41:14 +02:00
|
|
|
/**
|
|
|
|
* Error thrown when the function from the backend (via RPC) threw an error.
|
|
|
|
*/
|
2018-01-29 16:41:17 +01:00
|
|
|
export class WalletApiError extends Error {
|
|
|
|
constructor(message: string, public detail: any) {
|
|
|
|
super(message);
|
2019-08-29 23:12:55 +02:00
|
|
|
// restore prototype chain
|
|
|
|
Object.setPrototypeOf(this, new.target.prototype);
|
2018-01-29 16:41:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-01-23 17:05:58 +01:00
|
|
|
async function callBackend<T extends MessageType>(
|
|
|
|
type: T,
|
|
|
|
detail: MessageMap[T]["request"],
|
|
|
|
): Promise<MessageMap[T]["response"]> {
|
|
|
|
return new Promise<MessageMap[T]["response"]>((resolve, reject) => {
|
2016-10-12 02:55:53 +02:00
|
|
|
chrome.runtime.sendMessage({ type, detail }, (resp) => {
|
2018-01-29 16:41:17 +01:00
|
|
|
if (typeof resp === "object" && resp && resp.error) {
|
2019-08-31 13:27:12 +02:00
|
|
|
const e = new WalletApiError(resp.error.message, resp.error);
|
2018-01-29 16:41:17 +01:00
|
|
|
reject(e);
|
2017-04-28 23:28:27 +02:00
|
|
|
} else {
|
|
|
|
resolve(resp);
|
|
|
|
}
|
2016-10-12 02:55:53 +02:00
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
2017-05-30 18:33:28 +02:00
|
|
|
/**
|
|
|
|
* Query the wallet for the coins that would be used to withdraw
|
|
|
|
* from a given reserve.
|
|
|
|
*/
|
|
|
|
export function getReserveCreationInfo(baseUrl: string,
|
|
|
|
amount: AmountJson): Promise<ReserveCreationInfo> {
|
|
|
|
return callBackend("reserve-creation-info", { baseUrl, amount });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
/**
|
|
|
|
* Get all exchanges the wallet knows about.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getExchanges(): Promise<ExchangeRecord[]> {
|
2017-05-31 16:04:14 +02:00
|
|
|
return callBackend("get-exchanges", { });
|
2016-10-12 02:55:53 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all currencies the exchange knows about.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getCurrencies(): Promise<CurrencyRecord[]> {
|
2017-05-31 16:04:14 +02:00
|
|
|
return callBackend("get-currencies", { });
|
2017-03-24 17:54:22 +01:00
|
|
|
}
|
|
|
|
|
2017-04-28 23:28:27 +02:00
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
/**
|
|
|
|
* Get information about a specific currency.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getCurrency(name: string): Promise<CurrencyRecord|null> {
|
|
|
|
return callBackend("currency-info", {name});
|
2017-04-28 23:28:27 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information about a specific exchange.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getExchangeInfo(baseUrl: string): Promise<ExchangeRecord> {
|
|
|
|
return callBackend("exchange-info", {baseUrl});
|
2017-04-28 23:28:27 +02:00
|
|
|
}
|
|
|
|
|
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-05-30 18:33:28 +02:00
|
|
|
export function updateCurrency(currencyRecord: CurrencyRecord): Promise<void> {
|
|
|
|
return callBackend("update-currency", { currencyRecord });
|
2017-03-24 17:54:22 +01:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all reserves the wallet has at an exchange.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getReserves(exchangeBaseUrl: string): Promise<ReserveRecord[]> {
|
|
|
|
return callBackend("get-reserves", { exchangeBaseUrl });
|
2016-10-12 02:55:53 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all reserves for which a payback is available.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getPaybackReserves(): Promise<ReserveRecord[]> {
|
2017-05-31 16:04:14 +02:00
|
|
|
return callBackend("get-payback-reserves", { });
|
2017-05-01 04:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Withdraw the payback that is available for a reserve.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function withdrawPaybackReserve(reservePub: string): Promise<ReserveRecord[]> {
|
|
|
|
return callBackend("withdraw-payback-reserve", { reservePub });
|
2017-05-01 04:05:16 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all coins withdrawn from the given exchange.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getCoins(exchangeBaseUrl: string): Promise<CoinRecord[]> {
|
|
|
|
return callBackend("get-coins", { exchangeBaseUrl });
|
2016-10-12 02:55:53 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get all precoins withdrawn from the given exchange.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getPreCoins(exchangeBaseUrl: string): Promise<PreCoinRecord[]> {
|
|
|
|
return 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.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function getDenoms(exchangeBaseUrl: string): Promise<DenominationRecord[]> {
|
|
|
|
return callBackend("get-denoms", { exchangeBaseUrl });
|
2016-11-16 01:59:39 +01:00
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Start refreshing a coin.
|
|
|
|
*/
|
2017-05-30 18:33:28 +02:00
|
|
|
export function refresh(coinPub: string): Promise<void> {
|
|
|
|
return 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-30 18:33:28 +02:00
|
|
|
export function payback(coinPub: string): Promise<void> {
|
|
|
|
return callBackend("payback-coin", { coinPub });
|
2017-05-01 04:33:47 +02:00
|
|
|
}
|
2017-05-29 16:27:53 +02:00
|
|
|
|
|
|
|
/**
|
2017-05-31 16:04:14 +02:00
|
|
|
* Pay for a proposal.
|
2017-05-29 16:27:53 +02:00
|
|
|
*/
|
2018-01-17 03:49:54 +01:00
|
|
|
export function confirmPay(proposalId: number, sessionId: string | undefined): Promise<ConfirmPayResult> {
|
|
|
|
return callBackend("confirm-pay", { proposalId, sessionId });
|
2017-05-30 18:33:28 +02:00
|
|
|
}
|
|
|
|
|
2018-01-19 01:27:27 +01:00
|
|
|
/**
|
|
|
|
* Replay paying for a purchase.
|
|
|
|
*/
|
|
|
|
export function submitPay(contractTermsHash: string, sessionId: string | undefined): Promise<ConfirmPayResult> {
|
|
|
|
return callBackend("submit-pay", { contractTermsHash, sessionId });
|
|
|
|
}
|
|
|
|
|
2017-05-30 18:33:28 +02:00
|
|
|
/**
|
|
|
|
* Hash a contract. Throws if its not a valid contract.
|
|
|
|
*/
|
|
|
|
export function hashContract(contract: object): Promise<string> {
|
2017-05-31 16:04:14 +02:00
|
|
|
return callBackend("hash-contract", { contract });
|
2017-05-30 18:33:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mark a reserve as confirmed.
|
|
|
|
*/
|
|
|
|
export function confirmReserve(reservePub: string): Promise<void> {
|
|
|
|
return callBackend("confirm-reserve", { reservePub });
|
2017-05-29 16:27:53 +02:00
|
|
|
}
|
2017-05-31 11:56:26 +02:00
|
|
|
|
2017-06-05 03:20:28 +02:00
|
|
|
/**
|
|
|
|
* Check upgrade information
|
|
|
|
*/
|
|
|
|
export function checkUpgrade(): Promise<UpgradeResponse> {
|
|
|
|
return callBackend("check-upgrade", { });
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:17:55 +02:00
|
|
|
/**
|
|
|
|
* Create a reserve.
|
|
|
|
*/
|
2019-05-08 04:53:26 +02:00
|
|
|
export function createReserve(args: { amount: AmountJson, exchange: string, senderWire?: string }): Promise<any> {
|
2017-07-20 02:17:55 +02:00
|
|
|
return callBackend("create-reserve", args);
|
|
|
|
}
|
|
|
|
|
2017-06-05 03:20:28 +02:00
|
|
|
/**
|
|
|
|
* Reset database
|
|
|
|
*/
|
|
|
|
export function resetDb(): Promise<void> {
|
|
|
|
return callBackend("reset-db", { });
|
|
|
|
}
|
2017-08-14 04:16:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get balances for all currencies/exchanges.
|
|
|
|
*/
|
|
|
|
export function getBalance(): Promise<WalletBalance> {
|
|
|
|
return callBackend("balances", { });
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get possible sender wire infos for getting money
|
|
|
|
* wired from an exchange.
|
|
|
|
*/
|
|
|
|
export function getSenderWireInfos(): Promise<SenderWireInfos> {
|
|
|
|
return callBackend("get-sender-wire-infos", { });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return coins to a bank account.
|
|
|
|
*/
|
|
|
|
export function returnCoins(args: { amount: AmountJson, exchange: string, senderWire: object }): Promise<void> {
|
|
|
|
return callBackend("return-coins", args);
|
|
|
|
}
|
2017-08-25 18:08:37 +02:00
|
|
|
|
2017-08-27 03:56:19 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Record an error report and display it in a tabl.
|
|
|
|
*
|
|
|
|
* If sameTab is set, the error report will be opened in the current tab,
|
|
|
|
* otherwise in a new tab.
|
|
|
|
*/
|
2017-08-25 18:08:37 +02:00
|
|
|
export function logAndDisplayError(args: any): Promise<void> {
|
|
|
|
return callBackend("log-and-display-error", args);
|
|
|
|
}
|
|
|
|
|
2017-10-15 19:28:35 +02:00
|
|
|
/**
|
|
|
|
* Get an error report from the logging database for the
|
|
|
|
* given report UID.
|
|
|
|
*/
|
|
|
|
export function getReport(reportUid: string): Promise<any> {
|
2017-08-25 18:08:37 +02:00
|
|
|
return callBackend("get-report", { reportUid });
|
|
|
|
}
|
2017-08-27 03:56:19 +02:00
|
|
|
|
|
|
|
|
2017-10-15 19:28:35 +02:00
|
|
|
/**
|
|
|
|
* Look up a purchase in the wallet database from
|
|
|
|
* the contract terms hash.
|
|
|
|
*/
|
2019-08-31 13:27:12 +02:00
|
|
|
export function getPurchaseDetails(contractTermsHash: string): Promise<PurchaseDetails> {
|
|
|
|
return callBackend("get-purchase-details", { contractTermsHash });
|
2017-08-27 03:56:19 +02:00
|
|
|
}
|
2017-08-30 17:08:54 +02:00
|
|
|
|
2017-11-30 04:07:36 +01:00
|
|
|
|
2018-01-03 14:42:06 +01:00
|
|
|
/**
|
|
|
|
* Get the status of processing a tip.
|
|
|
|
*/
|
2019-08-30 17:27:59 +02:00
|
|
|
export function getTipStatus(talerTipUri: string): Promise<TipStatus> {
|
|
|
|
return callBackend("get-tip-status", { talerTipUri });
|
2017-11-30 04:07:36 +01:00
|
|
|
}
|
|
|
|
|
2018-01-03 14:42:06 +01:00
|
|
|
/**
|
|
|
|
* Mark a tip as accepted by the user.
|
|
|
|
*/
|
2019-08-30 17:27:59 +02:00
|
|
|
export function acceptTip(talerTipUri: string): Promise<void> {
|
|
|
|
return callBackend("accept-tip", { talerTipUri });
|
2017-11-30 04:07:36 +01:00
|
|
|
}
|
|
|
|
|
2017-12-12 16:39:55 +01:00
|
|
|
|
2018-01-03 14:42:06 +01:00
|
|
|
/**
|
|
|
|
* Clear notifications that the wallet shows to the user.
|
|
|
|
*/
|
2017-12-12 16:39:55 +01:00
|
|
|
export function clearNotification(): Promise<void> {
|
|
|
|
return callBackend("clear-notification", { });
|
|
|
|
}
|
2018-01-17 03:49:54 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Download a contract.
|
|
|
|
*/
|
|
|
|
export function downloadProposal(url: string): Promise<number> {
|
|
|
|
return callBackend("download-proposal", { url });
|
|
|
|
}
|
2018-01-22 01:12:08 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Download a refund and accept it.
|
|
|
|
*/
|
2019-08-31 13:27:12 +02:00
|
|
|
export function applyRefund(refundUrl: string): Promise<string> {
|
2018-01-22 01:12:08 +01:00
|
|
|
return callBackend("accept-refund", { refundUrl });
|
|
|
|
}
|
2018-01-29 16:41:17 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abort a failed payment and try to get a refund.
|
|
|
|
*/
|
|
|
|
export function abortFailedPayment(contractTermsHash: string) {
|
|
|
|
return callBackend("abort-failed-payment", { contractTermsHash });
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Abort a failed payment and try to get a refund.
|
|
|
|
*/
|
|
|
|
export function benchmarkCrypto(repetitions: number): Promise<BenchmarkResult> {
|
|
|
|
return callBackend("benchmark-crypto", { repetitions });
|
|
|
|
}
|
2019-08-29 23:12:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get details about a withdraw operation.
|
|
|
|
*/
|
|
|
|
export function getWithdrawDetails(talerWithdrawUri: string, maybeSelectedExchange: string | undefined) {
|
|
|
|
return callBackend("get-withdraw-details", { talerWithdrawUri, maybeSelectedExchange });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get details about a pay operation.
|
|
|
|
*/
|
|
|
|
export function preparePay(talerPayUri: string) {
|
|
|
|
return callBackend("prepare-pay", { talerPayUri });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get details about a withdraw operation.
|
|
|
|
*/
|
|
|
|
export function acceptWithdrawal(talerWithdrawUri: string, selectedExchange: string) {
|
|
|
|
return callBackend("accept-withdrawal", { talerWithdrawUri, selectedExchange });
|
2019-08-30 17:27:59 +02:00
|
|
|
}
|
2019-09-05 16:10:53 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get diagnostics information
|
|
|
|
*/
|
|
|
|
export function getDiagnostics(): Promise<WalletDiagnostics> {
|
|
|
|
return callBackend("get-diagnostics", {});
|
|
|
|
}
|