From 6ba70e5ab1618f27bd131859a5a879db9a0fca0a Mon Sep 17 00:00:00 2001
From: Damyan Mitev <damyan.mitev@vereign.com>
Date: Tue, 14 May 2019 15:55:32 +0300
Subject: [PATCH] Move useful methods implementation in the core file

---
 javascript/src/iframe/viamapi-iframe.js | 112 ++++++++++++++----------
 1 file changed, 68 insertions(+), 44 deletions(-)

diff --git a/javascript/src/iframe/viamapi-iframe.js b/javascript/src/iframe/viamapi-iframe.js
index 98391dd..0accdcc 100644
--- a/javascript/src/iframe/viamapi-iframe.js
+++ b/javascript/src/iframe/viamapi-iframe.js
@@ -414,6 +414,60 @@ function getCertificateForPassport(passportUUID, internal) {
   });
 }
 
+function stringToUtf8ByteArray(str) {
+  if (typeof str !== 'string') {
+    str = str.toString();
+  }
+  const res = Buffer.from(str, 'utf-8');
+  return res;
+}
+
+function utf8ByteArrayToString(ba) {
+  if (!Buffer.isBuffer(ba)) {
+    ba = Buffer.from(ba);
+  }
+  const res = ba.toString('utf-8');
+  return res;
+}
+
+function stringToUtf8Base64(str) {
+  if (!Buffer.isBuffer(str)) {
+    if (typeof str !== 'string') {
+      str = str.toString();
+    }
+    str = Buffer.from(str, 'utf-8');
+  }
+  const res = str.toString('base64');
+  return res;
+}
+
+function utf8Base64ToString(strBase64) {
+  if (!Buffer.isBuffer(strBase64)) {
+    if (typeof strBase64 !== 'string') {
+      strBase64 = strBase64.toString();
+    }
+    strBase64 = Buffer.from(strBase64, 'base64');
+  }
+  const res = strBase64.toString('utf-8');
+  return res;
+}
+
+function base64ToByteArray(strBase64) {
+  if (typeof strBase64 !== 'string') {
+    strBase64 = strBase64.toString();
+  }
+  const res = Buffer.from(strBase64, 'base64');
+  return res;
+}
+
+function byteArrayToBase64(ba) {
+  if (!Buffer.isBuffer(ba)) {
+    ba = Buffer.from(ba);
+  }
+  const res = ba.toString('base64');
+  return res;
+}
+
 const connection = Penpal.connectToParent({
   // Methods child is exposing to parent
   methods: {
@@ -981,11 +1035,11 @@ const connection = Penpal.connectToParent({
         return encodeResponse("400", "", downloadResponse.status);
       }
 
-      const pdfRaw = await this.base64ToByteArray(downloadResponse.data);
+      const pdfRaw = base64ToByteArray(downloadResponse.data);
 
       const signedPdf = await signPdf(pdfRaw, certificateOneTime, passportChain, privateKeyOneTime);
 
-      const signedPdfB64 = await this.byteArrayToBase64(signedPdf);
+      const signedPdfB64 = byteArrayToBase64(signedPdf);
 
       const uploadResponse = await executeRestfulFunction(
         "private", window.viamApi, window.viamApi.documentPutDocumentByUUID, null, documentUUID, pdfContentType, signedPdfB64);
@@ -1124,64 +1178,34 @@ const connection = Penpal.connectToParent({
       );
     },
     stringToUtf8ByteArray(str) {
-      if (typeof str !== 'string') {
-        str = str.toString()
-      }
-      let res = Buffer.from(str,'utf-8');
       return new Penpal.Promise(result => {
-        result(res)
-      })
+        result(stringToUtf8ByteArray(str));
+      });
     },
     utf8ByteArrayToString(ba) {
-      if (!Buffer.isBuffer(ba)) {
-        ba = Buffer.from(ba)
-      }
-      let res = ba.toString('utf-8');
       return new Penpal.Promise(result => {
-        result(res)
-      })
+        result(utf8ByteArrayToString(ba));
+      });
     },
     stringToUtf8Base64(str) {
-      if (!Buffer.isBuffer(str)) {
-        if (typeof str !== 'string') {
-          str = str.toString()
-        }
-        str = Buffer.from(str, 'utf-8')
-      }
-      let res = str.toString('base64');
       return new Penpal.Promise(result => {
-        result(res)
-      })
+        result(stringToUtf8Base64(str));
+      });
     },
     utf8Base64ToString(strBase64) {
-      if (!Buffer.isBuffer(strBase64)) {
-        if (typeof strBase64 !== 'string') {
-          strBase64 = strBase64.toString()
-        }
-        strBase64 = Buffer.from(strBase64, 'base64')
-      }
-      let res = strBase64.toString('utf-8');
       return new Penpal.Promise(result => {
-        result(res)
-      })
+        result(utf8Base64ToString(strBase64));
+      });
     },
     base64ToByteArray(strBase64) {
-      if (typeof strBase64 !== 'string') {
-        strBase64 = strBase64.toString()
-      }
-      let res = Buffer.from(strBase64, 'base64');
       return new Penpal.Promise(result => {
-        result(res)
-      })
+        result(base64ToByteArray(strBase64));
+      });
     },
     byteArrayToBase64(ba) {
-      if (!Buffer.isBuffer(ba)) {
-        ba = Buffer.from(ba)
-      }
-      let res = ba.toString('base64');
       return new Penpal.Promise(result => {
-        result(res)
-      })
+        result(byteArrayToBase64(ba));
+      });
     },
 
     // Collabora APIs
-- 
GitLab