From 2a5aa09a8ea98999dadd911fca179bb8ed829c21 Mon Sep 17 00:00:00 2001
From: Jono Wenger <jono@elixxir.io>
Date: Mon, 12 Sep 2022 18:33:56 -0700
Subject: [PATCH] Create javascript utils file

---
 examples/sendE2E/receiver.html |  3 +-
 examples/sendE2E/sender.html   |  3 +-
 examples/sendE2E/xxdk.js       | 43 --------------------------
 examples/utils.js              | 55 ++++++++++++++++++++++++++++++++++
 4 files changed, 59 insertions(+), 45 deletions(-)
 create mode 100644 examples/utils.js

diff --git a/examples/sendE2E/receiver.html b/examples/sendE2E/receiver.html
index 620de91e..e3279bfe 100644
--- a/examples/sendE2E/receiver.html
+++ b/examples/sendE2E/receiver.html
@@ -15,6 +15,7 @@
 	<link rel="shortcut icon" type="image/x-icon" href="receiver-favicon.ico"/>
 
 	<script type="text/javascript" src="xxdk.js"></script>
+	<script type="text/javascript" src="../utils.js"></script>
 	<script type="text/javascript" src="../wasm_exec.js"></script>
 	<script>
         const go = new Go();
@@ -39,7 +40,7 @@
             // Client specific parameters
             const recipientContactFile = '';
             const myContactFileName = 'receiverContact.xxc';
