|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | +pragma solidity ^0.8.13; |
| 3 | + |
| 4 | +import {Test} from "forge-std/Test.sol"; |
| 5 | +import {TheGuildInternalResolver} from "../src/TheGuildInternalResolver.sol"; |
| 6 | + |
| 7 | + |
| 8 | +import {EAS} from "eas-contracts/EAS.sol"; |
| 9 | +import {SchemaRegistry} from "eas-contracts/SchemaRegistry.sol"; |
| 10 | +import {IEAS, AttestationRequest, AttestationRequestData, Attestation, RevocationRequestData, RevocationRequest} from "eas-contracts/IEAS.sol"; |
| 11 | + |
| 12 | +contract TheGuildInternalResolverTest is Test { |
| 13 | + TheGuildInternalResolver private resolver; |
| 14 | + SchemaRegistry private schemaRegistry; |
| 15 | + EAS private eas; |
| 16 | + |
| 17 | + address private owner = address(this); |
| 18 | + address private authorizedAttester = address(0xA11CE); |
| 19 | + address private unauthorizedAttester = address(0xBEEF); |
| 20 | + address private recipient = address(0xCAFE); |
| 21 | + |
| 22 | + function setUp() public { |
| 23 | + |
| 24 | + schemaRegistry = new SchemaRegistry(); |
| 25 | + eas = new EAS(schemaRegistry); |
| 26 | + |
| 27 | + |
| 28 | + resolver = new TheGuildInternalResolver( |
| 29 | + IEAS(address(eas)), |
| 30 | + owner |
| 31 | + ); |
| 32 | + } |
| 33 | + |
| 34 | + function _registerSchema() internal returns (bytes32) { |
| 35 | + |
| 36 | + string memory schema = "string skillDescription, bytes32[] linkedBadges"; |
| 37 | + |
| 38 | + bytes32 schemaId = schemaRegistry.register(schema, resolver, true); |
| 39 | + return schemaId; |
| 40 | + } |
| 41 | + |
| 42 | + function test_AttestationByOwnerSucceeds() public { |
| 43 | + bytes32 schemaId = _registerSchema(); |
| 44 | + |
| 45 | + bytes32[] memory linkedBadges = new bytes32[](2); |
| 46 | + linkedBadges[0] = bytes32("Solidity"); |
| 47 | + linkedBadges[1] = bytes32("Rust"); |
| 48 | + |
| 49 | + AttestationRequestData memory data = AttestationRequestData({ |
| 50 | + recipient: recipient, |
| 51 | + expirationTime: 0, |
| 52 | + revocable: true, |
| 53 | + refUID: bytes32(0), |
| 54 | + data: abi.encode("Master of Smart Contracts", linkedBadges), |
| 55 | + value: 0 |
| 56 | + }); |
| 57 | + |
| 58 | + AttestationRequest memory request = AttestationRequest({ |
| 59 | + schema: schemaId, |
| 60 | + data: data |
| 61 | + }); |
| 62 | + |
| 63 | + |
| 64 | + eas.attest(request); |
| 65 | + |
| 66 | + } |
| 67 | + |
| 68 | + function test_AttestationByAuthorizedAttesterSucceeds() public { |
| 69 | + bytes32 schemaId = _registerSchema(); |
| 70 | + resolver.setAuthorizedAttester(authorizedAttester, true); |
| 71 | + |
| 72 | + bytes32[] memory linkedBadges = new bytes32[](1); |
| 73 | + linkedBadges[0] = bytes32("Solidity"); |
| 74 | + |
| 75 | + AttestationRequestData memory data = AttestationRequestData({ |
| 76 | + recipient: recipient, |
| 77 | + expirationTime: 0, |
| 78 | + revocable: true, |
| 79 | + refUID: bytes32(0), |
| 80 | + data: abi.encode("Solidity Dev", linkedBadges), |
| 81 | + value: 0 |
| 82 | + }); |
| 83 | + |
| 84 | + AttestationRequest memory request = AttestationRequest({ |
| 85 | + schema: schemaId, |
| 86 | + data: data |
| 87 | + }); |
| 88 | + |
| 89 | + vm.prank(authorizedAttester); |
| 90 | + eas.attest(request); |
| 91 | + |
| 92 | + } |
| 93 | + |
| 94 | + function test_AttestationByUnauthorizedAttesterFails() public { |
| 95 | + bytes32 schemaId = _registerSchema(); |
| 96 | + |
| 97 | + bytes32[] memory linkedBadges = new bytes32[](1); |
| 98 | + linkedBadges[0] = bytes32("Solidity"); |
| 99 | + |
| 100 | + AttestationRequestData memory data = AttestationRequestData({ |
| 101 | + recipient: recipient, |
| 102 | + expirationTime: 0, |
| 103 | + revocable: true, |
| 104 | + refUID: bytes32(0), |
| 105 | + data: abi.encode("Solidity Dev", linkedBadges), |
| 106 | + value: 0 |
| 107 | + }); |
| 108 | + |
| 109 | + AttestationRequest memory request = AttestationRequest({ |
| 110 | + schema: schemaId, |
| 111 | + data: data |
| 112 | + }); |
| 113 | + |
| 114 | + vm.prank(unauthorizedAttester); |
| 115 | + vm.expectRevert(); |
| 116 | + eas.attest(request); |
| 117 | + } |
| 118 | + |
| 119 | + function test_RevocationByAuthorizedAttesterSucceeds() public { |
| 120 | + bytes32 schemaId = _registerSchema(); |
| 121 | + resolver.setAuthorizedAttester(authorizedAttester, true); |
| 122 | + |
| 123 | + bytes32[] memory linkedBadges = new bytes32[](1); |
| 124 | + linkedBadges[0] = bytes32("Solidity"); |
| 125 | + |
| 126 | + AttestationRequestData memory data = AttestationRequestData({ |
| 127 | + recipient: recipient, |
| 128 | + expirationTime: 0, |
| 129 | + revocable: true, |
| 130 | + refUID: bytes32(0), |
| 131 | + data: abi.encode("Solidity Dev", linkedBadges), |
| 132 | + value: 0 |
| 133 | + }); |
| 134 | + |
| 135 | + AttestationRequest memory request = AttestationRequest({ |
| 136 | + schema: schemaId, |
| 137 | + data: data |
| 138 | + }); |
| 139 | + |
| 140 | + vm.prank(authorizedAttester); |
| 141 | + bytes32 uid = eas.attest(request); |
| 142 | + |
| 143 | + vm.prank(authorizedAttester); |
| 144 | + eas.revoke( |
| 145 | + RevocationRequest({ |
| 146 | + schema: schemaId, |
| 147 | + data: RevocationRequestData({uid: uid, value: 0}) |
| 148 | + }) |
| 149 | + ); |
| 150 | + // success == no revert |
| 151 | + } |
| 152 | + |
| 153 | + function test_RevocationByUnauthorizedAttesterFails() public { |
| 154 | + bytes32 schemaId = _registerSchema(); |
| 155 | + resolver.setAuthorizedAttester(authorizedAttester, true); |
| 156 | + |
| 157 | + bytes32[] memory linkedBadges = new bytes32[](1); |
| 158 | + linkedBadges[0] = bytes32("Solidity"); |
| 159 | + |
| 160 | + AttestationRequestData memory data = AttestationRequestData({ |
| 161 | + recipient: recipient, |
| 162 | + expirationTime: 0, |
| 163 | + revocable: true, |
| 164 | + refUID: bytes32(0), |
| 165 | + data: abi.encode("Solidity Dev", linkedBadges), |
| 166 | + value: 0 |
| 167 | + }); |
| 168 | + |
| 169 | + AttestationRequest memory request = AttestationRequest({ |
| 170 | + schema: schemaId, |
| 171 | + data: data |
| 172 | + }); |
| 173 | + |
| 174 | + vm.prank(authorizedAttester); |
| 175 | + bytes32 uid = eas.attest(request); |
| 176 | + |
| 177 | + vm.prank(unauthorizedAttester); |
| 178 | + vm.expectRevert(); |
| 179 | + eas.revoke( |
| 180 | + RevocationRequest({ |
| 181 | + schema: schemaId, |
| 182 | + data: RevocationRequestData({uid: uid, value: 0}) |
| 183 | + }) |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + function test_DeauthorizeAttester() public { |
| 188 | + bytes32 schemaId = _registerSchema(); |
| 189 | + resolver.setAuthorizedAttester(authorizedAttester, true); |
| 190 | + assertTrue(resolver.isAuthorizedAttester(authorizedAttester)); |
| 191 | + |
| 192 | + resolver.setAuthorizedAttester(authorizedAttester, false); |
| 193 | + assertFalse(resolver.isAuthorizedAttester(authorizedAttester)); |
| 194 | + |
| 195 | + bytes32[] memory linkedBadges = new bytes32[](1); |
| 196 | + linkedBadges[0] = bytes32("Solidity"); |
| 197 | + |
| 198 | + AttestationRequest memory request = AttestationRequest({ |
| 199 | + schema: schemaId, |
| 200 | + data: AttestationRequestData({ |
| 201 | + recipient: recipient, |
| 202 | + expirationTime: 0, |
| 203 | + revocable: true, |
| 204 | + refUID: bytes32(0), |
| 205 | + data: abi.encode("Solidity Dev", linkedBadges), |
| 206 | + value: 0 |
| 207 | + }) |
| 208 | + }); |
| 209 | + |
| 210 | + vm.prank(authorizedAttester); |
| 211 | + vm.expectRevert(); |
| 212 | + eas.attest(request); |
| 213 | + } |
| 214 | +} |
0 commit comments