Skip to content

Commit

Permalink
Add new schema hook example
Browse files Browse the repository at this point in the history
  • Loading branch information
boyuanx committed Sep 16, 2024
1 parent b0c6f97 commit b9e5014
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
40 changes: 40 additions & 0 deletions src/03-schema-hook-2/DataAttester.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { ISP } from "@ethsign/sign-protocol-evm/src/interfaces/ISP.sol";
import { Attestation } from "@ethsign/sign-protocol-evm/src/models/Attestation.sol";
import { DataLocation } from "@ethsign/sign-protocol-evm/src/models/DataLocation.sol";

contract DataAttester is Ownable {
ISP public spInstance;
uint64 public schemaId;

constructor() Ownable(_msgSender()) { }

function setSPInstance(address instance) external onlyOwner {
spInstance = ISP(instance);
}

function setSchemaID(uint64 schemaId_) external onlyOwner {
schemaId = schemaId_;
}

function attest(address recipient, uint256 someNumber) external returns (uint64) {
bytes[] memory recipients = new bytes[](2);
recipients[0] = abi.encode(recipient);
Attestation memory a = Attestation({
schemaId: schemaId,
linkedAttestationId: 0,
attestTimestamp: 0,
revokeTimestamp: 0,
attester: address(this),
validUntil: 0,
dataLocation: DataLocation.ONCHAIN,
revoked: false,
recipients: recipients,
data: abi.encode(someNumber)
});
return spInstance.attest(a, "", "", "");
}
}
84 changes: 84 additions & 0 deletions src/03-schema-hook-2/DataValidatorHook.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.26;

import { Ownable } from "@openzeppelin/contracts/access/Ownable.sol";
import { IERC20 } from "@openzeppelin/contracts/interfaces/IERC20.sol";
import { ISP } from "@ethsign/sign-protocol-evm/src/interfaces/ISP.sol";
import { ISPHook } from "@ethsign/sign-protocol-evm/src/interfaces/ISPHook.sol";
import { Attestation } from "@ethsign/sign-protocol-evm/src/models/Attestation.sol";

// @dev This contract manages attestation data validation logic.
contract DataValidator is Ownable {
uint256 public threshold;

error NumberBelowThreshold();

constructor() Ownable(_msgSender()) { }

function setThreshold(uint256 threshold_) external onlyOwner {
threshold = threshold_;
}

function _checkThreshold(uint256 number) internal view {
// solhint-disable-next-line custom-errors
require(number >= threshold, NumberBelowThreshold());
}
}

// @dev This contract implements the actual schema hook.
contract DataValidatorHook is ISPHook, DataValidator {
error UnsupportedOperation();

function didReceiveAttestation(
address, // attester
uint64, // schemaId
uint64 attestationId,
bytes calldata // extraData
)
external
payable
{
Attestation memory attestation = ISP(_msgSender()).getAttestation(attestationId);
_checkThreshold(abi.decode(attestation.data, (uint256)));
}

function didReceiveAttestation(
address, // attester
uint64, // schemaId
uint64, // attestationId
IERC20, // resolverFeeERC20Token
uint256, // resolverFeeERC20Amount
bytes calldata // extraData
)
external
pure
{
revert UnsupportedOperation();
}

function didReceiveRevocation(
address, // attester
uint64, // schemaId
uint64, // attestationId
bytes calldata // extraData
)
external
payable
{
revert UnsupportedOperation();
}

function didReceiveRevocation(
address, // attester
uint64, // schemaId
uint64, // attestationId
IERC20, // resolverFeeERC20Token
uint256, // resolverFeeERC20Amount
bytes calldata // extraData
)
external
pure
{
revert UnsupportedOperation();
}
}

0 comments on commit b9e5014

Please sign in to comment.