@gatedpath/receptor-mock 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,156 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.30;
3
+
4
+ import {Test} from "forge-std/Test.sol";
5
+ import {Gated} from "../src/Gated.sol";
6
+ import {EligibilityStatus} from "../src/IRedbellyVerifier.sol";
7
+ import {ReceptorMock} from "../src/ReceptorMock.sol";
8
+
9
+ /// @title GatedTest
10
+ /// @notice Abstract Foundry test base that proves a gate against all five credential states, so
11
+ /// a developer cannot forget one.
12
+ /// @dev Inherit it, call `_deployMock()` in `setUp`, build the contract under test on `mock`, then
13
+ /// hand `assertRevertsForAllInvalidStates` or `assertGatedPair` the target address and the ABI
14
+ /// encoded call. The call data must be safe to repeat: the helpers run it once per state.
15
+ /// The helpers set states on `mock` for the request id you pass, so pass the request id the
16
+ /// target is bound to. They work against mock-backed targets only; an adapter over a real
17
+ /// verifier cannot be put into `Expired`, `Revoked` or `WrongJurisdiction`.
18
+ abstract contract GatedTest is Test {
19
+ /// @notice The mock every gated contract under test should point at.
20
+ ReceptorMock internal mock;
21
+
22
+ /// @notice Deploys a fresh mock into `mock` and returns it.
23
+ function _deployMock() internal returns (ReceptorMock) {
24
+ mock = new ReceptorMock();
25
+ vm.label(address(mock), "ReceptorMock");
26
+ return mock;
27
+ }
28
+
29
+ /// @notice The four states that must be denied, in enum order.
30
+ function invalidStates() internal pure returns (EligibilityStatus[4] memory states) {
31
+ states[0] = EligibilityStatus.NeverIssued;
32
+ states[1] = EligibilityStatus.Expired;
33
+ states[2] = EligibilityStatus.Revoked;
34
+ states[3] = EligibilityStatus.WrongJurisdiction;
35
+ }
36
+
37
+ /// @notice Puts `wallet` into each of the four non-valid states, calls `target` as `wallet`
38
+ /// each time and asserts the revert is exactly `NotEligible(wallet, requestId)`; then sets
39
+ /// `Valid` and asserts the same call succeeds.
40
+ /// @param target The gated contract.
41
+ /// @param callData The ABI encoded call to a function guarded by `gated`.
42
+ /// @param wallet The caller the gate checks.
43
+ /// @param requestId The request id the target is bound to.
44
+ function assertRevertsForAllInvalidStates(address target, bytes memory callData, address wallet, uint64 requestId)
45
+ internal
46
+ {
47
+ _requireReady(target);
48
+ EligibilityStatus[4] memory states = invalidStates();
49
+ for (uint256 i = 0; i < states.length; ++i) {
50
+ mock.setStatus(wallet, requestId, states[i]);
51
+ _expectNotEligible(target, callData, wallet, wallet, requestId, states[i]);
52
+ }
53
+ mock.setStatus(wallet, requestId, EligibilityStatus.Valid);
54
+ _expectSuccess(target, callData, wallet, "Valid");
55
+ }
56
+
57
+ /// @notice Two-party check. With both wallets `Valid` the call succeeds; with `sender` in any
58
+ /// non-valid state (and `counterparty` valid) it reverts `NotEligible(sender, requestId)`; with
59
+ /// `counterparty` in any non-valid state (and `sender` valid) it reverts
60
+ /// `NotEligible(counterparty, requestId)`. Ends with both valid and a final success.
61
+ /// @param target The gated contract.
62
+ /// @param callData The ABI encoded call to a function guarded by `gated` and `gatedFor(counterparty)`.
63
+ /// @param sender The caller.
64
+ /// @param counterparty The second party named in the call data.
65
+ /// @param requestId The request id the target is bound to.
66
+ function assertGatedPair(
67
+ address target,
68
+ bytes memory callData,
69
+ address sender,
70
+ address counterparty,
71
+ uint64 requestId
72
+ ) internal {
73
+ _requireReady(target);
74
+ require(sender != counterparty, "GatedTest: sender and counterparty must differ");
75
+ EligibilityStatus[4] memory states = invalidStates();
76
+
77
+ mock.setStatus(sender, requestId, EligibilityStatus.Valid);
78
+ mock.setStatus(counterparty, requestId, EligibilityStatus.Valid);
79
+ _expectSuccess(target, callData, sender, "both Valid");
80
+
81
+ for (uint256 i = 0; i < states.length; ++i) {
82
+ mock.setStatus(sender, requestId, states[i]);
83
+ mock.setStatus(counterparty, requestId, EligibilityStatus.Valid);
84
+ _expectNotEligible(target, callData, sender, sender, requestId, states[i]);
85
+ }
86
+ for (uint256 i = 0; i < states.length; ++i) {
87
+ mock.setStatus(sender, requestId, EligibilityStatus.Valid);
88
+ mock.setStatus(counterparty, requestId, states[i]);
89
+ _expectNotEligible(target, callData, sender, counterparty, requestId, states[i]);
90
+ }
91
+
92
+ mock.setStatus(sender, requestId, EligibilityStatus.Valid);
93
+ mock.setStatus(counterparty, requestId, EligibilityStatus.Valid);
94
+ _expectSuccess(target, callData, sender, "both Valid again");
95
+ }
96
+
97
+ /// @notice Human name of a state, for assertion messages.
98
+ function stateName(EligibilityStatus state) internal pure returns (string memory) {
99
+ if (state == EligibilityStatus.NeverIssued) return "NeverIssued";
100
+ if (state == EligibilityStatus.Valid) return "Valid";
101
+ if (state == EligibilityStatus.Expired) return "Expired";
102
+ if (state == EligibilityStatus.Revoked) return "Revoked";
103
+ return "WrongJurisdiction";
104
+ }
105
+
106
+ function _requireReady(address target) private view {
107
+ require(address(mock) != address(0), "GatedTest: call _deployMock() in setUp first");
108
+ require(!mock.frozen(), "GatedTest: mock is frozen; the helpers need to set states");
109
+ require(target.code.length != 0, "GatedTest: target has no code");
110
+ }
111
+
112
+ function _expectNotEligible(
113
+ address target,
114
+ bytes memory callData,
115
+ address caller,
116
+ address deniedWallet,
117
+ uint64 requestId,
118
+ EligibilityStatus state
119
+ ) private {
120
+ vm.prank(caller);
121
+ // slither-disable-next-line low-level-calls
122
+ (bool ok, bytes memory ret) = target.call(callData);
123
+ assertFalse(
124
+ ok, string.concat("GatedTest: call succeeded while ", vm.toString(deniedWallet), " was ", stateName(state))
125
+ );
126
+ assertEq(
127
+ ret,
128
+ abi.encodeWithSelector(Gated.NotEligible.selector, deniedWallet, requestId),
129
+ string.concat(
130
+ "GatedTest: revert under ",
131
+ stateName(state),
132
+ " was not NotEligible(",
133
+ vm.toString(deniedWallet),
134
+ ", requestId)"
135
+ )
136
+ );
137
+ }
138
+
139
+ function _expectSuccess(address target, bytes memory callData, address caller, string memory label) private {
140
+ vm.prank(caller);
141
+ // slither-disable-next-line low-level-calls
142
+ (bool ok, bytes memory ret) = target.call(callData);
143
+ // casting to 'bytes4' is safe: only the selector, the first four bytes, is wanted here
144
+ // forge-lint: disable-next-line(unsafe-typecast)
145
+ if (!ok && ret.length >= 4 && bytes4(ret) == Gated.NotEligible.selector) {
146
+ fail(
147
+ string.concat(
148
+ "GatedTest: gate still denied with ",
149
+ label,
150
+ "; the target is probably bound to a different requestId than the one passed"
151
+ )
152
+ );
153
+ }
154
+ assertTrue(ok, string.concat("GatedTest: call failed with ", label, " for a reason other than the gate"));
155
+ }
156
+ }
@@ -0,0 +1,153 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.30;
3
+
4
+ import {Test} from "forge-std/Test.sol";
5
+ import {EligibilityStatus} from "../src/IRedbellyVerifier.sol";
6
+ import {ReceptorMock} from "../src/ReceptorMock.sol";
7
+
8
+ contract ReceptorMockTest is Test {
9
+ ReceptorMock internal mock;
10
+ address internal alice = makeAddr("alice");
11
+ uint64 internal constant R1 = 1;
12
+ uint64 internal constant R2 = 2;
13
+
14
+ function setUp() public {
15
+ mock = new ReceptorMock();
16
+ }
17
+
18
+ function _status(address wallet, uint64 requestId) internal view returns (EligibilityStatus) {
19
+ return mock.eligibilityStatus(wallet, requestId);
20
+ }
21
+
22
+ function test_unsetIsNeverIssued() public view {
23
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.NeverIssued));
24
+ assertFalse(mock.isEligible(alice, R1));
25
+ ReceptorMock.Record memory rec = mock.recordFor(alice, R1);
26
+ assertFalse(rec.isSet);
27
+ }
28
+
29
+ function test_setStatusEveryValueAndEvent() public {
30
+ for (uint8 i = 0; i <= uint8(EligibilityStatus.WrongJurisdiction); ++i) {
31
+ EligibilityStatus s = EligibilityStatus(i);
32
+ vm.expectEmit(address(mock));
33
+ emit ReceptorMock.StatusSet(alice, R1, s, 0);
34
+ mock.setStatus(alice, R1, s);
35
+ assertEq(uint8(_status(alice, R1)), i);
36
+ assertEq(mock.isEligible(alice, R1), s == EligibilityStatus.Valid);
37
+ }
38
+ }
39
+
40
+ function test_explicitNeverIssuedIsSetButStillDenied() public {
41
+ mock.setStatusAll(alice, EligibilityStatus.Valid);
42
+ mock.setStatus(alice, R1, EligibilityStatus.NeverIssued);
43
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.NeverIssued), "explicit record beats the wildcard");
44
+ assertTrue(mock.recordFor(alice, R1).isSet);
45
+ assertTrue(mock.isEligible(alice, R2), "other request ids still use the wildcard");
46
+ }
47
+
48
+ function test_setStatusAllAndPrecedence() public {
49
+ vm.expectEmit(address(mock));
50
+ emit ReceptorMock.StatusSetAll(alice, EligibilityStatus.Revoked, 0);
51
+ mock.setStatusAll(alice, EligibilityStatus.Revoked);
52
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.Revoked));
53
+ assertEq(uint8(_status(alice, type(uint64).max)), uint8(EligibilityStatus.Revoked));
54
+
55
+ mock.setStatus(alice, R1, EligibilityStatus.Valid);
56
+ assertTrue(mock.isEligible(alice, R1), "per-request record wins");
57
+ assertFalse(mock.isEligible(alice, R2), "wildcard still applies elsewhere");
58
+
59
+ vm.expectEmit(address(mock));
60
+ emit ReceptorMock.StatusCleared(alice, R1);
61
+ mock.clearStatus(alice, R1);
62
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.Revoked), "back to the wildcard");
63
+
64
+ vm.expectEmit(address(mock));
65
+ emit ReceptorMock.StatusClearedAll(alice);
66
+ mock.clearStatusAll(alice);
67
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.NeverIssued));
68
+ }
69
+
70
+ function test_expiryFromTimestamp() public {
71
+ uint64 expiresAt = uint64(block.timestamp + 3600);
72
+ vm.expectEmit(address(mock));
73
+ emit ReceptorMock.StatusSet(alice, R1, EligibilityStatus.Valid, expiresAt);
74
+ mock.setValidUntil(alice, R1, expiresAt);
75
+ assertTrue(mock.isEligible(alice, R1));
76
+
77
+ vm.warp(expiresAt - 1);
78
+ assertTrue(mock.isEligible(alice, R1), "valid up to the last second before expiry");
79
+
80
+ vm.warp(expiresAt);
81
+ assertFalse(mock.isEligible(alice, R1), "expired at expiresAt");
82
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.Expired));
83
+ assertEq(
84
+ uint8(mock.recordFor(alice, R1).status),
85
+ uint8(EligibilityStatus.Valid),
86
+ "stored state unchanged; derived at read"
87
+ );
88
+
89
+ vm.warp(expiresAt + 365 days);
90
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.Expired));
91
+ }
92
+
93
+ function test_expiryWildcard() public {
94
+ uint64 expiresAt = uint64(block.timestamp + 10);
95
+ vm.expectEmit(address(mock));
96
+ emit ReceptorMock.StatusSetAll(alice, EligibilityStatus.Valid, expiresAt);
97
+ mock.setValidUntilAll(alice, expiresAt);
98
+ assertTrue(mock.isEligible(alice, R1));
99
+ assertTrue(mock.isEligible(alice, R2));
100
+ vm.warp(expiresAt);
101
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.Expired));
102
+ assertEq(uint8(_status(alice, R2)), uint8(EligibilityStatus.Expired));
103
+ }
104
+
105
+ function test_zeroExpiryMeansNever() public {
106
+ mock.setValidUntil(alice, R1, 0);
107
+ vm.warp(type(uint64).max);
108
+ assertTrue(mock.isEligible(alice, R1));
109
+ }
110
+
111
+ function test_expiryOnlyAppliesToValid() public {
112
+ // A non-valid record is never rewritten to Expired, so the test's named reason survives.
113
+ mock.setStatus(alice, R1, EligibilityStatus.Revoked);
114
+ vm.warp(block.timestamp + 10 * 365 days);
115
+ assertEq(uint8(_status(alice, R1)), uint8(EligibilityStatus.Revoked));
116
+ }
117
+
118
+ function test_freezeBlocksEverySetter() public {
119
+ mock.setStatus(alice, R1, EligibilityStatus.Valid);
120
+ assertFalse(mock.frozen());
121
+
122
+ vm.expectEmit(address(mock));
123
+ emit ReceptorMock.MockFrozen(address(this));
124
+ mock.freeze();
125
+ assertTrue(mock.frozen());
126
+
127
+ vm.expectRevert(ReceptorMock.MockIsFrozen.selector);
128
+ mock.setStatus(alice, R1, EligibilityStatus.Revoked);
129
+ vm.expectRevert(ReceptorMock.MockIsFrozen.selector);
130
+ mock.setValidUntil(alice, R1, 1);
131
+ vm.expectRevert(ReceptorMock.MockIsFrozen.selector);
132
+ mock.setStatusAll(alice, EligibilityStatus.Revoked);
133
+ vm.expectRevert(ReceptorMock.MockIsFrozen.selector);
134
+ mock.setValidUntilAll(alice, 1);
135
+ vm.expectRevert(ReceptorMock.MockIsFrozen.selector);
136
+ mock.clearStatus(alice, R1);
137
+ vm.expectRevert(ReceptorMock.MockIsFrozen.selector);
138
+ mock.clearStatusAll(alice);
139
+ vm.expectRevert(ReceptorMock.MockIsFrozen.selector);
140
+ mock.freeze();
141
+
142
+ assertTrue(mock.isEligible(alice, R1), "reads keep working and the state did not loosen");
143
+ }
144
+
145
+ function test_refusesMainnetChainId() public {
146
+ vm.chainId(151);
147
+ vm.expectRevert(abi.encodeWithSelector(ReceptorMock.MainnetForbidden.selector, 151));
148
+ new ReceptorMock();
149
+
150
+ vm.chainId(153);
151
+ new ReceptorMock();
152
+ }
153
+ }
@@ -0,0 +1,58 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.30;
3
+
4
+ import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
5
+ import {Gated} from "../../src/Gated.sol";
6
+ import {IRedbellyVerifier} from "../../src/IRedbellyVerifier.sol";
7
+
8
+ /// @title GatedCounter
9
+ /// @notice The smallest useful `Gated` child: a counter only eligible wallets can move, plus a
10
+ /// two-party action that needs both sides eligible, plus a non-reverting path that logs the
11
+ /// denial. Wires `_authorizeVerifierChange` to OpenZeppelin `Ownable`.
12
+ contract GatedCounter is Gated, Ownable {
13
+ /// @notice Emitted on every increment.
14
+ event Incremented(address indexed by, uint256 newCount);
15
+
16
+ /// @notice Emitted on every nudge between two eligible wallets.
17
+ event Nudged(address indexed from, address indexed to, uint256 total);
18
+
19
+ /// @notice How many times `increment` has run.
20
+ uint256 public count;
21
+
22
+ /// @notice How many times `from` has nudged `to`.
23
+ mapping(address from => mapping(address to => uint256)) public nudges;
24
+
25
+ /// @param verifier_ The verifier (mock or adapter).
26
+ /// @param requestId_ The request id this counter is bound to.
27
+ constructor(IRedbellyVerifier verifier_, uint64 requestId_) Gated(verifier_, requestId_) Ownable(msg.sender) {}
28
+
29
+ /// @notice Adds one. Caller must be eligible.
30
+ function increment() external gated {
31
+ count += 1;
32
+ emit Incremented(msg.sender, count);
33
+ }
34
+
35
+ /// @notice Records a nudge. Both the caller and `to` must be eligible.
36
+ /// @param to The counterparty.
37
+ function nudge(address to) external gated gatedFor(to) {
38
+ nudges[msg.sender][to] += 1;
39
+ emit Nudged(msg.sender, to, nudges[msg.sender][to]);
40
+ }
41
+
42
+ /// @notice Adds one if the caller is eligible; otherwise logs the denial and returns false.
43
+ /// @dev Shows the non-reverting path: the denial stays on the log, which matters on a chain
44
+ /// without trace RPC methods.
45
+ /// @return True when the counter moved.
46
+ function tryIncrement() external returns (bool) {
47
+ if (!_checkEligible(msg.sender)) {
48
+ _recordDenial(msg.sender);
49
+ return false;
50
+ }
51
+ count += 1;
52
+ emit Incremented(msg.sender, count);
53
+ return true;
54
+ }
55
+
56
+ /// @dev Only the owner may repoint the verifier or request id.
57
+ function _authorizeVerifierChange() internal override onlyOwner {}
58
+ }
@@ -0,0 +1,56 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.30;
3
+
4
+ /// @dev Mimics `@iden3/contracts` 1.4.8 `ZKPVerifier`: a public `proofs` mapping, no revert for
5
+ /// unknown request ids.
6
+ contract FakeIden3V1 {
7
+ mapping(address => mapping(uint64 => bool)) public proofs;
8
+
9
+ function setProof(address sender, uint64 requestId, bool verified) external {
10
+ proofs[sender][requestId] = verified;
11
+ }
12
+ }
13
+
14
+ /// @dev Mimics `@iden3/contracts` 2.5.0 `ZKPVerifierBase.isProofVerified`, including the
15
+ /// `checkRequestExistence` revert for a request id that was never set.
16
+ contract FakeIden3V2 {
17
+ error RequestIdNotFound(uint64 requestId);
18
+
19
+ mapping(uint64 => bool) public requestExists;
20
+ mapping(address => mapping(uint64 => bool)) private _verified;
21
+
22
+ function setRequest(uint64 requestId) external {
23
+ requestExists[requestId] = true;
24
+ }
25
+
26
+ function setProof(address sender, uint64 requestId, bool verified) external {
27
+ _verified[sender][requestId] = verified;
28
+ }
29
+
30
+ function isProofVerified(address sender, uint64 requestId) external view returns (bool) {
31
+ if (!requestExists[requestId]) revert RequestIdNotFound(requestId);
32
+ return _verified[sender][requestId];
33
+ }
34
+ }
35
+
36
+ /// @dev Mimics `@iden3/contracts` 3.4.0 `Verifier.isRequestProofVerified` with `uint256`
37
+ /// request ids and the same revert for an unknown request.
38
+ contract FakeIden3V3 {
39
+ error RequestIdNotFound(uint256 requestId);
40
+
41
+ mapping(uint256 => bool) public requestExists;
42
+ mapping(uint256 => mapping(address => bool)) private _verified;
43
+
44
+ function setRequest(uint256 requestId) external {
45
+ requestExists[requestId] = true;
46
+ }
47
+
48
+ function setProof(address sender, uint256 requestId, bool verified) external {
49
+ _verified[requestId][sender] = verified;
50
+ }
51
+
52
+ function isRequestProofVerified(address sender, uint256 requestId) external view returns (bool) {
53
+ if (!requestExists[requestId]) revert RequestIdNotFound(requestId);
54
+ return _verified[requestId][sender];
55
+ }
56
+ }
@@ -0,0 +1,34 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity 0.8.30;
3
+
4
+ /// @dev Mimics the archived Redbelly example's `AvererVerifierContract`: a `VCVerifierBaseContract`
5
+ /// child whose `_postVerification` hook flips `verificationStatus[user]`. The base is not public,
6
+ /// so `verifyCredential` here just calls the hook for the sender; the signature is the one the
7
+ /// example's script calls.
8
+ contract FakeVCVerifierChild {
9
+ string public credentialType;
10
+ mapping(address => bool) public verificationStatus;
11
+
12
+ constructor(string memory credentialType_) {
13
+ credentialType = credentialType_;
14
+ }
15
+
16
+ function verifyCredential(string calldata, string calldata, string calldata) external {
17
+ _postVerification(msg.sender);
18
+ }
19
+
20
+ function revoke(address user) external {
21
+ verificationStatus[user] = false;
22
+ }
23
+
24
+ function _postVerification(address userAddress) internal {
25
+ verificationStatus[userAddress] = true;
26
+ }
27
+ }
28
+
29
+ /// @dev A contract with code that does not expose `verificationStatus`; every call reverts.
30
+ contract RevertingTarget {
31
+ fallback() external {
32
+ revert("nope");
33
+ }
34
+ }