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.
|
|
|
|
*/
|
|
|
|
|
2016-01-05 14:20:13 +01:00
|
|
|
|
2017-05-24 16:33:10 +02:00
|
|
|
/**
|
|
|
|
* Imports.
|
|
|
|
*/
|
2017-05-28 23:15:41 +02:00
|
|
|
import { BrowserHttpLib } from "../http";
|
|
|
|
import * as logging from "../logging";
|
2018-01-03 14:42:06 +01:00
|
|
|
|
|
|
|
import { AmountJson } from "../amounts";
|
|
|
|
|
2017-05-28 01:10:54 +02:00
|
|
|
import {
|
2017-08-14 04:16:12 +02:00
|
|
|
ConfirmReserveRequest,
|
|
|
|
CreateReserveRequest,
|
2017-05-28 01:10:54 +02:00
|
|
|
Notifier,
|
2017-08-14 04:16:12 +02:00
|
|
|
ReturnCoinsRequest,
|
2018-01-03 14:42:06 +01:00
|
|
|
} from "../walletTypes";
|
|
|
|
|
2016-09-06 14:39:45 +02:00
|
|
|
import {
|
2017-05-24 16:33:10 +02:00
|
|
|
Wallet,
|
2017-05-28 23:15:41 +02:00
|
|
|
} from "../wallet";
|
|
|
|
|
2018-02-20 15:12:45 +01:00
|
|
|
import { isFirefox } from "./compat";
|
|
|
|
|
2018-01-04 11:35:04 +01:00
|
|
|
import {
|
2018-01-19 01:27:27 +01:00
|
|
|
PurchaseRecord,
|
2018-01-04 11:35:04 +01:00
|
|
|
WALLET_DB_VERSION,
|
|
|
|
} from "../dbTypes";
|
|
|
|
|
2019-07-21 23:50:10 +02:00
|
|
|
import { openTalerDb, exportDb, importDb, deleteDb } from "../db";
|
|
|
|
|
2017-11-30 04:07:36 +01:00
|
|
|
|
2017-05-28 23:15:41 +02:00
|
|
|
import { ChromeBadge } from "./chromeBadge";
|
2017-05-31 16:04:14 +02:00
|
|
|
import { MessageType } from "./messages";
|
2017-06-05 03:20:28 +02:00
|
|
|
import * as wxApi from "./wxApi";
|
2017-05-30 18:33:28 +02:00
|
|
|
|
2017-05-28 23:15:41 +02:00
|
|
|
import URI = require("urijs");
|
2017-05-24 16:33:10 +02:00
|
|
|
import Port = chrome.runtime.Port;
|
2017-05-28 01:10:54 +02:00
|
|
|
import MessageSender = chrome.runtime.MessageSender;
|
2018-01-17 03:49:54 +01:00
|
|
|
import { TipToken } from "../talerTypes";
|
2016-02-09 21:56:06 +01:00
|
|
|
|
2017-06-05 03:20:28 +02:00
|
|
|
const NeedsWallet = Symbol("NeedsWallet");
|
|
|
|
|
|
|
|
function handleMessage(sender: MessageSender,
|
2017-05-31 16:04:14 +02:00
|
|
|
type: MessageType, detail: any): any {
|
|
|
|
function assertNotFound(t: never): never {
|
|
|
|
console.error(`Request type ${t as string} unknown`);
|
|
|
|
console.error(`Request detail was ${detail}`);
|
|
|
|
return { error: "request unknown", requestType: type } as never;
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
function needsWallet(): Wallet {
|
|
|
|
if (!currentWallet) {
|
|
|
|
throw NeedsWallet;
|
|
|
|
}
|
|
|
|
return currentWallet;
|
|
|
|
}
|
2017-05-31 16:04:14 +02:00
|
|
|
switch (type) {
|
2017-06-05 03:20:28 +02:00
|
|
|
case "balances": {
|
|
|
|
return needsWallet().getBalances();
|
|
|
|
}
|
|
|
|
case "dump-db": {
|
|
|
|
const db = needsWallet().db;
|
2016-02-17 15:56:48 +01:00
|
|
|
return exportDb(db);
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
|
|
|
case "import-db": {
|
|
|
|
const db = needsWallet().db;
|
2017-04-13 15:05:38 +02:00
|
|
|
return importDb(db, detail.dump);
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
|
|
|
case "ping": {
|
2016-10-11 20:26:37 +02:00
|
|
|
return Promise.resolve();
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
|
|
|
case "reset-db": {
|
|
|
|
if (currentWallet) {
|
|
|
|
const db = currentWallet.db;
|
2017-05-28 01:10:54 +02:00
|
|
|
const tx = db.transaction(Array.from(db.objectStoreNames), "readwrite");
|
|
|
|
// tslint:disable-next-line:prefer-for-of
|
2016-03-02 00:47:00 +01:00
|
|
|
for (let i = 0; i < db.objectStoreNames.length; i++) {
|
|
|
|
tx.objectStore(db.objectStoreNames[i]).clear();
|
|
|
|
}
|
2016-01-06 15:39:22 +01:00
|
|
|
}
|
2019-07-21 23:50:10 +02:00
|
|
|
deleteDb(indexedDB);
|
2018-02-20 16:17:05 +01:00
|
|
|
setBadgeText({ text: "" });
|
2016-01-06 15:39:22 +01:00
|
|
|
console.log("reset done");
|
2017-06-05 03:20:28 +02:00
|
|
|
if (!currentWallet) {
|
|
|
|
reinitWallet();
|
|
|
|
}
|
2016-02-17 15:56:48 +01:00
|
|
|
return Promise.resolve({});
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
2017-05-31 16:04:14 +02:00
|
|
|
case "create-reserve": {
|
2016-02-09 21:56:06 +01:00
|
|
|
const d = {
|
|
|
|
amount: detail.amount,
|
2017-05-28 01:10:54 +02:00
|
|
|
exchange: detail.exchange,
|
2017-07-20 02:17:55 +02:00
|
|
|
senderWire: detail.senderWire,
|
2016-02-09 21:56:06 +01:00
|
|
|
};
|
|
|
|
const req = CreateReserveRequest.checked(d);
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().createReserve(req);
|
2017-05-31 16:04:14 +02:00
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
case "confirm-reserve": {
|
2016-02-09 21:56:06 +01:00
|
|
|
const d = {
|
2017-05-28 00:38:50 +02:00
|
|
|
reservePub: detail.reservePub,
|
2016-01-06 15:39:22 +01:00
|
|
|
};
|
2016-02-09 21:56:06 +01:00
|
|
|
const req = ConfirmReserveRequest.checked(d);
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().confirmReserve(req);
|
|
|
|
}
|
|
|
|
case "confirm-pay": {
|
2017-05-31 16:04:14 +02:00
|
|
|
if (typeof detail.proposalId !== "number") {
|
|
|
|
throw Error("proposalId must be number");
|
2016-02-17 15:56:48 +01:00
|
|
|
}
|
2018-01-17 03:49:54 +01:00
|
|
|
return needsWallet().confirmPay(detail.proposalId, detail.sessionId);
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
2018-01-19 01:27:27 +01:00
|
|
|
case "submit-pay": {
|
|
|
|
if (typeof detail.contractTermsHash !== "string") {
|
|
|
|
throw Error("contractTermsHash must be a string");
|
|
|
|
}
|
|
|
|
return needsWallet().submitPay(detail.contractTermsHash, detail.sessionId);
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
case "check-pay": {
|
2017-05-31 16:04:14 +02:00
|
|
|
if (typeof detail.proposalId !== "number") {
|
|
|
|
throw Error("proposalId must be number");
|
2016-04-27 06:03:04 +02:00
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().checkPay(detail.proposalId);
|
|
|
|
}
|
|
|
|
case "query-payment": {
|
2016-09-28 19:37:05 +02:00
|
|
|
if (sender.tab && sender.tab.id) {
|
|
|
|
rateLimitCache[sender.tab.id]++;
|
|
|
|
if (rateLimitCache[sender.tab.id] > 10) {
|
2017-02-13 00:44:44 +01:00
|
|
|
console.warn("rate limit for query-payment exceeded");
|
2017-05-28 01:10:54 +02:00
|
|
|
const msg = {
|
2017-02-13 00:44:44 +01:00
|
|
|
error: "rate limit exceeded for query-payment",
|
2016-09-28 19:37:05 +02:00
|
|
|
hint: "Check for redirect loops",
|
2017-05-28 01:10:54 +02:00
|
|
|
rateLimitExceeded: true,
|
2016-09-28 19:37:05 +02:00
|
|
|
};
|
|
|
|
return Promise.resolve(msg);
|
|
|
|
}
|
|
|
|
}
|
2018-01-17 03:49:54 +01:00
|
|
|
return needsWallet().queryPaymentByFulfillmentUrl(detail.url);
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
|
|
|
case "exchange-info": {
|
2016-02-18 22:50:17 +01:00
|
|
|
if (!detail.baseUrl) {
|
2016-10-12 02:55:53 +02:00
|
|
|
return Promise.resolve({ error: "bad url" });
|
2016-02-18 22:50:17 +01:00
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().updateExchangeFromUrl(detail.baseUrl);
|
|
|
|
}
|
|
|
|
case "currency-info": {
|
2017-04-28 23:28:27 +02:00
|
|
|
if (!detail.name) {
|
|
|
|
return Promise.resolve({ error: "name missing" });
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().getCurrencyRecord(detail.name);
|
|
|
|
}
|
|
|
|
case "hash-contract": {
|
2016-09-28 23:41:34 +02:00
|
|
|
if (!detail.contract) {
|
2016-10-12 02:55:53 +02:00
|
|
|
return Promise.resolve({ error: "contract missing" });
|
2016-09-28 23:41:34 +02:00
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().hashContract(detail.contract).then((hash) => {
|
2017-05-31 16:04:14 +02:00
|
|
|
return hash;
|
2016-09-28 23:41:34 +02:00
|
|
|
});
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
|
|
|
case "reserve-creation-info": {
|
2016-02-18 22:50:17 +01:00
|
|
|
if (!detail.baseUrl || typeof detail.baseUrl !== "string") {
|
2016-10-12 02:55:53 +02:00
|
|
|
return Promise.resolve({ error: "bad url" });
|
2016-02-18 22:50:17 +01:00
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
const amount = AmountJson.checked(detail.amount);
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().getReserveCreationInfo(detail.baseUrl, amount);
|
|
|
|
}
|
|
|
|
case "get-history": {
|
2016-01-24 02:29:13 +01:00
|
|
|
// TODO: limit history length
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().getHistory();
|
|
|
|
}
|
|
|
|
case "get-proposal": {
|
|
|
|
return needsWallet().getProposal(detail.proposalId);
|
|
|
|
}
|
|
|
|
case "get-exchanges": {
|
|
|
|
return needsWallet().getExchanges();
|
|
|
|
}
|
|
|
|
case "get-currencies": {
|
|
|
|
return needsWallet().getCurrencies();
|
|
|
|
}
|
|
|
|
case "update-currency": {
|
|
|
|
return needsWallet().updateCurrency(detail.currencyRecord);
|
|
|
|
}
|
|
|
|
case "get-reserves": {
|
2016-10-12 02:55:53 +02:00
|
|
|
if (typeof detail.exchangeBaseUrl !== "string") {
|
|
|
|
return Promise.reject(Error("exchangeBaseUrl missing"));
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().getReserves(detail.exchangeBaseUrl);
|
|
|
|
}
|
|
|
|
case "get-payback-reserves": {
|
|
|
|
return needsWallet().getPaybackReserves();
|
|
|
|
}
|
|
|
|
case "withdraw-payback-reserve": {
|
2017-05-01 04:05:16 +02:00
|
|
|
if (typeof detail.reservePub !== "string") {
|
|
|
|
return Promise.reject(Error("reservePub missing"));
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().withdrawPaybackReserve(detail.reservePub);
|
|
|
|
}
|
|
|
|
case "get-coins": {
|
2016-10-12 02:55:53 +02:00
|
|
|
if (typeof detail.exchangeBaseUrl !== "string") {
|
|
|
|
return Promise.reject(Error("exchangBaseUrl missing"));
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().getCoins(detail.exchangeBaseUrl);
|
|
|
|
}
|
|
|
|
case "get-precoins": {
|
2016-10-12 02:55:53 +02:00
|
|
|
if (typeof detail.exchangeBaseUrl !== "string") {
|
|
|
|
return Promise.reject(Error("exchangBaseUrl missing"));
|
|
|
|
}
|
2017-10-15 19:28:35 +02:00
|
|
|
return needsWallet().getPreCoins(detail.exchangeBaseUrl);
|
2017-06-05 03:20:28 +02:00
|
|
|
}
|
|
|
|
case "get-denoms": {
|
2016-11-16 01:59:39 +01:00
|
|
|
if (typeof detail.exchangeBaseUrl !== "string") {
|
|
|
|
return Promise.reject(Error("exchangBaseUrl missing"));
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().getDenoms(detail.exchangeBaseUrl);
|
|
|
|
}
|
|
|
|
case "refresh-coin": {
|
2016-10-13 02:36:33 +02:00
|
|
|
if (typeof detail.coinPub !== "string") {
|
|
|
|
return Promise.reject(Error("coinPub missing"));
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().refresh(detail.coinPub);
|
|
|
|
}
|
|
|
|
case "payback-coin": {
|
2017-05-01 04:33:47 +02:00
|
|
|
if (typeof detail.coinPub !== "string") {
|
|
|
|
return Promise.reject(Error("coinPub missing"));
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
return needsWallet().payback(detail.coinPub);
|
|
|
|
}
|
2017-08-14 04:16:12 +02:00
|
|
|
case "get-sender-wire-infos": {
|
|
|
|
return needsWallet().getSenderWireInfos();
|
|
|
|
}
|
|
|
|
case "return-coins": {
|
|
|
|
const d = {
|
|
|
|
amount: detail.amount,
|
|
|
|
exchange: detail.exchange,
|
|
|
|
senderWire: detail.senderWire,
|
|
|
|
};
|
|
|
|
const req = ReturnCoinsRequest.checked(d);
|
|
|
|
return needsWallet().returnCoins(req);
|
|
|
|
}
|
2017-06-05 03:20:28 +02:00
|
|
|
case "check-upgrade": {
|
|
|
|
let dbResetRequired = false;
|
|
|
|
if (!currentWallet) {
|
|
|
|
dbResetRequired = true;
|
|
|
|
}
|
|
|
|
const resp: wxApi.UpgradeResponse = {
|
2017-06-05 03:36:33 +02:00
|
|
|
currentDbVersion: WALLET_DB_VERSION.toString(),
|
2017-10-15 19:28:35 +02:00
|
|
|
dbResetRequired,
|
2017-06-05 03:20:28 +02:00
|
|
|
oldDbVersion: (oldDbVersion || "unknown").toString(),
|
2017-10-15 19:28:35 +02:00
|
|
|
};
|
2017-06-05 03:20:28 +02:00
|
|
|
return resp;
|
|
|
|
}
|
2017-08-25 18:08:37 +02:00
|
|
|
case "log-and-display-error":
|
|
|
|
logging.storeReport(detail).then((reportUid) => {
|
2017-08-27 03:56:19 +02:00
|
|
|
const url = chrome.extension.getURL(`/src/webex/pages/error.html?reportUid=${reportUid}`);
|
|
|
|
if (detail.sameTab && sender && sender.tab && sender.tab.id) {
|
|
|
|
chrome.tabs.update(detail.tabId, { url });
|
|
|
|
} else {
|
|
|
|
chrome.tabs.create({ url });
|
|
|
|
}
|
2017-08-25 18:08:37 +02:00
|
|
|
});
|
|
|
|
return;
|
|
|
|
case "get-report":
|
|
|
|
return logging.getReport(detail.reportUid);
|
2017-10-15 19:28:35 +02:00
|
|
|
case "get-purchase": {
|
2017-08-27 03:56:19 +02:00
|
|
|
const contractTermsHash = detail.contractTermsHash;
|
|
|
|
if (!contractTermsHash) {
|
|
|
|
throw Error("contractTermsHash missing");
|
|
|
|
}
|
|
|
|
return needsWallet().getPurchase(contractTermsHash);
|
2017-10-15 19:28:35 +02:00
|
|
|
}
|
2017-08-30 17:08:54 +02:00
|
|
|
case "get-full-refund-fees":
|
|
|
|
return needsWallet().getFullRefundFees(detail.refundPermissions);
|
2018-01-22 01:12:08 +01:00
|
|
|
case "accept-refund":
|
|
|
|
return needsWallet().acceptRefund(detail.refundUrl);
|
2017-11-30 04:07:36 +01:00
|
|
|
case "get-tip-status": {
|
2018-01-19 01:27:27 +01:00
|
|
|
const tipToken = TipToken.checked(detail.tipToken);
|
|
|
|
return needsWallet().getTipStatus(tipToken);
|
2017-11-30 04:07:36 +01:00
|
|
|
}
|
|
|
|
case "accept-tip": {
|
2018-01-19 01:27:27 +01:00
|
|
|
const tipToken = TipToken.checked(detail.tipToken);
|
|
|
|
return needsWallet().acceptTip(tipToken);
|
2017-11-30 04:07:36 +01:00
|
|
|
}
|
2017-12-12 16:39:55 +01:00
|
|
|
case "clear-notification": {
|
|
|
|
return needsWallet().clearNotification();
|
|
|
|
}
|
2018-01-17 03:49:54 +01:00
|
|
|
case "download-proposal": {
|
|
|
|
return needsWallet().downloadProposal(detail.url);
|
|
|
|
}
|
2018-01-29 16:41:17 +01:00
|
|
|
case "abort-failed-payment": {
|
|
|
|
if (!detail.contractTermsHash) {
|
|
|
|
throw Error("contracTermsHash not given");
|
|
|
|
}
|
|
|
|
return needsWallet().abortFailedPayment(detail.contractTermsHash);
|
|
|
|
}
|
2018-01-17 03:49:54 +01:00
|
|
|
case "taler-pay": {
|
|
|
|
const senderUrl = sender.url;
|
|
|
|
if (!senderUrl) {
|
|
|
|
console.log("can't trigger payment, no sender URL");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const tab = sender.tab;
|
|
|
|
if (!tab) {
|
|
|
|
console.log("can't trigger payment, no sender tab");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const tabId = tab.id;
|
|
|
|
if (typeof tabId !== "string") {
|
|
|
|
console.log("can't trigger payment, no sender tab id");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
talerPay(detail, senderUrl, tabId);
|
|
|
|
return;
|
|
|
|
}
|
2018-09-20 02:56:13 +02:00
|
|
|
case "benchmark-crypto": {
|
|
|
|
if (!detail.repetitions) {
|
|
|
|
throw Error("repetitions not given");
|
|
|
|
}
|
|
|
|
return needsWallet().benchmarkCrypto(detail.repetitions);
|
|
|
|
}
|
2017-05-31 16:04:14 +02:00
|
|
|
default:
|
|
|
|
// Exhaustiveness check.
|
|
|
|
// See https://www.typescriptlang.org/docs/handbook/advanced-types.html
|
|
|
|
return assertNotFound(type);
|
2016-11-20 08:58:04 +01:00
|
|
|
}
|
2017-05-31 16:04:14 +02:00
|
|
|
}
|
2016-09-23 14:09:07 +02:00
|
|
|
|
2017-06-05 03:20:28 +02:00
|
|
|
async function dispatch(req: any, sender: any, sendResponse: any): Promise<void> {
|
2016-11-20 08:58:04 +01:00
|
|
|
try {
|
2017-06-05 03:20:28 +02:00
|
|
|
const p = handleMessage(sender, req.type, req.detail);
|
2017-05-28 01:10:54 +02:00
|
|
|
const r = await p;
|
2016-11-20 08:58:04 +01:00
|
|
|
try {
|
|
|
|
sendResponse(r);
|
|
|
|
} catch (e) {
|
|
|
|
// might fail if tab disconnected
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
console.log(`exception during wallet handler for '${req.type}'`);
|
|
|
|
console.log("request", req);
|
|
|
|
console.error(e);
|
2017-05-28 01:10:54 +02:00
|
|
|
let stack;
|
2017-04-28 23:28:27 +02:00
|
|
|
try {
|
|
|
|
stack = e.stack.toString();
|
|
|
|
} catch (e) {
|
|
|
|
// might fail
|
|
|
|
}
|
2016-11-20 08:58:04 +01:00
|
|
|
try {
|
|
|
|
sendResponse({
|
|
|
|
error: "exception",
|
2017-08-27 06:47:13 +02:00
|
|
|
message: e.message,
|
2017-05-28 01:10:54 +02:00
|
|
|
stack,
|
2016-11-20 08:58:04 +01:00
|
|
|
});
|
|
|
|
} catch (e) {
|
2017-04-28 23:28:27 +02:00
|
|
|
console.log(e);
|
2016-11-20 08:58:04 +01:00
|
|
|
// might fail if tab disconnected
|
|
|
|
}
|
2016-02-17 15:56:48 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
|
2016-02-18 23:41:29 +01:00
|
|
|
class ChromeNotifier implements Notifier {
|
2017-05-28 16:27:34 +02:00
|
|
|
private ports: Port[] = [];
|
2016-02-18 23:41:29 +01:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
chrome.runtime.onConnect.addListener((port) => {
|
|
|
|
console.log("got connect!");
|
|
|
|
this.ports.push(port);
|
|
|
|
port.onDisconnect.addListener(() => {
|
2017-05-28 01:10:54 +02:00
|
|
|
const i = this.ports.indexOf(port);
|
2016-02-18 23:41:29 +01:00
|
|
|
if (i >= 0) {
|
|
|
|
this.ports.splice(i, 1);
|
|
|
|
} else {
|
|
|
|
console.error("port already removed");
|
|
|
|
}
|
|
|
|
});
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
notify() {
|
2017-05-28 01:10:54 +02:00
|
|
|
for (const p of this.ports) {
|
2016-10-12 02:55:53 +02:00
|
|
|
p.postMessage({ notify: true });
|
2016-02-18 23:41:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-06 13:31:30 +02:00
|
|
|
|
2018-01-17 03:49:54 +01:00
|
|
|
async function talerPay(fields: any, url: string, tabId: number): Promise<string | undefined> {
|
|
|
|
if (!currentWallet) {
|
|
|
|
console.log("can't handle payment, no wallet");
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
const w = currentWallet;
|
|
|
|
|
2018-01-19 01:27:27 +01:00
|
|
|
const goToPayment = (p: PurchaseRecord): string => {
|
2018-01-17 03:49:54 +01:00
|
|
|
const nextUrl = new URI(p.contractTerms.fulfillment_url);
|
|
|
|
nextUrl.addSearch("order_id", p.contractTerms.order_id);
|
|
|
|
if (p.lastSessionSig) {
|
|
|
|
nextUrl.addSearch("session_sig", p.lastSessionSig);
|
|
|
|
}
|
2018-01-18 01:37:30 +01:00
|
|
|
return nextUrl.href();
|
2018-01-17 03:49:54 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
if (fields.resource_url) {
|
|
|
|
const p = await w.queryPaymentByFulfillmentUrl(fields.resource_url);
|
2018-01-18 01:37:30 +01:00
|
|
|
console.log("query for resource url", fields.resource_url, "result", p);
|
2018-01-19 01:27:27 +01:00
|
|
|
if (p && (fields.session_id === undefined || fields.session_id === p.lastSessionId)) {
|
2018-01-17 03:49:54 +01:00
|
|
|
return goToPayment(p);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (fields.contract_url) {
|
|
|
|
const proposalId = await w.downloadProposal(fields.contract_url);
|
|
|
|
const uri = new URI(chrome.extension.getURL("/src/webex/pages/confirm-contract.html"));
|
|
|
|
if (fields.session_id) {
|
|
|
|
uri.addSearch("sessionId", fields.session_id);
|
|
|
|
}
|
|
|
|
uri.addSearch("proposalId", proposalId);
|
|
|
|
const redirectUrl = uri.href();
|
|
|
|
return redirectUrl;
|
|
|
|
}
|
|
|
|
if (fields.offer_url) {
|
|
|
|
return fields.offer_url;
|
|
|
|
}
|
|
|
|
if (fields.refund_url) {
|
|
|
|
console.log("processing refund");
|
2018-01-22 01:12:08 +01:00
|
|
|
const uri = new URI(chrome.extension.getURL("/src/webex/pages/refund.html"));
|
|
|
|
return uri.query({ refundUrl: fields.refund_url }).href();
|
2018-01-17 03:49:54 +01:00
|
|
|
}
|
|
|
|
if (fields.tip) {
|
|
|
|
const uri = new URI(chrome.extension.getURL("/src/webex/pages/tip.html"));
|
2018-01-19 01:27:27 +01:00
|
|
|
return uri.query({ tip_token: fields.tip }).href();
|
2018-01-17 03:49:54 +01:00
|
|
|
}
|
|
|
|
return undefined;
|
|
|
|
}
|
2016-09-08 17:26:31 +02:00
|
|
|
|
2017-03-03 15:37:04 +01:00
|
|
|
|
2018-02-20 15:12:45 +01:00
|
|
|
function getTab(tabId: number): Promise<chrome.tabs.Tab> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
chrome.tabs.get(tabId, (tab: chrome.tabs.Tab) => resolve(tab));
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-20 16:17:05 +01:00
|
|
|
function setBadgeText(options: chrome.browserAction.BadgeTextDetails) {
|
|
|
|
// not supported by all browsers ...
|
|
|
|
if (chrome && chrome.browserAction && chrome.browserAction.setBadgeText) {
|
|
|
|
chrome.browserAction.setBadgeText(options);
|
|
|
|
} else {
|
|
|
|
console.warn("can't set badge text, not supported", options);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-02-20 15:12:45 +01:00
|
|
|
function waitMs(timeoutMs: number): Promise<void> {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
chrome.extension.getBackgroundPage().setTimeout(() => resolve(), timeoutMs);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-04-09 00:41:14 +02:00
|
|
|
function makeSyncWalletRedirect(url: string,
|
|
|
|
tabId: number,
|
|
|
|
oldUrl: string,
|
|
|
|
params?: {[name: string]: string | undefined}): object {
|
2018-02-07 16:15:40 +01:00
|
|
|
const innerUrl = new URI(chrome.extension.getURL("/src/webex/pages/" + url));
|
|
|
|
if (params) {
|
|
|
|
for (const key in params) {
|
|
|
|
if (params[key]) {
|
|
|
|
innerUrl.addSearch(key, params[key]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const outerUrl = new URI(chrome.extension.getURL("/src/webex/pages/redirect.html"));
|
|
|
|
outerUrl.addSearch("url", innerUrl);
|
2018-02-20 15:12:45 +01:00
|
|
|
if (isFirefox()) {
|
|
|
|
// Some platforms don't support the sync redirect (yet), so fall back to
|
|
|
|
// async redirect after a timeout.
|
|
|
|
const doit = async() => {
|
|
|
|
await waitMs(150);
|
|
|
|
const tab = await getTab(tabId);
|
|
|
|
if (tab.url === oldUrl) {
|
|
|
|
chrome.tabs.update(tabId, { url: outerUrl.href() });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
doit();
|
|
|
|
}
|
2018-02-07 16:15:40 +01:00
|
|
|
return { redirectUrl: outerUrl.href() };
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-03-03 15:37:04 +01:00
|
|
|
/**
|
|
|
|
* Handle a HTTP response that has the "402 Payment Required" status.
|
|
|
|
* In this callback we don't have access to the body, and must communicate via
|
|
|
|
* shared state with the content script that will later be run later
|
|
|
|
* in this tab.
|
|
|
|
*/
|
2017-02-13 00:44:44 +01:00
|
|
|
function handleHttpPayment(headerList: chrome.webRequest.HttpHeader[], url: string, tabId: number): any {
|
2018-01-17 03:49:54 +01:00
|
|
|
if (!currentWallet) {
|
|
|
|
console.log("can't handle payment, no wallet");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-10-12 02:55:53 +02:00
|
|
|
const headers: { [s: string]: string } = {};
|
2017-05-28 01:10:54 +02:00
|
|
|
for (const kv of headerList) {
|
2016-09-12 17:41:12 +02:00
|
|
|
if (kv.value) {
|
|
|
|
headers[kv.name.toLowerCase()] = kv.value;
|
|
|
|
}
|
2016-09-06 13:31:30 +02:00
|
|
|
}
|
|
|
|
|
2017-05-28 01:10:54 +02:00
|
|
|
const fields = {
|
|
|
|
contract_url: headers["x-taler-contract-url"],
|
2017-02-13 00:44:44 +01:00
|
|
|
offer_url: headers["x-taler-offer-url"],
|
2017-08-27 03:56:19 +02:00
|
|
|
refund_url: headers["x-taler-refund-url"],
|
2018-01-17 03:49:54 +01:00
|
|
|
resource_url: headers["x-taler-resource-url"],
|
|
|
|
session_id: headers["x-taler-session-id"],
|
2017-11-30 04:07:36 +01:00
|
|
|
tip: headers["x-taler-tip"],
|
2017-05-28 01:10:54 +02:00
|
|
|
};
|
2016-09-06 13:31:30 +02:00
|
|
|
|
2017-05-28 01:10:54 +02:00
|
|
|
const talerHeaderFound = Object.keys(fields).filter((x: any) => (fields as any)[x]).length !== 0;
|
2016-09-06 13:31:30 +02:00
|
|
|
|
2017-03-03 15:37:04 +01:00
|
|
|
if (!talerHeaderFound) {
|
2017-02-13 00:44:44 +01:00
|
|
|
// looks like it's not a taler request, it might be
|
|
|
|
// for a different payment system (or the shop is buggy)
|
|
|
|
console.log("ignoring non-taler 402 response");
|
2017-03-03 15:37:04 +01:00
|
|
|
return;
|
2017-02-13 00:44:44 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 03:49:54 +01:00
|
|
|
console.log("got pay detail", fields);
|
2017-02-13 00:44:44 +01:00
|
|
|
|
2018-01-19 01:27:27 +01:00
|
|
|
// Synchronous fast path for existing payment
|
2018-01-17 03:49:54 +01:00
|
|
|
if (fields.resource_url) {
|
2018-01-18 02:50:18 +01:00
|
|
|
const result = currentWallet.getNextUrlFromResourceUrl(fields.resource_url);
|
|
|
|
if (result && (fields.session_id === undefined || fields.session_id === result.lastSessionId)) {
|
|
|
|
return { redirectUrl: result.nextUrl };
|
2018-01-17 03:49:54 +01:00
|
|
|
}
|
|
|
|
}
|
2018-01-19 01:27:27 +01:00
|
|
|
// Synchronous fast path for new contract
|
|
|
|
if (fields.contract_url) {
|
2018-02-20 15:12:45 +01:00
|
|
|
return makeSyncWalletRedirect("confirm-contract.html", tabId, url, {
|
2018-02-07 16:15:40 +01:00
|
|
|
contractUrl: fields.contract_url,
|
|
|
|
resourceUrl: fields.resource_url,
|
2018-04-09 00:41:14 +02:00
|
|
|
sessionId: fields.session_id,
|
2018-02-07 16:15:40 +01:00
|
|
|
});
|
2018-01-19 01:27:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Synchronous fast path for tip
|
|
|
|
if (fields.tip) {
|
2018-02-20 15:12:45 +01:00
|
|
|
return makeSyncWalletRedirect("tip.html", tabId, url, { tip_token: fields.tip });
|
2018-01-17 03:49:54 +01:00
|
|
|
}
|
2017-02-13 00:44:44 +01:00
|
|
|
|
2018-01-22 01:12:08 +01:00
|
|
|
// Synchronous fast path for refund
|
|
|
|
if (fields.refund_url) {
|
|
|
|
console.log("processing refund");
|
2018-02-20 15:12:45 +01:00
|
|
|
return makeSyncWalletRedirect("refund.html", tabId, url, { refundUrl: fields.refund_url });
|
2018-01-22 01:12:08 +01:00
|
|
|
}
|
|
|
|
|
2018-01-17 03:49:54 +01:00
|
|
|
// We need to do some asynchronous operation, we can't directly redirect
|
|
|
|
talerPay(fields, url, tabId).then((nextUrl) => {
|
|
|
|
if (nextUrl) {
|
2018-01-22 01:12:08 +01:00
|
|
|
// We use chrome.tabs.executeScript instead of chrome.tabs.update
|
|
|
|
// because the latter is buggy when it does not execute in the same
|
|
|
|
// (micro-?)task as the header callback.
|
|
|
|
chrome.tabs.executeScript({
|
|
|
|
code: `document.location.href = decodeURIComponent("${encodeURI(nextUrl)}");`,
|
|
|
|
runAt: "document_start",
|
|
|
|
});
|
2018-01-17 03:49:54 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return;
|
2016-09-06 13:31:30 +02:00
|
|
|
}
|
|
|
|
|
2016-11-17 21:21:05 +01:00
|
|
|
|
|
|
|
function handleBankRequest(wallet: Wallet, headerList: chrome.webRequest.HttpHeader[],
|
2017-05-28 01:10:54 +02:00
|
|
|
url: string, tabId: number): any {
|
2016-11-17 21:21:05 +01:00
|
|
|
const headers: { [s: string]: string } = {};
|
2017-05-28 01:10:54 +02:00
|
|
|
for (const kv of headerList) {
|
2016-11-17 21:21:05 +01:00
|
|
|
if (kv.value) {
|
|
|
|
headers[kv.name.toLowerCase()] = kv.value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-20 02:17:55 +02:00
|
|
|
const operation = headers["x-taler-operation"];
|
|
|
|
|
|
|
|
if (!operation) {
|
|
|
|
// Not a taler related request.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-15 19:28:35 +02:00
|
|
|
if (operation === "confirm-reserve") {
|
2017-07-20 02:17:55 +02:00
|
|
|
const reservePub = headers["x-taler-reserve-pub"];
|
|
|
|
if (reservePub !== undefined) {
|
|
|
|
console.log(`confirming reserve ${reservePub} via 201`);
|
|
|
|
wallet.confirmReserve({reservePub});
|
2017-12-12 17:37:06 +01:00
|
|
|
} else {
|
|
|
|
console.warn("got 'X-Taler-Operation: confirm-reserve' without 'X-Taler-Reserve-Pub'");
|
2017-07-20 02:17:55 +02:00
|
|
|
}
|
2016-11-17 21:21:05 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-10-15 19:28:35 +02:00
|
|
|
if (operation === "create-reserve") {
|
2017-07-20 02:17:55 +02:00
|
|
|
const amount = headers["x-taler-amount"];
|
|
|
|
if (!amount) {
|
|
|
|
console.log("202 not understood (X-Taler-Amount missing)");
|
|
|
|
return;
|
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
const callbackUrl = headers["x-taler-callback-url"];
|
2016-11-17 21:21:05 +01:00
|
|
|
if (!callbackUrl) {
|
2016-11-18 17:15:12 +01:00
|
|
|
console.log("202 not understood (X-Taler-Callback-Url missing)");
|
2016-11-17 21:21:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-11-19 17:37:39 +01:00
|
|
|
try {
|
2017-12-10 23:02:00 +01:00
|
|
|
JSON.parse(amount);
|
2016-11-19 17:37:39 +01:00
|
|
|
} catch (e) {
|
2017-10-15 19:28:35 +02:00
|
|
|
const errUri = new URI(chrome.extension.getURL("/src/webex/pages/error.html"));
|
2017-05-28 01:10:54 +02:00
|
|
|
const p = {
|
2016-11-19 17:37:39 +01:00
|
|
|
message: `Can't parse amount ("${amount}"): ${e.message}`,
|
|
|
|
};
|
2017-10-15 19:28:35 +02:00
|
|
|
const errRedirectUrl = errUri.query(p).href();
|
2017-04-24 17:50:13 +02:00
|
|
|
// FIXME: use direct redirect when https://bugzilla.mozilla.org/show_bug.cgi?id=707624 is fixed
|
2017-10-15 19:28:35 +02:00
|
|
|
chrome.tabs.update(tabId, {url: errRedirectUrl});
|
2017-04-24 17:50:13 +02:00
|
|
|
return;
|
2016-11-19 17:37:39 +01:00
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
const wtTypes = headers["x-taler-wt-types"];
|
2016-11-17 21:21:05 +01:00
|
|
|
if (!wtTypes) {
|
2016-11-18 17:15:12 +01:00
|
|
|
console.log("202 not understood (X-Taler-Wt-Types missing)");
|
2016-11-17 21:21:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
const params = {
|
|
|
|
amount,
|
2016-11-17 21:21:05 +01:00
|
|
|
bank_url: url,
|
2017-05-28 01:10:54 +02:00
|
|
|
callback_url: new URI(callbackUrl) .absoluteTo(url),
|
2017-10-15 19:28:35 +02:00
|
|
|
sender_wire: headers["x-taler-sender-wire"],
|
2017-02-12 04:40:28 +01:00
|
|
|
suggested_exchange_url: headers["x-taler-suggested-exchange"],
|
2017-05-28 01:10:54 +02:00
|
|
|
wt_types: wtTypes,
|
2016-11-17 21:21:05 +01:00
|
|
|
};
|
2017-05-29 01:48:46 +02:00
|
|
|
const uri = new URI(chrome.extension.getURL("/src/webex/pages/confirm-create-reserve.html"));
|
2017-05-28 01:10:54 +02:00
|
|
|
const redirectUrl = uri.query(params).href();
|
2017-04-24 17:50:13 +02:00
|
|
|
console.log("redirecting to", redirectUrl);
|
|
|
|
// FIXME: use direct redirect when https://bugzilla.mozilla.org/show_bug.cgi?id=707624 is fixed
|
2018-01-17 03:49:54 +01:00
|
|
|
chrome.tabs.update(tabId, { url: redirectUrl });
|
2017-04-24 17:50:13 +02:00
|
|
|
return;
|
2016-11-17 21:21:05 +01:00
|
|
|
}
|
2017-07-20 02:17:55 +02:00
|
|
|
|
|
|
|
console.log("Ignoring unknown X-Taler-Operation:", operation);
|
2016-11-17 21:21:05 +01:00
|
|
|
}
|
|
|
|
|
2016-02-17 15:56:48 +01:00
|
|
|
|
2016-09-28 19:37:05 +02:00
|
|
|
// Rate limit cache for executePayment operations, to break redirect loops
|
2016-10-12 02:55:53 +02:00
|
|
|
let rateLimitCache: { [n: number]: number } = {};
|
2016-09-28 19:37:05 +02:00
|
|
|
|
|
|
|
function clearRateLimitCache() {
|
|
|
|
rateLimitCache = {};
|
|
|
|
}
|
|
|
|
|
2017-06-05 02:00:03 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Currently active wallet instance. Might be unloaded and
|
|
|
|
* re-instantiated when the database is reset.
|
|
|
|
*/
|
|
|
|
let currentWallet: Wallet|undefined;
|
|
|
|
|
2017-06-05 03:20:28 +02:00
|
|
|
/**
|
|
|
|
* Last version if an outdated DB, if applicable.
|
|
|
|
*/
|
|
|
|
let oldDbVersion: number|undefined;
|
|
|
|
|
2019-07-21 23:50:10 +02:00
|
|
|
function handleUpgradeUnsupported(oldDbVersion: number, newDbVersion: number) {
|
|
|
|
console.log("DB migration not supported");
|
|
|
|
chrome.tabs.create({
|
|
|
|
url: chrome.extension.getURL(
|
|
|
|
"/src/webex/pages/reset-required.html",
|
|
|
|
),
|
|
|
|
});
|
|
|
|
setBadgeText({ text: "err" });
|
|
|
|
chrome.browserAction.setBadgeBackgroundColor({ color: "#F00" });
|
|
|
|
}
|
|
|
|
|
2017-06-05 02:00:03 +02:00
|
|
|
|
|
|
|
async function reinitWallet() {
|
|
|
|
if (currentWallet) {
|
|
|
|
currentWallet.stop();
|
|
|
|
currentWallet = undefined;
|
|
|
|
}
|
2018-02-20 16:17:05 +01:00
|
|
|
setBadgeText({ text: "" });
|
2017-06-05 02:00:03 +02:00
|
|
|
const badge = new ChromeBadge();
|
|
|
|
let db: IDBDatabase;
|
|
|
|
try {
|
2019-07-21 23:50:10 +02:00
|
|
|
db = await openTalerDb(indexedDB, reinitWallet, handleUpgradeUnsupported);
|
2017-06-05 02:00:03 +02:00
|
|
|
} catch (e) {
|
|
|
|
console.error("could not open database", e);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const http = new BrowserHttpLib();
|
|
|
|
const notifier = new ChromeNotifier();
|
|
|
|
console.log("setting wallet");
|
|
|
|
const wallet = new Wallet(db, http, badge, notifier);
|
|
|
|
// Useful for debugging in the background page.
|
|
|
|
(window as any).talerWallet = wallet;
|
|
|
|
currentWallet = wallet;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-09 16:51:25 +02:00
|
|
|
/**
|
|
|
|
* Inject a script into a tab. Gracefully logs errors
|
|
|
|
* and works around a bug where the tab's URL does not match the internal URL,
|
|
|
|
* making the injection fail in a confusing way.
|
|
|
|
*/
|
|
|
|
function injectScript(tabId: number, details: chrome.tabs.InjectDetails, actualUrl: string): void {
|
|
|
|
chrome.tabs.executeScript(tabId, details, () => {
|
|
|
|
// Required to squelch chrome's "unchecked lastError" warning.
|
|
|
|
// Sometimes chrome reports the URL of a tab as http/https but
|
|
|
|
// injection fails. This can happen when a page is unloaded or
|
|
|
|
// shows a "no internet" page etc.
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
console.warn("injection failed on page", actualUrl, chrome.runtime.lastError.message);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-05-28 16:27:34 +02:00
|
|
|
/**
|
|
|
|
* Main function to run for the WebExtension backend.
|
|
|
|
*
|
|
|
|
* Sets up all event handlers and other machinery.
|
|
|
|
*/
|
2016-11-20 08:58:04 +01:00
|
|
|
export async function wxMain() {
|
2017-05-31 22:55:03 +02:00
|
|
|
// Explicitly unload the extension page as soon as an update is available,
|
|
|
|
// so the update gets installed as soon as possible.
|
|
|
|
chrome.runtime.onUpdateAvailable.addListener((details) => {
|
|
|
|
console.log("update available:", details);
|
|
|
|
chrome.runtime.reload();
|
2017-10-15 19:28:35 +02:00
|
|
|
});
|
2017-05-31 22:55:03 +02:00
|
|
|
|
2016-11-18 04:09:04 +01:00
|
|
|
window.onerror = (m, source, lineno, colno, error) => {
|
2018-09-20 02:56:13 +02:00
|
|
|
logging.record("error", "".concat(m as any, error as any), undefined, source || "(unknown)", lineno || 0, colno || 0);
|
2017-05-28 01:10:54 +02:00
|
|
|
};
|
2016-11-18 04:09:04 +01:00
|
|
|
|
2017-05-28 01:10:54 +02:00
|
|
|
chrome.tabs.query({}, (tabs) => {
|
2017-08-30 15:35:34 +02:00
|
|
|
console.log("got tabs", tabs);
|
2017-05-28 01:10:54 +02:00
|
|
|
for (const tab of tabs) {
|
2016-09-12 17:41:12 +02:00
|
|
|
if (!tab.url || !tab.id) {
|
2017-08-30 15:35:34 +02:00
|
|
|
continue;
|
2016-03-02 15:33:23 +01:00
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
const uri = new URI(tab.url);
|
2017-08-09 16:51:25 +02:00
|
|
|
if (uri.protocol() !== "http" && uri.protocol() !== "https") {
|
2017-08-30 15:35:34 +02:00
|
|
|
continue;
|
2016-03-02 15:33:23 +01:00
|
|
|
}
|
2017-08-09 16:51:25 +02:00
|
|
|
console.log("injecting into existing tab", tab.id, "with url", uri.href(), "protocol", uri.protocol());
|
2017-08-30 15:35:34 +02:00
|
|
|
injectScript(tab.id, { file: "/dist/contentScript-bundle.js", runAt: "document_start" }, uri.href());
|
2017-08-09 16:51:25 +02:00
|
|
|
const code = `
|
|
|
|
if (("taler" in window) || document.documentElement.getAttribute("data-taler-nojs")) {
|
|
|
|
document.dispatchEvent(new Event("taler-probe-result"));
|
|
|
|
}
|
|
|
|
`;
|
2017-08-30 15:35:34 +02:00
|
|
|
injectScript(tab.id, { code, runAt: "document_start" }, uri.href());
|
2016-03-02 15:33:23 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2016-11-20 08:47:22 +01:00
|
|
|
const tabTimers: {[n: number]: number[]} = {};
|
|
|
|
|
|
|
|
chrome.tabs.onRemoved.addListener((tabId, changeInfo) => {
|
2017-05-28 01:10:54 +02:00
|
|
|
const tt = tabTimers[tabId] || [];
|
|
|
|
for (const t of tt) {
|
2016-11-20 08:47:22 +01:00
|
|
|
chrome.extension.getBackgroundPage().clearTimeout(t);
|
|
|
|
}
|
|
|
|
});
|
2016-11-19 23:03:58 +01:00
|
|
|
chrome.tabs.onUpdated.addListener((tabId, changeInfo) => {
|
2017-05-28 01:10:54 +02:00
|
|
|
if (changeInfo.status !== "complete") {
|
2016-11-19 23:03:58 +01:00
|
|
|
return;
|
|
|
|
}
|
2016-11-20 08:47:22 +01:00
|
|
|
const timers: number[] = [];
|
|
|
|
|
|
|
|
const addRun = (dt: number) => {
|
|
|
|
const id = chrome.extension.getBackgroundPage().setTimeout(run, dt);
|
|
|
|
timers.push(id);
|
2017-05-28 01:10:54 +02:00
|
|
|
};
|
2016-11-20 08:47:22 +01:00
|
|
|
|
|
|
|
const run = () => {
|
|
|
|
timers.shift();
|
|
|
|
chrome.tabs.get(tabId, (tab) => {
|
|
|
|
if (chrome.runtime.lastError) {
|
|
|
|
return;
|
2016-11-19 23:03:58 +01:00
|
|
|
}
|
2016-11-20 08:47:22 +01:00
|
|
|
if (!tab.url || !tab.id) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
const uri = new URI(tab.url);
|
2017-05-28 00:38:50 +02:00
|
|
|
if (!(uri.protocol() === "http" || uri.protocol() === "https")) {
|
2016-11-20 08:47:22 +01:00
|
|
|
return;
|
|
|
|
}
|
2017-05-28 01:10:54 +02:00
|
|
|
const code = `
|
2016-11-20 08:47:22 +01:00
|
|
|
if (("taler" in window) || document.documentElement.getAttribute("data-taler-nojs")) {
|
|
|
|
document.dispatchEvent(new Event("taler-probe-result"));
|
|
|
|
}
|
|
|
|
`;
|
2017-08-09 16:51:25 +02:00
|
|
|
injectScript(tab.id!, { code, runAt: "document_start" }, uri.href());
|
2016-11-20 08:47:22 +01:00
|
|
|
});
|
|
|
|
};
|
2016-11-19 23:03:58 +01:00
|
|
|
|
2016-11-20 08:47:22 +01:00
|
|
|
addRun(0);
|
|
|
|
addRun(50);
|
|
|
|
addRun(300);
|
|
|
|
addRun(1000);
|
|
|
|
addRun(2000);
|
|
|
|
addRun(4000);
|
|
|
|
addRun(8000);
|
|
|
|
addRun(16000);
|
|
|
|
tabTimers[tabId] = timers;
|
2016-11-19 23:03:58 +01:00
|
|
|
});
|
|
|
|
|
2016-09-28 19:37:05 +02:00
|
|
|
chrome.extension.getBackgroundPage().setInterval(clearRateLimitCache, 5000);
|
|
|
|
|
2017-06-05 02:00:03 +02:00
|
|
|
reinitWallet();
|
2016-11-20 08:58:04 +01:00
|
|
|
|
|
|
|
// Handlers for messages coming directly from the content
|
|
|
|
// script on the page
|
|
|
|
chrome.runtime.onMessage.addListener((req, sender, sendResponse) => {
|
2017-06-05 03:20:28 +02:00
|
|
|
dispatch(req, sender, sendResponse);
|
2016-11-20 08:58:04 +01:00
|
|
|
return true;
|
|
|
|
});
|
2016-10-12 02:55:53 +02:00
|
|
|
|
2017-06-05 02:00:03 +02:00
|
|
|
|
2017-12-12 16:39:55 +01:00
|
|
|
// Clear notifications both when the popop opens,
|
|
|
|
// as well when it closes.
|
|
|
|
chrome.runtime.onConnect.addListener((port) => {
|
2018-01-03 14:42:06 +01:00
|
|
|
if (port.name === "popup") {
|
2017-12-12 16:39:55 +01:00
|
|
|
if (currentWallet) {
|
|
|
|
currentWallet.clearNotification();
|
|
|
|
}
|
|
|
|
port.onDisconnect.addListener(() => {
|
|
|
|
if (currentWallet) {
|
|
|
|
currentWallet.clearNotification();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2016-11-20 08:58:04 +01:00
|
|
|
// Handlers for catching HTTP requests
|
|
|
|
chrome.webRequest.onHeadersReceived.addListener((details) => {
|
2017-06-05 02:00:03 +02:00
|
|
|
const wallet = currentWallet;
|
|
|
|
if (!wallet) {
|
|
|
|
console.warn("wallet not available while handling header");
|
|
|
|
}
|
2017-05-28 00:38:50 +02:00
|
|
|
if (details.statusCode === 402) {
|
2016-11-20 08:58:04 +01:00
|
|
|
console.log(`got 402 from ${details.url}`);
|
|
|
|
return handleHttpPayment(details.responseHeaders || [],
|
|
|
|
details.url,
|
|
|
|
details.tabId);
|
2017-05-28 00:38:50 +02:00
|
|
|
} else if (details.statusCode === 202) {
|
2016-11-20 08:58:04 +01:00
|
|
|
return handleBankRequest(wallet!, details.responseHeaders || [],
|
|
|
|
details.url,
|
|
|
|
details.tabId);
|
|
|
|
}
|
|
|
|
}, { urls: ["<all_urls>"] }, ["responseHeaders", "blocking"]);
|
2016-10-11 20:26:37 +02:00
|
|
|
}
|