Created
July 26, 2018 06:52
-
-
Save cryptoquick/ac5b4368252b6f2dc7ff75bf00f2f48b to your computer and use it in GitHub Desktop.
Solidity Smart Contract for storing IPFS hashes in Collection format. (untested, WIP)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.4.21; | |
// ContentCollection is meant to store hashes from IPFS in a "Collection" format (array of structs), per user. | |
contract ContentCollection { | |
// Content types / Mime Types | |
// 0 - deleted | |
// 1 - text/plain | |
// 2 - application/json | |
// 3 - image/jpeg | |
// 4 - image/png | |
// Content Index corresponds to a variety of more meaningful content contexts in the application (avatar, profile data, contribution, comment, etc.) | |
// Content Hash corresponds to an IPFS hash. | |
struct Content { | |
bytes1 contentType; | |
bytes4 contentIndex; | |
bytes32 contentHash; | |
} | |
// Mapping of Content struct arrays (collections) by user address, allows only authorized users to edit their own data | |
mapping (address => Content[]) contents; | |
// Helps listening to events, either by sender or by hash. | |
event contentAdded ( | |
address contentOwner, | |
bytes32 contentHash | |
); | |
// Multiple records can be added at once, but they must be added by array. | |
function addContent(bytes1[] contentTypes, bytes4[] contentIndices, bytes32[] contentHashes) public { | |
require(contentTypes.length == contentIndices.length && contentIndices.length == contentHashes.length); | |
for (uint c = 0; c < contentTypes.length; c++) { | |
contents[msg.sender].push(Content(contentTypes[c], contentIndices[c], contentHashes[c])); | |
} | |
} | |
function transformContents(Content[] contentArray, bytes4 filterIndex) pure internal returns (bytes1[], bytes4[], bytes32[]) { | |
bytes1[] memory contentTypes; | |
bytes4[] memory contentIndices; | |
bytes32[] memory contentHashes; | |
for (uint c = 0; c < contentArray.length; c++) { | |
Content memory content = contentArray[c]; | |
// filterIndex == 0 not the same as deleted content. | |
if ((filterIndex > 0x0 && content.contentIndex == filterIndex) || filterIndex == 0x0) { | |
contentTypes[c] = content.contentType; | |
contentIndices[c] = content.contentIndex; | |
contentHashes[c] = content.contentHash; | |
} | |
} | |
return (contentTypes, contentIndices, contentHashes); | |
} | |
function getContentsByAddress(address user) public view returns (bytes1[], bytes4[], bytes32[]) { | |
return transformContents(contents[user], 0); | |
} | |
function getContentsByAddressAndIndex(address user, bytes4 index) public view returns (bytes1[], bytes4[], bytes32[]) { | |
require(index > 0x0); | |
return transformContents(contents[user], index); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment