Skip to content

Commit

Permalink
update submitResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
daveroga committed Jan 8, 2025
1 parent c6077eb commit d31f2ca
Show file tree
Hide file tree
Showing 9 changed files with 152 additions and 300 deletions.
10 changes: 4 additions & 6 deletions contracts/interfaces/IVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -158,16 +158,14 @@ interface IVerifier {

/**
* @dev Submits an array of responses and updates proofs status
* @param authResponses The list of auth responses including auth type and proof
* @param singleResponses The list of responses including request ID, proof and metadata for single requests
* @param groupedResponses The list of responses including request ID, proof and metadata for grouped requests
* @param authResponse Auth response including auth type and proof
* @param responses The list of responses including request ID, proof and metadata for requests
* @param crossChainProofs The list of cross chain proofs from universal resolver (oracle). This
* includes identities and global states.
*/
function submitResponse(
AuthResponse[] memory authResponses,
Response[] memory singleResponses,
GroupedResponses[] memory groupedResponses,
AuthResponse memory authResponse,
Response[] memory responses,
bytes memory crossChainProofs
) external;

Expand Down
24 changes: 10 additions & 14 deletions contracts/test-helpers/EmbeddedVerifierWrapper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,33 +8,29 @@ import {IVerifier} from "../interfaces/IVerifier.sol";

contract EmbeddedVerifierWrapper is EmbeddedVerifier {
event BeforeProofSubmit(

Check failure on line 10 in contracts/test-helpers/EmbeddedVerifierWrapper.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ⏎········AuthResponse·authResponse,⏎········Response[]·responses⏎···· with AuthResponse·authResponse,·Response[]·responses
AuthResponse[] authResponses,
Response[] singleResponses,
GroupedResponses[] groupedResponses
AuthResponse authResponse,
Response[] responses
);
event AfterProofSubmit(

Check failure on line 14 in contracts/test-helpers/EmbeddedVerifierWrapper.sol

View workflow job for this annotation

GitHub Actions / solhint

Replace ⏎········AuthResponse·authResponse,⏎········Response[]·responses⏎···· with AuthResponse·authResponse,·Response[]·responses
AuthResponse[] authResponses,
Response[] singleResponses,
GroupedResponses[] groupedResponses
AuthResponse authResponse,
Response[] responses
);

function initialize(address initialOwner, IState state) public initializer {
super.__EmbeddedVerifier_init(initialOwner, state);
}

function _beforeProofSubmit(
AuthResponse[] memory authResponses,
Response[] memory singleResponses,
GroupedResponses[] memory groupedResponses
AuthResponse memory authResponse,
Response[] memory responses
) internal override {
emit BeforeProofSubmit(authResponses, singleResponses, groupedResponses);
emit BeforeProofSubmit(authResponse, responses);
}

function _afterProofSubmit(
AuthResponse[] memory authResponses,
Response[] memory singleResponses,
GroupedResponses[] memory groupedResponses
AuthResponse memory authResponse,
Response[] memory responses
) internal override {
emit AfterProofSubmit(authResponses, singleResponses, groupedResponses);
emit AfterProofSubmit(authResponse, responses);
}
}
36 changes: 15 additions & 21 deletions contracts/verifiers/EmbeddedVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,44 +27,38 @@ abstract contract EmbeddedVerifier is Ownable2StepUpgradeable, Verifier {

/**
* @dev Submits an array of responses and updates proofs status
* @param authResponses The list of auth responses including auth type and proof
* @param singleResponses The list of responses including request ID, proof and metadata for single requests
* @param groupedResponses The list of responses including request ID, proof and metadata for grouped requests
* @param authResponse Auth response including auth type and proof
* @param responses The list of responses including request ID, proof and metadata for requests
* @param crossChainProofs The list of cross chain proofs from universal resolver (oracle). This
* includes identities and global states.
*/
function submitResponse(
AuthResponse[] memory authResponses,
Response[] memory singleResponses,
GroupedResponses[] memory groupedResponses,
AuthResponse memory authResponse,
Response[] memory responses,
bytes memory crossChainProofs
) public virtual override {
_beforeProofSubmit(authResponses, singleResponses, groupedResponses);
super.submitResponse(authResponses, singleResponses, groupedResponses, crossChainProofs);
_afterProofSubmit(authResponses, singleResponses, groupedResponses);
_beforeProofSubmit(authResponse, responses);
super.submitResponse(authResponse, responses, crossChainProofs);
_afterProofSubmit(authResponse, responses);
}

/**
* @dev Hook that is called before any proof response submit
* @param authResponses The list of auth responses including auth type and proof
* @param singleResponses The list of responses including request ID, proof and metadata for single requests
* @param groupedResponses The list of responses including request ID, proof and metadata for grouped requests
* @param authResponse Auth response including auth type and proof
* @param responses The list of responses including request ID, proof and metadata for requests
*/
function _beforeProofSubmit(
AuthResponse[] memory authResponses,
Response[] memory singleResponses,
GroupedResponses[] memory groupedResponses
AuthResponse memory authResponse,
Response[] memory responses
) internal virtual {}

/**
* @dev Hook that is called after any proof response submit
* @param authResponses The list of auth responses including auth type and proof
* @param singleResponses The list of responses including request ID, proof and metadata for single requests
* @param groupedResponses The list of responses including request ID, proof and metadata for grouped requests
* @param authResponse The list of auth responses including auth type and proof
* @param responses The list of responses including request ID, proof and metadata for requests
*/
function _afterProofSubmit(
AuthResponse[] memory authResponses,
Response[] memory singleResponses,
GroupedResponses[] memory groupedResponses
AuthResponse memory authResponse,
Response[] memory responses
) internal virtual {}
}
26 changes: 8 additions & 18 deletions contracts/verifiers/UniversalVerifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -129,31 +129,21 @@ contract UniversalVerifier is

/**
* @dev Submits an array of responses and updates proofs status
* @param authResponses The list of auth responses including auth type and proof
* @param singleResponses The list of responses including request ID, proof and metadata for single requests
* @param groupedResponses The list of responses including request ID, proof and metadata for grouped requests
* @param authResponse Auth responses including auth type and proof
* @param responses The list of responses including request ID, proof and metadata for requests
* @param crossChainProofs The list of cross chain proofs from universal resolver (oracle). This
* includes identities and global states.
*/
function submitResponse(
AuthResponse[] memory authResponses,
Response[] memory singleResponses,
GroupedResponses[] memory groupedResponses,
AuthResponse memory authResponse,
Response[] memory responses,
bytes memory crossChainProofs
) public override {
super.submitResponse(authResponses, singleResponses, groupedResponses, crossChainProofs);
for (uint256 i = 0; i < authResponses.length; i++) {
emit AuthResponseSubmitted(authResponses[i].authType, _msgSender());
}

for (uint256 i = 0; i < singleResponses.length; i++) {
emit ResponseSubmitted(singleResponses[i].requestId, _msgSender());
}
super.submitResponse(authResponse, responses, crossChainProofs);
emit AuthResponseSubmitted(authResponse.authType, _msgSender());

for (uint256 i = 0; i < groupedResponses.length; i++) {
for (uint256 j = 0; j < groupedResponses[i].responses.length; j++) {
emit ResponseSubmitted(groupedResponses[i].responses[j].requestId, _msgSender());
}
for (uint256 i = 0; i < responses.length; i++) {
emit ResponseSubmitted(responses[i].requestId, _msgSender());
}
}

Expand Down
63 changes: 11 additions & 52 deletions contracts/verifiers/Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ error AuthTypeNotFound(string authType);
error AuthTypeAlreadyExists(string authType);
error ValidatorNotWhitelisted(address validator);
error RequestIsAlreadyGrouped(uint256 requestId);
error AuthResponsesExactlyOneRequired();
error LinkIDNotTheSameForGroupedRequests(uint256 requestLinkID, uint256 requestLinkIDToCompare);
error UserIDNotFound(uint256 userID);
error UserIDNotLinkedToAddress(uint256 userID, address userAddress);
Expand Down Expand Up @@ -329,16 +328,14 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {

/**
* @dev Submits an array of responses and updates proofs status
* @param authResponses The list of auth responses including auth type and proof
* @param singleResponses The list of responses including request ID, proof and metadata for single requests
* @param groupedResponses The list of responses including request ID, proof and metadata for grouped requests
* @param authResponse Auth response including auth type and proof
* @param responses The list of responses including request ID, proof and metadata for requests
* @param crossChainProofs The list of cross chain proofs from universal resolver (oracle). This
* includes identities and global states.
*/
function submitResponse(
AuthResponse[] memory authResponses,
Response[] memory singleResponses,
GroupedResponses[] memory groupedResponses,
AuthResponse memory authResponse,
Response[] memory responses,
bytes memory crossChainProofs
) public virtual {
VerifierStorage storage $ = _getVerifierStorage();
Expand All @@ -349,17 +346,11 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {

// TODO: Get userID from responses that has userID informed (LinkedMultiquery doesn't have userID)

// 2. Process auth response first
if (authResponses.length != 1) {
// TODO: Check if it's already authenticated or it's an ethereum identity
revert AuthResponsesExactlyOneRequired();
}

uint256 userIDFromReponse;
AuthTypeData storage authTypeData = $._authMethods[authResponses[0].authType];
AuthTypeData storage authTypeData = $._authMethods[authResponse.authType];
// Authenticate user
IAuthValidator.ResponseField[] memory authSignals = authTypeData.validator.verify(
authResponses[0].proof,
authResponse.proof,
authTypeData.params,
sender,
$._state
Expand All @@ -374,24 +365,24 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {

// For some reason the auth request doesn't return the userID in the response
if (userIDFromReponse != 0) {
$.writeAuthProofResults(authResponses[0].authType, userIDFromReponse, authSignals);
$.writeAuthProofResults(authResponse.authType, userIDFromReponse, authSignals);
// Link userID and user address
$._user_address_to_id[sender] = userIDFromReponse;
$._id_to_user_address[userIDFromReponse] = sender;
$._user_auth_timestamp[userIDFromReponse][sender] = block.timestamp;
}

// 3. Get userID from latest auth response processed in this submitResponse or before
// 2. Get userID from latest auth response processed in this submitResponse or before
uint256 userID = $._user_address_to_id[sender];

if (userID == 0) {
revert("The user is not authenticated");
}

// 4. Verify all the single responses, write proof results (under the userID key from the auth of the user),
// 3. Verify all the responses, write proof results (under the userID key from the auth of the user),
// emit events (existing logic)
for (uint256 i = 0; i < singleResponses.length; i++) {
IVerifier.Response memory response = singleResponses[i];
for (uint256 i = 0; i < responses.length; i++) {
IVerifier.Response memory response = responses[i];
IVerifier.RequestData storage request = _getRequestIfCanBeVerified(response.requestId);

IRequestValidator.ResponseField[] memory signals = request.validator.verify(
Expand All @@ -407,10 +398,6 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {
revert("Metadata not supported yet");
}
}

// 5. Verify all the grouped responses, write proof results (under the userID key from the auth of the user),
// emit events (existing logic)
_verifyGroupedResponses(groupedResponses, userID, sender);
}

/**
Expand All @@ -434,34 +421,6 @@ abstract contract Verifier is IVerifier, ContextUpgradeable {
});
}

function _verifyGroupedResponses(
IVerifier.GroupedResponses[] memory groupedResponses,
uint256 userID,
address sender
) internal {
VerifierStorage storage $ = _getVerifierStorage();

for (uint256 i = 0; i < groupedResponses.length; i++) {
for (uint256 j = 0; j < groupedResponses[i].responses.length; j++) {
IVerifier.Response memory response = groupedResponses[i].responses[j];
IVerifier.RequestData storage request = $._requests[response.requestId];

IRequestValidator.ResponseField[] memory signals = request.validator.verify(
response.proof,
request.params,
sender,
$._state
);

$.writeProofResults(response.requestId, userID, signals);

if (response.metadata.length > 0) {
revert("Metadata not supported yet");
}
}
}
}

/**
* @dev Sets an auth type
* @param authType The auth type to add
Expand Down
51 changes: 16 additions & 35 deletions test/verifier/universal-verifier-multi-query.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,20 +246,17 @@ describe("Universal Verifier Multi-request", function () {
const metadatas = "0x";

const tx = await verifier.submitResponse(
[
{
authType: authType,
proof,
},
],
{
authType: authType,
proof,
},
[
{
requestId,
proof,
metadata: metadatas,
},
],
[],
crossChainProofs,
);

Expand Down Expand Up @@ -379,21 +376,13 @@ describe("Universal Verifier Multi-request", function () {
const metadatas = "0x";

const tx = await verifier.submitResponse(
{
authType: authType,
proof,
},
[
{
authType: authType,
proof,
},
],
[],
[
{
groupId: groupId,
responses: [
{ requestId: requestId2, proof, metadata: metadatas },
{ requestId: requestId3, proof, metadata: metadatas },
],
},
{ requestId: requestId2, proof, metadata: metadatas },
{ requestId: requestId3, proof, metadata: metadatas },
],
crossChainProofs,
);
Expand Down Expand Up @@ -487,21 +476,13 @@ describe("Universal Verifier Multi-request", function () {

const metadatas = "0x";
const tx = await verifier.submitResponse(
{
authType: authType,
proof,
},
[
{
authType: authType,
proof,
},
],
[],
[
{
groupId: groupId,
responses: [
{ requestId: requestId2, proof, metadata: metadatas },
{ requestId: requestId3, proof, metadata: metadatas },
],
},
{ requestId: requestId2, proof, metadata: metadatas },
{ requestId: requestId3, proof, metadata: metadatas },
],
crossChainProofs,
);
Expand Down
Loading

0 comments on commit d31f2ca

Please sign in to comment.