-            const statePath = 'statePathRecipient2';
+            const statePath = 'statePathRecipient';
             const statePass = 'password';
 
             document.getElementById('ndfInput').addEventListener('change', e => {
diff --git a/examples/sendE2E/sender.html b/examples/sendE2E/sender.html
index 7ece9fe5..b84fa827 100644
--- a/examples/sendE2E/sender.html
+++ b/examples/sendE2E/sender.html
@@ -15,6 +15,7 @@
 	<link rel="shortcut icon" type="image/x-icon" href="sender-favicon.ico"/>
 
 	<script type="text/javascript" src="xxdk.js"></script>
+	<script type="text/javascript" src="../utils.js"></script>
 	<script type="text/javascript" src="../wasm_exec.js"></script>
 	<script>
         const go = new Go();
@@ -39,7 +40,7 @@
             // Client specific parameters
             const recipientContactFile = '<xxc(2)I71lyX8LvKP6cJhedliFLIKzXN/2ArmBAfAxkk1QapADrgZ7Ugdw/BAr6RDo4K2MfHn7e06Kru7jW05ROtaaoNSyMIB3R77BIJy7xB/fvkzteTpxxmHtZdXPt9UpYVA9WKzbM57vkmy+U6BlRJIrN97tAlWbODIG1FGpjWI/BCdqwaLgu0wTYLzzEw38NNyax0E2MnvURgQoSfnX5V+m+Qd2sJBqfBKi9tlLuNcaFI+TGyKGbZdSs4AZdiL/Xm94w1/id4SDEoV73RaJb3RH+W+rEMIl1SnZ4U6Ifo39cZfRKevL2mXUZj/SE0NWGl4+10Jovy0Hp6NoW2iMH4zB30xv3fqyp8ATAYXYYO5ToFmHvP1fRLq7TKa+WtGnO70uiykBaYl219WcGYIz4jXDYyN3yNnOhihKo2tauyC0qGeGCSoACDxvzLGTHxHkS/cDA92L0MEsiXy3HIuv5+OYZBuiWl1dLxjkUKKo4f3VyDUaWjrlUFqVO95X29+Zuw++AK1TVnWtt/F4J+L91hwEfDGxYn5f2I6tNKLXnyXlPVDpa1EE6+d1LWznfXVHHzL58lBIK0Yk9MKGHQAAAgA7OaXVXCYpP2SfHIEp3s0tRA==xxc>';
             const myContactFileName = '';
-            const statePath = 'statePathSender2';
+            const statePath = 'statePathSender';
             const statePass = 'password';
 
             document.getElementById('ndfInput').addEventListener('change', e => {
diff --git a/examples/sendE2E/xxdk.js b/examples/sendE2E/xxdk.js
index 42aee6c6..204036d5 100644
--- a/examples/sendE2E/xxdk.js
+++ b/examples/sendE2E/xxdk.js
@@ -222,47 +222,4 @@ async function SendE2e(htmlConsole, stopNetworkFollowerBtn, ndf,
     } else {
         htmlConsole.log("No recipient specified. Waiting for request")
     }
-}
-
-function download(filename, text) {
-    let element = document.createElement('a');
-    element.setAttribute('href', 'data:text/plain;charset=utf-8,' + encodeURIComponent(text));
-    element.setAttribute('download', filename);
-
-    element.style.display = 'none';
-    document.body.appendChild(element);
-
-    element.click();
-
-    document.body.removeChild(element);
-}
-
-const sleepUntil = async (f, timeoutMs) => {
-    return new Promise((resolve, reject) => {
-        const timeWas = new Date();
-        const wait = setInterval(function() {
-            if (f()) {
-                console.log("resolved after", new Date() - timeWas, "ms");
-                clearInterval(wait);
-                resolve();
-            } else if (new Date() - timeWas > timeoutMs) { // Timeout
-                console.log("rejected after", new Date() - timeWas, "ms");
-                clearInterval(wait);
-                reject();
-            }
-        }, 20);
-    });
-}
-
-function newHtmlConsole(node) {
-    return {
-        log: function (message) {
-            console.log(message)
-            node.innerHTML += "<p>" + message + "</p>"
-        },
-        error: function (message) {
-            console.error(message)
-            node.innerHTML += "<p class='error'>" + message + "</p>"
-        }
-    };
 }
\ No newline at end of file
diff --git a/examples/utils.js b/examples/utils.js
new file mode 100644
index 00000000..b657062c
--- /dev/null
+++ b/examples/utils.js
@@ -0,0 +1,55 @@
+////////////////////////////////////////////////////////////////////////////////
+// Copyright © 2022 xx foundation                                             //
+//                                                                            //
+// Use of this source code is governed by a license that can be found in the  //
+// LICENSE file.                                                              //
+////////////////////////////////////////////////////////////////////////////////
+
+// Function to download data to a file.
+function download(filename, data) {
+    const file = new Blob([data], {type: 'text/plain'});
+    let a = document.createElement("a"),
+        url = URL.createObjectURL(file);
+    a.href = url;
+    a.download = filename;
+    document.body.appendChild(a);
+    a.click();
+    setTimeout(function() {
+        document.body.removeChild(a);
+        window.URL.revokeObjectURL(url);
+    }, 0);
+}
+
+// sleepUntil waits until the condition f is met or until the timeout is
+// reached.
+async function sleepUntil(f, timeoutMs) {
+    return new Promise((resolve, reject) => {
+        const timeWas = new Date();
+        const wait = setInterval(function() {
+            if (f()) {
+                console.log("resolved after", new Date() - timeWas, "ms");
+                clearInterval(wait);
+                resolve();
+            } else if (new Date() - timeWas > timeoutMs) { // Timeout
+                console.log("rejected after", new Date() - timeWas, "ms");
+                clearInterval(wait);
+                reject();
+            }
+        }, 20);
+    });
+}
+
+// newHtmlConsole returns an object that allows for printing log messages or
+// error messages to an element.
+function newHtmlConsole(elem) {
+    return {
+        log: function (message) {
+            console.log(message)
+            elem.innerHTML += "<p>" + message + "</p>"
+        },
+        error: function (message) {
+            console.error(message)
+            elem.innerHTML += "<p class='error'>" + message + "</p>"
+        }
+    };
+}
\ No newline at end of file
-- 
GitLab