From 5e85cd8b8fa25ed3fbfc260b48bcad098978407a Mon Sep 17 00:00:00 2001
From: Florian Dold <florian.dold@gmail.com>
Date: Tue, 9 Feb 2016 21:56:06 +0100
Subject: new reserve creation protocol

---
 extension/pages/confirm-create-reserve.tsx | 48 ++++++++++++++++++++++--------
 1 file changed, 36 insertions(+), 12 deletions(-)

(limited to 'extension/pages/confirm-create-reserve.tsx')

diff --git a/extension/pages/confirm-create-reserve.tsx b/extension/pages/confirm-create-reserve.tsx
index 88af96466..e4d2d27e6 100644
--- a/extension/pages/confirm-create-reserve.tsx
+++ b/extension/pages/confirm-create-reserve.tsx
@@ -14,6 +14,9 @@
  TALER; see the file COPYING.  If not, If not, see <http://www.gnu.org/licenses/>
  */
 
+import {AmountJson} from "../lib/wallet/types";
+import {amountToPretty} from "../lib/web-common";
+import {CreateReserveResponse} from "../lib/wallet/types";
 "use strict";
 
 
@@ -21,12 +24,8 @@ export function main() {
   function updateAmount() {
     let showAmount = document.getElementById("show-amount");
     console.log("Query is " + JSON.stringify(query));
-    let s = query.amount_str;
-    if (!s) {
-      document.body.innerHTML = "Oops, something went wrong.";
-      return;
-    }
-    showAmount.textContent = s;
+    let amount = AmountJson.checked(JSON.parse(query.amount));
+    showAmount.textContent = amountToPretty(amount);
   }
 
   let url = URI(document.location.href);
@@ -35,12 +34,37 @@ export function main() {
   updateAmount();
 
   document.getElementById("confirm").addEventListener("click", (e) => {
-    let d = Object.assign({}, query);
-    d.mint = (document.getElementById('mint-url') as HTMLInputElement).value;
+    const d = {
+      mint: (document.getElementById('mint-url') as HTMLInputElement).value,
+      amount: JSON.parse(query.amount)
+    };
+
+    if (!d.mint) {
+      // FIXME: indicate error instead!
+      throw Error("mint missing");
+    }
 
-    const cb = (resp) => {
-      if (resp.success === true) {
-        document.location.href = resp.backlink;
+    if (!d.amount) {
+      // FIXME: indicate error instead!
+      throw Error("amount missing");
+    }
+
+    const cb = (rawResp) => {
+      if (!rawResp) {
+        throw Error("empty response");
+      }
+      if (!rawResp.error) {
+        const resp = CreateReserveResponse.checked(rawResp);
+        let q = {
+          mint: resp.mint,
+          reserve_pub: resp.reservePub,
+          amount: query.amount,
+        };
+        let url = URI(query.callback_url).addQuery(q);
+        if (!url.is("absolute")) {
+          throw Error("callback url is not absolute");
+        }
+        document.location.href = url.href();
       } else {
         document.body.innerHTML =
           `Oops, something went wrong.  It looks like the bank could not
@@ -48,6 +72,6 @@ export function main() {
             to check what happened.`;
       }
     };
-    chrome.runtime.sendMessage({type: 'confirm-reserve', detail: d}, cb);
+    chrome.runtime.sendMessage({type: 'create-reserve', detail: d}, cb);
   });
 }
\ No newline at end of file
-- 
cgit v1.2.3

