Skip to content

Commit 53ce02c

Browse files
committed
refactor(contracts): apply new naming conventions to contracts
1 parent c1ec974 commit 53ce02c

18 files changed

+293
-285
lines changed

packages/contracts/contracts/src/core/checker/AdvancedChecker.sol renamed to packages/contracts/contracts/src/core/AdvancedChecker.sol

Lines changed: 25 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// SPDX-License-Identifier: MIT
22
pragma solidity 0.8.27;
33

4-
import {IAdvancedChecker, Check} from "./IAdvancedChecker.sol";
4+
import {IAdvancedChecker, Check} from "./interfaces/IAdvancedChecker.sol";
55

66
struct CheckStatus {
77
bool pre;
@@ -34,42 +34,46 @@ abstract contract AdvancedChecker is IAdvancedChecker {
3434
allowMultipleMain = _allowMultipleMain;
3535
}
3636

37-
/// @notice Public method to check the validity of the provided data for a given address and check type.
38-
/// @param passerby The address to be checked.
39-
/// @param data The data associated with the check.
37+
/// @notice Public method to check the validity of the provided evidence for a given address and check type.
38+
/// @param subject The address to be checked.
39+
/// @param evidence The evidence associated with the check.
4040
/// @param checkType The type of check to perform (PRE, MAIN, POST).
41-
function check(address passerby, bytes memory data, Check checkType) external view override returns (bool checked) {
42-
return _check(passerby, data, checkType);
41+
function check(
42+
address subject,
43+
bytes memory evidence,
44+
Check checkType
45+
) external view override returns (bool checked) {
46+
return _check(subject, evidence, checkType);
4347
}
4448

4549
/// @notice Internal method to orchestrate the validation process based on the specified check type.
46-
/// @param passerby The address to be checked.
47-
/// @param data The data associated with the check.
50+
/// @param subject The address to be checked.
51+
/// @param evidence The evidence associated with the check.
4852
/// @param checkType The type of check to perform (PRE, MAIN, POST).
49-
function _check(address passerby, bytes memory data, Check checkType) internal view returns (bool checked) {
53+
function _check(address subject, bytes memory evidence, Check checkType) internal view returns (bool checked) {
5054
if (!skipPre && checkType == Check.PRE) {
51-
return _checkPre(passerby, data);
55+
return _checkPre(subject, evidence);
5256
} else if (!skipPost && checkType == Check.POST) {
53-
return _checkPost(passerby, data);
57+
return _checkPost(subject, evidence);
5458
} else if (checkType == Check.MAIN) {
55-
return _checkMain(passerby, data);
59+
return _checkMain(subject, evidence);
5660
}
5761

5862
return false;
5963
}
6064

6165
/// @notice Internal method for performing pre-condition checks.
62-
/// @param passerby The address to be checked.
63-
/// @param data The data associated with the check.
64-
function _checkPre(address passerby, bytes memory data) internal view virtual returns (bool checked) {}
66+
/// @param subject The address to be checked.
67+
/// @param evidence The evidence associated with the check.
68+
function _checkPre(address subject, bytes memory evidence) internal view virtual returns (bool checked) {}
6569

6670
/// @notice Internal method for performing main checks.
67-
/// @param passerby The address to be checked.
68-
/// @param data The data associated with the check.
69-
function _checkMain(address passerby, bytes memory data) internal view virtual returns (bool checked) {}
71+
/// @param subject The address to be checked.
72+
/// @param evidence The evidence associated with the check.
73+
function _checkMain(address subject, bytes memory evidence) internal view virtual returns (bool checked) {}
7074

7175
/// @notice Internal method for performing post-condition checks.
72-
/// @param passerby The address to be checked.
73-
/// @param data The data associated with the check.
74-
function _checkPost(address passerby, bytes memory data) internal view virtual returns (bool checked) {}
76+
/// @param subject The address to be checked.
77+
/// @param evidence The evidence associated with the check.
78+
function _checkPost(address subject, bytes memory evidence) internal view virtual returns (bool checked) {}
7579
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.27;
3+
4+
import {Policy} from "./Policy.sol";
5+
import {IAdvancedPolicy, Check} from "./interfaces/IAdvancedPolicy.sol";
6+
import {AdvancedChecker, CheckStatus} from "./AdvancedChecker.sol";
7+
8+
/// @title AdvancedPolicy
9+
/// @notice Abstract base contract which can be extended to implement a specific `AdvancedPolicy`.
10+
abstract contract AdvancedPolicy is IAdvancedPolicy, Policy {
11+
/// @dev Reference to the AdvancedChecker contract for validation.
12+
AdvancedChecker public immutable ADVANCED_CHECKER;
13+
14+
/// @dev Tracks the check status of each address.
15+
mapping(address => mapping(address => CheckStatus)) public enforced;
16+
17+
/// @notice Constructor to initialize the AdvancedChecker contract.
18+
/// @param _advancedChecker The address of the AdvancedChecker contract.
19+
constructor(AdvancedChecker _advancedChecker) {
20+
ADVANCED_CHECKER = _advancedChecker;
21+
}
22+
23+
/// @notice Enforces the custom target logic.
24+
/// @dev Calls the internal `_enforce` function to enforce the target logic.
25+
/// @dev Must call the `check` to handle the logic of checking subject for specific target.
26+
/// @param subject The address of those who have successfully enforced the check.
27+
/// @param evidence Additional data required for the check (e.g., encoded token identifier).
28+
/// @param checkType The type of the check to be enforced for the subject with the given data.
29+
function enforce(address subject, bytes calldata evidence, Check checkType) external override onlyTarget {
30+
_enforce(subject, evidence, checkType);
31+
}
32+
33+
/// @notice Internal function to enforce the target logic.
34+
/// @param subject The address of those who have successfully enforced the check.
35+
/// @param evidence Additional data required for the check (e.g., encoded token identifier).
36+
/// @param checkType The type of the check to be enforced for the subject with the given data.
37+
function _enforce(address subject, bytes calldata evidence, Check checkType) internal {
38+
bool checked = ADVANCED_CHECKER.check(subject, evidence, checkType);
39+
40+
if (!checked) revert UnsuccessfulCheck();
41+
42+
if (checkType == Check.PRE) {
43+
if (ADVANCED_CHECKER.skipPre()) revert PreCheckSkipped();
44+
else if (enforced[msg.sender][subject].pre) revert AlreadyEnforced();
45+
else enforced[msg.sender][subject].pre = true;
46+
} else if (checkType == Check.POST) {
47+
if (ADVANCED_CHECKER.skipPost()) revert PostCheckSkipped();
48+
else if (enforced[msg.sender][subject].post) revert AlreadyEnforced();
49+
else enforced[msg.sender][subject].post = true;
50+
} else if (checkType == Check.MAIN) {
51+
if (!ADVANCED_CHECKER.allowMultipleMain() && enforced[msg.sender][subject].main > 0) {
52+
revert MainCheckAlreadyEnforced();
53+
} else {
54+
enforced[msg.sender][subject].main += 1;
55+
}
56+
}
57+
58+
emit Enforced(subject, target, evidence, checkType);
59+
}
60+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.27;
3+
4+
import {IBaseChecker} from "./interfaces/IBaseChecker.sol";
5+
6+
/// @title BaseChecker.
7+
/// @notice Abstract base contract which can be extended to implement a specific `BaseChecker`.
8+
/// @dev The `BaseChecker` contract provides a foundational structure for implementing specific checker logic.
9+
/// It defines a method `check` that invokes a protected `_check` method, which must be implemented by derived
10+
/// contracts.
11+
abstract contract BaseChecker is IBaseChecker {
12+
/// @notice Checks the validity of the provided evidence for a given address.
13+
/// @param subject The address to be checked.
14+
/// @param evidence The evidence associated with the check.
15+
function check(address subject, bytes memory evidence) external view override returns (bool checked) {
16+
return _check(subject, evidence);
17+
}
18+
19+
/// @notice Internal method to perform the actual check logic.
20+
/// @param subject The address to be checked.
21+
/// @param evidence The evidence associated with the check.
22+
function _check(address subject, bytes memory evidence) internal view virtual returns (bool checked);
23+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.27;
3+
4+
import {IBasePolicy} from "./interfaces/IBasePolicy.sol";
5+
import {Policy} from "./Policy.sol";
6+
import {BaseChecker} from "./BaseChecker.sol";
7+
8+
/// @title BasePolicy
9+
/// @notice Abstract base contract which can be extended to implement a specific `BasePolicy`.
10+
abstract contract BasePolicy is Policy, IBasePolicy {
11+
/// @dev Reference to the BaseChecker contract for validation.
12+
BaseChecker public immutable BASE_CHECKER;
13+
14+
/// @dev Tracks whether the check has been enforced for a subject.
15+
mapping(address => mapping(address => bool)) public enforced;
16+
17+
/// @notice Constructor to initialize the BaseChecker contract.
18+
/// @param _baseChecker The address of the BaseChecker contract.
19+
constructor(BaseChecker _baseChecker) {
20+
BASE_CHECKER = _baseChecker;
21+
}
22+
23+
/// @notice Enforces the custom target enforcing logic.
24+
/// @dev Must call the `check` to handle the logic of checking subject for specific target.
25+
/// @param subject The address of those who have successfully enforced the check.
26+
/// @param evidence Additional data required for the check (e.g., encoded token identifier).
27+
function enforce(address subject, bytes calldata evidence) external override onlyTarget {
28+
_enforce(subject, evidence);
29+
}
30+
31+
/// @notice Enforces the custom target enforcing logic.
32+
/// @param subject The address of those who have successfully enforced the check.
33+
/// @param evidence Additional data required for the check (e.g., encoded token identifier).
34+
function _enforce(address subject, bytes calldata evidence) internal {
35+
bool checked = BASE_CHECKER.check(subject, evidence);
36+
37+
if (enforced[msg.sender][subject]) revert AlreadyEnforced();
38+
if (!checked) revert UnsuccessfulCheck();
39+
40+
enforced[msg.sender][subject] = checked;
41+
42+
emit Enforced(subject, target, evidence);
43+
}
44+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity 0.8.27;
3+
4+
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
5+
import {IPolicy} from "./interfaces/IPolicy.sol";
6+
7+
/// @title Policy abstract contract.
8+
/// @dev This contract implements the IPolicy interface and manages the target address.
9+
abstract contract Policy is IPolicy, Ownable(msg.sender) {
10+
/// @notice The Policy-protected contract address.
11+
/// @dev The target can be any contract address that requires a prior check to enable logic.
12+
/// For example, the target is a Semaphore group that requires the subject
13+
/// to meet certain criteria before joining.
14+
address public target;
15+
16+
/// @notice Modifier that restricts access to the target address.
17+
modifier onlyTarget() {
18+
if (msg.sender != target) revert TargetOnly();
19+
_;
20+
}
21+
22+
/// @notice Sets the target address.
23+
/// @dev Only the owner can set the destination `target` address.
24+
/// @param _target The address of the contract to be set as the target.
25+
function setTarget(address _target) public virtual onlyOwner {
26+
if (_target == address(0)) revert ZeroAddress();
27+
if (target != address(0)) revert TargetAlreadySet();
28+
29+
target = _target;
30+
31+
emit TargetSet(_target);
32+
}
33+
}

packages/contracts/contracts/src/core/checker/BaseChecker.sol

Lines changed: 0 additions & 23 deletions
This file was deleted.

packages/contracts/contracts/src/core/checker/IBaseChecker.sol

Lines changed: 0 additions & 11 deletions
This file was deleted.

packages/contracts/contracts/src/core/gatekeeper/AdvancedExcubia.sol

Lines changed: 0 additions & 59 deletions
This file was deleted.

packages/contracts/contracts/src/core/gatekeeper/BaseExcubia.sol

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)