File size: 8,150 Bytes
7fc80d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// SPDX-License-Identifier: MIT
pragma solidity 0.8.17;

import {ERC721Upgradeable} from "@openzeppelin/contracts-upgradeable/token/ERC721/ERC721Upgradeable.sol";
import {AccessControlUpgradeable} from "@openzeppelin/contracts-upgradeable/access/AccessControlUpgradeable.sol";
import {CountersUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/CountersUpgradeable.sol";
import {UUPSUpgradeable} from "@openzeppelin/contracts-upgradeable/proxy/utils/UUPSUpgradeable.sol";
import {Initializable} from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import {ECDSAUpgradeable} from "@openzeppelin/contracts-upgradeable/utils/cryptography/ECDSAUpgradeable.sol";
import {IERC5192} from "./IERC5192.sol";
import {IInvictusOrderApplication} from "./IInvictusOrderApplication.sol";

/// @title Invictus Order Soulbound Tokens
/// @author Kfish n Chips
/// @custom:security-contact security@kfishnchips.com
contract InvictusOrderApplication is
    Initializable,
    UUPSUpgradeable,
    ERC721Upgradeable,
    AccessControlUpgradeable,
    IERC5192,
    IInvictusOrderApplication
{
    using CountersUpgradeable for CountersUpgradeable.Counter;

    /// @notice Minter Role
    /// @dev Will be granted to the Orchestrator
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");
    /// @notice  Upgrader Role
    bytes32 public constant UPGRADER_ROLE = keccak256("UPGRADER_ROLE");
    /// @notice Token ID counter
    CountersUpgradeable.Counter private _tokenIdCounter;
    /// @notice base URI used to retrieve metadata
    /// @dev tokenURI will use .json at the end for each token
    string public baseURI;
    /// @notice Enable/disable tokens transfer
    bool private _tokenLocked;
    /// @notice Backend signer address
    address internal signer;
    /// @notice Contract owner
    address public owner;
    /// @notice Keep track of minters
    mapping(bytes32 => bool) private hashes;

    /// @notice Emitted when the token transfer status change.
    event ToggleTokenLocked(address sender, bool state);
    /// @notice Emitted when the baseURI change.
    event BaseURIChanged(
        address indexed sender,
        string previousURI,
        string newURI
    );
     /// @notice Emitted when signer change.
    event SignerChanged(
        address indexed sender,
        address previousSigner,
        address newSigner
    );

    /// @notice enable/disable Token Transfer
    /// Emits an {ToggleTokenLocked} event.
    function toggleTokenLocked() external onlyRole(DEFAULT_ADMIN_ROLE) {
        _tokenLocked = !_tokenLocked;
        emit ToggleTokenLocked(msg.sender, _tokenLocked);
    }

    /// @notice Used to set the signer
    /// @param _signer the new signer
    /// Emits an {SignerChanged} event.
    function setSigner(address _signer)
        external
        onlyRole(DEFAULT_ADMIN_ROLE)
    {
        if (_signer == address(0)) revert AddressZero();
        emit SignerChanged(msg.sender, signer, _signer);
        signer = _signer;
    }

    /// @notice Used to set the baseURI for metadata
    /// @param newBaseURI the base URI
    /// Emits an {BaseURIChanged} event.
    function setBaseURI(string memory newBaseURI)
        external
        onlyRole(DEFAULT_ADMIN_ROLE)
    {
        if (bytes(newBaseURI).length == 0) revert InvalidBaseURI();
        emit BaseURIChanged(msg.sender, baseURI, newBaseURI);
        baseURI = newBaseURI;
    }

    /// @notice Safely mints and transfers it to `msg.sender`.
    /// @param messageHash Hashed application
    /// @param _v Recovery value + 27
    /// @param _r First 32 bytes of the signature
    /// @param _s Next 32 bytes of the signature
    /// Emits a {Transfer} event.
    function mint(bytes32 messageHash, uint8 _v, bytes32 _r, bytes32 _s) external {
        if(hashes[messageHash]) revert AlreadyMinted();
        address _signer = ECDSAUpgradeable.recover(messageHash, _v, _r, _s);

        if(signer != _signer) revert InvalidSender();
        hashes[messageHash] = true;
        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(msg.sender, tokenId);

        if (_tokenLocked) emit Locked(tokenId);
        else emit Unlocked(tokenId);
    }

    /// @notice Mint to a specific address
    /// @dev Requires MINTER_ROLE
    /// @param to the receiver address
    function mintTo(address to) external onlyRole(MINTER_ROLE) {
        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
        if (_tokenLocked) emit Locked(tokenId);
        else emit Unlocked(tokenId);
    }

    /// @inheritdoc	IERC5192
    function locked(uint256 tokenId) external view returns (bool) {
        if (!_exists(tokenId)) revert InvalidTokenID();
        return _tokenLocked;
    }

    /// @notice Total supply minted
    function totalSupply() external view returns (uint256) {
        return _tokenIdCounter.current() - 1;
    }

    function initialize(address _signer, string memory _newBaseURI) public initializer {
        __AccessControl_init_unchained();
        __ERC721_init_unchained("Invictus Order Application", "IOAPP");
        _grantRole(DEFAULT_ADMIN_ROLE, msg.sender);
        _grantRole(MINTER_ROLE, msg.sender);
        _grantRole(UPGRADER_ROLE, msg.sender);
        baseURI = _newBaseURI;
        signer = _signer;
        _tokenLocked = true;
        _tokenIdCounter.increment();
        transferOwnership(msg.sender);
    }

    /// @notice Approve operator for a token
    /// @dev overriden to avoid approvals for SBT
    function approve(address to, uint256 tokenId) public virtual override {
        if (_tokenLocked) revert ApprovalNotAllowed();
        super.approve(to, tokenId);
    }

    /// @notice Ownership Transfer
    /// @dev owner is only used because some marketplaces require it
    /// Emits an {OwnershipTransferred} event.
    function transferOwnership(address _owner) public onlyRole(DEFAULT_ADMIN_ROLE) {
        if (_owner == address(0) || _owner == address(this)) revert InvalidAddress();
        address previousOwner = owner;
        owner = _owner;

        emit OwnershipTransferred(previousOwner, _owner);
    }

    /// @notice Approve operator for an address
    /// @dev overriden to avoid approvals for SBT
    function setApprovalForAll(address operator, bool approved)
        public
        virtual
        override
    {
        if (_tokenLocked) revert ApprovalNotAllowed();
        super.setApprovalForAll(operator, approved);
    }

    /// @notice Override of ERC721, AccessControl supportsInterface function
    /// @param _interfaceId the interfaceId
    /// @return bool if interfaceId is supported or not
    function supportsInterface(bytes4 _interfaceId)
        public
        view
        override(ERC721Upgradeable, AccessControlUpgradeable)
        returns (bool)
    {
        bytes4 interfaceIDIERC5192 = 0xb45a3c0e;
        return
            _interfaceId == interfaceIDIERC5192 ||
            super.supportsInterface(_interfaceId);
    }

    /// @notice Hook to check whether a key is transferrable
    /// @dev admins can always transfer regardless of whether keys are flagged
    /// @param _from address that holds the tokenId
    /// @param _to address that will receive the tokenId
    /// @param _startTokenId index of first tokenId that will be transferred
    /// @param _quantity amount that will be transferred
    function _beforeTokenTransfer(
        address _from,
        address _to,
        uint256 _startTokenId,
        uint256 _quantity
    ) internal override {
        if (_tokenLocked && _from != address(0))
            revert TransferNotAllowed();
        super._beforeTokenTransfer(_from, _to, _startTokenId, _quantity);
    }

    /// @inheritdoc	ERC721Upgradeable
    function _baseURI() internal view override returns (string memory) {
        return baseURI;
    }

    /// @inheritdoc UUPSUpgradeable
    function _authorizeUpgrade(address newImplementation) // solhint-disable-line
        internal
        virtual
        override
        onlyRole(UPGRADER_ROLE)
    {} // solhint-disable-line
}