wallet-core/packages/taler-wallet-webextension/src/wxApi.ts

377 lines
9.8 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.
*/
2020-08-13 20:43:51 +02:00
import {
AcceptExchangeTosRequest,
AcceptManualWithdrawalResult, AcceptTipRequest, AcceptWithdrawalResponse,
AddExchangeRequest, ApplyRefundResponse, BalancesResponse, ConfirmPayResult,
CoreApiResponse, DeleteTransactionRequest, ExchangesListRespose,
GetExchangeTosResult, GetExchangeWithdrawalInfo,
GetWithdrawalDetailsForUriRequest, NotificationType, PreparePayResult, PrepareTipRequest,
PrepareTipResult, RetryTransactionRequest,
SetWalletDeviceIdRequest, TransactionsResponse, WalletDiagnostics, WithdrawUriInfoResponse
2021-03-27 14:35:58 +01:00
} from "@gnu-taler/taler-util";
2021-11-15 15:18:58 +01:00
import {
AddBackupProviderRequest, BackupInfo, OperationFailedError,
PendingOperationsResponse,
RemoveBackupProviderRequest
2021-11-15 15:18:58 +01:00
} from "@gnu-taler/taler-wallet-core";
import { ExchangeWithdrawDetails } from "@gnu-taler/taler-wallet-core/src/operations/withdraw";
import { MessageFromBackend } from "./wxBackend.js";
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
2021-04-27 23:42:25 +02:00
* that can't be automatically upgraded?
2017-06-05 03:20:28 +02:00
*/
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> {
return new Promise<any>((resolve, reject) => {
// eslint-disable-next-line no-undef
2020-08-13 20:43:51 +02:00
chrome.runtime.sendMessage({ operation, payload, id: "(none)" }, (resp) => {
// eslint-disable-next-line no-undef
2019-12-28 16:21:21 +01:00
if (chrome.runtime.lastError) {
console.log("Error calling backend");
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;
}
2020-08-13 20:43:51 +02:00
resolve(r.result);
2016-10-12 02:55:53 +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 });
}
/**
* Pay for a proposal.
*/
export function confirmPay(
proposalId: string,
sessionId: string | undefined,
2020-08-12 09:11:00 +02:00
): Promise<ConfirmPayResult> {
return callBackend("confirmPay", { 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> {
return callBackend("check-upgrade", {});
2017-06-05 03:20:28 +02:00
}
/**
* Reset database
*/
export function resetDb(): Promise<void> {
return callBackend("reset-db", {});
2017-06-05 03:20:28 +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", {});
}
/**
2021-06-30 23:24:08 +02:00
* Retrieve the full event history for this wallet.
*/
export function getTransactions(): Promise<TransactionsResponse> {
return callBackend("getTransactions", {});
}
2021-07-01 05:35:41 +02:00
interface CurrencyInfo {
name: string;
baseUrl: string;
pub: string;
}
interface ListOfKnownCurrencies {
2021-11-15 15:18:58 +01:00
auditors: CurrencyInfo[];
exchanges: CurrencyInfo[];
2021-07-01 05:35:41 +02:00
}
/**
* Get a list of currencies from known auditors and exchanges
*/
export function listKnownCurrencies(): Promise<ListOfKnownCurrencies> {
2021-11-15 15:18:58 +01:00
return callBackend("listCurrencies", {}).then((result) => {
console.log("result list", result);
const auditors = result.trustedAuditors.map(
(a: Record<string, string>) => ({
name: a.currency,
baseUrl: a.auditorBaseUrl,
pub: a.auditorPub,
}),
);
const exchanges = result.trustedExchanges.map(
(a: Record<string, string>) => ({
name: a.currency,
baseUrl: a.exchangeBaseUrl,
pub: a.exchangeMasterPub,
}),
);
return { auditors, exchanges };
2021-07-01 05:35:41 +02:00
});
}
2021-10-11 20:59:55 +02:00
export function listExchanges(): Promise<ExchangesListRespose> {
2021-11-15 15:18:58 +01:00
return callBackend("listExchanges", {});
2021-10-11 20:59:55 +02:00
}
2021-06-30 23:24:08 +02:00
/**
2021-07-01 05:35:41 +02:00
* Get information about the current state of wallet backups.
2021-06-30 23:24:08 +02:00
*/
2021-07-01 16:33:26 +02:00
export function getBackupInfo(): Promise<BackupInfo> {
2021-11-15 15:18:58 +01:00
return callBackend("getBackupInfo", {});
2021-06-30 23:24:08 +02:00
}
2021-07-01 16:33:26 +02:00
/**
* Add a backup provider and activate it
*/
2021-11-15 15:18:58 +01:00
export function addBackupProvider(
backupProviderBaseUrl: string,
name: string,
): Promise<void> {
2021-07-01 16:33:26 +02:00
return callBackend("addBackupProvider", {
2021-11-15 15:18:58 +01:00
backupProviderBaseUrl,
activate: true,
name,
} as AddBackupProviderRequest);
2021-07-01 16:33:26 +02:00
}
2021-07-06 17:44:25 +02:00
export function setWalletDeviceId(walletDeviceId: string): Promise<void> {
return callBackend("setWalletDeviceId", {
2021-11-15 15:18:58 +01:00
walletDeviceId,
} as SetWalletDeviceIdRequest);
2021-07-06 17:44:25 +02:00
}
2021-07-01 16:33:26 +02:00
export function syncAllProviders(): Promise<void> {
2021-11-15 15:18:58 +01:00
return callBackend("runBackupCycle", {});
2021-07-01 16:33:26 +02:00
}
export function syncOneProvider(url: string): Promise<void> {
2021-11-15 15:18:58 +01:00
return callBackend("runBackupCycle", { providers: [url] });
}
export function removeProvider(url: string): Promise<void> {
2021-11-15 15:18:58 +01:00
return callBackend("removeBackupProvider", {
provider: url,
} as RemoveBackupProviderRequest);
}
export function extendedProvider(url: string): Promise<void> {
2021-11-15 15:18:58 +01:00
return callBackend("extendBackupProvider", { provider: url });
}
2021-07-01 16:33:26 +02:00
2021-06-03 06:07:29 +02:00
/**
* Retry a transaction
2021-11-15 15:18:58 +01:00
* @param transactionId
* @returns
*/
2021-07-01 05:35:41 +02:00
export function retryTransaction(transactionId: string): Promise<void> {
return callBackend("retryTransaction", {
2021-11-15 15:18:58 +01:00
transactionId,
} as RetryTransactionRequest);
}
/**
* Permanently delete a transaction from the transaction list
2021-06-03 06:07:29 +02:00
*/
export function deleteTransaction(transactionId: string): Promise<void> {
return callBackend("deleteTransaction", {
2021-11-15 15:18:58 +01:00
transactionId,
2021-06-03 06:07:29 +02:00
} as DeleteTransactionRequest);
}
2018-01-22 01:12:08 +01:00
/**
* Download a refund and accept it.
*/
export function applyRefund(
talerRefundUri: string,
): Promise<ApplyRefundResponse> {
return callBackend("applyRefund", { talerRefundUri });
2018-09-20 02:56:13 +02:00
}
/**
* Get details about a pay operation.
*/
2020-08-12 09:11:00 +02:00
export function preparePay(talerPayUri: string): Promise<PreparePayResult> {
return callBackend("preparePay", { talerPayUri });
}
/**
* Get details about a withdraw operation.
*/
export function acceptWithdrawal(
talerWithdrawUri: string,
selectedExchange: string,
2020-08-12 09:11:00 +02:00
): Promise<AcceptWithdrawalResponse> {
return callBackend("acceptBankIntegratedWithdrawal", {
talerWithdrawUri,
exchangeBaseUrl: selectedExchange,
});
2019-08-30 17:27:59 +02:00
}
2021-09-20 19:05:40 +02:00
/**
* Create a reserve into the exchange that expect the amount indicated
2021-11-15 15:18:58 +01:00
* @param exchangeBaseUrl
* @param amount
* @returns
2021-09-20 19:05:40 +02:00
*/
export function acceptManualWithdrawal(
exchangeBaseUrl: string,
amount: string,
): Promise<AcceptManualWithdrawalResult> {
return callBackend("acceptManualWithdrawal", {
2021-11-15 15:18:58 +01:00
amount,
exchangeBaseUrl,
2021-09-20 19:05:40 +02:00
});
}
export function setExchangeTosAccepted(
exchangeBaseUrl: string,
2021-11-15 15:18:58 +01:00
etag: string | undefined,
): Promise<void> {
return callBackend("setExchangeTosAccepted", {
2021-11-15 15:18:58 +01:00
exchangeBaseUrl,
etag,
} as AcceptExchangeTosRequest);
}
/**
* Get diagnostics information
*/
2020-08-12 09:11:00 +02:00
export function getDiagnostics(): Promise<WalletDiagnostics> {
return callBackend("wxGetDiagnostics", {});
}
/**
* Get diagnostics information
*/
export function setExtendedPermissions(
value: boolean,
): Promise<ExtendedPermissionsResponse> {
return callBackend("wxSetExtendedPermissions", { value });
}
/**
* Get diagnostics information
*/
export function getExtendedPermissions(): Promise<ExtendedPermissionsResponse> {
return callBackend("wxGetExtendedPermissions", {});
}
2020-08-13 20:43:51 +02:00
/**
* Get diagnostics information
*/
export function getWithdrawalDetailsForUri(
req: GetWithdrawalDetailsForUriRequest,
): Promise<WithdrawUriInfoResponse> {
return callBackend("getWithdrawalDetailsForUri", req);
}
/**
* Get diagnostics information
*/
export function getExchangeWithdrawalInfo(
req: GetExchangeWithdrawalInfo,
): Promise<ExchangeWithdrawDetails> {
return callBackend("getExchangeWithdrawalInfo", req);
}
2021-10-13 19:26:18 +02:00
export function getExchangeTos(
exchangeBaseUrl: string,
acceptedFormat: string[],
): Promise<GetExchangeTosResult> {
return callBackend("getExchangeTos", {
2021-11-15 15:18:58 +01:00
exchangeBaseUrl,
acceptedFormat,
2021-10-13 19:26:18 +02:00
});
}
2021-10-13 11:40:16 +02:00
export function getPendingOperations(): Promise<PendingOperationsResponse> {
return callBackend("getPendingOperations", {});
}
2021-11-15 15:18:58 +01:00
export function addExchange(req: AddExchangeRequest): Promise<void> {
2021-10-13 11:40:16 +02:00
return callBackend("addExchange", req);
}
export function prepareTip(req: PrepareTipRequest): Promise<PrepareTipResult> {
return callBackend("prepareTip", req);
}
export function acceptTip(req: AcceptTipRequest): Promise<void> {
return callBackend("acceptTip", req);
}
2021-12-01 18:57:37 +01:00
export function exportDB(): Promise<any> {
return callBackend("exportDb", {});
}
export function onUpdateNotification(messageTypes: Array<NotificationType>, doCallback: () => void): () => void {
// eslint-disable-next-line no-undef
const port = chrome.runtime.connect({ name: "notifications" });
const listener = (message: MessageFromBackend): void => {
const shouldNotify = messageTypes.includes(message.type)
console.log("Notification arrived, should notify?", shouldNotify, message.type, messageTypes)
if (shouldNotify) {
doCallback();
}
};
port.onMessage.addListener(listener);
return () => {
port.onMessage.removeListener(listener);
};
}