-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add
TrustedCodehashAccess
contract and interface
This enables whitelisting in the staking manager contract using the `TrustedCodehashAccess` interface. These changes were largely taken from #39 with few changes: - Due to different OZ versions, `Ownable()` constructor needs to be called with owner Closes #15
- Loading branch information
Showing
7 changed files
with
136 additions
and
50 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.26; | ||
|
||
import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol"; | ||
import { ITrustedCodehashAccess } from "./interfaces/ITrustedCodehashAccess.sol"; | ||
/** | ||
* @title TrustedCodehashAccess | ||
* @author Ricardo Guilherme Schmidt <ricardo3@status.im> | ||
* @notice Ensures that only specific contract bytecode hashes are trusted to | ||
* interact with the functions using the `onlyTrustedCodehash` modifier. | ||
*/ | ||
|
||
contract TrustedCodehashAccess is ITrustedCodehashAccess, Ownable { | ||
mapping(bytes32 codehash => bool permission) private trustedCodehashes; | ||
|
||
/** | ||
* @notice Restricts access based on the codehash of the caller. | ||
* Only contracts with trusted codehashes can execute functions using this modifier. | ||
*/ | ||
modifier onlyTrustedCodehash() { | ||
bytes32 codehash = msg.sender.codehash; | ||
if (!trustedCodehashes[codehash]) { | ||
revert TrustedCodehashAccess__UnauthorizedCodehash(); | ||
} | ||
_; | ||
} | ||
|
||
constructor(address _owner) Ownable(_owner) { } | ||
|
||
/** | ||
* @notice Allows the owner to set or update the trust status for a contract's codehash. | ||
* @dev Emits the `TrustedCodehashUpdated` event whenever a codehash is updated. | ||
* @param _codehash The bytecode hash of the contract. | ||
* @param _trusted Boolean flag to designate the contract as trusted or not. | ||
*/ | ||
function setTrustedCodehash(bytes32 _codehash, bool _trusted) external onlyOwner { | ||
trustedCodehashes[_codehash] = _trusted; | ||
emit TrustedCodehashUpdated(_codehash, _trusted); | ||
} | ||
|
||
/** | ||
* @notice Checks if a contract's codehash is trusted to interact with protected functions. | ||
* @param _codehash The bytecode hash of the contract. | ||
* @return bool True if the codehash is trusted, false otherwise. | ||
*/ | ||
function isTrustedCodehash(bytes32 _codehash) external view returns (bool) { | ||
return trustedCodehashes[_codehash]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.26; | ||
|
||
/** | ||
* @title TrustedCodehashAccess | ||
* @author Ricardo Guilherme Schmidt <ricardo3@status.im> | ||
* @notice Ensures that only specific contract bytecode hashes are trusted to | ||
* interact with the functions using the `onlyTrustedCodehash` modifier. | ||
*/ | ||
interface ITrustedCodehashAccess { | ||
error TrustedCodehashAccess__UnauthorizedCodehash(); | ||
|
||
event TrustedCodehashUpdated(bytes32 indexed codehash, bool trusted); | ||
|
||
/** | ||
* @notice Allows the owner to set or update the trust status for a contract's codehash. | ||
* @dev Emits the `TrustedCodehashUpdated` event whenever a codehash is updated. | ||
* @param _codehash The bytecode hash of the contract. | ||
* @param _trusted Boolean flag to designate the contract as trusted or not. | ||
*/ | ||
function setTrustedCodehash(bytes32 _codehash, bool _trusted) external; | ||
|
||
/** | ||
* @notice Checks if a contract's codehash is trusted to interact with protected functions. | ||
* @param _codehash The bytecode hash of the contract. | ||
* @return bool True if the codehash is trusted, false otherwise. | ||
*/ | ||
function isTrustedCodehash(bytes32 _codehash) external view returns (bool); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters