// SPDX-License-Identifier: CC0-1.0 pragma solidity ^0.8.20; import {IKYAPolicyRegistry, IKYAPolicyEvaluator} from "./interfaces/IKYAPolicyRegistry.sol"; import {IKYARegistry} from "./interfaces/IKYARegistry.sol"; import {IERC165} from "./interfaces/IERC165.sol"; /// @title KYAPolicyRegistry — reference implementation (base + optional evaluator) contract KYAPolicyRegistry is IKYAPolicyRegistry, IKYAPolicyEvaluator, IERC165 { struct PolicyRec { address owner; string policyURI; bytes32 policyHash; bytes32 rulesHash; bool exists; } IKYARegistry public immutable kyaRegistry; mapping(bytes32 => PolicyRec) private _policies; mapping(bytes32 => Rule[]) private _rules; mapping(address => uint256) private _nonces; constructor(address kyaRegistry_) { kyaRegistry = IKYARegistry(kyaRegistry_); } function registerPolicy(string calldata policyURI, bytes32 policyHash) external returns (bytes32 policyId) { policyId = _register(policyURI, policyHash, bytes32(0)); } function registerPolicyWithRules(string calldata policyURI, bytes32 policyHash, Rule[] calldata allOf) external returns (bytes32 policyId) { if (allOf.length == 0) revert KYA_NoOnchainRules(bytes32(0)); for (uint256 i = 0; i < allOf.length; i++) { if (allOf[i].issuers.length == 0) revert KYA_EmptyIssuers(); // the projection is defined over ordered-level schemes with a positive threshold only (Section 7) if (allOf[i].minLevel == 0) revert KYA_ZeroThreshold(); (uint8 kind,) = kyaRegistry.resultDomain(allOf[i].schemeId); // reverts KYA_SchemeNotFound if unknown if (kind != uint8(ResultKind.ORDERED_LEVEL)) revert KYA_NotOrderedLevel(allOf[i].schemeId, kind); } policyId = _register(policyURI, policyHash, rulesHashOf(allOf)); for (uint256 i = 0; i < allOf.length; i++) { _rules[policyId].push(allOf[i]); } } function rulesHashOf(Rule[] calldata allOf) public pure returns (bytes32) { return keccak256(abi.encode(allOf)); } function getPolicy(bytes32 policyId) external view returns (address owner, string memory policyURI, bytes32 policyHash, bytes32 rulesHash) { PolicyRec storage p = _policies[policyId]; if (!p.exists) revert KYA_PolicyNotFound(policyId); return (p.owner, p.policyURI, p.policyHash, p.rulesHash); } function getRules(bytes32 policyId) external view returns (Rule[] memory) { return _rules[policyId]; } function evaluate(Subject calldata subject, bytes32 policyId) external view returns (bool) { if (!_policies[policyId].exists) revert KYA_PolicyNotFound(policyId); Rule[] storage rules = _rules[policyId]; if (rules.length == 0) revert KYA_NoOnchainRules(policyId); for (uint256 i = 0; i < rules.length; i++) { if (!kyaRegistry.check(subject, rules[i].schemeId, rules[i].minLevel, rules[i].issuers)) return false; } return true; } function _register(string calldata policyURI, bytes32 policyHash, bytes32 rulesHash) internal returns (bytes32 policyId) { uint256 nonce = _nonces[msg.sender]++; policyId = keccak256(abi.encode(msg.sender, policyHash, rulesHash, nonce)); _policies[policyId] = PolicyRec({owner: msg.sender, policyURI: policyURI, policyHash: policyHash, rulesHash: rulesHash, exists: true}); emit PolicyRegistered(policyId, msg.sender, policyURI, policyHash, rulesHash); } function supportsInterface(bytes4 interfaceId) public pure virtual returns (bool) { return interfaceId == type(IKYAPolicyRegistry).interfaceId || interfaceId == type(IKYAPolicyEvaluator).interfaceId || interfaceId == type(IERC165).interfaceId; } }