Skip to content
Verified contractExactMIT

MarkovsDreamFiles

Address0x374a8cfd2bfc53fa886145b323d6aa2a5a4576d2
Balance

0 ETH

 

Transactions

15

 

Compiler

solc 0.8.30

 

Same code

Only this contract

 

Details

Compiler
solc 0.8.30+commit.73712a01
Language
solidity
Optimization
Yes, 200 runs
EVM version
prague
License
MIT
Match
Exactcreation code: yes, runtime code: yes
Verified on Sourcify
Created in block
26,400,588
Creation transaction
0x9418472f96…5c075406db

src/MarkovsDreamFiles.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

import {IMarkovsDream} from "./IMarkovsDream.sol";

/// @title MarkovsDreamFiles — on-chain file store for Markov's Dream's artwork code
/// @author Harm van den Dorpel
/// @notice Holds the large files (the artwork's JavaScript, the page shell) separately from the metadata contract,
/// so a metadata contract can be replaced without uploading the code again: a new MarkovsDreamMetadata is deployed
/// with this store's address and adds its versions from the files already here.
/// A file (e.g. "/lib/pixi-6.4.2") is a list of chunk contracts: each chunk's code is 0x00 (STOP, so it can never be
/// called) followed by up to 24,575 bytes of the file. A sealed file never changes again — its chunk list is fixed
/// and a chunk's code cannot change — so metadata contracts only use sealed files.
/// Who may write: the owner of Markov's Dream (read from that contract) and the editor (the deploy key, set at
/// deployment). Writing only adds: files are appended to and sealed, never changed or removed. The owner can
/// replace or remove the editor; the editor can step down.
contract MarkovsDreamFiles {
    uint256 public constant MAX_CHUNK = 24_575;

    IMarkovsDream public immutable markovsDream;
    address public editor;

    mapping(bytes32 => address[]) internal _chunks; // keccak256(path) => chunk contracts, in order
    mapping(bytes32 => uint256) public fileSize; // keccak256(path) => total bytes
    mapping(bytes32 => bool) public isSealed; // keccak256(path) => no more appends

    event EditorSet(address indexed editor);
    event FileAppended(string path, uint256 chunks, uint256 size);
    event FileSealed(string path, uint256 size);

    /// @param markovsDream_ Markov's Dream. The deployer becomes the editor.
    constructor(IMarkovsDream markovsDream_) {
        markovsDream = markovsDream_;
        editor = msg.sender;
        emit EditorSet(msg.sender);
    }

    /// @notice The owner of Markov's Dream — the admin of this store.
    function owner() public view returns (address) {
        return markovsDream.owner();
    }

    modifier onlyOwnerOrEditor() {
        require(msg.sender == owner() || (msg.sender == editor && editor != address(0)), "not owner or editor");
        _;
    }

    /// @notice Replace the editor (address(0) = no editor). Owner only.
    function setEditor(address newEditor) external {
        require(msg.sender == owner(), "not owner");
        editor = newEditor;
        emit EditorSet(newEditor);
    }

    /// @notice The editor steps down.
    function renounceEditor() external {
        require(msg.sender == editor && editor != address(0), "not editor");
        editor = address(0);
        emit EditorSet(address(0));
    }

    /// @notice Append chunks (each 1..24,575 bytes) to the file at `path`.
    function append(string calldata path, bytes[] calldata parts) external onlyOwnerOrEditor {
        bytes32 k = keccak256(bytes(path));
        require(!isSealed[k], "sealed");
        for (uint256 i; i < parts.length; ++i) {
            bytes calldata d = parts[i];
            require(d.length != 0 && d.length <= MAX_CHUNK, "chunk size");
            // init code: PUSH2 len, DUP1, PUSH1 0x0a, RETURNDATASIZE, CODECOPY, RETURNDATASIZE, RETURN;
            // runtime code (copied from offset 10): 0x00 ++ data
            // forge-lint: disable-next-line(unsafe-typecast)
            bytes memory init = abi.encodePacked(hex"61", uint16(d.length + 1), hex"80600a3d393df300", d);
            address a;
            assembly ("memory-safe") {
                a := create(0, add(init, 0x20), mload(init))
            }
            require(a != address(0), "create failed");
            _chunks[k].push(a);
            fileSize[k] += d.length;
        }
        emit FileAppended(path, _chunks[k].length, fileSize[k]);
    }

    /// @notice Make the file at `path` permanent; only sealed files can be used in a version of the artwork.
    function seal(string calldata path) external onlyOwnerOrEditor {
        bytes32 k = keccak256(bytes(path));
        require(_chunks[k].length != 0, "no file");
        require(!isSealed[k], "sealed");
        isSealed[k] = true;
        emit FileSealed(path, fileSize[k]);
    }

    /// @notice The chunk contracts of the file at `path`, in order.
    function chunks(string calldata path) external view returns (address[] memory) {
        return _chunks[keccak256(bytes(path))];
    }

    /// @notice The whole file at `path`.
    function read(string calldata path) external view returns (bytes memory out) {
        bytes32 k = keccak256(bytes(path));
        address[] storage cs = _chunks[k];
        out = new bytes(fileSize[k]);
        uint256 dst;
        assembly ("memory-safe") {
            dst := add(out, 0x20)
        }
        for (uint256 i; i < cs.length; ++i) {
            address a = cs[i];
            assembly ("memory-safe") {
                let l := sub(extcodesize(a), 1)
                extcodecopy(a, dst, 1, l)
                dst := add(dst, l)
            }
        }
    }
}

src/IMarkovsDream.sol

// SPDX-License-Identifier: MIT
pragma solidity 0.8.30;

/// @notice The parts of Markov's Dream (ERC-721 0x518f0c4a832b998ee793d87f0e934467b8b6e587) the on-chain metadata uses.
interface IMarkovsDream {
    function owner() external view returns (address);
    function tokenURI(uint256 tokenId) external view returns (string memory);
}
Compiler settingsShow
{
  "viaIR": false,
  "metadata": {
    "appendCBOR": true,
    "bytecodeHash": "ipfs",
    "useLiteralContent": false
  },
  "libraries": {},
  "optimizer": {
    "runs": 200,
    "enabled": true
  },
  "evmVersion": "prague",
  "remappings": [
    "@openzeppelin/contracts/=lib/openzeppelin-contracts/",
    "forge-std/=lib/forge-std/src/",
    "openzeppelin-contracts/=lib/openzeppelin-contracts/"
  ]
}

Source code and metadata: Sourcify.

The source code belongs to the authors of the contract.