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
|
|
|
*/
|
|
|
|
|
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 {
|
2022-11-16 20:04:52 +01: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-11-16 20:04:52 +01:00
|
|
|
TalerError,
|
|
|
|
WalletCoreApiClient,
|
2022-10-16 23:11:34 +02:00
|
|
|
WalletCoreOpKeys,
|
|
|
|
WalletCoreRequestType,
|
2022-11-16 20:04:52 +01: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";
|
2020-08-03 09:30:48 +02:00
|
|
|
|
2022-02-23 19:18:37 +01:00
|
|
|
/**
|
2022-03-22 21:16:38 +01:00
|
|
|
*
|
2022-06-26 20:52:32 +02:00
|
|
|
* @author Florian Dold
|
|
|
|
* @author sebasjm
|
2022-02-23 19:18:37 +01:00
|
|
|
*/
|
|
|
|
|
2020-08-03 09:30:48 +02:00
|
|
|
export interface ExtendedPermissionsResponse {
|
|
|
|
newValue: boolean;
|
|
|
|
}
|
2022-06-09 19:16:28 +02:00
|
|
|
const logger = new Logger("wxApi");
|
2020-08-03 09:30:48 +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
|
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;
|
|
|
|
}
|
|
|
|
|
2022-10-16 23:11:34 +02:00
|
|
|
/**
|
|
|
|
* @deprecated Use {@link WxWalletCoreApiClient} instead.
|
|
|
|
*/
|
2020-08-13 20:43:51 +02:00
|
|
|
async function callBackend(operation: string, payload: any): Promise<any> {
|
2022-03-23 14:50:12 +01:00
|
|
|
let response: CoreApiResponse;
|
|
|
|
try {
|
2022-03-25 20:57:27 +01:00
|
|
|
response = await platform.sendMessageToWalletBackground(operation, payload);
|
2022-03-23 14:50:12 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log("Error calling backend");
|
2022-03-23 21:24:23 +01:00
|
|
|
throw new Error(`Error contacting backend: ${e}`);
|
2022-03-23 14:50:12 +01:00
|
|
|
}
|
2022-06-09 19:16:28 +02:00
|
|
|
logger.info("got response", response);
|
2022-03-23 14:50:12 +01:00
|
|
|
if (response.type === "error") {
|
2022-03-23 20:20:39 +01:00
|
|
|
throw TalerError.fromUncheckedDetail(response.error);
|
2022-03-23 14:50:12 +01:00
|
|
|
}
|
|
|
|
return response.result;
|
2016-10-12 02:55:53 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 23:11:34 +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;
|
|
|
|
}
|
2016-11-13 08:16:12 +01:00
|
|
|
}
|
2017-05-01 04:33:47 +02:00
|
|
|
|
2022-10-25 17:23:08 +02:00
|
|
|
export class BackgroundApiClient {
|
|
|
|
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", {});
|
|
|
|
}
|
2020-11-18 12:44:06 +01:00
|
|
|
|
2022-10-25 17:23:08 +02:00
|
|
|
public toggleHeaderListener(
|
|
|
|
value: boolean,
|
|
|
|
): Promise<ExtendedPermissionsResponse> {
|
|
|
|
return callBackend("toggleHeaderListener", { value });
|
|
|
|
}
|
2020-11-18 12:44:06 +01:00
|
|
|
|
2022-10-25 17:23:08 +02:00
|
|
|
public runGarbageCollector(): Promise<void> {
|
|
|
|
return callBackend("run-gc", {});
|
|
|
|
}
|
2022-01-13 05:33:24 +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
|
2022-11-16 20:04:52 +01:00
|
|
|
if (!doCallback)
|
|
|
|
return () => {
|
|
|
|
return;
|
|
|
|
};
|
2022-03-25 20:57:27 +01:00
|
|
|
const onNewMessage = (message: MessageFromBackend): void => {
|
2022-03-22 21:16:38 +01:00
|
|
|
const shouldNotify = messageTypes.includes(message.type);
|
2021-11-24 12:57:26 +01:00
|
|
|
if (shouldNotify) {
|
2021-11-19 18:51:27 +01:00
|
|
|
doCallback();
|
|
|
|
}
|
2020-05-04 15:22:54 +02:00
|
|
|
};
|
2022-03-25 20:57:27 +01:00
|
|
|
return platform.listenToWalletBackground(onNewMessage);
|
2020-05-15 09:23:35 +02:00
|
|
|
}
|
2022-08-31 05:20:35 +02:00
|
|
|
|
2022-12-15 21:11:24 +01:00
|
|
|
export type WxApiType = {
|
|
|
|
wallet: WalletCoreApiClient;
|
|
|
|
background: BackgroundApiClient;
|
|
|
|
listener: {
|
|
|
|
onUpdateNotification: typeof onUpdateNotification;
|
2022-12-15 21:12:03 +01:00
|
|
|
};
|
|
|
|
};
|
2022-12-15 21:11:24 +01:00
|
|
|
|
2022-10-25 17:23:08 +02:00
|
|
|
export const wxApi = {
|
|
|
|
wallet: new WxWalletCoreApiClient(),
|
|
|
|
background: new BackgroundApiClient(),
|
|
|
|
listener: {
|
2022-11-16 20:04:52 +01:00
|
|
|
onUpdateNotification,
|
|
|
|
},
|
|
|
|
};
|