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

301 lines
8.4 KiB
TypeScript
Raw Normal View History

2016-01-05 14:20:13 +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-01-05 14:20:13 +01:00
*/
/**
* Messaging for the WebExtensions wallet. Should contain
* parts that are specific for WebExtensions, but as little business
* logic as possible.
*/
/**
* Imports.
*/
2021-03-27 14:35:58 +01:00
import {
classifyTalerUri,
CoreApiResponse,
CoreApiResponseSuccess, TalerErrorCode,
2021-03-27 14:35:58 +01:00
TalerUriType,
WalletDiagnostics
2021-03-27 14:35:58 +01:00
} from "@gnu-taler/taler-util";
import {
2022-02-03 14:36:37 +01:00
DbAccess,
deleteTalerDatabase,
makeErrorDetail,
2022-02-03 14:36:37 +01:00
OpenedPromise,
openPromise,
2022-02-03 14:36:37 +01:00
openTalerDatabase,
Wallet,
WalletStoresV1
} from "@gnu-taler/taler-wallet-core";
import { SetTimeoutTimerAPI, TimerGroup } from "@gnu-taler/taler-wallet-core/src/util/timer";
2022-03-29 04:41:07 +02:00
import { BrowserCryptoWorkerFactory } from "./browserCryptoWorkerFactory.js";
import { BrowserHttpLib } from "./browserHttpLib.js";
import { getReadRequestPermissions } from "./permissions.js";
import { MessageFromBackend, platform } from "./platform/api.js";
import { SynchronousCryptoWorkerFactory } from "./serviceWorkerCryptoWorkerFactory.js";
import { ServiceWorkerHttpLib } from "./serviceWorkerHttpLib.js";
import { ServiceWorkerTimerAPI } from "./serviceWorkerTimerAPI.js";
2020-04-06 20:02:01 +02:00
/**
* Currently active wallet instance. Might be unloaded and
* re-instantiated when the database is reset.
*
2021-07-14 14:34:58 +02:00
* FIXME: Maybe move the wallet resetting into the Wallet class?
2020-04-06 20:02:01 +02:00
*/
let currentWallet: Wallet | undefined;
2020-04-06 20:02:01 +02:00
2021-06-09 15:14:17 +02:00
let currentDatabase: DbAccess<typeof WalletStoresV1> | undefined;
2020-04-06 20:02:01 +02:00
/**
2022-02-03 14:36:37 +01:00
* Last version of an outdated DB, if applicable.
2020-04-06 20:02:01 +02:00
*/
let outdatedDbVersion: number | undefined;
2020-08-12 09:11:00 +02:00
const walletInit: OpenedPromise<void> = openPromise<void>();
2020-04-06 20:02:01 +02:00
async function getDiagnostics(): Promise<WalletDiagnostics> {
const manifestData = platform.getWalletVersion();
const errors: string[] = [];
let firefoxIdbProblem = false;
let dbOutdated = false;
try {
await walletInit.promise;
} catch (e) {
errors.push("Error during wallet initialization: " + e);
if (
currentDatabase === undefined &&
outdatedDbVersion === undefined &&
platform.isFirefox()
) {
firefoxIdbProblem = true;
}
}
if (!currentWallet) {
errors.push("Could not create wallet backend.");
}
if (!currentDatabase) {
errors.push("Could not open database");
}
if (outdatedDbVersion !== undefined) {
errors.push(`Outdated DB version: ${outdatedDbVersion}`);
dbOutdated = true;
}
const diagnostics: WalletDiagnostics = {
walletManifestDisplayVersion: manifestData.version_name || "(undefined)",
walletManifestVersion: manifestData.version,
errors,
firefoxIdbProblem,
dbOutdated,
};
return diagnostics;
}
2019-08-24 19:31:24 +02:00
async function dispatch(
req: any,
sender: any,
sendResponse: any,
): Promise<void> {
let r: CoreApiResponse;
const wrapResponse = (result: unknown): CoreApiResponseSuccess => {
return {
type: "response",
id: req.id,
operation: req.operation,
result,
};
};
switch (req.operation) {
case "wxGetDiagnostics": {
r = wrapResponse(await getDiagnostics());
break;
}
case "reset-db": {
await deleteTalerDatabase(indexedDB as any);
r = wrapResponse(await reinitWallet());
break;
}
case "wxGetExtendedPermissions": {
const res = await platform.getPermissionsApi().contains(getReadRequestPermissions());
r = wrapResponse({ newValue: res });
break;
}
case "wxSetExtendedPermissions": {
const newVal = req.payload.value;
console.log("new extended permissions value", newVal);
if (newVal) {
platform.registerTalerHeaderListener(parseTalerUriAndRedirect);
r = wrapResponse({ newValue: true });
} else {
const rem = await platform.getPermissionsApi().remove(getReadRequestPermissions());
console.log("permissions removed:", rem);
r = wrapResponse({ newVal: false });
}
break;
}
2021-05-07 15:38:28 +02:00
default: {
const w = currentWallet;
if (!w) {
r = {
type: "error",
id: req.id,
operation: req.operation,
error: makeErrorDetail(
TalerErrorCode.WALLET_CORE_NOT_AVAILABLE,
{},
"wallet core not available",
),
};
break;
}
r = await w.handleCoreApiRequest(req.operation, req.id, req.payload);
break;
2021-05-07 15:38:28 +02:00
}
2020-08-13 20:43:51 +02:00
}
2016-11-20 08:58:04 +01:00
try {
2020-08-13 20:43:51 +02:00
sendResponse(r);
2016-11-20 08:58:04 +01:00
} catch (e) {
2020-08-13 20:43:51 +02:00
// might fail if tab disconnected
}
}
2020-04-06 20:02:01 +02:00
async function reinitWallet(): Promise<void> {
2017-06-05 02:00:03 +02:00
if (currentWallet) {
currentWallet.stop();
currentWallet = undefined;
}
currentDatabase = undefined;
// setBadgeText({ text: "" });
2017-06-05 02:00:03 +02:00
try {
currentDatabase = await openTalerDatabase(indexedDB as any, reinitWallet);
2017-06-05 02:00:03 +02:00
} catch (e) {
console.error("could not open database", e);
walletInit.reject(e);
2017-06-05 02:00:03 +02:00
return;
}
let httpLib;
let cryptoWorker;
let timer;
if (platform.useServiceWorkerAsBackgroundProcess()) {
2022-02-03 14:36:37 +01:00
httpLib = new ServiceWorkerHttpLib();
cryptoWorker = new SynchronousCryptoWorkerFactory();
timer = new ServiceWorkerTimerAPI();
} else {
2022-02-03 14:36:37 +01:00
httpLib = new BrowserHttpLib();
cryptoWorker = new BrowserCryptoWorkerFactory();
timer = new SetTimeoutTimerAPI();
}
2017-06-05 02:00:03 +02:00
console.log("setting wallet");
const wallet = await Wallet.create(currentDatabase, httpLib, timer, cryptoWorker);
2021-08-09 15:42:56 +02:00
try {
await wallet.handleCoreApiRequest("initWallet", "native-init", {});
} catch (e) {
console.error("could not initialize wallet", e);
walletInit.reject(e);
return;
}
2020-05-04 14:11:22 +02:00
wallet.addNotificationListener((x) => {
const message: MessageFromBackend = { type: x.type };
platform.sendMessageToAllChannels(message)
2020-05-04 14:11:22 +02:00
});
wallet.runTaskLoop().catch((e) => {
console.log("error during wallet task loop", e);
2019-12-02 17:35:47 +01:00
});
2017-06-05 02:00:03 +02:00
// Useful for debugging in the background page.
if (typeof window !== "undefined") {
(window as any).talerWallet = wallet;
}
2017-06-05 02:00:03 +02:00
currentWallet = wallet;
return walletInit.resolve();
2017-06-05 02:00:03 +02:00
}
2022-03-29 15:02:35 +02:00
function parseTalerUriAndRedirect(tabId: number, talerUri: string): void {
const uriType = classifyTalerUri(talerUri);
switch (uriType) {
case TalerUriType.TalerWithdraw:
return platform.redirectTabToWalletPage(
tabId,
`/cta/withdraw?talerWithdrawUri=${talerUri}`,
);
case TalerUriType.TalerPay:
return platform.redirectTabToWalletPage(
tabId,
`/cta/pay?talerPayUri=${talerUri}`,
);
case TalerUriType.TalerTip:
return platform.redirectTabToWalletPage(
tabId,
`/cta/tip?talerTipUri=${talerUri}`,
);
case TalerUriType.TalerRefund:
return platform.redirectTabToWalletPage(
tabId,
`/cta/refund?talerRefundUri=${talerUri}`,
);
case TalerUriType.TalerNotifyReserve:
// FIXME: Is this still useful?
// handleNotifyReserve(w);
break;
default:
console.warn(
"Response with HTTP 402 has Taler header, but header value is not a taler:// URI.",
);
break;
}
}
2019-11-02 00:46:57 +01:00
/**
* Main function to run for the WebExtension backend.
*
* Sets up all event handlers and other machinery.
*/
2020-04-06 20:02:01 +02:00
export async function wxMain(): Promise<void> {
const afterWalletIsInitialized = reinitWallet();
2016-11-20 08:58:04 +01:00
platform.registerReloadOnNewVersion();
2016-11-20 08:58:04 +01:00
// Handlers for messages coming directly from the content
// script on the page
2022-03-25 20:57:27 +01:00
platform.listenToAllChannels((message, sender, callback) => {
afterWalletIsInitialized.then(() => {
dispatch(message, sender, callback);
2022-02-03 14:36:37 +01:00
});
})
2016-10-12 02:55:53 +02:00
platform.registerAllIncomingConnections()
2020-05-04 14:11:22 +02:00
2020-06-03 12:51:09 +02:00
try {
platform.registerTalerHeaderListener(parseTalerUriAndRedirect);
2020-06-03 12:51:09 +02:00
} catch (e) {
console.log(e);
}
2020-05-04 14:11:22 +02:00
2020-06-03 12:51:09 +02:00
// On platforms that support it, also listen to external
// modification of permissions.
2022-03-25 20:57:27 +01:00
platform.getPermissionsApi().addPermissionsListener((perm, lastError) => {
if (lastError) {
console.error(lastError);
2020-05-01 11:34:12 +02:00
return;
}
platform.registerTalerHeaderListener(parseTalerUriAndRedirect);
2020-05-01 11:34:12 +02:00
});
2016-10-11 20:26:37 +02:00
}