I managed to get the compression of the JSON working and then uploaded it on the smart contract I also wrote.
It all works fine when I try to compress and decompress the JSON, but when I compress, store on the blockchain, and then decompress, it doesn’t work anymore and throws unknown compression method
.
Here is the function that writes onto the contract from the javascript API (unsing web3 and NodeJS)
async function writeContract(jsonstring){
console.log("Writing value to contract...");
console.log("JSON string to write:", jsonstring);
// compress
jsonstring = pako.deflate(JSON.stringify(jsonstring));
console.log("Compressed string:", jsonstring);
const tx = await contract.methods.storeJson(jsonstring).send({from: userAddress});
}
And here is the code that reads the smart contract, and decompress the file:
async function readJSON(){
console.log("Reading JSON string from contract...");
var value = await contract.methods.JSONfile().call();
// decompress
console.log("Reading compressed JSON string from contract:", value);
const decompressed = JSON.parse(pako.inflate(value, { to: 'string' }));
console.log("Reading JSON output", decompressed);
return decompressed;
}
The smart contract (written in Solidity):
contract Storage {
bytes public JSONfile;
event newStrIsStored(address indexed _from, bytes JSONfile, bytes32 JSONhash);
function storeJson(bytes memory json_in_str) public {
JSONfile = json_in_str;
emit newStrIsStored(msg.sender, JSONfile, JSONhash);
}
}
I tried to change the encoding, the parameters type of the function call in the smart contract, the type of of the input but nothing.
The expected output is the original JSON file.
Thank you for your help !