pragma solidity ^0.4.26; contract Thesis { struct HealthRecord { string ipfsHash; string prevIpfsHash; uint timestamp; } HealthRecord[] healthrecords; function exists(string hash) public view returns (bool, bool) { bool hash1exists = false; bool hash2exists = false; for (uint i = 0; i < healthrecords.length - 1; i++) { string storage a = healthrecords[i].ipfsHash; string storage b = healthrecords[i].prevIpfsHash; if (keccak256(abi.encodePacked(a)) == keccak256(abi.encodePacked(hash))) { hash1exists = true; } if (keccak256(abi.encodePacked(b)) == keccak256(abi.encodePacked(hash))) { hash2exists = true; } } return (hash1exists, hash2exists); } function addHealthRecord(string ipfsHash, string prevIpfsHash) public { bool a; bool b; bool c; bool d; //require that ipfsHash doesn't exist in the blockchain (a, b) = exists(ipfsHash); require(a == false && b == false); //check if prevIpfsHash exists as an ipfsHash //but there should not be a prevIpfsHash in the blockchain that has the same hash if (keccak256(abi.encodePacked(prevIpfsHash)) != keccak256(abi.encodePacked(""))){ (c, d) = exists(prevIpfsHash); require(c == true && d == false); } healthrecords.push(HealthRecord(ipfsHash, prevIpfsHash, now)); } /* function getHealthRecords(uint[] indexes) public returns (string[], string[], uint[]) { string[] memory hashes = new string[](indexes.length); string[] memory prev_hashes = new string[](indexes.length); uint[] memory timestamps = new uint[](indexes.length); for (uint i = 0; i < indexes.length; i++) { HealthRecord storage healthrecord = healthrecords[indexes[i]]; hashes[i] = healthrecord.ipfsHash; prev_hashes[i] = healthrecord.prevIpfsHash; timestamps[i] = healthrecord.timestamp; } return (hashes, prev_hashes, timestamps); } */ }
0.4.26