creation_sourcecode: {{
    "language": "Solidity",
    "sources": {
      "contracts/DestraFileZ1Periphery.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\nimport \"@openzeppelin/contracts/access/Ownable.sol\";\nimport \"./IDestraCoreFileZ1.sol\";\n\ncontract DestraFileZ1Periphery is Ownable {\n  IDestraCoreFileZ1 public destraCoreFileZ1;\n\n  mapping(address => string[]) private userFileStorageHashes;\n\n  mapping(string => address) public hashOwnership;\n\n  event HashAssigned(address indexed user, string fileStorageHash);\n  event HashReleased(address indexed user, string fileStorageHash);\n  event HashUpdated(\n    address indexed user,\n    string oldFileStorageHash,\n    string newFileStorageHash\n  );\n  event HashesAssignedInBulk(address indexed user, string[] fileStorageHashes);\n  event HashesReleasedInBulk(address indexed user, string[] fileStorageHashes);\n  event HashesUpdatedInBulk(\n    address indexed user,\n    string[] oldFileStorageHashes,\n    string[] newFileStorageHashes\n  );\n  event DestraCoreFileZ1Set(address destraCoreFileZ1Address);\n\n  constructor(address _destraCoreFileZ1Address) Ownable(msg.sender) {\n    destraCoreFileZ1 = IDestraCoreFileZ1(_destraCoreFileZ1Address);\n  }\n\n  function assignHash(string memory fileStorageHash) public {\n    require(\n      hashOwnership[fileStorageHash] == address(0),\n      \"Hash already assigned\"\n    );\n    uint256 totalValidPlanSize = destraCoreFileZ1.getUserTotalValidPlanSize(\n      msg.sender\n    );\n    require(\n      userFileStorageHashes[msg.sender].length < totalValidPlanSize,\n      \"Storage limit reached\"\n    );\n\n    userFileStorageHashes[msg.sender].push(fileStorageHash);\n    hashOwnership[fileStorageHash] = msg.sender;\n\n    emit HashAssigned(msg.sender, fileStorageHash);\n  }\n\n  function releaseHash(string memory fileStorageHash) public {\n    require(\n      hashOwnership[fileStorageHash] == msg.sender,\n      \"Only the owner can release the hash\"\n    );\n\n    for (uint i = 0; i < userFileStorageHashes[msg.sender].length; i++) {\n      if (\n        keccak256(bytes(userFileStorageHashes[msg.sender][i])) ==\n        keccak256(bytes(fileStorageHash))\n      ) {\n        userFileStorageHashes[msg.sender][i] = userFileStorageHashes[\n          msg.sender\n        ][userFileStorageHashes[msg.sender].length - 1];\n        userFileStorageHashes[msg.sender].pop();\n        break;\n      }\n    }\n\n    delete hashOwnership[fileStorageHash];\n\n    emit HashReleased(msg.sender, fileStorageHash);\n  }\n\n  function updateHash(\n    string memory oldFileStorageHash,\n    string memory newFileStorageHash\n  ) public {\n    require(\n      hashOwnership[oldFileStorageHash] == msg.sender,\n      \"Only the owner can update the hash\"\n    );\n    releaseHash(oldFileStorageHash);\n    assignHash(newFileStorageHash);\n\n    emit HashUpdated(msg.sender, oldFileStorageHash, newFileStorageHash);\n  }\n\n  function assignHashesInBulk(string[] memory fileStorageHashes) public {\n    uint256 totalValidPlanSize = destraCoreFileZ1.getUserTotalValidPlanSize(\n      msg.sender\n    );\n    require(\n      userFileStorageHashes[msg.sender].length + fileStorageHashes.length <=\n        totalValidPlanSize,\n      \"Storage limit exceeded with bulk add\"\n    );\n\n    for (uint i = 0; i < fileStorageHashes.length; i++) {\n      string memory fileStorageHash = fileStorageHashes[i];\n      require(\n        hashOwnership[fileStorageHash] == address(0),\n        \"One or more hashes already assigned\"\n      );\n\n      userFileStorageHashes[msg.sender].push(fileStorageHash);\n      hashOwnership[fileStorageHash] = msg.sender;\n    }\n\n    emit HashesAssignedInBulk(msg.sender, fileStorageHashes);\n  }\n\n  function updateHashesInBulk(\n    string[] memory oldFileStorageHashes,\n    string[] memory newFileStorageHashes\n  ) public {\n    require(\n      oldFileStorageHashes.length == newFileStorageHashes.length,\n      \"Arrays must be of the same length\"\n    );\n\n    for (uint i = 0; i < oldFileStorageHashes.length; i++) {\n      require(\n        hashOwnership[oldFileStorageHashes[i]] == msg.sender,\n        \"Only the owner can update the hash\"\n      );\n      releaseHash(oldFileStorageHashes[i]);\n      assignHash(newFileStorageHashes[i]);\n    }\n\n    emit HashesUpdatedInBulk(\n      msg.sender,\n      oldFileStorageHashes,\n      newFileStorageHashes\n    );\n  }\n\n  function releaseHashesInBulk(string[] memory fileStorageHashes) public {\n    for (uint i = 0; i < fileStorageHashes.length; i++) {\n      string memory fileStorageHash = fileStorageHashes[i];\n      require(\n        hashOwnership[fileStorageHash] == msg.sender,\n        \"Only the owner can release the hash\"\n      );\n\n      for (uint j = 0; j < userFileStorageHashes[msg.sender].length; j++) {\n        if (\n          keccak256(bytes(userFileStorageHashes[msg.sender][j])) ==\n          keccak256(bytes(fileStorageHash))\n        ) {\n          userFileStorageHashes[msg.sender][j] = userFileStorageHashes[\n            msg.sender\n          ][userFileStorageHashes[msg.sender].length - 1];\n          userFileStorageHashes[msg.sender].pop();\n          delete hashOwnership[fileStorageHash];\n          break;\n        }\n      }\n    }\n\n    emit HashesReleasedInBulk(msg.sender, fileStorageHashes);\n  }\n\n  function getUserFileHashes(\n    address user\n  ) public view returns (string[] memory) {\n    return userFileStorageHashes[user];\n  }\n\n  function setDestraCoreFileZ1(\n    address _destraCoreFileZ1Address\n  ) external onlyOwner {\n    require(\n      _destraCoreFileZ1Address == address(0),\n      \"Invalid Core File Z1 address.\"\n    );\n\n    destraCoreFileZ1 = IDestraCoreFileZ1(_destraCoreFileZ1Address);\n    emit DestraCoreFileZ1Set(_destraCoreFileZ1Address);\n  }\n}\n"
      },
      "contracts/IDestraCoreFileZ1.sol": {
        "content": "// SPDX-License-Identifier: MIT\npragma solidity ^0.8.24;\n\ninterface IDestraCoreFileZ1 {\n  // Data structures\n  struct StoragePlan {\n    uint256 size;\n    uint256 expirationDate;\n  }\n\n  // Events\n  event StoragePlanPurchased(\n    address indexed user,\n    uint256 planSize,\n    uint256 _years,\n    uint256 expirationDate\n  );\n  event StoragePlanTransferred(\n    address indexed from,\n    address indexed to,\n    uint256 planIndex,\n    uint256 newOwnerPlanIndex\n  );\n  event StoragePlanRenewed(\n    address indexed user,\n    uint256 planIndex,\n    uint256 _years,\n    uint256 newExpirationDate\n  );\n  event TransferForeignToken(address token, uint256 amount);\n  event PricePerOneGBUpdate(uint256 newPrice);\n  event ProofOfSyncFactorySet(address indexed proofOfSyncFactory);\n  event SetMaxPlanSize(uint256 newLimit);\n  event Withdrawal(address indexed to, uint256 amount);\n\n  // Function signatures\n  function buyStoragePlan(uint256 planSize, uint256 _years) external payable;\n\n  function transferPlan(address to, uint256 planIndex) external;\n\n  function renewStoragePlan(uint256 planIndex, uint256 _years) external payable;\n\n  function setPricePerOneGB(uint256 newPrice) external;\n\n  function setMaxPlanSize(uint256 newSize) external;\n\n  function getUserTotalValidPlanSize(\n    address user\n  ) external view returns (uint256);\n\n  function getUserStoragePlans(\n    address user\n  ) external view returns (StoragePlan[] memory);\n\n  function withdrawBalance(address payable recipient, uint256 amount) external;\n\n  function setDestraFactory(address _proofOfSyncFactory) external;\n\n  function claimBalance() external;\n\n  function transferForeignToken(\n    address _token,\n    address _to\n  ) external returns (bool _sent);\n\n  function isPlanValid(uint256 planIndex) external view returns (bool isValid);\n}\n"
      },
      "@openzeppelin/contracts/access/Ownable.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.0) (access/Ownable.sol)\n\npragma solidity ^0.8.20;\n\nimport {Context} from \"../utils/Context.sol\";\n\n/**\n * @dev Contract module which provides a basic access control mechanism, where\n * there is an account (an owner) that can be granted exclusive access to\n * specific functions.\n *\n * The initial owner is set to the address provided by the deployer. This can\n * later be changed with {transferOwnership}.\n *\n * This module is used through inheritance. It will make available the modifier\n * `onlyOwner`, which can be applied to your functions to restrict their use to\n * the owner.\n */\nabstract contract Ownable is Context {\n    address private _owner;\n\n    /**\n     * @dev The caller account is not authorized to perform an operation.\n     */\n    error OwnableUnauthorizedAccount(address account);\n\n    /**\n     * @dev The owner is not a valid owner account. (eg. `address(0)`)\n     */\n    error OwnableInvalidOwner(address owner);\n\n    event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);\n\n    /**\n     * @dev Initializes the contract setting the address provided by the deployer as the initial owner.\n     */\n    constructor(address initialOwner) {\n        if (initialOwner == address(0)) {\n            revert OwnableInvalidOwner(address(0));\n        }\n        _transferOwnership(initialOwner);\n    }\n\n    /**\n     * @dev Throws if called by any account other than the owner.\n     */\n    modifier onlyOwner() {\n        _checkOwner();\n        _;\n    }\n\n    /**\n     * @dev Returns the address of the current owner.\n     */\n    function owner() public view virtual returns (address) {\n        return _owner;\n    }\n\n    /**\n     * @dev Throws if the sender is not the owner.\n     */\n    function _checkOwner() internal view virtual {\n        if (owner() != _msgSender()) {\n            revert OwnableUnauthorizedAccount(_msgSender());\n        }\n    }\n\n    /**\n     * @dev Leaves the contract without owner. It will not be possible to call\n     * `onlyOwner` functions. Can only be called by the current owner.\n     *\n     * NOTE: Renouncing ownership will leave the contract without an owner,\n     * thereby disabling any functionality that is only available to the owner.\n     */\n    function renounceOwnership() public virtual onlyOwner {\n        _transferOwnership(address(0));\n    }\n\n    /**\n     * @dev Transfers ownership of the contract to a new account (`newOwner`).\n     * Can only be called by the current owner.\n     */\n    function transferOwnership(address newOwner) public virtual onlyOwner {\n        if (newOwner == address(0)) {\n            revert OwnableInvalidOwner(address(0));\n        }\n        _transferOwnership(newOwner);\n    }\n\n    /**\n     * @dev Transfers ownership of the contract to a new account (`newOwner`).\n     * Internal function without access restriction.\n     */\n    function _transferOwnership(address newOwner) internal virtual {\n        address oldOwner = _owner;\n        _owner = newOwner;\n        emit OwnershipTransferred(oldOwner, newOwner);\n    }\n}\n"
      },
      "@openzeppelin/contracts/utils/Context.sol": {
        "content": "// SPDX-License-Identifier: MIT\n// OpenZeppelin Contracts (last updated v5.0.1) (utils/Context.sol)\n\npragma solidity ^0.8.20;\n\n/**\n * @dev Provides information about the current execution context, including the\n * sender of the transaction and its data. While these are generally available\n * via msg.sender and msg.data, they should not be accessed in such a direct\n * manner, since when dealing with meta-transactions the account sending and\n * paying for execution may not be the actual sender (as far as an application\n * is concerned).\n *\n * This contract is only required for intermediate, library-like contracts.\n */\nabstract contract Context {\n    function _msgSender() internal view virtual returns (address) {\n        return msg.sender;\n    }\n\n    function _msgData() internal view virtual returns (bytes calldata) {\n        return msg.data;\n    }\n\n    function _contextSuffixLength() internal view virtual returns (uint256) {\n        return 0;\n    }\n}\n"
      }
    },
    "settings": {
      "optimizer": {
        "enabled": true,
        "runs": 200
      },
      "outputSelection": {
        "*": {
          "*": [
            "evm.bytecode",
            "evm.deployedBytecode",
            "devdoc",
            "userdoc",
            "metadata",
            "abi"
          ]
        }
      }
    }
  }}