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

159 lines
4.0 KiB
TypeScript
Raw Normal View History

2016-02-19 04:23:00 +01:00
/*
2022-06-06 17:05:26 +02:00
This file is part of GNU Taler
(C) 2022 Taler Systems S.A.
2016-02-19 04:23:00 +01:00
2022-06-06 17:05:26 +02:00
GNU Taler is free software; you can redistribute it and/or modify it under the
2016-02-19 04:23:00 +01:00
terms of the GNU General Public License as published by the Free Software
Foundation; either version 3, or (at your option) any later version.
2022-06-06 17:05:26 +02:00
GNU Taler is distributed in the hope that it will be useful, but WITHOUT ANY
2016-02-19 04:23:00 +01:00
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
2022-06-06 17:05:26 +02:00
GNU 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 {
2022-10-25 17:23:08 +02:00
CoreApiResponse, Logger, NotificationType, WalletDiagnostics
2021-03-27 14:35:58 +01:00
} from "@gnu-taler/taler-util";
2021-11-15 15:18:58 +01:00
import {
2022-10-25 17:23:08 +02:00
TalerError, WalletCoreApiClient,
WalletCoreOpKeys,
WalletCoreRequestType,
2022-10-25 17:23:08 +02:00
WalletCoreResponseType
2021-11-15 15:18:58 +01:00
} from "@gnu-taler/taler-wallet-core";
2022-09-23 20:18:18 +02:00
import { MessageFromBackend, platform } from "./platform/api.js";
2022-10-25 17:23:08 +02:00
import { nullFunction } from "./test-utils.js";
2022-02-23 19:18:37 +01:00
/**
*
2022-06-26 20:52:32 +02:00
* @author Florian Dold
* @author sebasjm
2022-02-23 19:18:37 +01:00
*/
export interface ExtendedPermissionsResponse {
newValue: boolean;
}
2022-06-09 19:16:28 +02:00
const logger = new Logger("wxApi");
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;
}
/**
* @deprecated Use {@link WxWalletCoreApiClient} instead.
*/
2020-08-13 20:43:51 +02:00
async function callBackend(operation: string, payload: any): Promise<any> {
let response: CoreApiResponse;
try {
2022-03-25 20:57:27 +01:00
response = await platform.sendMessageToWalletBackground(operation, payload);
} catch (e) {
console.log("Error calling backend");
2022-03-23 21:24:23 +01:00
throw new Error(`Error contacting backend: ${e}`);
}
2022-06-09 19:16:28 +02:00
logger.info("got response", response);
if (response.type === "error") {
throw TalerError.fromUncheckedDetail(response.error);
}
return response.result;
2016-10-12 02:55:53 +02:00
}
export class WxWalletCoreApiClient implements WalletCoreApiClient {
async call<Op extends WalletCoreOpKeys>(
operation: Op,
payload: WalletCoreRequestType<Op>,
): Promise<WalletCoreResponseType<Op>> {
let response: CoreApiResponse;
try {
response = await platform.sendMessageToWalletBackground(
operation,
payload,
);
} catch (e) {
console.log("Error calling backend");
throw new Error(`Error contacting backend: ${e}`);
}
logger.info("got response", response);
if (response.type === "error") {
throw TalerError.fromUncheckedDetail(response.error);
}
return response.result as any;
}
}
2022-10-25 17:23:08 +02:00
export class BackgroundApiClient {
2022-10-25 17:23:08 +02:00
public resetDb(): Promise<void> {
return callBackend("reset-db", {});
}
2021-10-13 11:40:16 +02:00
2022-10-25 17:23:08 +02:00
public containsHeaderListener(): Promise<ExtendedPermissionsResponse> {
return callBackend("containsHeaderListener", {});
}
2022-05-03 00:21:34 +02:00
2022-10-25 17:23:08 +02:00
public getDiagnostics(): Promise<WalletDiagnostics> {
return callBackend("wxGetDiagnostics", {});
}
2022-10-25 17:23:08 +02:00
public toggleHeaderListener(
value: boolean,
): Promise<ExtendedPermissionsResponse> {
return callBackend("toggleHeaderListener", { value });
}
2022-10-25 17:23:08 +02:00
public runGarbageCollector(): Promise<void> {
return callBackend("run-gc", {});
}
2021-12-01 18:57:37 +01:00
}
2022-10-25 17:23:08 +02:00
function onUpdateNotification(
2022-03-23 21:24:23 +01:00
messageTypes: Array<NotificationType>,
2022-10-25 17:23:08 +02:00
doCallback: undefined | (() => void),
2022-03-23 21:24:23 +01:00
): () => void {
2022-10-25 17:23:08 +02:00
//if no callback, then ignore
if (!doCallback) return () => {
return
};
2022-03-25 20:57:27 +01:00
const onNewMessage = (message: MessageFromBackend): void => {
const shouldNotify = messageTypes.includes(message.type);
if (shouldNotify) {
doCallback();
}
};
2022-03-25 20:57:27 +01:00
return platform.listenToWalletBackground(onNewMessage);
}
2022-08-31 05:20:35 +02:00
2022-10-25 17:23:08 +02:00
export const wxApi = {
wallet: new WxWalletCoreApiClient(),
background: new BackgroundApiClient(),
listener: {
onUpdateNotification
}
2022-08-31 05:20:35 +02:00
}