From 600df9822cbd3cf434f3a945740e08ff3c2e87c1 Mon Sep 17 00:00:00 2001 From: Yash <72552910+kumaryash90@users.noreply.github.com> Date: Thu, 12 Oct 2023 17:40:06 +0530 Subject: [PATCH] core router (#490) * special cases * v3.9.3-0 * core router * fix * v3.9.3-1 * update dynamic-contracts * v3.10.1-0 * special case * v3.10.2-1 --- contracts/package.json | 2 +- .../unaudited/contract-builder/CoreRouter.sol | 60 +++++++++++++++++++ .../extension/PermissionOverride.sol | 52 ++++++++++++++++ scripts/package-release.ts | 7 ++- 4 files changed, 119 insertions(+), 2 deletions(-) create mode 100644 contracts/prebuilts/unaudited/contract-builder/CoreRouter.sol create mode 100644 contracts/prebuilts/unaudited/contract-builder/extension/PermissionOverride.sol diff --git a/contracts/package.json b/contracts/package.json index 644c6304f..ee393638e 100644 --- a/contracts/package.json +++ b/contracts/package.json @@ -1,7 +1,7 @@ { "name": "@thirdweb-dev/contracts", "description": "Collection of smart contracts deployable via the thirdweb SDK, dashboard and CLI", - "version": "3.10.1", + "version": "3.10.2-1", "license": "Apache-2.0", "repository": { "type": "git", diff --git a/contracts/prebuilts/unaudited/contract-builder/CoreRouter.sol b/contracts/prebuilts/unaudited/contract-builder/CoreRouter.sol new file mode 100644 index 000000000..d504362f2 --- /dev/null +++ b/contracts/prebuilts/unaudited/contract-builder/CoreRouter.sol @@ -0,0 +1,60 @@ +// SPDX-License-Identifier: MIT +// @author: thirdweb (https://github.com/thirdweb-dev/dynamic-contracts) + +pragma solidity ^0.8.0; + +// Interface +import "lib/dynamic-contracts/src/presets/BaseRouter.sol"; + +// Core +import "lib/dynamic-contracts/src/core/Router.sol"; + +// Utils +import "lib/dynamic-contracts/src/lib/StringSet.sol"; +import "./extension/PermissionOverride.sol"; + +// Fixed extensions +import "../../../extension/Ownable.sol"; +import "../../../extension/ContractMetadata.sol"; + +/** + * //////////// + * + * NOTE: This contract is a work in progress, and has not been audited. + * + * //////////// + */ + +contract CoreRouter is BaseRouter, ContractMetadata, Ownable { + using StringSet for StringSet.Set; + + /*/////////////////////////////////////////////////////////////// + Constructor + //////////////////////////////////////////////////////////////*/ + + constructor(address _owner, Extension[] memory _extensions) BaseRouter(_extensions) { + // Initialize extensions + __BaseRouter_init(); + + _setupOwner(_owner); + } + + /*/////////////////////////////////////////////////////////////// + Internal functions + //////////////////////////////////////////////////////////////*/ + + /// @dev Returns whether all relevant permission and other checks are met before any upgrade. + function _isAuthorizedCallToUpgrade() internal view virtual override returns (bool) { + return msg.sender == owner(); + } + + /// @dev Returns whether contract metadata can be set in the given execution context. + function _canSetContractURI() internal view virtual override returns (bool) { + return msg.sender == owner(); + } + + /// @dev Returns whether owner can be set in the given execution context. + function _canSetOwner() internal view virtual override returns (bool) { + return msg.sender == owner(); + } +} diff --git a/contracts/prebuilts/unaudited/contract-builder/extension/PermissionOverride.sol b/contracts/prebuilts/unaudited/contract-builder/extension/PermissionOverride.sol new file mode 100644 index 000000000..ba5ba5dcf --- /dev/null +++ b/contracts/prebuilts/unaudited/contract-builder/extension/PermissionOverride.sol @@ -0,0 +1,52 @@ +// SPDX-License-Identifier: Apache-2.0 +pragma solidity ^0.8.0; + +/// @author thirdweb + +/** + * //////////// + * + * NOTE: This contract is a work in progress, and has not been audited. + * + * //////////// + */ + +library PermissionsStorage { + bytes32 public constant PERMISSIONS_STORAGE_POSITION = keccak256("permissions.storage"); + + struct Data { + /// @dev Map from keccak256 hash of a role => a map from address => whether address has role. + mapping(bytes32 => mapping(address => bool)) _hasRole; + /// @dev Map from keccak256 hash of a role to role admin. See {getRoleAdmin}. + mapping(bytes32 => bytes32) _getRoleAdmin; + } + + function permissionsStorage() internal pure returns (Data storage permissionsData) { + bytes32 position = PERMISSIONS_STORAGE_POSITION; + assembly { + permissionsData.slot := position + } + } +} + +contract PermissionOverrideCoreRouter { + bytes32 private constant DEFAULT_ADMIN_ROLE = 0x00; + bytes32 private constant EXTENSION_ROLE = keccak256("EXTENSION_ROLE"); + + function canSetContractURI(address _caller) public view returns (bool) { + return _hasRole(DEFAULT_ADMIN_ROLE, _caller); + } + + function canSetOwner(address _caller) public view returns (bool) { + return _hasRole(DEFAULT_ADMIN_ROLE, _caller); + } + + function canSetExtension(address _caller) public view returns (bool) { + return _hasRole(DEFAULT_ADMIN_ROLE, _caller); + } + + function _hasRole(bytes32 role, address account) internal view returns (bool) { + PermissionsStorage.Data storage data = PermissionsStorage.permissionsStorage(); + return data._hasRole[role][account]; + } +} diff --git a/scripts/package-release.ts b/scripts/package-release.ts index 56137e90d..8babd386d 100644 --- a/scripts/package-release.ts +++ b/scripts/package-release.ts @@ -6,7 +6,12 @@ const artifactsForgeDir = path.join(__dirname, "..", "artifacts_forge"); const contractsDir = path.join(__dirname, "..", "contracts"); const contractArtifactsDir = path.join(__dirname, "..", "contract_artifacts"); -const specialCases: string[] = ["IBaseRouter.sol", "MockContractPublisher.sol"]; +const specialCases: string[] = [ + "IRouterState.sol", + "BaseRouter.sol", + "ExtensionManager.sol", + "MockContractPublisher.sol", +]; async function getAllSolidityFiles(dir: string): Promise { const dirents = await fs.readdir(dir, { withFileTypes: true });