Skip to content

Commit

Permalink
refactor(excubiae): improve structure and disposition of _pass and _c…
Browse files Browse the repository at this point in the history
…heck; add unit test
  • Loading branch information
0xjei committed Jun 14, 2024
1 parent 04a996e commit 580db6b
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 21 deletions.
22 changes: 8 additions & 14 deletions packages/excubiae/contracts/extensions/EASExcubia.sol
Original file line number Diff line number Diff line change
Expand Up @@ -53,21 +53,22 @@ contract EASExcubia is Excubia {
/// @param passerby The address of the entity attempting to pass the gate.
/// @param data Encoded attestation ID.
function _pass(address passerby, bytes calldata data) internal override {
super._pass(passerby, data);
bytes32 attestationId = abi.decode(data, (bytes32));

// Avoiding double check of the same attestation.
if (registeredAttestations[attestationId]) revert AlreadyRegistered();

registeredAttestations[decodeAttestationId(data)] = true;
registeredAttestations[attestationId] = true;

super._pass(passerby, data);
}

/// @notice Overrides the `_check` function to validate the attestation against specific criteria.
/// @param passerby The address of the entity attempting to pass the gate.
/// @param data Encoded attestation ID.
/// @return True if the attestation meets all criteria, revert otherwise.
function _check(address passerby, bytes calldata data) internal view override returns (bool) {
bytes32 attestationId = decodeAttestationId(data);

if (registeredAttestations[attestationId]) revert AlreadyRegistered();

Attestation memory attestation = EAS.getAttestation(attestationId);
Attestation memory attestation = EAS.getAttestation(abi.decode(data, (bytes32)));

if (attestation.schema != SCHEMA) revert UnexpectedSchema();
if (attestation.attester != ATTESTER) revert UnexpectedAttester();
Expand All @@ -76,11 +77,4 @@ contract EASExcubia is Excubia {

return true;
}

/// @notice Decodes an EAS attestation identifier from the encoded form.
/// @param data Encoded attestation ID.
/// @return Decoded attestation ID.
function decodeAttestationId(bytes calldata data) private pure returns (bytes32) {
return abi.decode(data, (bytes32));
}
}
4 changes: 0 additions & 4 deletions packages/excubiae/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,5 @@
"ts-node": "^10.9.2",
"typechain": "^8.3.0",
"typescript": "^5.3.3"
},
"dependencies": {
"@ethereum-attestation-service/eas-contracts": "^1.7.1",
"@openzeppelin/contracts": "^5.0.2"
}
}
6 changes: 5 additions & 1 deletion packages/excubiae/test/EASExcubia.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect } from "chai"
import { ethers } from "hardhat"
import { BigNumberish, Signer, ZeroAddress, getDefaultProvider } from "ethers"
import { Signer, ZeroAddress } from "ethers"
import { EASExcubia, EASExcubia__factory, MockEAS, MockEAS__factory } from "../typechain-types"

describe("EASExcubia", function () {
Expand Down Expand Up @@ -74,6 +74,10 @@ describe("EASExcubia", function () {
expect(await easExcubia.gate()).to.eq(gateAddress)
})

it("Should fail to set the gate if already set", async function () {
await expect(easExcubia.setGate(gateAddress)).to.be.revertedWithCustomError(easExcubia, "GateAlreadySet")
})

it("should throw when the callee is not the gate", async () => {
await expect(
easExcubia.connect(signer).pass(signerAddress, invalidRecipientAttestationId)
Expand Down
2 changes: 0 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3470,13 +3470,11 @@ __metadata:
version: 0.0.0-use.local
resolution: "excubiae@workspace:packages/excubiae"
dependencies:
"@ethereum-attestation-service/eas-contracts": "npm:^1.7.1"
"@nomicfoundation/hardhat-chai-matchers": "npm:^2.0.3"
"@nomicfoundation/hardhat-ethers": "npm:^3.0.0"
"@nomicfoundation/hardhat-network-helpers": "npm:^1.0.0"
"@nomicfoundation/hardhat-toolbox": "npm:^4.0.0"
"@nomicfoundation/hardhat-verify": "npm:^2.0.0"
"@openzeppelin/contracts": "npm:^5.0.2"
"@typechain/ethers-v6": "npm:^0.5.0"
"@typechain/hardhat": "npm:^9.0.0"
"@types/chai": "npm:^4.2.0"
Expand Down

0 comments on commit 580db6b

Please sign in to comment.