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
|
|
|
*/
|
|
|
|
|
2017-05-24 16:33:10 +02: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,
|
2020-08-21 17:26:25 +02:00
|
|
|
CoreApiResponse,
|
2022-03-23 14:50:12 +01:00
|
|
|
CoreApiResponseSuccess, TalerErrorCode,
|
2021-03-27 14:35:58 +01:00
|
|
|
TalerUriType,
|
2022-03-23 14:50:12 +01:00
|
|
|
WalletDiagnostics
|
2021-03-27 14:35:58 +01:00
|
|
|
} from "@gnu-taler/taler-util";
|
2022-01-16 21:54:48 +01:00
|
|
|
import {
|
2022-02-03 14:36:37 +01:00
|
|
|
DbAccess,
|
|
|
|
deleteTalerDatabase,
|
2022-03-22 21:16:38 +01:00
|
|
|
makeErrorDetail,
|
2022-02-03 14:36:37 +01:00
|
|
|
OpenedPromise,
|
2022-01-16 21:54:48 +01:00
|
|
|
openPromise,
|
2022-02-03 14:36:37 +01:00
|
|
|
openTalerDatabase,
|
|
|
|
Wallet,
|
2022-03-23 14:50:12 +01:00
|
|
|
WalletStoresV1
|
2022-01-16 21:54:48 +01:00
|
|
|
} from "@gnu-taler/taler-wallet-core";
|
2022-04-13 18:54:54 +02:00
|
|
|
import { SetTimeoutTimerAPI } from "@gnu-taler/taler-wallet-core";
|
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";
|
2022-04-11 20:11:44 +02:00
|
|
|
import { ServiceWorkerTimerAPI } from "./serviceWorkerTimerAPI.js";
|
2020-08-03 09:30:48 +02:00
|
|
|
|
2020-04-06 20:02:01 +02:00
|
|
|
/**
|
|
|
|
* Currently active wallet instance. Might be unloaded and
|
|
|
|
* re-instantiated when the database is reset.
|
2021-08-19 15:12:33 +02:00
|
|
|
*
|
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
|
|
|
*/
|
2021-06-17 21:06:45 +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
|
|
|
|
2020-08-21 17:26:25 +02:00
|
|
|
async function getDiagnostics(): Promise<WalletDiagnostics> {
|
2022-03-23 14:50:12 +01:00
|
|
|
const manifestData = platform.getWalletVersion();
|
2020-08-21 17:26:25 +02:00
|
|
|
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 &&
|
2022-03-23 14:50:12 +01:00
|
|
|
platform.isFirefox()
|
2020-08-21 17:26:25 +02:00
|
|
|
) {
|
|
|
|
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> {
|
2020-08-21 17:26:25 +02:00
|
|
|
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;
|
|
|
|
}
|
2021-06-04 15:47:51 +02:00
|
|
|
case "reset-db": {
|
2022-01-05 17:28:56 +01:00
|
|
|
await deleteTalerDatabase(indexedDB as any);
|
2021-06-04 15:47:51 +02:00
|
|
|
r = wrapResponse(await reinitWallet());
|
|
|
|
break;
|
|
|
|
}
|
2020-08-21 17:26:25 +02:00
|
|
|
case "wxGetExtendedPermissions": {
|
2022-03-23 14:50:12 +01:00
|
|
|
const res = await platform.getPermissionsApi().contains(getReadRequestPermissions());
|
2020-08-21 17:26:25 +02:00
|
|
|
r = wrapResponse({ newValue: res });
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case "wxSetExtendedPermissions": {
|
|
|
|
const newVal = req.payload.value;
|
|
|
|
console.log("new extended permissions value", newVal);
|
|
|
|
if (newVal) {
|
2022-03-23 14:50:12 +01:00
|
|
|
platform.registerTalerHeaderListener(parseTalerUriAndRedirect);
|
2020-08-21 17:26:25 +02:00
|
|
|
r = wrapResponse({ newValue: true });
|
|
|
|
} else {
|
2022-03-23 14:50:12 +01:00
|
|
|
const rem = await platform.getPermissionsApi().remove(getReadRequestPermissions());
|
|
|
|
console.log("permissions removed:", rem);
|
2020-08-21 17:26:25 +02:00
|
|
|
r = wrapResponse({ newVal: false });
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-05-07 15:38:28 +02:00
|
|
|
default: {
|
2020-08-21 17:26:25 +02:00
|
|
|
const w = currentWallet;
|
|
|
|
if (!w) {
|
|
|
|
r = {
|
|
|
|
type: "error",
|
|
|
|
id: req.id,
|
|
|
|
operation: req.operation,
|
2022-03-22 21:16:38 +01:00
|
|
|
error: makeErrorDetail(
|
2020-08-21 17:26:25 +02:00
|
|
|
TalerErrorCode.WALLET_CORE_NOT_AVAILABLE,
|
|
|
|
{},
|
2022-03-22 21:16:38 +01:00
|
|
|
"wallet core not available",
|
2020-08-21 17:26:25 +02:00
|
|
|
),
|
|
|
|
};
|
|
|
|
break;
|
|
|
|
}
|
2021-06-17 21:06:45 +02:00
|
|
|
r = await w.handleCoreApiRequest(req.operation, req.id, req.payload);
|
2020-08-21 17:26:25 +02:00
|
|
|
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
|
2016-02-17 15:56:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2019-09-05 16:10:53 +02:00
|
|
|
currentDatabase = undefined;
|
2022-03-23 14:50:12 +01:00
|
|
|
// setBadgeText({ text: "" });
|
2017-06-05 02:00:03 +02:00
|
|
|
try {
|
2022-01-05 17:28:56 +01:00
|
|
|
currentDatabase = await openTalerDatabase(indexedDB as any, reinitWallet);
|
2017-06-05 02:00:03 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error("could not open database", e);
|
2019-09-05 16:10:53 +02:00
|
|
|
walletInit.reject(e);
|
2017-06-05 02:00:03 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-01-16 21:54:48 +01:00
|
|
|
let httpLib;
|
|
|
|
let cryptoWorker;
|
2022-04-11 20:11:44 +02:00
|
|
|
let timer;
|
2022-01-16 21:54:48 +01:00
|
|
|
|
2022-03-23 14:50:12 +01:00
|
|
|
if (platform.useServiceWorkerAsBackgroundProcess()) {
|
2022-02-03 14:36:37 +01:00
|
|
|
httpLib = new ServiceWorkerHttpLib();
|
2022-01-16 21:54:48 +01:00
|
|
|
cryptoWorker = new SynchronousCryptoWorkerFactory();
|
2022-04-11 20:11:44 +02:00
|
|
|
timer = new ServiceWorkerTimerAPI();
|
2022-01-16 21:54:48 +01:00
|
|
|
} else {
|
2022-02-03 14:36:37 +01:00
|
|
|
httpLib = new BrowserHttpLib();
|
|
|
|
cryptoWorker = new BrowserCryptoWorkerFactory();
|
2022-04-11 20:11:44 +02:00
|
|
|
timer = new SetTimeoutTimerAPI();
|
2022-01-16 21:54:48 +01:00
|
|
|
}
|
|
|
|
|
2017-06-05 02:00:03 +02:00
|
|
|
console.log("setting wallet");
|
2022-04-11 20:11:44 +02:00
|
|
|
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) => {
|
2022-03-23 14:50:12 +01:00
|
|
|
const message: MessageFromBackend = { type: x.type };
|
|
|
|
platform.sendMessageToAllChannels(message)
|
2020-05-04 14:11:22 +02:00
|
|
|
});
|
2021-08-19 15:12:33 +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.
|
2022-01-16 21:54:48 +01:00
|
|
|
if (typeof window !== "undefined") {
|
|
|
|
(window as any).talerWallet = wallet;
|
|
|
|
}
|
2017-06-05 02:00:03 +02:00
|
|
|
currentWallet = wallet;
|
2022-04-11 20:11:44 +02:00
|
|
|
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 {
|
2022-03-23 14:50:12 +01:00
|
|
|
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;
|
2020-05-01 10:46:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
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> {
|
2022-01-16 21:54:48 +01:00
|
|
|
const afterWalletIsInitialized = reinitWallet();
|
2016-11-20 08:58:04 +01:00
|
|
|
|
2022-03-23 14:50:12 +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) => {
|
2022-01-16 21:54:48 +01:00
|
|
|
afterWalletIsInitialized.then(() => {
|
2022-03-23 14:50:12 +01:00
|
|
|
dispatch(message, sender, callback);
|
2022-02-03 14:36:37 +01:00
|
|
|
});
|
2022-03-23 14:50:12 +01:00
|
|
|
})
|
2016-10-12 02:55:53 +02:00
|
|
|
|
2022-03-23 14:50:12 +01:00
|
|
|
platform.registerAllIncomingConnections()
|
2020-05-04 14:11:22 +02:00
|
|
|
|
2020-06-03 12:51:09 +02:00
|
|
|
try {
|
2022-03-23 14:50:12 +01:00
|
|
|
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) => {
|
2022-03-23 14:50:12 +01:00
|
|
|
if (lastError) {
|
|
|
|
console.error(lastError);
|
2020-05-01 11:34:12 +02:00
|
|
|
return;
|
|
|
|
}
|
2022-03-23 14:50:12 +01:00
|
|
|
platform.registerTalerHeaderListener(parseTalerUriAndRedirect);
|
2020-05-01 11:34:12 +02:00
|
|
|
});
|
2016-10-11 20:26:37 +02:00
|
|
|
}
|