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 {
|
2023-01-10 21:11:34 +01:00
|
|
|
AbsoluteTime,
|
2022-11-16 20:04:52 +01:00
|
|
|
CoreApiResponse,
|
|
|
|
Logger,
|
2023-01-19 21:05:34 +01:00
|
|
|
LogLevel,
|
2022-11-16 20:04:52 +01:00
|
|
|
NotificationType,
|
2023-01-09 12:38:48 +01:00
|
|
|
TalerErrorCode,
|
|
|
|
TalerErrorDetail,
|
2023-02-17 20:29:09 +01:00
|
|
|
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
|
|
|
WalletCoreApiClient,
|
2022-10-16 23:11:34 +02:00
|
|
|
WalletCoreOpKeys,
|
|
|
|
WalletCoreRequestType,
|
2023-02-17 20:29:09 +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 {
|
2023-01-09 12:38:48 +01:00
|
|
|
MessageFromBackend,
|
|
|
|
MessageFromFrontendBackground,
|
2023-02-17 20:29:09 +01:00
|
|
|
MessageFromFrontendWallet,
|
2023-01-09 12:38:48 +01:00
|
|
|
} from "./platform/api.js";
|
|
|
|
import { platform } from "./platform/foreground.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: {
|
2022-12-22 16:13:59 +01:00
|
|
|
request: number[];
|
|
|
|
response: number;
|
|
|
|
};
|
2022-12-21 20:21:25 +01:00
|
|
|
resetDb: {
|
|
|
|
request: void;
|
|
|
|
response: void;
|
|
|
|
};
|
|
|
|
getDiagnostics: {
|
|
|
|
request: void;
|
|
|
|
response: WalletDiagnostics;
|
|
|
|
};
|
|
|
|
runGarbageCollector: {
|
|
|
|
request: void;
|
|
|
|
response: void;
|
|
|
|
};
|
2023-01-19 21:05:34 +01:00
|
|
|
setLoggingLevel: {
|
|
|
|
request: {
|
2023-01-20 19:41:08 +01:00
|
|
|
tag?: string;
|
|
|
|
level: LogLevel;
|
2023-01-19 21:05:34 +01:00
|
|
|
};
|
|
|
|
response: void;
|
|
|
|
};
|
2022-12-21 20:21:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-01-09 12:38:48 +01:00
|
|
|
export class BackgroundError extends Error {
|
|
|
|
public errorDetail: TalerErrorDetail;
|
|
|
|
|
2023-01-10 00:20:09 +01:00
|
|
|
constructor(title: string, e: TalerErrorDetail) {
|
|
|
|
super(title);
|
2023-01-09 12:38:48 +01:00
|
|
|
this.errorDetail = e;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
2023-01-09 12:38:48 +01:00
|
|
|
} catch (error) {
|
|
|
|
if (error instanceof Error) {
|
|
|
|
throw new BackgroundError(operation, {
|
|
|
|
code: TalerErrorCode.GENERIC_UNEXPECTED_REQUEST_ERROR,
|
2023-01-10 21:11:34 +01:00
|
|
|
when: AbsoluteTime.now(),
|
2023-01-09 12:38:48 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
throw error;
|
2022-12-21 20:21:25 +01:00
|
|
|
}
|
|
|
|
if (response.type === "error") {
|
2023-01-10 00:20:09 +01:00
|
|
|
throw new BackgroundError(
|
|
|
|
`Background operation "${operation}" failed`,
|
|
|
|
response.error,
|
|
|
|
);
|
2022-12-21 20:21:25 +01:00
|
|
|
}
|
2023-01-19 21:05:34 +01:00
|
|
|
logger.trace("response", response);
|
2022-12-21 20:21:25 +01:00
|
|
|
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) {
|
2023-04-19 17:42:47 +02:00
|
|
|
logger.error("Error calling backend", e);
|
2022-10-16 23:11:34 +02:00
|
|
|
throw new Error(`Error contacting backend: ${e}`);
|
|
|
|
}
|
|
|
|
if (response.type === "error") {
|
2023-01-10 00:20:09 +01:00
|
|
|
throw new BackgroundError(
|
|
|
|
`Wallet operation "${operation}" failed`,
|
|
|
|
response.error,
|
|
|
|
);
|
2022-10-16 23:11:34 +02:00
|
|
|
}
|
2023-01-19 21:05:34 +01:00
|
|
|
logger.trace("got response", response);
|
2022-10-16 23:11:34 +02:00
|
|
|
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,
|
|
|
|
},
|
|
|
|
};
|