I am storing addresses on the blockchain for IPFS objects, and the addresses are more than 32 bytes. I was using bytes32 for testing but I have truncation. I am seeking a method to read data from the blockchain (array of strings) and then return that into the reactjs to process.
For example I have –
Zipfs.deployed().then(instance=>instance.getIpfsData());
[ '0x516d586465315a5552556442524c68706f4178587a324b7a585371646f6e446d',
'0x516d6538414d69774d434d446478415936675a3338714a6b6570566b516b5964' ]
I was using bytes32[] but that is not working as a return as follows –
pragma solidity ^0.4.8;
contract Zipfs {
IpfsData[] public ipfsrecs;
struct IpfsData {
bytes32 reviewData;
}
function addIpfs (bytes32 _reviewData) payable returns (bool success) {
IpfsData memory newIpfsData;
newIpfsData.reviewData = _reviewData;
ipfsrecs.push(newIpfsData);
return true;
}
function getIpfsData() constant returns (bytes32[]) {
uint length = ipfsrecs.length;
bytes32[] memory reviews = new bytes32[](length);
for (uint i=0; i<ipfsrecs.length; i++) {
IpfsData memory currentIpfsRec;
currentIpfsRec = ipfsrecs[i];
reviews[i]=currentIpfsRec.reviewData;
}
return (reviews);
}
}
What can I do as it appears arrays of strings are not supported?
Best Answer
Life is easier if you can assume 32 bytes. If you are able to restrict the multihash formats you accept or assume
sha2-256
, which is common now, you could just drop some meta-data so that they fit in 32 bytes. To convert to abytes32
, base-58-decode the data, then strip what should be0x1220
off the front. The12
indicates the format (sha2-256
), and the20
is the length of the data, ie 32 bytes.If you want to handle all the legal multihash formats properly, you could concatenate them together as a single byte array, then manage a separate array holding the start offset of each entry.