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.
|
|
|
|
*/
|
2020-08-13 20:43:51 +02:00
|
|
|
import {
|
|
|
|
AmountJson,
|
|
|
|
ConfirmPayResult,
|
|
|
|
BalancesResponse,
|
|
|
|
PurchaseDetails,
|
|
|
|
TipStatus,
|
|
|
|
BenchmarkResult,
|
|
|
|
PreparePayResult,
|
|
|
|
AcceptWithdrawalResponse,
|
|
|
|
WalletDiagnostics,
|
|
|
|
CoreApiResponse,
|
|
|
|
OperationFailedError,
|
|
|
|
GetWithdrawalDetailsForUriRequest,
|
|
|
|
WithdrawUriInfoResponse,
|
|
|
|
} from "taler-wallet-core";
|
2020-08-03 09:30:48 +02:00
|
|
|
|
|
|
|
export interface ExtendedPermissionsResponse {
|
|
|
|
newValue: boolean;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-08-13 20:43:51 +02:00
|
|
|
async function callBackend(operation: string, payload: any): Promise<any> {
|
2020-07-28 10:52:35 +02:00
|
|
|
return new Promise<any>((resolve, reject) => {
|
2020-08-13 20:43:51 +02:00
|
|
|
chrome.runtime.sendMessage({ operation, payload, id: "(none)" }, (resp) => {
|
2019-12-28 16:21:21 +01:00
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
console.log("Error calling backend");
|
2020-03-11 20:14:28 +01:00
|
|
|
reject(
|
|
|
|
new Error(
|
|
|
|
`Error contacting backend: chrome.runtime.lastError.message`,
|
|
|
|
),
|
|
|
|
);
|
2019-12-28 16:21:21 +01:00
|
|
|
}
|
2020-08-13 20:43:51 +02:00
|
|
|
console.log("got response", resp);
|
|
|
|
const r = resp as CoreApiResponse;
|
|
|
|
if (r.type === "error") {
|
|
|
|
reject(new OperationFailedError(r.error));
|
|
|
|
return;
|
2017-04-28 23:28:27 +02:00
|
|
|
}
|
2020-08-13 20:43:51 +02:00
|
|
|
resolve(r.result);
|
2016-10-12 02:55:53 +02: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-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
|
|
|
*/
|
2020-03-11 20:14:28 +01:00
|
|
|
export function confirmPay(
|
|
|
|
proposalId: string,
|
|
|
|
sessionId: string | undefined,
|
2020-08-12 09:11:00 +02:00
|
|
|
): Promise<ConfirmPayResult> {
|
2018-01-17 03:49:54 +01:00
|
|
|
return callBackend("confirm-pay", { proposalId, sessionId });
|
2017-05-30 18:33:28 +02:00
|
|
|
}
|
|
|
|
|
2017-06-05 03:20:28 +02:00
|
|
|
/**
|
|
|
|
* Check upgrade information
|
|
|
|
*/
|
|
|
|
export function checkUpgrade(): Promise<UpgradeResponse> {
|
2020-03-11 20:14:28 +01:00
|
|
|
return callBackend("check-upgrade", {});
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Reset database
|
|
|
|
*/
|
|
|
|
export function resetDb(): Promise<void> {
|
2020-03-11 20:14:28 +01:00
|
|
|
return callBackend("reset-db", {});
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
2017-08-14 04:16:12 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get balances for all currencies/exchanges.
|
|
|
|
*/
|
2020-08-12 09:11:00 +02:00
|
|
|
export function getBalance(): Promise<BalancesResponse> {
|
2020-08-13 20:43:51 +02:00
|
|
|
return callBackend("getBalances", {});
|
2017-08-14 04:16:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return coins to a bank account.
|
|
|
|
*/
|
2020-03-11 20:14:28 +01:00
|
|
|
export function returnCoins(args: {
|
|
|
|
amount: AmountJson;
|
|
|
|
exchange: string;
|
2020-07-28 10:52:35 +02:00
|
|
|
senderWire: string;
|
2020-03-11 20:14:28 +01:00
|
|
|
}): Promise<void> {
|
2017-08-14 04:16:12 +02:00
|
|
|
return callBackend("return-coins", args);
|
|
|
|
}
|
2017-08-25 18:08:37 +02:00
|
|
|
|
2017-10-15 19:28:35 +02:00
|
|
|
/**
|
|
|
|
* Look up a purchase in the wallet database from
|
|
|
|
* the contract terms hash.
|
|
|
|
*/
|
2020-03-11 20:14:28 +01:00
|
|
|
export function getPurchaseDetails(
|
2020-05-15 09:23:35 +02:00
|
|
|
proposalId: string,
|
2020-08-12 09:11:00 +02:00
|
|
|
): Promise<PurchaseDetails> {
|
2020-05-15 09:23:35 +02:00
|
|
|
return callBackend("get-purchase-details", { proposalId });
|
2017-08-27 03:56:19 +02:00
|
|
|
}
|
2017-08-30 17:08:54 +02:00
|
|
|
|
2018-01-03 14:42:06 +01:00
|
|
|
/**
|
|
|
|
* Get the status of processing a tip.
|
|
|
|
*/
|
2020-08-12 09:11:00 +02:00
|
|
|
export function getTipStatus(talerTipUri: string): Promise<TipStatus> {
|
2019-08-30 17:27:59 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-01-22 01:12:08 +01:00
|
|
|
/**
|
|
|
|
* Download a refund and accept it.
|
|
|
|
*/
|
2020-05-15 09:23:35 +02:00
|
|
|
export function applyRefund(
|
|
|
|
refundUrl: string,
|
|
|
|
): Promise<{ contractTermsHash: string; proposalId: 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.
|
|
|
|
*/
|
2020-04-06 20:02:01 +02:00
|
|
|
export function abortFailedPayment(contractTermsHash: string): Promise<void> {
|
2018-01-29 16:41:17 +01:00
|
|
|
return callBackend("abort-failed-payment", { contractTermsHash });
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Abort a failed payment and try to get a refund.
|
|
|
|
*/
|
2020-08-12 09:11:00 +02:00
|
|
|
export function benchmarkCrypto(repetitions: number): Promise<BenchmarkResult> {
|
2018-09-20 02:56:13 +02:00
|
|
|
return callBackend("benchmark-crypto", { repetitions });
|
|
|
|
}
|
2019-08-29 23:12:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get details about a pay operation.
|
|
|
|
*/
|
2020-08-12 09:11:00 +02:00
|
|
|
export function preparePay(talerPayUri: string): Promise<PreparePayResult> {
|
2019-08-29 23:12:55 +02:00
|
|
|
return callBackend("prepare-pay", { talerPayUri });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get details about a withdraw operation.
|
|
|
|
*/
|
2020-03-11 20:14:28 +01:00
|
|
|
export function acceptWithdrawal(
|
|
|
|
talerWithdrawUri: string,
|
|
|
|
selectedExchange: string,
|
2020-08-12 09:11:00 +02:00
|
|
|
): Promise<AcceptWithdrawalResponse> {
|
2020-03-11 20:14:28 +01:00
|
|
|
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
|
|
|
|
*/
|
2020-08-12 09:11:00 +02:00
|
|
|
export function getDiagnostics(): Promise<WalletDiagnostics> {
|
2019-09-05 16:10:53 +02:00
|
|
|
return callBackend("get-diagnostics", {});
|
|
|
|
}
|
2020-05-01 10:46:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get diagnostics information
|
|
|
|
*/
|
2020-05-15 09:23:35 +02:00
|
|
|
export function setExtendedPermissions(
|
|
|
|
value: boolean,
|
|
|
|
): Promise<ExtendedPermissionsResponse> {
|
2020-05-01 10:46:56 +02:00
|
|
|
return callBackend("set-extended-permissions", { value });
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get diagnostics information
|
|
|
|
*/
|
|
|
|
export function getExtendedPermissions(): Promise<ExtendedPermissionsResponse> {
|
|
|
|
return callBackend("get-extended-permissions", {});
|
|
|
|
}
|
2020-05-04 15:22:54 +02:00
|
|
|
|
2020-08-13 20:43:51 +02:00
|
|
|
/**
|
|
|
|
* Get diagnostics information
|
|
|
|
*/
|
|
|
|
export function getWithdrawalDetailsForUri(
|
|
|
|
req: GetWithdrawalDetailsForUriRequest,
|
|
|
|
): Promise<WithdrawUriInfoResponse> {
|
|
|
|
return callBackend("getWithdrawalDetailsForUri", req);
|
|
|
|
}
|
|
|
|
|
2020-05-04 15:22:54 +02:00
|
|
|
export function onUpdateNotification(f: () => void): () => void {
|
|
|
|
const port = chrome.runtime.connect({ name: "notifications" });
|
|
|
|
const listener = (): void => {
|
|
|
|
f();
|
|
|
|
};
|
|
|
|
port.onMessage.addListener(listener);
|
|
|
|
return () => {
|
|
|
|
port.onMessage.removeListener(listener);
|
|
|
|
};
|
2020-05-15 09:23:35 +02:00
|
|
|
}
|