From f054ea1c8e9986b523e213f6b1bd3ea9cd9c59b6 Mon Sep 17 00:00:00 2001 From: tyjkawa <107705896+tyjkawa@users.noreply.github.com> Date: Thu, 20 Jun 2024 19:02:05 -0700 Subject: [PATCH 1/5] Initial Draft of Verax Reader Added Functionality --- src/core/SP_reader.sol | 214 ++++++++++++++++++++++++++++++++++++++ src/interfaces/IVerax.sol | 21 ++++ 2 files changed, 235 insertions(+) create mode 100644 src/core/SP_reader.sol create mode 100644 src/interfaces/IVerax.sol diff --git a/src/core/SP_reader.sol b/src/core/SP_reader.sol new file mode 100644 index 0000000..b0cb341 --- /dev/null +++ b/src/core/SP_reader.sol @@ -0,0 +1,214 @@ +// SPDX-License-Identifier: GNU AGPLv3 +pragma solidity ^0.8.20; + +import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol"; +import { Attestation as SPAttestation } from "../models/Attestation.sol"; // Ignoring Offchain Attestations +import { Attestation as VeraxAttestation, IVerax } from "src/interfaces/IVerax.sol"; +import { SP } from "src/core/SP.sol"; +/* + Found Information Above on Github: + - Verax's Attestation struct + - Interface I created to call a certain function -- getAttestation() +*/ + +contract Reader is OwnableUpgradeable { + /// @custom:storage-location erc7201:ethsign.SP-Reader + struct ReaderStorage { + SP signProtocol; + IVerax veraxRegistry; + } + + // keccak256(abi.encode(uint256(keccak256("ethsign.SP-Reader")) - 1)) & ~bytes32(uint256(0xff)) + bytes32 private constant ReaderStorageLocation = 0xb209ab9212fdbbb86ea9ba187ed37f12ac1d5679ecdf9a605a02f993a67d7400; + + error Invalid(); + error Incompatible(); + error DoesNotExist(); + + function _getReaderStorage() internal pure returns (ReaderStorage storage $) { + assembly { + $.slot := ReaderStorageLocation + } + } + + constructor() { + if (block.chainid != 31_337) { + _disableInitializers(); + } + } + + function initialize(address _spAddress, address _veraxAddress) public initializer { + ReaderStorage storage $ = _getReaderStorage(); + __Ownable_init(_msgSender()); + $.signProtocol = SP(_spAddress); + $.veraxRegistry = IVerax(_veraxAddress); + } + + /** + * @notice Updates the address of the Verax Registry. + * @dev Only the owner of the contract can call this function. Reverts if the provided address is the zero address. + * @param _veraxAddress The new address of the Verax Registry. + */ + function updateVeraxAddress(address _veraxAddress) external onlyOwner { + ReaderStorage storage $ = _getReaderStorage(); + if (_veraxAddress == address(0)) revert Invalid(); + $.veraxRegistry = IVerax(_veraxAddress); + } + + /** + * @notice Updates the address of the Sign Protocol Contract. + * @dev Only the owner of the contract can call this function. Reverts if the provided address is the zero address. + * @param _spAddress The new address of the Sign Protocol Contract. + */ + function updateSPAddress(address _spAddress) external onlyOwner { + ReaderStorage storage $ = _getReaderStorage(); + if (_spAddress == address(0)) revert Invalid(); + $.signProtocol = SP(_spAddress); + } + + /** + * @param id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses + * @return Sign Protocol Attestation + */ + function getAttestationMethod1(bytes32 id) external view returns (SPAttestation memory) { + ReaderStorage storage $ = _getReaderStorage(); + + uint256 spId = uint256(id); // Converting bytes32 to uint256 for comparison + + if (spId > type(uint64).max) { + // ID is out of range for Sign Protocol + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(id); + return _convertVeraxToSP_noID(vAttestation); + } else { + // ID is not out of range for Sign Protocol + SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); + if (attestation.attestTimestamp != 0) { + return attestation; + } else { + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(id); + return _convertVeraxToSP(vAttestation, uint64(spId)); + } + } + } + + /** + * @notice Helper Function: Transforms a Verax Attestation to a Sign Protocol Attestation + * @param _vAttestation - The Verax Attestation Format + * @param _id - AttestationId + * @return Sign Protocol Attestation + */ + function _convertVeraxToSP( + VeraxAttestation memory _vAttestation, + uint64 _id + ) + private + pure + returns (SPAttestation memory) + { + SPAttestation memory attestation; + + if (uint256(_vAttestation.schemaId) > type(uint64).max) { + revert Incompatible(); + } + + attestation.linkedAttestationId = _id; + attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); + attestation.attestTimestamp = _vAttestation.attestedDate; + attestation.revokeTimestamp = _vAttestation.revocationDate; + attestation.attester = _vAttestation.attester; + attestation.validUntil = _vAttestation.expirationDate; + attestation.revoked = _vAttestation.revoked; + attestation.recipients = new bytes[](1); + attestation.recipients[0] = _vAttestation.subject; + attestation.data = _vAttestation.attestationData; + // attestation.dataLocation (I do not believe they have a comparable property) + + return attestation; + } + + /** + * @notice Helper Function: Transforms a Verax Attestation to a Sign Protocol Attestation + * @param _vAttestation - The Verax Attestation Format + * @return Sign Protocol Attestation + */ + + // Question -- linkedAttestationId -- should it be 0 or some other value + + function _convertVeraxToSP_noID(VeraxAttestation memory _vAttestation) + private + pure + returns (SPAttestation memory) + { + SPAttestation memory attestation; + + if (uint256(_vAttestation.schemaId) > type(uint64).max) { + revert Incompatible(); + } + + attestation.linkedAttestationId = 0; // Can create an arbitary number as the id is not in the uint64 range + attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); + attestation.attestTimestamp = _vAttestation.attestedDate; + attestation.revokeTimestamp = _vAttestation.revocationDate; + attestation.attester = _vAttestation.attester; + attestation.validUntil = _vAttestation.expirationDate; + attestation.revoked = _vAttestation.revoked; + attestation.recipients = new bytes[](1); + attestation.recipients[0] = _vAttestation.subject; + attestation.data = _vAttestation.attestationData; + // attestation.dataLocation (I do not believe they have a comparable property) + + return attestation; + } + + /** + * @param id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses + * @return Verax Attestation + */ + function getAttestationMethod2(bytes32 id) public view returns (VeraxAttestation memory) { + ReaderStorage storage $ = _getReaderStorage(); + + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(id); + + if (vAttestation.attestedDate != 0) { + return vAttestation; + } + + uint256 spId = uint256(id); + + if (spId < type(uint64).max) { + SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); + if (attestation.attestTimestamp != 0) { + return _convertSPToVerax(attestation); + } + revert Invalid(); // If the id is not in Verax or Sign Protocol (revert) + } + revert Invalid(); // This id is not possible in Sign Protocol + } + + /** + * @notice Helper Function: Transforms a Sign Protocol Attestation to a Verax Attestation + * @param attestation - The Sign Protocol Attestation Format + * @return Verax Attestation + */ + function _convertSPToVerax(SPAttestation memory attestation) private pure returns (VeraxAttestation memory) { + VeraxAttestation memory vAttestation; + + vAttestation.attestationId = bytes32(uint256(attestation.linkedAttestationId)); + vAttestation.schemaId = bytes32(uint256(attestation.schemaId)); + vAttestation.attester = attestation.attester; + vAttestation.attestedDate = attestation.attestTimestamp; + vAttestation.expirationDate = attestation.validUntil; + vAttestation.revocationDate = attestation.revokeTimestamp; + vAttestation.revoked = attestation.revoked; + vAttestation.attestationData = attestation.data; + + vAttestation.subject = attestation.recipients[0]; + // Sign Protocol has a array while Verax only has one (I have decided to take the first) + + // vAttestation.replacedBy = 0; -- Does not exist in Sign Protocol + // vAttestation.portal = 0; -- Does not exist in Sign Protocol + // vAttestation.version = 0; -- Does not exist in Sign Protocol + + return vAttestation; + } +} diff --git a/src/interfaces/IVerax.sol b/src/interfaces/IVerax.sol new file mode 100644 index 0000000..31ec44f --- /dev/null +++ b/src/interfaces/IVerax.sol @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: GNU AGPLv3 +pragma solidity ^0.8.20; + +struct Attestation { + bytes32 attestationId; // The unique identifier of the attestation. + bytes32 schemaId; // The identifier of the schema this attestation adheres to. + bytes32 replacedBy; // Whether the attestation was replaced by a new one. + address attester; // The address issuing the attestation to the subject. + address portal; // The id of the portal that created the attestation. + uint64 attestedDate; // The date the attestation is issued. + uint64 expirationDate; // The expiration date of the attestation. + uint64 revocationDate; // The date when the attestation was revoked. + uint16 version; // Version of the registry when the attestation was created. + bool revoked; // Whether the attestation is revoked or not. + bytes subject; // The ID of the attestee, EVM address, DID, URL etc. + bytes attestationData; // The attestation data. +} + +interface IVerax { + function getAttestation(bytes32 attestationId) external view returns (Attestation memory); +} From 13aed6d1bc392a187f93cc294d15c0ce83083f70 Mon Sep 17 00:00:00 2001 From: tyjkawa <107705896+tyjkawa@users.noreply.github.com> Date: Fri, 21 Jun 2024 15:32:54 -0700 Subject: [PATCH 2/5] Removed Reverts from Added Verax Reader Functionality --- src/core/SP_reader.sol | 89 ++++++++++++++++++++++-------------------- 1 file changed, 46 insertions(+), 43 deletions(-) diff --git a/src/core/SP_reader.sol b/src/core/SP_reader.sol index b0cb341..885f6b8 100644 --- a/src/core/SP_reader.sol +++ b/src/core/SP_reader.sol @@ -67,25 +67,27 @@ contract Reader is OwnableUpgradeable { } /** - * @param id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses + * @param _id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses * @return Sign Protocol Attestation */ - function getAttestationMethod1(bytes32 id) external view returns (SPAttestation memory) { + function getAttestationMethod1(bytes32 _id) external view returns (SPAttestation memory) { ReaderStorage storage $ = _getReaderStorage(); - uint256 spId = uint256(id); // Converting bytes32 to uint256 for comparison + uint256 spId = uint256(_id); // Converting bytes32 to uint256 for comparison if (spId > type(uint64).max) { // ID is out of range for Sign Protocol - VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(id); - return _convertVeraxToSP_noID(vAttestation); + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); + return _convertVeraxToSP_nullID(vAttestation); // Attestation ID is out of range for Sign Protocol } else { - // ID is not out of range for Sign Protocol - SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); + // ID is in the range for Sign Protocol + SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); // uint256 spID -> uint64 if (attestation.attestTimestamp != 0) { + // Checks if attestation is empty return attestation; } else { - VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(id); + // If SP Protocol Attestation does not exist + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); return _convertVeraxToSP(vAttestation, uint64(spId)); } } @@ -93,7 +95,7 @@ contract Reader is OwnableUpgradeable { /** * @notice Helper Function: Transforms a Verax Attestation to a Sign Protocol Attestation - * @param _vAttestation - The Verax Attestation Format + * @param _vAttestation - The Verax Attestation * @param _id - AttestationId * @return Sign Protocol Attestation */ @@ -108,11 +110,12 @@ contract Reader is OwnableUpgradeable { SPAttestation memory attestation; if (uint256(_vAttestation.schemaId) > type(uint64).max) { - revert Incompatible(); + attestation.schemaId = 0; // Leaving it null + } else { + attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); // bytes32 -> uint256 -> uint64 } attestation.linkedAttestationId = _id; - attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); attestation.attestTimestamp = _vAttestation.attestedDate; attestation.revokeTimestamp = _vAttestation.revocationDate; attestation.attester = _vAttestation.attester; @@ -121,20 +124,17 @@ contract Reader is OwnableUpgradeable { attestation.recipients = new bytes[](1); attestation.recipients[0] = _vAttestation.subject; attestation.data = _vAttestation.attestationData; - // attestation.dataLocation (I do not believe they have a comparable property) + // attestation.dataLocation (I do not believe they have a comparable property - thus leaving as null) return attestation; } /** * @notice Helper Function: Transforms a Verax Attestation to a Sign Protocol Attestation - * @param _vAttestation - The Verax Attestation Format + * @param _vAttestation - The Verax Attestation * @return Sign Protocol Attestation */ - - // Question -- linkedAttestationId -- should it be 0 or some other value - - function _convertVeraxToSP_noID(VeraxAttestation memory _vAttestation) + function _convertVeraxToSP_nullID(VeraxAttestation memory _vAttestation) private pure returns (SPAttestation memory) @@ -142,11 +142,13 @@ contract Reader is OwnableUpgradeable { SPAttestation memory attestation; if (uint256(_vAttestation.schemaId) > type(uint64).max) { - revert Incompatible(); + attestation.schemaId = 0; // Leaving it null + } else { + attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); // bytes32 -> uint256 -> uint64 } attestation.linkedAttestationId = 0; // Can create an arbitary number as the id is not in the uint64 range - attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); + attestation.attestTimestamp = _vAttestation.attestedDate; attestation.revokeTimestamp = _vAttestation.revocationDate; attestation.attester = _vAttestation.attester; @@ -155,54 +157,55 @@ contract Reader is OwnableUpgradeable { attestation.recipients = new bytes[](1); attestation.recipients[0] = _vAttestation.subject; attestation.data = _vAttestation.attestationData; - // attestation.dataLocation (I do not believe they have a comparable property) + // attestation.dataLocation (I do not believe they have a comparable property - thus leaving as null) return attestation; } /** - * @param id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses + * @param _id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses * @return Verax Attestation */ - function getAttestationMethod2(bytes32 id) public view returns (VeraxAttestation memory) { + function getAttestationMethod2(bytes32 _id) external view returns (VeraxAttestation memory) { ReaderStorage storage $ = _getReaderStorage(); - VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(id); + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); + // Checks if the Verax Attestation Exists if (vAttestation.attestedDate != 0) { return vAttestation; - } - - uint256 spId = uint256(id); + } else { + uint256 spId = uint256(_id); - if (spId < type(uint64).max) { - SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); - if (attestation.attestTimestamp != 0) { - return _convertSPToVerax(attestation); + if (spId < type(uint64).max) { + SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); + if (attestation.attestTimestamp != 0) { + return _convertSPToVerax(attestation); + } } - revert Invalid(); // If the id is not in Verax or Sign Protocol (revert) + + return vAttestation; // Returns the empty Verax Attestation } - revert Invalid(); // This id is not possible in Sign Protocol } /** * @notice Helper Function: Transforms a Sign Protocol Attestation to a Verax Attestation - * @param attestation - The Sign Protocol Attestation Format + * @param _attestation - The Sign Protocol Attestation * @return Verax Attestation */ - function _convertSPToVerax(SPAttestation memory attestation) private pure returns (VeraxAttestation memory) { + function _convertSPToVerax(SPAttestation memory _attestation) private pure returns (VeraxAttestation memory) { VeraxAttestation memory vAttestation; - vAttestation.attestationId = bytes32(uint256(attestation.linkedAttestationId)); - vAttestation.schemaId = bytes32(uint256(attestation.schemaId)); - vAttestation.attester = attestation.attester; - vAttestation.attestedDate = attestation.attestTimestamp; - vAttestation.expirationDate = attestation.validUntil; - vAttestation.revocationDate = attestation.revokeTimestamp; - vAttestation.revoked = attestation.revoked; - vAttestation.attestationData = attestation.data; + vAttestation.attestationId = bytes32(uint256(_attestation.linkedAttestationId)); // uint64 -> uint256 -> bytes32 + vAttestation.schemaId = bytes32(uint256(_attestation.schemaId)); // uint64 -> uint256 -> bytes32 + vAttestation.attester = _attestation.attester; + vAttestation.attestedDate = _attestation.attestTimestamp; + vAttestation.expirationDate = _attestation.validUntil; + vAttestation.revocationDate = _attestation.revokeTimestamp; + vAttestation.revoked = _attestation.revoked; + vAttestation.attestationData = _attestation.data; - vAttestation.subject = attestation.recipients[0]; + vAttestation.subject = _attestation.recipients[0]; // Sign Protocol has a array while Verax only has one (I have decided to take the first) // vAttestation.replacedBy = 0; -- Does not exist in Sign Protocol From 22d7813733c550e78eeccc1a82e7ef73b3950891 Mon Sep 17 00:00:00 2001 From: tyjkawa <107705896+tyjkawa@users.noreply.github.com> Date: Fri, 28 Jun 2024 13:56:17 -0700 Subject: [PATCH 3/5] Updated Version After Fixing Corrections to Naming Conventions --- src/core/{SP_reader.sol => SPReader.sol} | 153 ++++++++--------------- 1 file changed, 53 insertions(+), 100 deletions(-) rename src/core/{SP_reader.sol => SPReader.sol} (56%) diff --git a/src/core/SP_reader.sol b/src/core/SPReader.sol similarity index 56% rename from src/core/SP_reader.sol rename to src/core/SPReader.sol index 885f6b8..f7d4cff 100644 --- a/src/core/SP_reader.sol +++ b/src/core/SPReader.sol @@ -5,29 +5,24 @@ import { OwnableUpgradeable } from "@openzeppelin/contracts-upgradeable/access/O import { Attestation as SPAttestation } from "../models/Attestation.sol"; // Ignoring Offchain Attestations import { Attestation as VeraxAttestation, IVerax } from "src/interfaces/IVerax.sol"; import { SP } from "src/core/SP.sol"; -/* - Found Information Above on Github: - - Verax's Attestation struct - - Interface I created to call a certain function -- getAttestation() -*/ - -contract Reader is OwnableUpgradeable { - /// @custom:storage-location erc7201:ethsign.SP-Reader - struct ReaderStorage { + +contract SPReader is OwnableUpgradeable { + /// @custom:storage-location erc7201:ethsign.SPReader + struct SPReaderStorage { SP signProtocol; IVerax veraxRegistry; } - // keccak256(abi.encode(uint256(keccak256("ethsign.SP-Reader")) - 1)) & ~bytes32(uint256(0xff)) - bytes32 private constant ReaderStorageLocation = 0xb209ab9212fdbbb86ea9ba187ed37f12ac1d5679ecdf9a605a02f993a67d7400; + // keccak256(abi.encode(uint256(keccak256("ethsign.SPReader")) - 1)) & ~bytes32(uint256(0xff)) + bytes32 private constant SPReaderStorageLocation = + 0x82ea65dbd7b30e84b1db8304693baa92edebefa6829498cda2f2368355a52200; - error Invalid(); error Incompatible(); error DoesNotExist(); - function _getReaderStorage() internal pure returns (ReaderStorage storage $) { + function _getSPReaderStorage() internal pure returns (SPReaderStorage storage $) { assembly { - $.slot := ReaderStorageLocation + $.slot := SPReaderStorageLocation } } @@ -37,48 +32,46 @@ contract Reader is OwnableUpgradeable { } } - function initialize(address _spAddress, address _veraxAddress) public initializer { - ReaderStorage storage $ = _getReaderStorage(); + function initialize(address spAddress, address veraxAddress) public initializer { + SPReaderStorage storage $ = _getSPReaderStorage(); __Ownable_init(_msgSender()); - $.signProtocol = SP(_spAddress); - $.veraxRegistry = IVerax(_veraxAddress); + $.signProtocol = SP(spAddress); + $.veraxRegistry = IVerax(veraxAddress); } /** * @notice Updates the address of the Verax Registry. * @dev Only the owner of the contract can call this function. Reverts if the provided address is the zero address. - * @param _veraxAddress The new address of the Verax Registry. + * @param veraxAddress The new address of the Verax Registry. */ - function updateVeraxAddress(address _veraxAddress) external onlyOwner { - ReaderStorage storage $ = _getReaderStorage(); - if (_veraxAddress == address(0)) revert Invalid(); - $.veraxRegistry = IVerax(_veraxAddress); + function updateVeraxAddress(address veraxAddress) external onlyOwner { + SPReaderStorage storage $ = _getSPReaderStorage(); + $.veraxRegistry = IVerax(veraxAddress); } /** * @notice Updates the address of the Sign Protocol Contract. * @dev Only the owner of the contract can call this function. Reverts if the provided address is the zero address. - * @param _spAddress The new address of the Sign Protocol Contract. + * @param spAddress The new address of the Sign Protocol Contract. */ - function updateSPAddress(address _spAddress) external onlyOwner { - ReaderStorage storage $ = _getReaderStorage(); - if (_spAddress == address(0)) revert Invalid(); - $.signProtocol = SP(_spAddress); + function updateSPAddress(address spAddress) external onlyOwner { + SPReaderStorage storage $ = _getSPReaderStorage(); + $.signProtocol = SP(spAddress); } /** * @param _id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses * @return Sign Protocol Attestation */ - function getAttestationMethod1(bytes32 _id) external view returns (SPAttestation memory) { - ReaderStorage storage $ = _getReaderStorage(); + function getAttestationSP(bytes32 _id) external view returns (SPAttestation memory) { + SPReaderStorage storage $ = _getSPReaderStorage(); uint256 spId = uint256(_id); // Converting bytes32 to uint256 for comparison if (spId > type(uint64).max) { // ID is out of range for Sign Protocol VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); - return _convertVeraxToSP_nullID(vAttestation); // Attestation ID is out of range for Sign Protocol + return _convertVeraxToSP(vAttestation); // Attestation ID is out of range for Sign Protocol } else { // ID is in the range for Sign Protocol SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); // uint256 spID -> uint64 @@ -88,67 +81,51 @@ contract Reader is OwnableUpgradeable { } else { // If SP Protocol Attestation does not exist VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); - return _convertVeraxToSP(vAttestation, uint64(spId)); + return _convertVeraxToSP(vAttestation); } } } /** - * @notice Helper Function: Transforms a Verax Attestation to a Sign Protocol Attestation - * @param _vAttestation - The Verax Attestation - * @param _id - AttestationId - * @return Sign Protocol Attestation + * @param _id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses + * @return Verax Attestation */ - function _convertVeraxToSP( - VeraxAttestation memory _vAttestation, - uint64 _id - ) - private - pure - returns (SPAttestation memory) - { - SPAttestation memory attestation; + function getAttestationVerax(bytes32 _id) external view returns (VeraxAttestation memory) { + SPReaderStorage storage $ = _getSPReaderStorage(); - if (uint256(_vAttestation.schemaId) > type(uint64).max) { - attestation.schemaId = 0; // Leaving it null + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); + + // Checks if the Verax Attestation Exists + if (vAttestation.attestedDate != 0) { + return vAttestation; } else { - attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); // bytes32 -> uint256 -> uint64 - } + uint256 spId = uint256(_id); - attestation.linkedAttestationId = _id; - attestation.attestTimestamp = _vAttestation.attestedDate; - attestation.revokeTimestamp = _vAttestation.revocationDate; - attestation.attester = _vAttestation.attester; - attestation.validUntil = _vAttestation.expirationDate; - attestation.revoked = _vAttestation.revoked; - attestation.recipients = new bytes[](1); - attestation.recipients[0] = _vAttestation.subject; - attestation.data = _vAttestation.attestationData; - // attestation.dataLocation (I do not believe they have a comparable property - thus leaving as null) + if (spId <= type(uint64).max) { + SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); + if (attestation.attestTimestamp != 0) { + return _convertSPToVerax(attestation); + } + } - return attestation; + return vAttestation; // Returns the empty Verax Attestation + } } /** * @notice Helper Function: Transforms a Verax Attestation to a Sign Protocol Attestation * @param _vAttestation - The Verax Attestation - * @return Sign Protocol Attestation + * @return attestation Sign Protocol Attestation */ - function _convertVeraxToSP_nullID(VeraxAttestation memory _vAttestation) + function _convertVeraxToSP(VeraxAttestation memory _vAttestation) private pure - returns (SPAttestation memory) + returns (SPAttestation memory attestation) { - SPAttestation memory attestation; - - if (uint256(_vAttestation.schemaId) > type(uint64).max) { - attestation.schemaId = 0; // Leaving it null - } else { + if (uint256(_vAttestation.schemaId) <= type(uint64).max) { attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); // bytes32 -> uint256 -> uint64 } - attestation.linkedAttestationId = 0; // Can create an arbitary number as the id is not in the uint64 range - attestation.attestTimestamp = _vAttestation.attestedDate; attestation.revokeTimestamp = _vAttestation.revocationDate; attestation.attester = _vAttestation.attester; @@ -157,45 +134,22 @@ contract Reader is OwnableUpgradeable { attestation.recipients = new bytes[](1); attestation.recipients[0] = _vAttestation.subject; attestation.data = _vAttestation.attestationData; + // attestation.linkedAttestationId = 0; // attestation.dataLocation (I do not believe they have a comparable property - thus leaving as null) return attestation; } - /** - * @param _id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses - * @return Verax Attestation - */ - function getAttestationMethod2(bytes32 _id) external view returns (VeraxAttestation memory) { - ReaderStorage storage $ = _getReaderStorage(); - - VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); - - // Checks if the Verax Attestation Exists - if (vAttestation.attestedDate != 0) { - return vAttestation; - } else { - uint256 spId = uint256(_id); - - if (spId < type(uint64).max) { - SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); - if (attestation.attestTimestamp != 0) { - return _convertSPToVerax(attestation); - } - } - - return vAttestation; // Returns the empty Verax Attestation - } - } - /** * @notice Helper Function: Transforms a Sign Protocol Attestation to a Verax Attestation * @param _attestation - The Sign Protocol Attestation * @return Verax Attestation */ - function _convertSPToVerax(SPAttestation memory _attestation) private pure returns (VeraxAttestation memory) { - VeraxAttestation memory vAttestation; - + function _convertSPToVerax(SPAttestation memory _attestation) + private + pure + returns (VeraxAttestation memory vAttestation) + { vAttestation.attestationId = bytes32(uint256(_attestation.linkedAttestationId)); // uint64 -> uint256 -> bytes32 vAttestation.schemaId = bytes32(uint256(_attestation.schemaId)); // uint64 -> uint256 -> bytes32 vAttestation.attester = _attestation.attester; @@ -204,7 +158,6 @@ contract Reader is OwnableUpgradeable { vAttestation.revocationDate = _attestation.revokeTimestamp; vAttestation.revoked = _attestation.revoked; vAttestation.attestationData = _attestation.data; - vAttestation.subject = _attestation.recipients[0]; // Sign Protocol has a array while Verax only has one (I have decided to take the first) From 4f25f674e5a71d66951d3bdbe1c9918be47e9145 Mon Sep 17 00:00:00 2001 From: tyjkawa <107705896+tyjkawa@users.noreply.github.com> Date: Fri, 28 Jun 2024 14:53:29 -0700 Subject: [PATCH 4/5] Implemented Corrections and Removed Unnecessary Comments --- src/core/SPReader.sol | 64 +++++++++++++++++++------------------------ 1 file changed, 28 insertions(+), 36 deletions(-) diff --git a/src/core/SPReader.sol b/src/core/SPReader.sol index f7d4cff..5ff68e1 100644 --- a/src/core/SPReader.sol +++ b/src/core/SPReader.sol @@ -17,9 +17,6 @@ contract SPReader is OwnableUpgradeable { bytes32 private constant SPReaderStorageLocation = 0x82ea65dbd7b30e84b1db8304693baa92edebefa6829498cda2f2368355a52200; - error Incompatible(); - error DoesNotExist(); - function _getSPReaderStorage() internal pure returns (SPReaderStorage storage $) { assembly { $.slot := SPReaderStorageLocation @@ -40,9 +37,7 @@ contract SPReader is OwnableUpgradeable { } /** - * @notice Updates the address of the Verax Registry. - * @dev Only the owner of the contract can call this function. Reverts if the provided address is the zero address. - * @param veraxAddress The new address of the Verax Registry. + * @notice Updates the address of the Verax Registry */ function updateVeraxAddress(address veraxAddress) external onlyOwner { SPReaderStorage storage $ = _getSPReaderStorage(); @@ -50,9 +45,7 @@ contract SPReader is OwnableUpgradeable { } /** - * @notice Updates the address of the Sign Protocol Contract. - * @dev Only the owner of the contract can call this function. Reverts if the provided address is the zero address. - * @param spAddress The new address of the Sign Protocol Contract. + * @notice Updates the address of the Sign Protocol Contract */ function updateSPAddress(address spAddress) external onlyOwner { SPReaderStorage storage $ = _getSPReaderStorage(); @@ -60,51 +53,50 @@ contract SPReader is OwnableUpgradeable { } /** - * @param _id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses + * @param id -- The unique id of the attestation (bytes32) * @return Sign Protocol Attestation */ - function getAttestationSP(bytes32 _id) external view returns (SPAttestation memory) { + function getAttestationSP(bytes32 id) external view returns (SPAttestation memory) { SPReaderStorage storage $ = _getSPReaderStorage(); - uint256 spId = uint256(_id); // Converting bytes32 to uint256 for comparison + uint256 spId = uint256(id); // Converting id bytes32 to spID uint256 for comparison - if (spId > type(uint64).max) { - // ID is out of range for Sign Protocol - VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); - return _convertVeraxToSP(vAttestation); // Attestation ID is out of range for Sign Protocol - } else { - // ID is in the range for Sign Protocol + // Checks if Attestation ID is in the range of SP + if (spId <= type(uint64).max) { + // Attestation ID is in range for Sign Protocol SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); // uint256 spID -> uint64 + // Checks if attestation is empty if (attestation.attestTimestamp != 0) { - // Checks if attestation is empty return attestation; - } else { - // If SP Protocol Attestation does not exist - VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); - return _convertVeraxToSP(vAttestation); } } + // Attestation ID is out of range for Sign Protocol or SP Attestation is Empty + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(id); + return _convertVeraxToSP(vAttestation); } /** - * @param _id The unique id of the attestation. Takes a bytes32 as this is the datatype Verax uses + * @param _id The unique id of the attestation (bytes32) * @return Verax Attestation */ - function getAttestationVerax(bytes32 _id) external view returns (VeraxAttestation memory) { + function getAttestationVerax(bytes32 id) external view returns (VeraxAttestation memory) { SPReaderStorage storage $ = _getSPReaderStorage(); - VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(_id); + VeraxAttestation memory vAttestation = $.veraxRegistry.getAttestation(id); // Checks if the Verax Attestation Exists if (vAttestation.attestedDate != 0) { return vAttestation; } else { - uint256 spId = uint256(_id); + // Verax Attestation does not exist + uint256 spId = uint256(id); + // Checks if Attestation ID is in the range of SP if (spId <= type(uint64).max) { SPAttestation memory attestation = $.signProtocol.getAttestation(uint64(spId)); + // Checks If Attestation is Empty if (attestation.attestTimestamp != 0) { - return _convertSPToVerax(attestation); + return _convertSPToVerax(attestation, id); } } @@ -114,8 +106,6 @@ contract SPReader is OwnableUpgradeable { /** * @notice Helper Function: Transforms a Verax Attestation to a Sign Protocol Attestation - * @param _vAttestation - The Verax Attestation - * @return attestation Sign Protocol Attestation */ function _convertVeraxToSP(VeraxAttestation memory _vAttestation) private @@ -124,7 +114,7 @@ contract SPReader is OwnableUpgradeable { { if (uint256(_vAttestation.schemaId) <= type(uint64).max) { attestation.schemaId = uint64(uint256(_vAttestation.schemaId)); // bytes32 -> uint256 -> uint64 - } + } // Otherwise null attestation.attestTimestamp = _vAttestation.attestedDate; attestation.revokeTimestamp = _vAttestation.revocationDate; @@ -134,7 +124,8 @@ contract SPReader is OwnableUpgradeable { attestation.recipients = new bytes[](1); attestation.recipients[0] = _vAttestation.subject; attestation.data = _vAttestation.attestationData; - // attestation.linkedAttestationId = 0; + + // attestation.linkedAttestationId (I do not believe they have a comparable property - thus leaving as null) // attestation.dataLocation (I do not believe they have a comparable property - thus leaving as null) return attestation; @@ -142,15 +133,16 @@ contract SPReader is OwnableUpgradeable { /** * @notice Helper Function: Transforms a Sign Protocol Attestation to a Verax Attestation - * @param _attestation - The Sign Protocol Attestation - * @return Verax Attestation */ - function _convertSPToVerax(SPAttestation memory _attestation) + function _convertSPToVerax( + SPAttestation memory _attestation, + bytes32 id + ) private pure returns (VeraxAttestation memory vAttestation) { - vAttestation.attestationId = bytes32(uint256(_attestation.linkedAttestationId)); // uint64 -> uint256 -> bytes32 + vAttestation.attestationId = id; vAttestation.schemaId = bytes32(uint256(_attestation.schemaId)); // uint64 -> uint256 -> bytes32 vAttestation.attester = _attestation.attester; vAttestation.attestedDate = _attestation.attestTimestamp; From 77324b63fbb277f9bd9310ee72c1b4e7d7397a84 Mon Sep 17 00:00:00 2001 From: tyjkawa <107705896+tyjkawa@users.noreply.github.com> Date: Fri, 28 Jun 2024 14:54:38 -0700 Subject: [PATCH 5/5] Implemented Corrections --- src/core/SPReader.sol | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/core/SPReader.sol b/src/core/SPReader.sol index 5ff68e1..51164e1 100644 --- a/src/core/SPReader.sol +++ b/src/core/SPReader.sol @@ -76,7 +76,7 @@ contract SPReader is OwnableUpgradeable { } /** - * @param _id The unique id of the attestation (bytes32) + * @param id The unique id of the attestation (bytes32) * @return Verax Attestation */ function getAttestationVerax(bytes32 id) external view returns (VeraxAttestation memory) {