wallet-core/src/webex/wxApi.ts

332 lines
7.9 KiB
TypeScript
Raw Normal View History

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
*/
/**
* Interface to the wallet through WebExtension messaging.
*/
/**
* Imports.
*/
2016-10-12 02:55:53 +02:00
import {
AmountJson,
CheckPayResult,
2016-11-15 15:07:17 +01:00
CoinRecord,
2017-05-29 16:58:03 +02:00
ConfirmPayResult,
2017-05-28 01:10:54 +02:00
CurrencyRecord,
DenominationRecord,
ExchangeRecord,
2016-11-15 15:07:17 +01:00
PreCoinRecord,
QueryPaymentResult,
2016-10-12 02:55:53 +02:00
ReserveCreationInfo,
2017-05-28 01:10:54 +02:00
ReserveRecord,
SenderWireInfos,
WalletBalance,
} from "../types";
2016-02-19 04:23:00 +01:00
import { MessageType, MessageMap } from "./messages";
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;
}
async function callBackend<T extends MessageType>(type: T, detail: MessageMap[T]["request"]): Promise<any> {
return new Promise<any>((resolve, reject) => {
2016-10-12 02:55:53 +02:00
chrome.runtime.sendMessage({ type, detail }, (resp) => {
if (resp && resp.error) {
reject(resp);
} else {
resolve(resp);
}
2016-10-12 02:55:53 +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 });
}
/**
* Get all exchanges the wallet knows about.
*/
2017-05-30 18:33:28 +02:00
export function getExchanges(): Promise<ExchangeRecord[]> {
return callBackend("get-exchanges", { });
2016-10-12 02:55:53 +02:00
}
/**
* Get all currencies the exchange knows about.
*/
2017-05-30 18:33:28 +02:00
export function getCurrencies(): Promise<CurrencyRecord[]> {
return callBackend("get-currencies", { });
2017-03-24 17:54:22 +01: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});
}
/**
* 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});
}
/**
* 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
}
/**
* 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
}
/**
* Get all reserves for which a payback is available.
*/
2017-05-30 18:33:28 +02:00
export function getPaybackReserves(): Promise<ReserveRecord[]> {
return callBackend("get-payback-reserves", { });
}
/**
* 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 });
}
/**
* 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
}
/**
* 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
}
/**
* 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 });
}
/**
* Start refreshing a coin.
*/
2017-05-30 18:33:28 +02:00
export function refresh(coinPub: string): Promise<void> {
return callBackend("refresh-coin", { coinPub });
}
/**
* 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 });
}
/**
* Get a proposal stored in the wallet by its proposal id.
*/
export function getProposal(proposalId: number) {
return callBackend("get-proposal", { proposalId });
}
/**
* Check if payment is possible or already done.
*/
export function checkPay(proposalId: number): Promise<CheckPayResult> {
return callBackend("check-pay", { proposalId });
}
/**
* Pay for a proposal.
*/
export function confirmPay(proposalId: number): Promise<ConfirmPayResult> {
return callBackend("confirm-pay", { proposalId });
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> {
return callBackend("hash-contract", { contract });
2017-05-30 18:33:28 +02:00
}
2017-05-30 18:33:28 +02:00
/**
* Save a proposal in the wallet. Returns the proposal id that
* the proposal is stored under.
2017-05-30 18:33:28 +02:00
*/
export function saveProposal(proposal: any): Promise<number> {
return callBackend("save-proposal", proposal);
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 });
}
/**
* Query for a payment by fulfillment URL.
*/
2017-06-01 18:46:07 +02:00
export function queryPayment(url: string): Promise<QueryPaymentResult> {
return callBackend("query-payment", { url });
}
/**
* Add a new history item.
*/
export function putHistory(historyEntry: any): Promise<void> {
return callBackend("put-history-entry", { historyEntry });
}
/**
* Mark a payment as succeeded.
*/
export function paymentSucceeded(contractTermsHash: string, merchantSig: string): Promise<void> {
return callBackend("payment-succeeded", { contractTermsHash, merchantSig });
}
/**
* Mark a payment as succeeded.
*/
export function paymentFailed(contractTermsHash: string): Promise<void> {
return callBackend("payment-failed", { contractTermsHash });
}
/**
* Get the payment cookie for the current tab, or undefined if no payment
* cookie was set.
*/
export function getTabCookie(): Promise<any> {
return callBackend("get-tab-cookie", { });
}
/**
* Generate a contract nonce (EdDSA key pair), store it in the wallet's
* database and return the public key.
*/
export function generateNonce(): Promise<string> {
return callBackend("generate-nonce", { });
}
2017-06-05 03:20:28 +02:00
/**
* Check upgrade information
*/
export function checkUpgrade(): Promise<UpgradeResponse> {
return callBackend("check-upgrade", { });
}
/**
* Create a reserve.
*/
export function createReserve(args: { amount: AmountJson, exchange: string, senderWire?: object }): Promise<any> {
return callBackend("create-reserve", args);
}
2017-06-05 03:20:28 +02:00
/**
* Reset database
*/
export function resetDb(): Promise<void> {
return callBackend("reset-db", { });
}
/**
* 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);
}
export function logAndDisplayError(args: any): Promise<void> {
return callBackend("log-and-display-error", args);
}
export function getReport(reportUid: string): Promise<void> {
return callBackend("get-report", { reportUid });
}