all files / contracts/ Manifestations.sol

100% Statements 21/21
100% Branches 10/10
100% Functions 9/9
100% Lines 22/22
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111                                                        5×                 33× 31×     23× 19× 19× 19×                   18× 18× 18×                           3× 1× 1× 1× 3× 1×               15×                 10× 8×           40×       1×       1×      
// SPDX-License-Identifier: GPL-3.0-or-later
 
pragma solidity ^0.8.6;
 
import "openzeppelin-solidity/contracts/access/Ownable.sol";
import "openzeppelin-solidity/contracts/security/Pausable.sol";
import "./ExpirableLib.sol";
import "./Evidencable.sol";
import "./Stakable.sol";
 
/// @title Contract for copyright authorship registration through creations manifestations
/// @author Roberto García (http://rhizomik.net/~roberto/)
contract Manifestations is Ownable, Pausable, Evidencable, Stakable {
 
    using ExpirableLib for ExpirableLib.TimeAndExpiry;
 
    struct Manifestation {
        string title;
        address[] authors;
        ExpirableLib.TimeAndExpiry time;
    }
 
    uint32 public timeToExpiry;
    mapping(string => Manifestation) private manifestations;
 
    event ManifestEvent(string hash, string title, address indexed manifester);
 
    constructor(uint32 _timeToExpiry) {
        timeToExpiry = _timeToExpiry;
    }
 
    /// @dev Modifier implementing the common logic for single and joint authorship.
    /// Checks title and that hash not registered or expired. Then stores title and sets expiry.
    /// Finally, emits ManifestEvent
    /// @param hash Hash of the manifestation content, for instance IPFS Base58 Hash
    /// @param title The title of the manifestation
    modifier registerIfAvailable(string memory hash, string memory title) {
        require(bytes(title).length > 0, "A title is required");
        require(manifestations[hash].authors.length == 0 ||
                (manifestations[hash].time.isExpired() && !isStaked(hash)),
            "Already registered and not expired or with stake");
        _;
        manifestations[hash].title = title;
        manifestations[hash].time.setExpiry(timeToExpiry);
        emit ManifestEvent(hash, title, msg.sender);
    }
 
    /// @notice Register single authorship for `msg.sender` of the manifestation with title `title`
    /// and hash `hash`. Requires hash not previously registered or expired.
    /// @dev To be used when there is just one author, which is considered to be the message sender
    /// @param hash Hash of the manifestation content, for instance IPFS Base58 Hash
    /// @param title The title of the manifestation
    function manifestAuthorship(string memory hash, string memory title)
    public registerIfAvailable(hash, title) whenNotPaused() {
        address[] memory authors = new address[](1);
        authors[0] = msg.sender;
        manifestations[hash].authors = authors;
    }
 
    /// @notice Register joint authorship for `msg.sender` plus additional authors
    /// `additionalAuthors` of the manifestation with title `title` and hash `hash`.
    /// Requires hash not previously registered or expired and at most 64 authors,
    /// including the one registering.
    /// @dev To be used when there are multiple authors
    /// @param hash Hash of the manifestation content, for instance IPFS Base58 Hash
    /// @param title The title of the manifestation
    /// @param additionalAuthors The additional authors,
    /// including the one registering that becomes the first author
    function manifestJointAuthorship(string memory hash, string memory title, address[] memory additionalAuthors)
    public registerIfAvailable(hash, title) whenNotPaused() {
        require(additionalAuthors.length < 64, "Joint authorship limited to 64 authors");
        address[] memory authors = new address[](additionalAuthors.length + 1);
        authors[0] = msg.sender;
        for (uint8 i = 0; i < additionalAuthors.length; i++)
            authors[i+1] = additionalAuthors[i];
        manifestations[hash].authors = authors;
    }
 
    /// @notice Retrieve the title and authors of the manifestation with content hash `hash`.
    /// @param hash Hash of the manifestation content, for instance IPFS Base58 Hash
    /// @return The title and authors of the manifestation
    function getManifestation(string memory hash) public view
    returns (string memory, address[] memory, uint256, uint256) {
        return (manifestations[hash].title,
                manifestations[hash].authors,
                manifestations[hash].time.creationTime,
                manifestations[hash].time.expiryTime);
    }
 
    /// @notice Adds an evidence if there is already a manifestation for `hash`.
    /// @param hash Hash of the manifestation content, for instance IPFS Base58 Hash
    function addEvidence(string memory hash) public override {
        require(bytes(manifestations[hash].title).length > 0, "The manifestation evidenced should exist");
        super.addEvidence(hash);
    }
 
    /// @notice Checks if a manifestation for `hash` exists and thus can get stake.
    /// @param hash Hash of the manifestation content, for instance IPFS Base58 Hash
    function isStakable(string memory hash) public view override {
        require(bytes(manifestations[hash].title).length > 0, "The manifestation should exist to accept stake");
    }
 
    function pause() public onlyOwner {
        super._pause();
    }
 
    function unpause() public onlyOwner {
        super._unpause();
    }
}