[Ethereum] Sample showing how to store and retrieve a list of IPFS hash on Ethereum

ipfssolidityweb3js

I want to store a list of IPFS hash on a smart contract's function exposing a bytes32[] array.

I'm using web3.js and Solidity.

I have used multi-hash and/or some examples using bs58, but the value I retrieved and converted from the smart contract gives me a hash different from IPFS.

Do you any tips to share with me?

Thanks

Said

Best Answer

Ok, I got it thanks to this great sample: https://bitbucket.org/edmundedgar/realitycheck/src/3ec966f1cf253c59caa224ee0e8e0e0d1037741d/assets/js/scripts/main.js?at=master&fileviewer=file-view-default#main.js-407

I have used these functions and was able to store/retrieve my IPFS hash to/from bytes32 Solidity data type:

function ipfsHashToBytes32(ipfs_hash) {
    var h = bs58.decode(ipfs_hash).toString('hex').replace(/^1220/, '');
    if (h.length != 64) {
        console.log('invalid ipfs format', ipfs_hash, h);
        return null;
    }
    return '0x' + h;
}

function bytes32ToIPFSHash(hash_hex) {
    //console.log('bytes32ToIPFSHash starts with hash_buffer', hash_hex.replace(/^0x/, ''));
    var buf = new Buffer(hash_hex.replace(/^0x/, '1220'), 'hex')
    return bs58.encode(buf)
}
Related Topic