diff options
author | Florian Dold <florian.dold@gmail.com> | 2017-03-03 15:37:04 +0100 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2017-03-03 15:37:17 +0100 |
commit | 16371276588b34769a09d940e608426b06ec3163 (patch) | |
tree | a3ac6a169502797cfa0145e726f9b7da0edf30bc /src | |
parent | ac342628533ddf1922c050d9c37428d73538d8d6 (diff) |
have from contract, remove extended contract query
Diffstat (limited to 'src')
-rw-r--r-- | src/content_scripts/notify.ts | 9 | ||||
-rw-r--r-- | src/types.ts | 3 | ||||
-rw-r--r-- | src/wallet.ts | 26 | ||||
-rw-r--r-- | src/wxBackend.ts | 38 |
4 files changed, 28 insertions, 48 deletions
diff --git a/src/content_scripts/notify.ts b/src/content_scripts/notify.ts index a731e4da1..07ce87900 100644 --- a/src/content_scripts/notify.ts +++ b/src/content_scripts/notify.ts @@ -76,11 +76,10 @@ namespace TalerNotify { }); } - function queryPayment(query: any): Promise<any> { - // current URL without fragment + function queryPayment(url: string): Promise<any> { const walletMsg = { type: "query-payment", - detail: query, + detail: { url }, }; return new Promise((resolve, reject) => { chrome.runtime.sendMessage(walletMsg, (resp: any) => { @@ -331,7 +330,9 @@ namespace TalerNotify { }); addHandler("taler-pay", async(msg: any, sendResponse: any) => { - let res = await queryPayment(msg.contract_query); + // current URL without fragment + let url = URI(document.location.href).fragment("").href(); + let res = await queryPayment(url); logVerbose && console.log("taler-pay: got response", res); if (res && res.payReq) { sendResponse(res); diff --git a/src/types.ts b/src/types.ts index 28c989a0f..f28cdb411 100644 --- a/src/types.ts +++ b/src/types.ts @@ -484,6 +484,9 @@ export class Contract { order_id: string; @Checkable.String + pay_url: string; + + @Checkable.String fulfillment_url: string; @Checkable.Any diff --git a/src/wallet.ts b/src/wallet.ts index 01d26c297..50febf946 100644 --- a/src/wallet.ts +++ b/src/wallet.ts @@ -669,27 +669,13 @@ export class Wallet { /** - * Retrieve all necessary information for looking up the contract - * with the given hash. + * Retrieve information required to pay for a contract, where the + * contract is identified via the fulfillment url. */ - async queryPayment(query: any): Promise<any> { - let t: TransactionRecord | undefined; - - console.log("query for payment", query); - - switch (query.type) { - case "fulfillment_url": - t = await this.q().getIndexed(Stores.transactions.fulfillmentUrlIndex, query.value); - break; - case "order_id": - t = await this.q().getIndexed(Stores.transactions.orderIdIndex, query.value); - break; - case "hash": - t = await this.q().get<TransactionRecord>(Stores.transactions, query.value); - break; - default: - throw Error("invalid type"); - } + async queryPayment(url: string): Promise<any> { + console.log("query for payment", url); + + const t = await this.q().getIndexed(Stores.transactions.fulfillmentUrlIndex, url); if (!t) { console.log("query for payment failed"); diff --git a/src/wxBackend.ts b/src/wxBackend.ts index 5957e1e1b..ad738acfb 100644 --- a/src/wxBackend.ts +++ b/src/wxBackend.ts @@ -155,7 +155,7 @@ function makeHandlers(db: IDBDatabase, return Promise.resolve(msg); } } - return wallet.queryPayment(detail); + return wallet.queryPayment(detail.url); }, ["exchange-info"]: function (detail) { if (!detail.baseUrl) { @@ -318,6 +318,13 @@ class ChromeNotifier implements Notifier { */ let paymentRequestCookies: { [n: number]: any } = {}; + +/** + * 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. + */ function handleHttpPayment(headerList: chrome.webRequest.HttpHeader[], url: string, tabId: number): any { const headers: { [s: string]: string } = {}; for (let kv of headerList) { @@ -330,44 +337,26 @@ function handleHttpPayment(headerList: chrome.webRequest.HttpHeader[], url: stri contract_url: headers["x-taler-contract-url"], contract_query: headers["x-taler-contract-query"], offer_url: headers["x-taler-offer-url"], - pay_url: headers["x-taler-pay-url"], } - let n: number = 0; - - for (let key of Object.keys(fields)) { - if ((fields as any)[key]) { - n++; - } - } + let talerHeaderFound = Object.keys(fields).filter((x: any) => (fields as any)[x]).length != 0; - if (n == 0) { + if (!talerHeaderFound) { // 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"); - } - - let contract_query = undefined; - // parse " type [ ':' value ] " format - if (fields.contract_query) { - let res = /[-a-zA-Z0-9_.,]+(:.*)?/.exec(fields.contract_query); - if (res) { - contract_query = {type: res[0], value: res[1]}; - if (contract_query.type == "fulfillment_url" && !contract_query.value) { - contract_query.value = url; - } - } + return; } let payDetail = { - contract_query, contract_url: fields.contract_url, offer_url: fields.offer_url, - pay_url: fields.pay_url, }; console.log("got pay detail", payDetail) + // This cookie will be read by the injected content script + // in the tab that displays the page. paymentRequestCookies[tabId] = { type: "pay", payDetail, @@ -375,6 +364,7 @@ function handleHttpPayment(headerList: chrome.webRequest.HttpHeader[], url: stri } + function handleBankRequest(wallet: Wallet, headerList: chrome.webRequest.HttpHeader[], url: string, tabId: number): any { const headers: { [s: string]: string } = {}; |