Search code examples
javascriptencryptionwebsocketinflatepako

Inflate response from websocket API


I am getting the following message from a websocket endpoint and would like to know how to inflate the message and get a json. The response is from a cryptocurrency websocket api. I generally use pako but am unable to get pako inflate the below

Response

["78da6552cb8ac3300cfc17c39e3618c5b22dbbbf524a48936c6bfa2471d943e9bfafbd7152418f9a198d9e4fb10ffd2436dbadb0ca0180a844ad2428f4646b9523b1ab3249662619648b5e1a4f5a6bf08c33b31ca4331c9d338cf486bc43c5393d67e854dc32184b5d2b019df60a1d27df7ebc3135e728e018147b67c1794745bfab443b9dca06b25d1621cada594fc8264258fbb36955889a71f5624e8ea006eb38b76ea2b60eade35c694ae93c1cc79701d8045886454948356a438cd3508ad8345d5a2537d34b036c45582ea15102696335b9751f97763c0db1e9dbd88acd53748f711caeb199c2e12a36e23be97ec238c5e63e866e4888f1e511b262e89b3e4cb1bdfe53a8aa993ab72c61fdb4d36ddc87d82c15f6b15b35993e86c3f19d64ca079e6fbfac74b94afae24ffb74d837b8dc767a5c5248e9be52a577fa6cbbb977315b4ba42ff17afd018426bb16"]

Thank you


Solution

  • To use pako you must first convert your data from a hexadecimal string to a byte array. Here's one way to do this:

    function decodeHex(hexString) { 
      let result = new Uint8Array(hexString.length / 2); 
      for (let i = 0; i < result.length; i++) {
        result[i] = parseInt(hexString.slice(2 * i, 2 * i + 2), 16); 
      } 
      return result; 
    }
    let json = pako.inflate(decodeHex("your string"), 
                            { to: 'string' });
    let decoded = JSON.parse(json);