|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | +pragma solidity ^0.8.0; |
| 3 | + |
| 4 | +/// @author thirdweb |
| 5 | + |
| 6 | +import "../../extension/interface/IDelayedReveal.sol"; |
| 7 | + |
| 8 | +library DelayedRevealStorage { |
| 9 | + bytes32 public constant DELAYED_REVEAL_STORAGE_POSITION = keccak256("delayed.reveal.storage"); |
| 10 | + |
| 11 | + struct Data { |
| 12 | + /// @dev Mapping from tokenId of a batch of tokens => to delayed reveal data. |
| 13 | + mapping(uint256 => bytes) encryptedData; |
| 14 | + } |
| 15 | + |
| 16 | + function delayedRevealStorage() internal pure returns (Data storage delayedRevealData) { |
| 17 | + bytes32 position = DELAYED_REVEAL_STORAGE_POSITION; |
| 18 | + assembly { |
| 19 | + delayedRevealData.slot := position |
| 20 | + } |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +/** |
| 25 | + * @title Delayed Reveal |
| 26 | + * @notice Thirdweb's `DelayedReveal` is a contract extension for base NFT contracts. It lets you create batches of |
| 27 | + * 'delayed-reveal' NFTs. You can learn more about the usage of delayed reveal NFTs here - https://blog.thirdweb.com/delayed-reveal-nfts |
| 28 | + */ |
| 29 | + |
| 30 | +abstract contract DelayedReveal is IDelayedReveal { |
| 31 | + /// @dev Mapping from tokenId of a batch of tokens => to delayed reveal data. |
| 32 | + function encryptedData(uint256 _tokenId) public view returns (bytes memory) { |
| 33 | + DelayedRevealStorage.Data storage data = DelayedRevealStorage.delayedRevealStorage(); |
| 34 | + return data.encryptedData[_tokenId]; |
| 35 | + } |
| 36 | + |
| 37 | + /// @dev Sets the delayed reveal data for a batchId. |
| 38 | + function _setEncryptedData(uint256 _batchId, bytes memory _encryptedData) internal { |
| 39 | + DelayedRevealStorage.Data storage data = DelayedRevealStorage.delayedRevealStorage(); |
| 40 | + data.encryptedData[_batchId] = _encryptedData; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @notice Returns revealed URI for a batch of NFTs. |
| 45 | + * @dev Reveal encrypted base URI for `_batchId` with caller/admin's `_key` used for encryption. |
| 46 | + * Reverts if there's no encrypted URI for `_batchId`. |
| 47 | + * See {encryptDecrypt}. |
| 48 | + * |
| 49 | + * @param _batchId ID of the batch for which URI is being revealed. |
| 50 | + * @param _key Secure key used by caller/admin for encryption of baseURI. |
| 51 | + * |
| 52 | + * @return revealedURI Decrypted base URI. |
| 53 | + */ |
| 54 | + function getRevealURI(uint256 _batchId, bytes calldata _key) public view returns (string memory revealedURI) { |
| 55 | + DelayedRevealStorage.Data storage data = DelayedRevealStorage.delayedRevealStorage(); |
| 56 | + |
| 57 | + bytes memory dataForBatch = data.encryptedData[_batchId]; |
| 58 | + if (dataForBatch.length == 0) { |
| 59 | + revert("Nothing to reveal"); |
| 60 | + } |
| 61 | + |
| 62 | + (bytes memory encryptedURI, bytes32 provenanceHash) = abi.decode(dataForBatch, (bytes, bytes32)); |
| 63 | + |
| 64 | + revealedURI = string(encryptDecrypt(encryptedURI, _key)); |
| 65 | + |
| 66 | + require(keccak256(abi.encodePacked(revealedURI, _key, block.chainid)) == provenanceHash, "Incorrect key"); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * @notice Encrypt/decrypt data on chain. |
| 71 | + * @dev Encrypt/decrypt given `data` with `key`. Uses inline assembly. |
| 72 | + * See: https://ethereum.stackexchange.com/questions/69825/decrypt-message-on-chain |
| 73 | + * |
| 74 | + * @param data Bytes of data to encrypt/decrypt. |
| 75 | + * @param key Secure key used by caller for encryption/decryption. |
| 76 | + * |
| 77 | + * @return result Output after encryption/decryption of given data. |
| 78 | + */ |
| 79 | + function encryptDecrypt(bytes memory data, bytes calldata key) public pure override returns (bytes memory result) { |
| 80 | + // Store data length on stack for later use |
| 81 | + uint256 length = data.length; |
| 82 | + |
| 83 | + // solhint-disable-next-line no-inline-assembly |
| 84 | + assembly { |
| 85 | + // Set result to free memory pointer |
| 86 | + result := mload(0x40) |
| 87 | + // Increase free memory pointer by lenght + 32 |
| 88 | + mstore(0x40, add(add(result, length), 32)) |
| 89 | + // Set result length |
| 90 | + mstore(result, length) |
| 91 | + } |
| 92 | + |
| 93 | + // Iterate over the data stepping by 32 bytes |
| 94 | + for (uint256 i = 0; i < length; i += 32) { |
| 95 | + // Generate hash of the key and offset |
| 96 | + bytes32 hash = keccak256(abi.encodePacked(key, i)); |
| 97 | + |
| 98 | + bytes32 chunk; |
| 99 | + // solhint-disable-next-line no-inline-assembly |
| 100 | + assembly { |
| 101 | + // Read 32-bytes data chunk |
| 102 | + chunk := mload(add(data, add(i, 32))) |
| 103 | + } |
| 104 | + // XOR the chunk with hash |
| 105 | + chunk ^= hash; |
| 106 | + // solhint-disable-next-line no-inline-assembly |
| 107 | + assembly { |
| 108 | + // Write 32-byte encrypted chunk |
| 109 | + mstore(add(result, add(i, 32)), chunk) |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * @notice Returns whether the relvant batch of NFTs is subject to a delayed reveal. |
| 116 | + * @dev Returns `true` if `_batchId`'s base URI is encrypted. |
| 117 | + * @param _batchId ID of a batch of NFTs. |
| 118 | + */ |
| 119 | + function isEncryptedBatch(uint256 _batchId) public view returns (bool) { |
| 120 | + DelayedRevealStorage.Data storage data = DelayedRevealStorage.delayedRevealStorage(); |
| 121 | + return data.encryptedData[_batchId].length > 0; |
| 122 | + } |
| 123 | +} |
0 commit comments