From 2258cb2cd11cd03c07efdd617c559a5ad5c5de4e Mon Sep 17 00:00:00 2001 From: motemotech Date: Sat, 11 Jan 2025 04:00:31 +0900 Subject: [PATCH] remove commentout in contract --- contracts/contracts/OpenPassportVerifier.sol | 496 +++++++++--------- .../register/OpenPassportRegister.sol | 210 ++++---- .../register/OpenPassportRegistry.sol | 40 +- .../contracts/verifiers/GenericVerifier.sol | 126 ++--- 4 files changed, 436 insertions(+), 436 deletions(-) diff --git a/contracts/contracts/OpenPassportVerifier.sol b/contracts/contracts/OpenPassportVerifier.sol index efcba892..d1512720 100644 --- a/contracts/contracts/OpenPassportVerifier.sol +++ b/contracts/contracts/OpenPassportVerifier.sol @@ -1,250 +1,250 @@ -// // SPDX-License-Identifier: MIT -// pragma solidity ^0.8.28; - -// import {IGenericVerifier} from "./interfaces/IGenericVerifier.sol"; -// import {IOpenPassportVerifier} from "./interfaces/IOpenPassportVerifier.sol"; -// import "./constants/OpenPassportConstants.sol"; -// import "./libraries/OpenPassportFormatter.sol"; -// import "./libraries/Dg1Disclosure.sol"; -// import "./libraries/OpenPassportAttributeSelector.sol"; -// import "./libraries/OpenPassportAttributeHandler.sol"; - -// contract OpenPassportVerifier is IOpenPassportVerifier { - -// IGenericVerifier public genericVerifier; - -// constructor (address _genericVerifier) { -// genericVerifier = IGenericVerifier(_genericVerifier); -// } - -// function discloseIssuingState( -// OpenPassportAttestation memory attestation -// ) public returns (string memory) { -// uint256 selector = OpenPassportAttributeSelector.ISSUING_STATE_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit IssuingStateDisclosed(attrs.issuingState); -// return attrs.issuingState; -// } - -// function discloseName( -// OpenPassportAttestation memory attestation -// ) public returns (string memory) { -// uint256 selector = OpenPassportAttributeSelector.NAME_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit NameDisclosed(attrs.name); -// return attrs.name; -// } - -// function disclosePassportNumber( -// OpenPassportAttestation memory attestation -// ) public returns (string memory) { -// uint256 selector = OpenPassportAttributeSelector.PASSPORT_NUMBER_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit PassportNumberDisclosed(attrs.passportNumber); -// return attrs.passportNumber; -// } - -// function discloseNationality( -// OpenPassportAttestation memory attestation -// ) public returns (string memory) { -// uint256 selector = OpenPassportAttributeSelector.NATIONALITY_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit NationalityDisclosed(attrs.nationality); -// return attrs.nationality; -// } - -// function discloseDateOfBirth( -// OpenPassportAttestation memory attestation -// ) public returns (string memory) { -// uint256 selector = OpenPassportAttributeSelector.DATE_OF_BIRTH_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit DateOfBirthDisclosed(attrs.dateOfBirth); -// return attrs.dateOfBirth; -// } - -// function discloseGender( -// OpenPassportAttestation memory attestation -// ) public returns (string memory) { -// uint256 selector = OpenPassportAttributeSelector.GENDER_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit GenderDisclosed(attrs.gender); -// return attrs.gender; -// } - -// function discloseExpiryDate( -// OpenPassportAttestation memory attestation -// ) public returns (string memory) { -// uint256 selector = OpenPassportAttributeSelector.EXPIRY_DATE_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit ExpiryDateDisclosed(attrs.expiryDate); -// return attrs.expiryDate; -// } - -// function discloseOlderThan( -// OpenPassportAttestation memory attestation -// ) public returns (uint256) { -// uint256 selector = OpenPassportAttributeSelector.OLDER_THAN_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit OlderThanDisclosed(attrs.olderThan); -// return attrs.olderThan; -// } - -// function discloseOfacResult( -// OpenPassportAttestation memory attestation -// ) public returns (bool) { -// uint256 selector = OpenPassportAttributeSelector.OFAC_RESULT_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit OfacResultDisclosed(attrs.ofacResult); -// return attrs.ofacResult; -// } - -// function discloseForbiddenCountries( -// OpenPassportAttestation memory attestation -// ) public returns (bytes3[20] memory) { -// uint256 selector = OpenPassportAttributeSelector.FORBIDDEN_COUNTRIES_SELECTOR; -// PassportAttributes memory attrs = verifyAndDiscloseAttributes( -// attestation, -// selector -// ); -// emit ForbiddenCountriesDisclosed(attrs.forbiddenCountries); -// return attrs.forbiddenCountries; -// } - -// function verifyAndDiscloseAttributes( -// OpenPassportAttestation memory attestation, -// uint256 attributeSelector -// ) public returns (PassportAttributes memory) { -// verify(attestation); -// uint[3] memory revealedData_packed; -// for (uint256 i = 0; i < 3; i++) { -// if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.RSA) { -// revealedData_packed[i] = attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_REVEALED_DATA_PACKED_INDEX + i]; -// } else if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.ECDSA) { -// revealedData_packed[i] = attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_REVEALED_DATA_PACKED_INDEX + i]; -// } else { -// revert INVALID_SIGNATURE_TYPE(); -// } -// } -// bytes memory charcodes = OpenPassportFormatter.fieldElementsToBytes( -// revealedData_packed -// ); - -// PassportAttributes memory attrs; - -// if ((attributeSelector & OpenPassportAttributeSelector.ISSUING_STATE_SELECTOR) != 0) { -// attrs.issuingState = Dg1Disclosure.getIssuingState(charcodes); -// } - -// if ((attributeSelector & OpenPassportAttributeSelector.NAME_SELECTOR) != 0) { -// attrs.name = Dg1Disclosure.getName(charcodes); -// } - -// if ((attributeSelector & OpenPassportAttributeSelector.PASSPORT_NUMBER_SELECTOR) != 0) { -// attrs.passportNumber = Dg1Disclosure.getPassportNumber(charcodes); -// } - -// if ((attributeSelector & OpenPassportAttributeSelector.NATIONALITY_SELECTOR) != 0) { -// attrs.nationality = Dg1Disclosure.getNationality(charcodes); -// } - -// if ((attributeSelector & OpenPassportAttributeSelector.DATE_OF_BIRTH_SELECTOR) != 0) { -// attrs.dateOfBirth = Dg1Disclosure.getDateOfBirth(charcodes); -// } - -// if ((attributeSelector & OpenPassportAttributeSelector.GENDER_SELECTOR) != 0) { -// attrs.gender = Dg1Disclosure.getGender(charcodes); -// } - -// if ((attributeSelector & OpenPassportAttributeSelector.EXPIRY_DATE_SELECTOR) != 0) { -// attrs.expiryDate = Dg1Disclosure.getExpiryDate(charcodes); -// } +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.28; + +import {IGenericVerifier} from "./interfaces/IGenericVerifier.sol"; +import {IOpenPassportVerifier} from "./interfaces/IOpenPassportVerifier.sol"; +import "./constants/OpenPassportConstants.sol"; +import "./libraries/OpenPassportFormatter.sol"; +import "./libraries/Dg1Disclosure.sol"; +import "./libraries/OpenPassportAttributeSelector.sol"; +import "./libraries/OpenPassportAttributeHandler.sol"; + +contract OpenPassportVerifier is IOpenPassportVerifier { + + IGenericVerifier public genericVerifier; + + constructor (address _genericVerifier) { + genericVerifier = IGenericVerifier(_genericVerifier); + } + + function discloseIssuingState( + OpenPassportAttestation memory attestation + ) public returns (string memory) { + uint256 selector = OpenPassportAttributeSelector.ISSUING_STATE_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit IssuingStateDisclosed(attrs.issuingState); + return attrs.issuingState; + } + + function discloseName( + OpenPassportAttestation memory attestation + ) public returns (string memory) { + uint256 selector = OpenPassportAttributeSelector.NAME_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit NameDisclosed(attrs.name); + return attrs.name; + } + + function disclosePassportNumber( + OpenPassportAttestation memory attestation + ) public returns (string memory) { + uint256 selector = OpenPassportAttributeSelector.PASSPORT_NUMBER_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit PassportNumberDisclosed(attrs.passportNumber); + return attrs.passportNumber; + } + + function discloseNationality( + OpenPassportAttestation memory attestation + ) public returns (string memory) { + uint256 selector = OpenPassportAttributeSelector.NATIONALITY_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit NationalityDisclosed(attrs.nationality); + return attrs.nationality; + } + + function discloseDateOfBirth( + OpenPassportAttestation memory attestation + ) public returns (string memory) { + uint256 selector = OpenPassportAttributeSelector.DATE_OF_BIRTH_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit DateOfBirthDisclosed(attrs.dateOfBirth); + return attrs.dateOfBirth; + } + + function discloseGender( + OpenPassportAttestation memory attestation + ) public returns (string memory) { + uint256 selector = OpenPassportAttributeSelector.GENDER_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit GenderDisclosed(attrs.gender); + return attrs.gender; + } + + function discloseExpiryDate( + OpenPassportAttestation memory attestation + ) public returns (string memory) { + uint256 selector = OpenPassportAttributeSelector.EXPIRY_DATE_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit ExpiryDateDisclosed(attrs.expiryDate); + return attrs.expiryDate; + } + + function discloseOlderThan( + OpenPassportAttestation memory attestation + ) public returns (uint256) { + uint256 selector = OpenPassportAttributeSelector.OLDER_THAN_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit OlderThanDisclosed(attrs.olderThan); + return attrs.olderThan; + } + + function discloseOfacResult( + OpenPassportAttestation memory attestation + ) public returns (bool) { + uint256 selector = OpenPassportAttributeSelector.OFAC_RESULT_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit OfacResultDisclosed(attrs.ofacResult); + return attrs.ofacResult; + } + + function discloseForbiddenCountries( + OpenPassportAttestation memory attestation + ) public returns (bytes3[20] memory) { + uint256 selector = OpenPassportAttributeSelector.FORBIDDEN_COUNTRIES_SELECTOR; + PassportAttributes memory attrs = verifyAndDiscloseAttributes( + attestation, + selector + ); + emit ForbiddenCountriesDisclosed(attrs.forbiddenCountries); + return attrs.forbiddenCountries; + } + + function verifyAndDiscloseAttributes( + OpenPassportAttestation memory attestation, + uint256 attributeSelector + ) public returns (PassportAttributes memory) { + verify(attestation); + uint[3] memory revealedData_packed; + for (uint256 i = 0; i < 3; i++) { + if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.RSA) { + revealedData_packed[i] = attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_REVEALED_DATA_PACKED_INDEX + i]; + } else if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.ECDSA) { + revealedData_packed[i] = attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_REVEALED_DATA_PACKED_INDEX + i]; + } else { + revert INVALID_SIGNATURE_TYPE(); + } + } + bytes memory charcodes = OpenPassportFormatter.fieldElementsToBytes( + revealedData_packed + ); + + PassportAttributes memory attrs; + + if ((attributeSelector & OpenPassportAttributeSelector.ISSUING_STATE_SELECTOR) != 0) { + attrs.issuingState = Dg1Disclosure.getIssuingState(charcodes); + } + + if ((attributeSelector & OpenPassportAttributeSelector.NAME_SELECTOR) != 0) { + attrs.name = Dg1Disclosure.getName(charcodes); + } + + if ((attributeSelector & OpenPassportAttributeSelector.PASSPORT_NUMBER_SELECTOR) != 0) { + attrs.passportNumber = Dg1Disclosure.getPassportNumber(charcodes); + } + + if ((attributeSelector & OpenPassportAttributeSelector.NATIONALITY_SELECTOR) != 0) { + attrs.nationality = Dg1Disclosure.getNationality(charcodes); + } + + if ((attributeSelector & OpenPassportAttributeSelector.DATE_OF_BIRTH_SELECTOR) != 0) { + attrs.dateOfBirth = Dg1Disclosure.getDateOfBirth(charcodes); + } + + if ((attributeSelector & OpenPassportAttributeSelector.GENDER_SELECTOR) != 0) { + attrs.gender = Dg1Disclosure.getGender(charcodes); + } + + if ((attributeSelector & OpenPassportAttributeSelector.EXPIRY_DATE_SELECTOR) != 0) { + attrs.expiryDate = Dg1Disclosure.getExpiryDate(charcodes); + } -// if ((attributeSelector & OpenPassportAttributeSelector.OLDER_THAN_SELECTOR) != 0) { -// attrs.olderThan = OpenPassportAttributeHandler.extractOlderThan(attestation); -// } - -// if ((attributeSelector & OpenPassportAttributeSelector.OFAC_RESULT_SELECTOR) != 0) { -// attrs.ofacResult = OpenPassportAttributeHandler.extractOfacResult(attestation); -// } - -// if ((attributeSelector & OpenPassportAttributeSelector.FORBIDDEN_COUNTRIES_SELECTOR) != 0) { -// attrs.forbiddenCountries = OpenPassportAttributeHandler.extractForbiddenCountries(attestation); -// } - -// return attrs; -// } - -// function verify( -// OpenPassportAttestation memory attestation -// ) public returns (IGenericVerifier.ProveCircuitProof memory) { - -// uint[6] memory dateNum; -// for (uint i = 0; i < 6; i++) { -// dateNum[i] = attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_CURRENT_DATE_INDEX + i]; -// } -// uint currentTimestamp = OpenPassportFormatter.proofDateToUnixTimestamp(dateNum); - -// // Check that the current date is within a +/- 1 day range -// if( -// currentTimestamp < block.timestamp - 1 days || -// currentTimestamp > block.timestamp + 1 days -// ) { -// revert CURRENT_DATE_NOT_IN_VALID_RANGE(); -// } - -// // check blinded dcs -// bytes memory blindedDscCommitment; -// if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.RSA) { -// blindedDscCommitment = abi.encodePacked(attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_BLINDED_DSC_COMMITMENT_INDEX]); -// } else if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.ECDSA) { -// blindedDscCommitment = abi.encodePacked(attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_BLINDED_DSC_COMMITMENT_INDEX]); -// } - -// // TODO: After merged new RSA circuits and fix modal server, fix this code -// // if ( -// // keccak256(blindedDscCommitment) != -// // keccak256(abi.encodePacked(attestation.dProof.pubSignals[OpenPassportConstants.DSC_BLINDED_DSC_COMMITMENT_INDEX])) -// // ) { -// // revert UNEQUAL_BLINDED_DSC_COMMITMENT(); -// // } - -// if (!genericVerifier.verifyWithProveVerifier(attestation.proveVerifierId, attestation.pProof)) { -// revert INVALID_PROVE_PROOF(); -// } - -// if (!genericVerifier.verifyWithDscVerifier(attestation.dscVerifierId, attestation.dProof)) { -// revert INVALID_DSC_PROOF(); -// } - -// return attestation.pProof; -// } - -// } + if ((attributeSelector & OpenPassportAttributeSelector.OLDER_THAN_SELECTOR) != 0) { + attrs.olderThan = OpenPassportAttributeHandler.extractOlderThan(attestation); + } + + if ((attributeSelector & OpenPassportAttributeSelector.OFAC_RESULT_SELECTOR) != 0) { + attrs.ofacResult = OpenPassportAttributeHandler.extractOfacResult(attestation); + } + + if ((attributeSelector & OpenPassportAttributeSelector.FORBIDDEN_COUNTRIES_SELECTOR) != 0) { + attrs.forbiddenCountries = OpenPassportAttributeHandler.extractForbiddenCountries(attestation); + } + + return attrs; + } + + function verify( + OpenPassportAttestation memory attestation + ) public returns (IGenericVerifier.ProveCircuitProof memory) { + + uint[6] memory dateNum; + for (uint i = 0; i < 6; i++) { + dateNum[i] = attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_CURRENT_DATE_INDEX + i]; + } + uint currentTimestamp = OpenPassportFormatter.proofDateToUnixTimestamp(dateNum); + + // Check that the current date is within a +/- 1 day range + if( + currentTimestamp < block.timestamp - 1 days || + currentTimestamp > block.timestamp + 1 days + ) { + revert CURRENT_DATE_NOT_IN_VALID_RANGE(); + } + + // check blinded dcs + bytes memory blindedDscCommitment; + if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.RSA) { + blindedDscCommitment = abi.encodePacked(attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_BLINDED_DSC_COMMITMENT_INDEX]); + } else if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.ECDSA) { + blindedDscCommitment = abi.encodePacked(attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_BLINDED_DSC_COMMITMENT_INDEX]); + } + + // TODO: After merged new RSA circuits and fix modal server, fix this code + // if ( + // keccak256(blindedDscCommitment) != + // keccak256(abi.encodePacked(attestation.dProof.pubSignals[OpenPassportConstants.DSC_BLINDED_DSC_COMMITMENT_INDEX])) + // ) { + // revert UNEQUAL_BLINDED_DSC_COMMITMENT(); + // } + + if (!genericVerifier.verifyWithProveVerifier(attestation.proveVerifierId, attestation.pProof)) { + revert INVALID_PROVE_PROOF(); + } + + if (!genericVerifier.verifyWithDscVerifier(attestation.dscVerifierId, attestation.dProof)) { + revert INVALID_DSC_PROOF(); + } + + return attestation.pProof; + } + +} diff --git a/contracts/contracts/register/OpenPassportRegister.sol b/contracts/contracts/register/OpenPassportRegister.sol index 6f0d9222..33fa336a 100644 --- a/contracts/contracts/register/OpenPassportRegister.sol +++ b/contracts/contracts/register/OpenPassportRegister.sol @@ -1,105 +1,105 @@ -// // SPDX-License-Identifier: MIT - -// pragma solidity ^0.8.3; - -// import {IOpenPassportRegister} from "../interfaces/IOpenPassportRegister.sol"; -// import {OpenPassportRegistry} from "./OpenPassportRegistry.sol"; -// import {IOpenPassportVerifier} from "../interfaces/IOpenPassportVerifier.sol"; -// import {IGenericVerifier} from "../interfaces/IGenericVerifier.sol"; -// import {Base64} from "../libraries/Base64.sol"; -// import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; -// import "../constants/OpenPassportConstants.sol"; -// import "@openzeppelin/contracts/utils/Strings.sol"; -// import "@zk-kit/imt.sol/internal/InternalLeanIMT.sol"; - -// contract OpenPassportRegister is IOpenPassportRegister, Ownable { -// OpenPassportRegistry public immutable openPassportRegistry; -// IOpenPassportVerifier public immutable openPassportVerifier; - -// using Base64 for *; -// using Strings for uint256; - -// using InternalLeanIMT for LeanIMTData; -// LeanIMTData internal imt; - -// // poseidon("E-PASSPORT") -// bytes32 public attestationId = -// bytes32( -// 0x12d57183e0a41615471a14e5a93c87b9db757118c1d7a6a9f73106819d656f24 -// ); - -// mapping(uint256 => bool) public nullifiers; -// mapping(uint256 => bool) public merkleRootsCreated; - -// constructor(OpenPassportRegistry _openPassportRegistry, address _openPassportVerifier) Ownable(msg.sender) { -// openPassportRegistry = _openPassportRegistry; -// openPassportVerifier = IOpenPassportVerifier(_openPassportVerifier); - -// transferOwnership(msg.sender); -// } - -// function registerCommitment( -// IOpenPassportVerifier.OpenPassportAttestation memory attestation -// ) external { -// openPassportVerifier.verify(attestation); - -// if (!openPassportRegistry.checkRoot(bytes32(attestation.dProof.pubSignals[OpenPassportConstants.DSC_MERKLE_ROOT_INDEX]))) { -// revert("Register__InvalidMerkleRoot"); -// } - -// // if (nullifiers[proof.nullifier]) { -// // revert("YouAreUsingTheSameNullifierTwice"); -// // } - -// // if (bytes32(attestation.pProof.pubSignals[OpenPassportConstants.PROVE_RSA_USER_IDENTIFIER_INDEX]) != attestationId) { -// // revert("Register__InvalidAttestationId"); -// // } - -// if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.RSA) { -// nullifiers[attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_COMMITMENT_INDEX]] = true; -// _addCommitment(attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_COMMITMENT_INDEX]); -// emit ProofValidated( -// attestation.dProof.pubSignals[OpenPassportConstants.DSC_MERKLE_ROOT_INDEX], -// attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_NULLIFIER_INDEX], -// attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_COMMITMENT_INDEX] -// ); -// } else if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.ECDSA) { -// nullifiers[attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_COMMITMENT_INDEX]] = true; -// _addCommitment(attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_COMMITMENT_INDEX]); -// emit ProofValidated( -// attestation.dProof.pubSignals[OpenPassportConstants.DSC_MERKLE_ROOT_INDEX], -// attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_NULLIFIER_INDEX], -// attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_COMMITMENT_INDEX] -// ); -// } else { -// revert Register__InvalidProveProof(); -// } -// } - -// function _addCommitment(uint256 commitment) internal { -// uint256 index = getMerkleTreeSize(); -// uint256 imt_root = imt._insert(commitment); -// merkleRootsCreated[imt_root] = true; -// emit AddCommitment(index, commitment, imt_root); -// } - -// function checkRoot(uint256 root) external view returns (bool) { -// return merkleRootsCreated[root]; -// } - -// function getMerkleTreeSize() public view returns (uint256) { -// return imt.size; -// } - -// function getMerkleRoot() public view returns (uint256) { -// return imt._root(); -// } - -// function indexOf(uint commitment) public view returns (uint256) { -// return imt._indexOf(commitment); -// } - -// function devAddCommitment(uint commitment) external onlyOwner { -// _addCommitment(commitment); -// } -// } \ No newline at end of file +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.3; + +import {IOpenPassportRegister} from "../interfaces/IOpenPassportRegister.sol"; +import {OpenPassportRegistry} from "./OpenPassportRegistry.sol"; +import {IOpenPassportVerifier} from "../interfaces/IOpenPassportVerifier.sol"; +import {IGenericVerifier} from "../interfaces/IGenericVerifier.sol"; +import {Base64} from "../libraries/Base64.sol"; +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; +import "../constants/OpenPassportConstants.sol"; +import "@openzeppelin/contracts/utils/Strings.sol"; +import "@zk-kit/imt.sol/internal/InternalLeanIMT.sol"; + +contract OpenPassportRegister is IOpenPassportRegister, Ownable { + OpenPassportRegistry public immutable openPassportRegistry; + IOpenPassportVerifier public immutable openPassportVerifier; + + using Base64 for *; + using Strings for uint256; + + using InternalLeanIMT for LeanIMTData; + LeanIMTData internal imt; + + // poseidon("E-PASSPORT") + bytes32 public attestationId = + bytes32( + 0x12d57183e0a41615471a14e5a93c87b9db757118c1d7a6a9f73106819d656f24 + ); + + mapping(uint256 => bool) public nullifiers; + mapping(uint256 => bool) public merkleRootsCreated; + + constructor(OpenPassportRegistry _openPassportRegistry, address _openPassportVerifier) Ownable(msg.sender) { + openPassportRegistry = _openPassportRegistry; + openPassportVerifier = IOpenPassportVerifier(_openPassportVerifier); + + transferOwnership(msg.sender); + } + + function registerCommitment( + IOpenPassportVerifier.OpenPassportAttestation memory attestation + ) external { + openPassportVerifier.verify(attestation); + + if (!openPassportRegistry.checkRoot(bytes32(attestation.dProof.pubSignals[OpenPassportConstants.DSC_MERKLE_ROOT_INDEX]))) { + revert("Register__InvalidMerkleRoot"); + } + + // if (nullifiers[proof.nullifier]) { + // revert("YouAreUsingTheSameNullifierTwice"); + // } + + // if (bytes32(attestation.pProof.pubSignals[OpenPassportConstants.PROVE_RSA_USER_IDENTIFIER_INDEX]) != attestationId) { + // revert("Register__InvalidAttestationId"); + // } + + if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.RSA) { + nullifiers[attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_COMMITMENT_INDEX]] = true; + _addCommitment(attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_COMMITMENT_INDEX]); + emit ProofValidated( + attestation.dProof.pubSignals[OpenPassportConstants.DSC_MERKLE_ROOT_INDEX], + attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_NULLIFIER_INDEX], + attestation.pProof.pubSignalsRSA[OpenPassportConstants.PROVE_RSA_COMMITMENT_INDEX] + ); + } else if (attestation.pProof.signatureType == IGenericVerifier.SignatureType.ECDSA) { + nullifiers[attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_COMMITMENT_INDEX]] = true; + _addCommitment(attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_COMMITMENT_INDEX]); + emit ProofValidated( + attestation.dProof.pubSignals[OpenPassportConstants.DSC_MERKLE_ROOT_INDEX], + attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_NULLIFIER_INDEX], + attestation.pProof.pubSignalsECDSA[OpenPassportConstants.PROVE_ECDSA_COMMITMENT_INDEX] + ); + } else { + revert Register__InvalidProveProof(); + } + } + + function _addCommitment(uint256 commitment) internal { + uint256 index = getMerkleTreeSize(); + uint256 imt_root = imt._insert(commitment); + merkleRootsCreated[imt_root] = true; + emit AddCommitment(index, commitment, imt_root); + } + + function checkRoot(uint256 root) external view returns (bool) { + return merkleRootsCreated[root]; + } + + function getMerkleTreeSize() public view returns (uint256) { + return imt.size; + } + + function getMerkleRoot() public view returns (uint256) { + return imt._root(); + } + + function indexOf(uint commitment) public view returns (uint256) { + return imt._indexOf(commitment); + } + + function devAddCommitment(uint commitment) external onlyOwner { + _addCommitment(commitment); + } +} \ No newline at end of file diff --git a/contracts/contracts/register/OpenPassportRegistry.sol b/contracts/contracts/register/OpenPassportRegistry.sol index f3956444..a07549ca 100644 --- a/contracts/contracts/register/OpenPassportRegistry.sol +++ b/contracts/contracts/register/OpenPassportRegistry.sol @@ -1,26 +1,26 @@ -// // SPDX-License-Identifier: MIT -// pragma solidity ^0.8.18; +// SPDX-License-Identifier: MIT +pragma solidity ^0.8.18; -// import "hardhat/console.sol"; -// import "@openzeppelin/contracts/access/Ownable.sol"; +import "hardhat/console.sol"; +import "@openzeppelin/contracts/access/Ownable.sol"; -// contract OpenPassportRegistry is Ownable { -// bytes32 public merkleRoot; +contract OpenPassportRegistry is Ownable { + bytes32 public merkleRoot; -// constructor(bytes32 _merkleRoot) Ownable(msg.sender) { -// merkleRoot = _merkleRoot; -// transferOwnership(msg.sender); -// } + constructor(bytes32 _merkleRoot) Ownable(msg.sender) { + merkleRoot = _merkleRoot; + transferOwnership(msg.sender); + } -// function update(bytes32 _merkleRoot) public onlyOwner { -// merkleRoot = _merkleRoot; -// } + function update(bytes32 _merkleRoot) public onlyOwner { + merkleRoot = _merkleRoot; + } -// function checkRoot(bytes32 _merkleRoot) public view returns (bool) { -// return merkleRoot == _merkleRoot; -// } + function checkRoot(bytes32 _merkleRoot) public view returns (bool) { + return merkleRoot == _merkleRoot; + } -// function getMerkleRoot() public view returns (bytes32) { -// return merkleRoot; -// } -// } \ No newline at end of file + function getMerkleRoot() public view returns (bytes32) { + return merkleRoot; + } +} \ No newline at end of file diff --git a/contracts/contracts/verifiers/GenericVerifier.sol b/contracts/contracts/verifiers/GenericVerifier.sol index 1a170600..283594b5 100644 --- a/contracts/contracts/verifiers/GenericVerifier.sol +++ b/contracts/contracts/verifiers/GenericVerifier.sol @@ -1,71 +1,71 @@ -// //SPDX-License-Identifier: MIT -// pragma solidity ^0.8.28; +//SPDX-License-Identifier: MIT +pragma solidity ^0.8.28; -// import {IGenericVerifier, IRSAProveVerifier, IECDSAProveVerifier, IDscVerifier} from "../interfaces/IGenericVerifier.sol"; -// import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; +import {IGenericVerifier, IRSAProveVerifier, IECDSAProveVerifier, IDscVerifier} from "../interfaces/IGenericVerifier.sol"; +import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol"; -// contract GenericVerifier is IGenericVerifier, Ownable { +contract GenericVerifier is IGenericVerifier, Ownable { -// mapping (uint256 => address) public signatureTypeIdToVerifiers; + mapping (uint256 => address) public signatureTypeIdToVerifiers; -// constructor () Ownable(msg.sender) {} + constructor () Ownable(msg.sender) {} -// function verifyWithProveVerifier( -// uint256 signatureTypeId, -// ProveCircuitProof memory proof -// ) public view returns (bool) { -// bool result; -// if (proof.signatureType == SignatureType.RSA) { -// result = IRSAProveVerifier(signatureTypeIdToVerifiers[signatureTypeId]) -// .verifyProof( -// proof.a, -// proof.b, -// proof.c, -// proof.pubSignalsRSA -// ); -// } else if (proof.signatureType == SignatureType.ECDSA) { -// result = IECDSAProveVerifier(signatureTypeIdToVerifiers[signatureTypeId]) -// .verifyProof( -// proof.a, -// proof.b, -// proof.c, -// proof.pubSignalsECDSA -// ); -// } else { -// revert INVALID_SIGNATURE_TYPE(); -// } -// return result; -// } + function verifyWithProveVerifier( + uint256 signatureTypeId, + ProveCircuitProof memory proof + ) public view returns (bool) { + bool result; + if (proof.signatureType == SignatureType.RSA) { + result = IRSAProveVerifier(signatureTypeIdToVerifiers[signatureTypeId]) + .verifyProof( + proof.a, + proof.b, + proof.c, + proof.pubSignalsRSA + ); + } else if (proof.signatureType == SignatureType.ECDSA) { + result = IECDSAProveVerifier(signatureTypeIdToVerifiers[signatureTypeId]) + .verifyProof( + proof.a, + proof.b, + proof.c, + proof.pubSignalsECDSA + ); + } else { + revert INVALID_SIGNATURE_TYPE(); + } + return result; + } -// function verifyWithDscVerifier( -// uint256 signatureTypeId, -// DscCircuitProof memory proof -// ) public view returns (bool) { -// bool result = IDscVerifier(signatureTypeIdToVerifiers[signatureTypeId]) -// .verifyProof( -// proof.a, -// proof.b, -// proof.c, -// proof.pubSignals -// ); -// return result; -// } + function verifyWithDscVerifier( + uint256 signatureTypeId, + DscCircuitProof memory proof + ) public view returns (bool) { + bool result = IDscVerifier(signatureTypeIdToVerifiers[signatureTypeId]) + .verifyProof( + proof.a, + proof.b, + proof.c, + proof.pubSignals + ); + return result; + } -// // TODO: add batch update function -// function updateVerifier( -// VerificationType vType, -// uint256 signatureTypeId, -// address verifierAddress -// ) external onlyOwner { -// if (verifierAddress == address(0)) { -// revert ZERO_ADDRESS(); -// } -// if (vType == VerificationType.Prove) { -// signatureTypeIdToVerifiers[signatureTypeId] = verifierAddress; -// } -// if (vType == VerificationType.Dsc) { -// signatureTypeIdToVerifiers[signatureTypeId] = verifierAddress; -// } -// } + // TODO: add batch update function + function updateVerifier( + VerificationType vType, + uint256 signatureTypeId, + address verifierAddress + ) external onlyOwner { + if (verifierAddress == address(0)) { + revert ZERO_ADDRESS(); + } + if (vType == VerificationType.Prove) { + signatureTypeIdToVerifiers[signatureTypeId] = verifierAddress; + } + if (vType == VerificationType.Dsc) { + signatureTypeIdToVerifiers[signatureTypeId] = verifierAddress; + } + } -// } \ No newline at end of file +} \ No newline at end of file