diff options
author | Florian Dold <florian.dold@gmail.com> | 2016-09-29 01:40:29 +0200 |
---|---|---|
committer | Florian Dold <florian.dold@gmail.com> | 2016-09-29 01:40:29 +0200 |
commit | 693e7c92e0dd23ee0577dea7d5f5a44eee1fa5be (patch) | |
tree | e18719ec5c626cccb29914346ea4520166fbac56 /content_scripts | |
parent | cc34929da6adf64acdf5434f91a3a0cb38df76e3 (diff) | |
download | wallet-core-693e7c92e0dd23ee0577dea7d5f5a44eee1fa5be.tar.xz |
history aggregation
Diffstat (limited to 'content_scripts')
-rw-r--r-- | content_scripts/notify.ts | 135 |
1 files changed, 93 insertions, 42 deletions
diff --git a/content_scripts/notify.ts b/content_scripts/notify.ts index e50c93c4d..ed704aaf0 100644 --- a/content_scripts/notify.ts +++ b/content_scripts/notify.ts @@ -45,10 +45,54 @@ namespace TalerNotify { interface Handler { type: string; - listener: (e: CustomEvent) => void; + listener: (e: CustomEvent) => void|Promise<void>; } const handlers: Handler[] = []; + function hashContract(contract: string): Promise<string> { + let walletHashContractMsg = { + type: "hash-contract", + detail: {contract} + }; + return new Promise((resolve, reject) => { + chrome.runtime.sendMessage(walletHashContractMsg, (resp: any) => { + if (!resp.hash) { + console.log("error", resp); + reject(Error("hashing failed")); + } + resolve(resp.hash); + }); + }); + } + + function checkRepurchase(contract: string): Promise<any> { + const walletMsg = { + type: "check-repurchase", + detail: { + contract: contract + }, + }; + return new Promise((resolve, reject) => { + chrome.runtime.sendMessage(walletMsg, (resp: any) => { + resolve(resp); + }); + }); + } + + function putHistory(historyEntry: any): Promise<void> { + const walletMsg = { + type: "put-history-entry", + detail: { + historyEntry, + }, + }; + return new Promise((resolve, reject) => { + chrome.runtime.sendMessage(walletMsg, (resp: any) => { + resolve(); + }); + }); + } + function init() { chrome.runtime.sendMessage({type: "ping"}, (resp) => { if (chrome.runtime.lastError) { @@ -150,7 +194,7 @@ namespace TalerNotify { }); - addHandler("taler-confirm-contract", (msg: any) => { + addHandler("taler-confirm-contract", async(msg: any) => { if (!msg.contract_wrapper) { console.error("contract wrapper missing"); return; @@ -173,53 +217,60 @@ namespace TalerNotify { detail: {contract: offer.contract} }; - chrome.runtime.sendMessage(walletHashContractMsg, (resp: any) => { + let contractHash = await hashContract(offer.contract); - if (!resp.hash) { - console.log("error", resp); - throw Error("hashing failed"); - } + if (contractHash != offer.H_contract) { + console.error("merchant-supplied contract hash is wrong"); + return; + } - if (resp.hash != offer.H_contract) { - console.error("merchant-supplied contract hash is wrong"); - return; + let resp = await checkRepurchase(offer.contract); + + if (resp.error) { + console.error("wallet backend error", resp); + return; + } + + if (resp.isRepurchase) { + console.log("doing repurchase"); + console.assert(resp.existingFulfillmentUrl); + console.assert(resp.existingContractHash); + window.location.href = subst(resp.existingFulfillmentUrl, + resp.existingContractHash); + + } else { + + let merchantName = "(unknown)"; + try { + merchantName = offer.contract.merchant.name; + } catch (e) { + // bad contract / name not included } - const walletMsg = { - type: "check-repurchase", + let historyEntry = { + timestamp: (new Date).getTime(), + subjectId: `contract-${contractHash}`, + type: "offer-contract", detail: { - contract: offer.contract - }, - }; - - chrome.runtime.sendMessage(walletMsg, (resp: any) => { - if (resp.error) { - console.error("wallet backend error", resp); - return; + contractHash, + merchantName, } - if (resp.isRepurchase) { - console.log("doing repurchase"); - console.assert(resp.existingFulfillmentUrl); - console.assert(resp.existingContractHash); - window.location.href = subst(resp.existingFulfillmentUrl, - resp.existingContractHash); + }; + await putHistory(historyEntry); - } else { - const uri = URI(chrome.extension.getURL( - "pages/confirm-contract.html")); - const params = { - offer: JSON.stringify(offer), - merchantPageUrl: document.location.href, - }; - const target = uri.query(params).href(); - if (msg.replace_navigation === true) { - document.location.replace(target); - } else { - document.location.href = target; - } - } - }); - }); + const uri = URI(chrome.extension.getURL( + "pages/confirm-contract.html")); + const params = { + offer: JSON.stringify(offer), + merchantPageUrl: document.location.href, + }; + const target = uri.query(params).href(); + if (msg.replace_navigation === true) { + document.location.replace(target); + } else { + document.location.href = target; + } + } }); addHandler("taler-payment-failed", (msg: any, sendResponse: any) => { |