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-12-21 20:21:25 +01:00
|
|
|
import {
|
|
|
|
MessageFromBackend,
|
|
|
|
MessageFromFrontendBackground,
|
|
|
|
MessageFromFrontendWallet,
|
|
|
|
platform,
|
|
|
|
} from "./platform/api.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 sebasjm
|
2022-02-23 19:18:37 +01:00
|
|
|
*/
|
|
|
|
|
2022-12-21 20:21:25 +01:00
|
|
|
const logger = new Logger("wxApi");
|
|
|
|
|
2020-08-03 09:30:48 +02:00
|
|
|
export interface ExtendedPermissionsResponse {
|
|
|
|
newValue: boolean;
|
|
|
|
}
|
|
|
|
|
2022-12-21 20:21:25 +01:00
|
|
|
export interface BackgroundOperations {
|
2022-12-21 20:36:24 +01:00
|
|
|
freeze: {
|
|
|
|
request: number;
|
|
|
|
response: void;
|
|
|
|
};
|
|
|
|
sum: {
|
|
|
|
request: number[],
|
|
|
|
response: number,
|
|
|
|
}
|
2022-12-21 20:21:25 +01:00
|
|
|
resetDb: {
|
|
|
|
request: void;
|
|
|
|
response: void;
|
|
|
|
};
|
|
|
|
containsHeaderListener: {
|
|
|
|
request: void;
|
|
|
|
response: ExtendedPermissionsResponse;
|
|
|
|
};
|
|
|
|
getDiagnostics: {
|
|
|
|
request: void;
|
|
|
|
response: WalletDiagnostics;
|
|
|
|
};
|
|
|
|
toggleHeaderListener: {
|
|
|
|
request: boolean;
|
|
|
|
response: ExtendedPermissionsResponse;
|
|
|
|
};
|
|
|
|
runGarbageCollector: {
|
|
|
|
request: void;
|
|
|
|
response: void;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
export interface BackgroundApiClient {
|
|
|
|
call<Op extends keyof BackgroundOperations>(
|
|
|
|
operation: Op,
|
|
|
|
payload: BackgroundOperations[Op]["request"],
|
|
|
|
): Promise<BackgroundOperations[Op]["response"]>;
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
|
|
|
|
2022-10-16 23:11:34 +02:00
|
|
|
/**
|
2022-12-21 20:21:25 +01:00
|
|
|
* BackgroundApiClient integration with browser platform
|
2022-10-16 23:11:34 +02:00
|
|
|
*/
|
2022-12-21 20:21:25 +01:00
|
|
|
class BackgroundApiClientImpl implements BackgroundApiClient {
|
|
|
|
async call<Op extends keyof BackgroundOperations>(
|
|
|
|
operation: Op,
|
|
|
|
payload: BackgroundOperations[Op]["request"],
|
|
|
|
): Promise<BackgroundOperations[Op]["response"]> {
|
|
|
|
let response: CoreApiResponse;
|
|
|
|
|
|
|
|
const message: MessageFromFrontendBackground<Op> = {
|
|
|
|
channel: "background",
|
|
|
|
operation,
|
|
|
|
payload,
|
|
|
|
};
|
|
|
|
|
|
|
|
try {
|
|
|
|
response = await platform.sendMessageToBackground(message);
|
|
|
|
} 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-03-23 14:50:12 +01:00
|
|
|
}
|
2016-10-12 02:55:53 +02:00
|
|
|
}
|
|
|
|
|
2022-12-21 20:21:25 +01:00
|
|
|
/**
|
|
|
|
* WalletCoreApiClient integration with browser platform
|
|
|
|
*/
|
|
|
|
class WalletApiClientImpl implements WalletCoreApiClient {
|
2022-10-16 23:11:34 +02:00
|
|
|
async call<Op extends WalletCoreOpKeys>(
|
|
|
|
operation: Op,
|
|
|
|
payload: WalletCoreRequestType<Op>,
|
|
|
|
): Promise<WalletCoreResponseType<Op>> {
|
|
|
|
let response: CoreApiResponse;
|
|
|
|
try {
|
2022-12-21 20:21:25 +01:00
|
|
|
const message: MessageFromFrontendWallet<Op> = {
|
|
|
|
channel: "wallet",
|
2022-10-16 23:11:34 +02:00
|
|
|
operation,
|
|
|
|
payload,
|
2022-12-21 20:21:25 +01:00
|
|
|
};
|
|
|
|
response = await platform.sendMessageToBackground(message);
|
2022-10-16 23:11:34 +02:00
|
|
|
} 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
|
|
|
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 = {
|
2022-12-21 20:21:25 +01:00
|
|
|
wallet: new WalletApiClientImpl(),
|
|
|
|
background: new BackgroundApiClientImpl(),
|
2022-10-25 17:23:08 +02:00
|
|
|
listener: {
|
2022-11-16 20:04:52 +01:00
|
|
|
onUpdateNotification,
|
|
|
|
},
|
|
|
|
};
|