1

I'm storing IPFS hash in solidity using this guide

https://www.reddit.com/r/ethdev/comments/6lbmhy/a_practical_guide_to_cheap_ipfs_hash_storage_in/

    const cid = "QmNXnCWPS2szLaQGVA6TFtiUAJB2YnFTJJFTXPGuc4wocQ";

    const fromIPFSHash = hash => {
        const bytes = bs58.decode(hash);
        const multiHashId = 2;
        // remove the multihash hash id
        return bytes.slice(multiHashId, bytes.length);
    };
    const toIPFSHash = str => {
        // remove leading 0x
        const remove0x = str.slice(2, str.length);
        // add back the multihash id
        const bytes = Buffer.from(`1220${remove0x}`, "hex");
        const hash = bs58.encode(bytes);
        return hash;
    };


    it('Store encoded ', async () => {
       const bytes32 = fromIPFSHash(cid);
          const str = bytes32.toString('hex')
          console.log(str);

          await instance.setBytes.sendTransaction(str);
          const returnedBytes = await instance.getBytes.call();

          console.log(returnedBytes);
          const originalCid = toIPFSHash(returnedBytes);
          console.log(originalCid);//QmUu4JqHgHcT6Q8PxvDn1UzZq2utkgf7RfgGv4eeYZ3Lyu

          assert(originalCid == cid); // it fails
});

The above assert fails .

pragma solidity ^0.4.4;

contract Hello {
    bytes32 x;
    function getBytes() returns(bytes32){
        return x;
    }
    function setBytes(bytes32 b)  {
        x = b;
    }

}
3

Try prepending 0x to let web3.js know that the argument should be interpreted as hex and not a string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.