Speichern von IPFS-Hash in Solidität

Ich speichere IPFS-Hash mithilfe dieser Anleitung in Solidität

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
});

Die obige Behauptung schlägt fehl.

pragma solidity ^0.4.4;

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

}

Antworten (1)

Versuchen Sie 0x, web3.js voranzustellen, damit es weiß, dass das Argument als Hex und nicht als Zeichenfolge interpretiert werden soll.