From 19798cb6b5a266c8acc5c61da9c4fe84b3154804 Mon Sep 17 00:00:00 2001
From: Zdravko Iliev <zdravko.iliev@vereign.com>
Date: Mon, 14 Feb 2022 18:14:07 +0200
Subject: [PATCH] [#29](bugfix): add text encoder for first ipfs version on
 decrypt

---
 dist/services/IPFSService.js                   |  8 ++++++--
 package.json                                   |  2 +-
 src/services/CryptoService/CryptoServiceWeb.ts |  2 ++
 src/services/IPFSService.ts                    | 12 +++++++++---
 4 files changed, 18 insertions(+), 6 deletions(-)

diff --git a/dist/services/IPFSService.js b/dist/services/IPFSService.js
index 72d5a8a..fa99b55 100644
--- a/dist/services/IPFSService.js
+++ b/dist/services/IPFSService.js
@@ -24,7 +24,7 @@ class IPFSDataRetriever {
     getDecodedContent(contentData, ipfsVersion) {
         return __awaiter(this, void 0, void 0, function* () {
             const rawContent = yield this.getRawContent(contentData.cid, contentData.head, ipfsVersion);
-            return this.decryptContent(rawContent, contentData.key);
+            return this.decryptContent(rawContent, contentData.key, ipfsVersion);
         });
     }
     getRawContent(cid, head, ipfsVersion) {
@@ -54,10 +54,14 @@ class IPFSDataRetriever {
             return assembledData;
         });
     }
-    decryptContent(content, decryptionKey) {
+    decryptContent(content, decryptionKey, ipfsVersion) {
         return __awaiter(this, void 0, void 0, function* () {
             const { key, data: iv } = QrCodeDataService_1.default.decodeKeyDataPair(decryptionKey);
             const decryptedZip = yield CryptoService_1.default.decryptAESGCM(content, (0, common_1.base64ToArrayBuffer)(key), (0, common_1.base64ToArrayBuffer)(iv), true);
+            if (ipfsVersion === undefined) {
+                const decodedZip = new TextDecoder().decode(decryptedZip);
+                return (0, common_1.decompressData)(decodedZip);
+            }
             const decompressed = (0, common_1.decompressData)(decryptedZip);
             return decompressed;
         });
diff --git a/package.json b/package.json
index e86e759..dcb129d 100644
--- a/package.json
+++ b/package.json
@@ -1,6 +1,6 @@
 {
   "name": "@vereign/light-utils",
-  "version": "2.0.4",
+  "version": "v2.0.7",
   "license": "MIT",
   "main": "./dist/index.js",
   "types": "./dist/index.d.ts",
diff --git a/src/services/CryptoService/CryptoServiceWeb.ts b/src/services/CryptoService/CryptoServiceWeb.ts
index d5ef7a0..06534df 100644
--- a/src/services/CryptoService/CryptoServiceWeb.ts
+++ b/src/services/CryptoService/CryptoServiceWeb.ts
@@ -101,9 +101,11 @@ class CryptoServiceWeb implements ICryptoService {
       importedKey,
       data
     );
+
     if (returnBuffer) {
       return decrypted;
     }
+
     return new TextDecoder().decode(decrypted);
   }
 
diff --git a/src/services/IPFSService.ts b/src/services/IPFSService.ts
index 18a89c5..c2644aa 100644
--- a/src/services/IPFSService.ts
+++ b/src/services/IPFSService.ts
@@ -21,7 +21,7 @@ class IPFSDataRetriever {
       contentData.head,
       ipfsVersion
     );
-    return this.decryptContent(rawContent, contentData.key);
+    return this.decryptContent(rawContent, contentData.key, ipfsVersion);
   }
 
   private async getRawContent(
@@ -37,6 +37,7 @@ class IPFSDataRetriever {
           url: `${this.ipfsUrl}${GET_CONTENT_PATH}/${cid}`,
           method: "GET",
         });
+
         ipfsFileContent = base64ToArrayBuffer(response.data);
       } else if (ipfsVersion === "v2") {
         const result = await axios.get(
@@ -59,7 +60,8 @@ class IPFSDataRetriever {
 
   private async decryptContent(
     content: ArrayBuffer,
-    decryptionKey: string
+    decryptionKey: string,
+    ipfsVersion: string
   ): Promise<ArrayBuffer> {
     const { key, data: iv } =
       QrCodeDataService.decodeKeyDataPair(decryptionKey);
@@ -71,8 +73,12 @@ class IPFSDataRetriever {
       true
     );
 
-    const decompressed = decompressData(decryptedZip);
+    if (ipfsVersion === undefined) {
+      const decodedZip = new TextDecoder().decode(decryptedZip);
+      return decompressData(decodedZip);
+    }
 
+    const decompressed = decompressData(decryptedZip);
     return decompressed;
   }
 }
-- 
GitLab