diff options
author | Florian Dold <florian.dold@gmail.com> | 2019-12-02 00:42:40 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2019-12-02 00:42:40 +0100 |
commit | e1369ff7e8fc02116b9c4261036f0e42e3423cf4 (patch) | |
tree | c621067ebda8977a888bfed34b7bbecf64b3b0f0 /src/webex | |
parent | aaf7e1338d6cdb1b4e01ad318938b3eaea2f922b (diff) |
the giant refactoring: split wallet into multiple parts
Diffstat (limited to 'src/webex')
-rw-r--r-- | src/webex/messages.ts | 14 | ||||
-rw-r--r-- | src/webex/notify.ts | 28 | ||||
-rw-r--r-- | src/webex/pages/add-auditor.tsx | 27 | ||||
-rw-r--r-- | src/webex/pages/pay.tsx | 13 | ||||
-rw-r--r-- | src/webex/pages/popup.tsx | 20 | ||||
-rw-r--r-- | src/webex/pages/refund.tsx | 6 | ||||
-rw-r--r-- | src/webex/pages/return-coins.tsx | 6 | ||||
-rw-r--r-- | src/webex/pages/tip.tsx | 10 | ||||
-rw-r--r-- | src/webex/pages/withdraw.tsx | 6 | ||||
-rw-r--r-- | src/webex/renderHtml.tsx | 4 | ||||
-rw-r--r-- | src/webex/wxApi.ts | 25 | ||||
-rw-r--r-- | src/webex/wxBackend.ts | 60 |
12 files changed, 78 insertions, 141 deletions
diff --git a/src/webex/messages.ts b/src/webex/messages.ts index e321e5ac1..cf409b44e 100644 --- a/src/webex/messages.ts +++ b/src/webex/messages.ts @@ -21,7 +21,7 @@ // Messages are already documented in wxApi. /* tslint:disable:completed-docs */ -import { AmountJson } from "../amounts"; +import { AmountJson } from "../util/amounts"; import * as dbTypes from "../dbTypes"; import * as talerTypes from "../talerTypes"; import * as walletTypes from "../walletTypes"; @@ -113,10 +113,6 @@ export interface MessageMap { request: { reservePub: string }; response: dbTypes.ReserveRecord[]; }; - "get-planchets": { - request: { exchangeBaseUrl: string }; - response: dbTypes.PlanchetRecord[]; - }; "get-denoms": { request: { exchangeBaseUrl: string }; response: dbTypes.DenominationRecord[]; @@ -153,14 +149,6 @@ export interface MessageMap { request: {}; response: void; }; - "download-proposal": { - request: { url: string }; - response: number; - }; - "submit-pay": { - request: { contractTermsHash: string; sessionId: string | undefined }; - response: walletTypes.ConfirmPayResult; - }; "accept-refund": { request: { refundUrl: string }; response: string; diff --git a/src/webex/notify.ts b/src/webex/notify.ts index 4e53c3e1f..61a96cb1b 100644 --- a/src/webex/notify.ts +++ b/src/webex/notify.ts @@ -24,8 +24,6 @@ /** * Imports. */ -import URI = require("urijs"); - import wxApi = require("./wxApi"); declare var cloneInto: any; @@ -180,25 +178,19 @@ function registerHandlers() { }); addHandler("taler-create-reserve", (msg: any) => { - const params = { - amount: JSON.stringify(msg.amount), - bank_url: document.location.href, - callback_url: new URI(msg.callback_url) .absoluteTo(document.location.href), - suggested_exchange_url: msg.suggested_exchange_url, - wt_types: JSON.stringify(msg.wt_types), - }; - const uri = new URI(chrome.extension.getURL("/src/webex/pages/confirm-create-reserve.html")); - const redirectUrl = uri.query(params).href(); - window.location.href = redirectUrl; + const uri = new URL(chrome.extension.getURL("/src/webex/pages/confirm-create-reserve.html")); + uri.searchParams.set("amount", JSON.stringify(msg.amount)); + uri.searchParams.set("bank_url", document.location.href); + uri.searchParams.set("callback_url", new URL(msg.callback_url, document.location.href).href); + uri.searchParams.set("suggested_exchange_url", msg.suggested_exchange_url); + uri.searchParams.set("wt_types", JSON.stringify(msg.wt_types)); + window.location.href = uri.href; }); addHandler("taler-add-auditor", (msg: any) => { - const params = { - req: JSON.stringify(msg), - }; - const uri = new URI(chrome.extension.getURL("/src/webex/pages/add-auditor.html")); - const redirectUrl = uri.query(params).href(); - window.location.href = redirectUrl; + const uri = new URL(chrome.extension.getURL("/src/webex/pages/add-auditor.html")); + uri.searchParams.set("req", JSON.stringify(msg)) + window.location.href = uri.href; }); addHandler("taler-confirm-reserve", async (msg: any, sendResponse: any) => { diff --git a/src/webex/pages/add-auditor.tsx b/src/webex/pages/add-auditor.tsx index 7e3e06322..766db9c5d 100644 --- a/src/webex/pages/add-auditor.tsx +++ b/src/webex/pages/add-auditor.tsx @@ -23,7 +23,6 @@ import { CurrencyRecord } from "../../dbTypes"; import { getCurrencies, updateCurrency } from "../wxApi"; import React, { useState } from "react"; -import URI = require("urijs"); import { registerMountPage } from "../renderHtml"; interface ConfirmAuditorProps { @@ -118,14 +117,24 @@ function ConfirmAuditor(props: ConfirmAuditorProps) { registerMountPage(() => { - const walletPageUrl = new URI(document.location.href); - const query: any = JSON.parse( - (URI.parseQuery(walletPageUrl.query()) as any).req, - ); - const url = query.url; - const currency: string = query.currency; - const auditorPub: string = query.auditorPub; - const expirationStamp = Number.parseInt(query.expirationStamp); + const walletPageUrl = new URL(document.location.href); + const url = walletPageUrl.searchParams.get("url"); + if (!url) { + throw Error("missign parameter (url)"); + } + const currency = walletPageUrl.searchParams.get("currency"); + if (!currency) { + throw Error("missing parameter (currency)"); + } + const auditorPub = walletPageUrl.searchParams.get("auditorPub"); + if (!auditorPub) { + throw Error("missing parameter (auditorPub)"); + } + const auditorStampStr = walletPageUrl.searchParams.get("expirationStamp"); + if (!auditorStampStr) { + throw Error("missing parameter (auditorStampStr)"); + } + const expirationStamp = Number.parseInt(auditorStampStr); const args = { url, currency, auditorPub, expirationStamp }; return <ConfirmAuditor {...args}/>; }); diff --git a/src/webex/pages/pay.tsx b/src/webex/pages/pay.tsx index 7f2a174b7..cff2f9461 100644 --- a/src/webex/pages/pay.tsx +++ b/src/webex/pages/pay.tsx @@ -30,9 +30,8 @@ import { renderAmount, ProgressButton, registerMountPage } from "../renderHtml"; import * as wxApi from "../wxApi"; import React, { useState, useEffect } from "react"; -import URI = require("urijs"); -import * as Amounts from "../../amounts"; +import * as Amounts from "../../util/amounts"; function TalerPayDialog({ talerPayUri }: { talerPayUri: string }) { const [payStatus, setPayStatus] = useState<PreparePayResult | undefined>(); @@ -164,10 +163,10 @@ function TalerPayDialog({ talerPayUri }: { talerPayUri: string }) { } registerMountPage(() => { - const url = new URI(document.location.href); - const query: any = URI.parseQuery(url.query()); - - let talerPayUri = query.talerPayUri; - + const url = new URL(document.location.href); + const talerPayUri = url.searchParams.get("talerPayUri"); + if (!talerPayUri) { + throw Error("invalid parameter"); + } return <TalerPayDialog talerPayUri={talerPayUri} />; }); diff --git a/src/webex/pages/popup.tsx b/src/webex/pages/popup.tsx index 78b7374b3..27d5dddba 100644 --- a/src/webex/pages/popup.tsx +++ b/src/webex/pages/popup.tsx @@ -26,8 +26,8 @@ */ import * as i18n from "../../i18n"; -import { AmountJson } from "../../amounts"; -import * as Amounts from "../../amounts"; +import { AmountJson } from "../../util/amounts"; +import * as Amounts from "../../util/amounts"; import { HistoryEvent, @@ -44,9 +44,6 @@ import { import * as wxApi from "../wxApi"; import * as React from "react"; -import * as ReactDOM from "react-dom"; - -import URI = require("urijs"); function onUpdateNotification(f: () => void): () => void { const port = chrome.runtime.connect({ name: "notifications" }); @@ -339,7 +336,7 @@ function formatHistoryItem(historyItem: HistoryEvent) { </i18n.Translate> ); case "confirm-reserve": { - const exchange = new URI(d.exchangeBaseUrl).host(); + const exchange = new URL(d.exchangeBaseUrl).host; const pub = abbrev(d.reservePub); return ( <i18n.Translate wrap="p"> @@ -359,7 +356,7 @@ function formatHistoryItem(historyItem: HistoryEvent) { } case "depleted-reserve": { const exchange = d.exchangeBaseUrl - ? new URI(d.exchangeBaseUrl).host() + ? new URL(d.exchangeBaseUrl).host : "??"; const amount = renderAmount(d.requestedAmount); const pub = abbrev(d.reservePub); @@ -396,11 +393,10 @@ function formatHistoryItem(historyItem: HistoryEvent) { ); } case "tip": { - const tipPageUrl = new URI( - chrome.extension.getURL("/src/webex/pages/tip.html"), - ); - const params = { tip_id: d.tipId, merchant_domain: d.merchantDomain }; - const url = tipPageUrl.query(params).href(); + const tipPageUrl = new URL(chrome.extension.getURL("/src/webex/pages/tip.html")); + tipPageUrl.searchParams.set("tip_id", d.tipId); + tipPageUrl.searchParams.set("merchant_domain", d.merchantDomain); + const url = tipPageUrl.href; const tipLink = <a href={url} onClick={openTab(url)}>{i18n.str`tip`}</a>; // i18n: Tip return ( diff --git a/src/webex/pages/refund.tsx b/src/webex/pages/refund.tsx index 79cadcdc9..5196c9ea6 100644 --- a/src/webex/pages/refund.tsx +++ b/src/webex/pages/refund.tsx @@ -22,7 +22,6 @@ import React, { useEffect, useState } from "react"; import ReactDOM from "react-dom"; -import URI = require("urijs"); import * as wxApi from "../wxApi"; import { PurchaseDetails } from "../../walletTypes"; @@ -76,8 +75,7 @@ function RefundStatusView(props: { talerRefundUri: string }) { } async function main() { - const url = new URI(document.location.href); - const query: any = URI.parseQuery(url.query()); + const url = new URL(document.location.href); const container = document.getElementById("container"); if (!container) { @@ -85,7 +83,7 @@ async function main() { return; } - const talerRefundUri = query.talerRefundUri; + const talerRefundUri = url.searchParams.get("talerRefundUri"); if (!talerRefundUri) { console.error("taler refund URI requred"); return; diff --git a/src/webex/pages/return-coins.tsx b/src/webex/pages/return-coins.tsx index b5d53c31e..be65b4121 100644 --- a/src/webex/pages/return-coins.tsx +++ b/src/webex/pages/return-coins.tsx @@ -25,8 +25,8 @@ * Imports. */ -import { AmountJson } from "../../amounts"; -import * as Amounts from "../../amounts"; +import { AmountJson } from "../../util/amounts"; +import * as Amounts from "../../util/amounts"; import { SenderWireInfos, @@ -35,7 +35,7 @@ import { import * as i18n from "../../i18n"; -import * as wire from "../../wire"; +import * as wire from "../../util/wire"; import { getBalance, diff --git a/src/webex/pages/tip.tsx b/src/webex/pages/tip.tsx index 148b8203c..ac904cf0d 100644 --- a/src/webex/pages/tip.tsx +++ b/src/webex/pages/tip.tsx @@ -23,7 +23,6 @@ import * as React from "react"; import * as ReactDOM from "react-dom"; -import URI = require("urijs"); import * as i18n from "../../i18n"; @@ -31,7 +30,7 @@ import { acceptTip, getReserveCreationInfo, getTipStatus } from "../wxApi"; import { WithdrawDetailView, renderAmount, ProgressButton } from "../renderHtml"; -import * as Amounts from "../../amounts"; +import * as Amounts from "../../util/amounts"; import { useState, useEffect } from "react"; import { TipStatus } from "../../walletTypes"; @@ -68,7 +67,7 @@ function TipDisplay(props: { talerTipUri: string }) { const accept = async () => { setLoading(true); - await acceptTip(props.talerTipUri); + await acceptTip(tipStatus.tipId); setFinished(true); }; @@ -101,9 +100,8 @@ function TipDisplay(props: { talerTipUri: string }) { async function main() { try { - const url = new URI(document.location.href); - const query: any = URI.parseQuery(url.query()); - const talerTipUri = query.talerTipUri; + const url = new URL(document.location.href); + const talerTipUri = url.searchParams.get("talerTipUri"); if (typeof talerTipUri !== "string") { throw Error("talerTipUri must be a string"); } diff --git a/src/webex/pages/withdraw.tsx b/src/webex/pages/withdraw.tsx index 39b27f2d8..6b7152dc2 100644 --- a/src/webex/pages/withdraw.tsx +++ b/src/webex/pages/withdraw.tsx @@ -32,7 +32,6 @@ import { WithdrawDetailView, renderAmount } from "../renderHtml"; import React, { useState, useEffect } from "react"; import * as ReactDOM from "react-dom"; -import URI = require("urijs"); import { getWithdrawDetails, acceptWithdrawal } from "../wxApi"; function NewExchangeSelection(props: { talerWithdrawUri: string }) { @@ -199,9 +198,8 @@ function NewExchangeSelection(props: { talerWithdrawUri: string }) { async function main() { try { - const url = new URI(document.location.href); - const query: any = URI.parseQuery(url.query()); - let talerWithdrawUri = query.talerWithdrawUri; + const url = new URL(document.location.href); + const talerWithdrawUri = url.searchParams.get("talerWithdrawUri"); if (!talerWithdrawUri) { throw Error("withdraw URI required"); } diff --git a/src/webex/renderHtml.tsx b/src/webex/renderHtml.tsx index 42bcdbabc..945719b65 100644 --- a/src/webex/renderHtml.tsx +++ b/src/webex/renderHtml.tsx @@ -23,8 +23,8 @@ /** * Imports. */ -import { AmountJson } from "../amounts"; -import * as Amounts from "../amounts"; +import { AmountJson } from "../util/amounts"; +import * as Amounts from "../util/amounts"; import { DenominationRecord } from "../dbTypes"; import { ReserveCreationInfo } from "../walletTypes"; import * as moment from "moment"; diff --git a/src/webex/wxApi.ts b/src/webex/wxApi.ts index a8b35ed34..ea26cd2eb 100644 --- a/src/webex/wxApi.ts +++ b/src/webex/wxApi.ts @@ -22,7 +22,7 @@ /** * Imports. */ -import { AmountJson } from "../amounts"; +import { AmountJson } from "../util/amounts"; import { CoinRecord, CurrencyRecord, @@ -174,14 +174,6 @@ export function getCoins(exchangeBaseUrl: string): Promise<CoinRecord[]> { /** - * Get all planchets withdrawn from the given exchange. - */ -export function getPlanchets(exchangeBaseUrl: string): Promise<PlanchetRecord[]> { - return callBackend("get-planchets", { exchangeBaseUrl }); -} - - -/** * Get all denoms offered by the given exchange. */ export function getDenoms(exchangeBaseUrl: string): Promise<DenominationRecord[]> { @@ -211,13 +203,6 @@ export function confirmPay(proposalId: string, sessionId: string | undefined): P return callBackend("confirm-pay", { proposalId, sessionId }); } -/** - * Replay paying for a purchase. - */ -export function submitPay(contractTermsHash: string, sessionId: string | undefined): Promise<ConfirmPayResult> { - return callBackend("submit-pay", { contractTermsHash, sessionId }); -} - /** * Mark a reserve as confirmed. @@ -302,14 +287,6 @@ export function clearNotification(): Promise<void> { return callBackend("clear-notification", { }); } - -/** - * Download a contract. - */ -export function downloadProposal(url: string): Promise<number> { - return callBackend("download-proposal", { url }); -} - /** * Download a refund and accept it. */ diff --git a/src/webex/wxBackend.ts b/src/webex/wxBackend.ts index 78c86a976..2d7f963e9 100644 --- a/src/webex/wxBackend.ts +++ b/src/webex/wxBackend.ts @@ -23,8 +23,8 @@ /** * Imports. */ -import { BrowserHttpLib } from "../http"; -import { AmountJson } from "../amounts"; +import { BrowserHttpLib } from "../util/http"; +import { AmountJson } from "../util/amounts"; import { ConfirmReserveRequest, CreateReserveRequest, @@ -39,11 +39,10 @@ import { openTalerDb, exportDb, importDb, deleteDb } from "../db"; import { ChromeBadge } from "./chromeBadge"; import { MessageType } from "./messages"; import * as wxApi from "./wxApi"; -import URI = require("urijs"); import Port = chrome.runtime.Port; import MessageSender = chrome.runtime.MessageSender; import { BrowserCryptoWorkerFactory } from "../crypto/cryptoApi"; -import { OpenedPromise, openPromise } from "../promiseUtils"; +import { OpenedPromise, openPromise } from "../util/promiseUtils"; const NeedsWallet = Symbol("NeedsWallet"); @@ -122,15 +121,6 @@ async function handleMessage( } return needsWallet().confirmPay(detail.proposalId, detail.sessionId); } - case "submit-pay": { - if (typeof detail.contractTermsHash !== "string") { - throw Error("contractTermsHash must be a string"); - } - return needsWallet().submitPay( - detail.contractTermsHash, - detail.sessionId, - ); - } case "exchange-info": { if (!detail.baseUrl) { return Promise.resolve({ error: "bad url" }); @@ -170,7 +160,7 @@ async function handleMessage( if (typeof detail.reservePub !== "string") { return Promise.reject(Error("reservePub missing")); } - return needsWallet().withdrawPaybackReserve(detail.reservePub); + throw Error("not implemented"); } case "get-coins": { if (typeof detail.exchangeBaseUrl !== "string") { @@ -178,12 +168,6 @@ async function handleMessage( } return needsWallet().getCoinsForExchange(detail.exchangeBaseUrl); } - case "get-planchets": { - if (typeof detail.exchangeBaseUrl !== "string") { - return Promise.reject(Error("exchangBaseUrl missing")); - } - return needsWallet().getPlanchets(detail.exchangeBaseUrl); - } case "get-denoms": { if (typeof detail.exchangeBaseUrl !== "string") { return Promise.reject(Error("exchangBaseUrl missing")); @@ -244,9 +228,6 @@ async function handleMessage( case "clear-notification": { return needsWallet().clearNotification(); } - case "download-proposal": { - return needsWallet().downloadProposal(detail.url); - } case "abort-failed-payment": { if (!detail.contractTermsHash) { throw Error("contracTermsHash not given"); @@ -404,18 +385,19 @@ function makeSyncWalletRedirect( oldUrl: string, params?: { [name: string]: string | undefined }, ): object { - const innerUrl = new URI(chrome.extension.getURL("/src/webex/pages/" + url)); + const innerUrl = new URL(chrome.extension.getURL("/src/webex/pages/" + url)); if (params) { for (const key in params) { - if (params[key]) { - innerUrl.addSearch(key, params[key]); + const p = params[key]; + if (p) { + innerUrl.searchParams.set(key, p); } } } - const outerUrl = new URI( + const outerUrl = new URL( chrome.extension.getURL("/src/webex/pages/redirect.html"), ); - outerUrl.addSearch("url", innerUrl); + outerUrl.searchParams.set("url", innerUrl.href); if (isFirefox()) { // Some platforms don't support the sync redirect (yet), so fall back to // async redirect after a timeout. @@ -423,12 +405,12 @@ function makeSyncWalletRedirect( await waitMs(150); const tab = await getTab(tabId); if (tab.url === oldUrl) { - chrome.tabs.update(tabId, { url: outerUrl.href() }); + chrome.tabs.update(tabId, { url: outerUrl.href }); } }; doit(); } - return { redirectUrl: outerUrl.href() }; + return { redirectUrl: outerUrl.href }; } /** @@ -549,29 +531,29 @@ export async function wxMain() { if (!tab.url || !tab.id) { continue; } - const uri = new URI(tab.url); - if (uri.protocol() !== "http" && uri.protocol() !== "https") { + const uri = new URL(tab.url); + if (uri.protocol !== "http:" && uri.protocol !== "https:") { continue; } console.log( "injecting into existing tab", tab.id, "with url", - uri.href(), + uri.href, "protocol", - uri.protocol(), + uri.protocol, ); injectScript( tab.id, { file: "/dist/contentScript-bundle.js", runAt: "document_start" }, - uri.href(), + uri.href, ); const code = ` if (("taler" in window) || document.documentElement.getAttribute("data-taler-nojs")) { document.dispatchEvent(new Event("taler-probe-result")); } `; - injectScript(tab.id, { code, runAt: "document_start" }, uri.href()); + injectScript(tab.id, { code, runAt: "document_start" }, uri.href); } }); @@ -603,8 +585,8 @@ export async function wxMain() { if (!tab.url || !tab.id) { return; } - const uri = new URI(tab.url); - if (!(uri.protocol() === "http" || uri.protocol() === "https")) { + const uri = new URL(tab.url); + if (!(uri.protocol === "http:" || uri.protocol === "https:")) { return; } const code = ` @@ -612,7 +594,7 @@ export async function wxMain() { document.dispatchEvent(new Event("taler-probe-result")); } `; - injectScript(tab.id!, { code, runAt: "document_start" }, uri.href()); + injectScript(tab.id!, { code, runAt: "document_start" }, uri.href); }); }; |