Solidity

可靠地儲存 IPFS 雜湊

  • February 1, 2018

我正在使用本指南可靠地儲存 IPFS 雜湊

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

上述斷言失敗。

pragma solidity ^0.4.4;

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

}

嘗試預先0x讓 web3.js 知道參數應該被解釋為十六進製而不是字元串。

引用自:https://ethereum.stackexchange.com/questions/38147