3

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

4

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

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.