-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
160 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// SPDX-License-Identifier: MIT | ||
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol) | ||
// From commit https://github.com/OpenZeppelin/openzeppelin-contracts/commit/8b778fa20d6d76340c5fac1ed66c80273f05b95a | ||
// @dev modified version without storage that is intended to be anchored to a ACL | ||
pragma solidity ^0.8.0; | ||
|
||
import '../oz-common/Context.sol'; | ||
|
||
/** | ||
* @dev Contract module which provides a basic access control mechanism, where | ||
* there is an account (an owner) that can be granted exclusive access to | ||
* specific functions. | ||
* | ||
* By default, the owner account will be the one that deploys the contract. This | ||
* can later be changed with {transferOwnership}. | ||
* | ||
* This module is used through inheritance. It will make available the modifier | ||
* `onlyOwner`, which can be applied to your functions to restrict their use to | ||
* the owner. | ||
*/ | ||
abstract contract StatelessOwnable is Context { | ||
/** | ||
* @dev Throws if called by any account other than the owner. | ||
*/ | ||
modifier onlyOwner() { | ||
_checkOwner(); | ||
_; | ||
} | ||
|
||
/** | ||
* @dev Returns the address of the current owner. | ||
*/ | ||
function owner() public view virtual returns (address); | ||
|
||
/** | ||
* @dev Throws if the sender is not the owner. | ||
*/ | ||
function _checkOwner() internal view virtual { | ||
require(owner() == _msgSender(), 'Ownable: caller is not the owner'); | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/contracts/access-control/StatelessOwnableWithGuardian.sol
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.7.0; | ||
|
||
import {IStatelessOwnableWithGuardian} from './interfaces/IStatelessOwnableWithGuardian.sol'; | ||
import {StatelessOwnable} from './StatelessOwnable.sol'; | ||
|
||
abstract contract StatelessOwnableWithGuardian is StatelessOwnable, IStatelessOwnableWithGuardian { | ||
modifier onlyGuardian() { | ||
_checkGuardian(); | ||
_; | ||
} | ||
|
||
modifier onlyOwnerOrGuardian() { | ||
_checkOwnerOrGuardian(); | ||
_; | ||
} | ||
|
||
/// @inheritdoc IStatelessOwnableWithGuardian | ||
function guardian() public view virtual returns (address); | ||
|
||
function _checkGuardian() internal view { | ||
if (guardian() != _msgSender()) revert OnlyGuardianInvalidCaller(_msgSender()); | ||
} | ||
|
||
function _checkOwnerOrGuardian() internal view { | ||
if (_msgSender() != owner() && _msgSender() != guardian()) | ||
revert OnlyGuardianOrOwnerInvalidCaller(_msgSender()); | ||
} | ||
} |
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
19 changes: 19 additions & 0 deletions
19
src/contracts/access-control/interfaces/IStatelessOwnableWithGuardian.sol
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,19 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity >=0.7.0; | ||
|
||
interface IStatelessOwnableWithGuardian { | ||
/** | ||
* @dev The caller account is not authorized to perform an operation. | ||
*/ | ||
error OnlyGuardianInvalidCaller(address account); | ||
|
||
/** | ||
* @dev The caller account is not authorized to perform an operation. | ||
*/ | ||
error OnlyGuardianOrOwnerInvalidCaller(address account); | ||
|
||
/** | ||
* @dev get guardian address; | ||
*/ | ||
function guardian() external view returns (address); | ||
} |
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,64 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity ^0.8.20; | ||
|
||
import 'forge-std/Test.sol'; | ||
import {IStatelessOwnableWithGuardian} from '../src/contracts/access-control/interfaces/IStatelessOwnableWithGuardian.sol'; | ||
import {StatelessOwnableWithGuardian} from '../src/contracts/access-control/StatelessOwnableWithGuardian.sol'; | ||
|
||
contract ImplStatlessOwnableWithGuardian is StatelessOwnableWithGuardian { | ||
address private _owner; | ||
address private _guardian; | ||
|
||
constructor(address owner, address guardian) { | ||
_owner = owner; | ||
_guardian = guardian; | ||
} | ||
|
||
function owner() public view override returns (address) { | ||
return _owner; | ||
} | ||
|
||
function guardian() public view override returns (address) { | ||
return _guardian; | ||
} | ||
|
||
function mock_onlyGuardian() external onlyGuardian {} | ||
|
||
function mock_onlyOwnerOrGuardian() external onlyOwnerOrGuardian {} | ||
} | ||
|
||
contract TestOfUpgradableOwnableWithGuardian is Test { | ||
ImplStatlessOwnableWithGuardian public withGuardian; | ||
|
||
address owner = address(0x4); | ||
address guardian = address(0x8); | ||
|
||
function setUp() public { | ||
withGuardian = new ImplStatlessOwnableWithGuardian(owner, guardian); | ||
} | ||
|
||
function test_getters() external { | ||
assertEq(withGuardian.owner(), owner); | ||
assertEq(withGuardian.guardian(), guardian); | ||
} | ||
|
||
function test_onlyGuardian() external { | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
IStatelessOwnableWithGuardian.OnlyGuardianInvalidCaller.selector, | ||
address(this) | ||
) | ||
); | ||
withGuardian.mock_onlyGuardian(); | ||
} | ||
|
||
function test_onlyOwnerOrGuardian() external { | ||
vm.expectRevert( | ||
abi.encodeWithSelector( | ||
IStatelessOwnableWithGuardian.OnlyGuardianOrOwnerInvalidCaller.selector, | ||
address(this) | ||
) | ||
); | ||
withGuardian.mock_onlyOwnerOrGuardian(); | ||
} | ||
} |
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