Skip to content

Commit

Permalink
Merge pull request #10 from 0xPolygonID/fix/onchain-tutorial
Browse files Browse the repository at this point in the history
update according to the State contract changes
  • Loading branch information
vmidyllic authored May 18, 2023
2 parents 5183cdb + 2c2c964 commit 2cbfd65
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 33 deletions.
6 changes: 3 additions & 3 deletions on-chain-verification/contracts/ERC20Verifier.sol
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ contract ERC20Verifier is ERC20, ZKPVerifier {
"proof can not be submitted more than once"
);

// address didn't get airdrop tokens
uint256 id = inputs[validator.getChallengeInputIndex()];
// get user id
uint256 id = inputs[1];
// additional check didn't get airdrop tokens before
if (idToAddress[id] == address(0)) {
if (idToAddress[id] == address(0) && addressToId[_msgSender()] == 0 ) {
super._mint(_msgSender(), TOKEN_AMOUNT_FOR_AIRDROP_PER_ID);
addressToId[_msgSender()] = id;
idToAddress[id] = _msgSender();
Expand Down
68 changes: 49 additions & 19 deletions on-chain-verification/contracts/interfaces/IState.sol
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
pragma solidity ^0.8.0;
// SPDX-License-Identifier: GPL-3.0
pragma solidity 0.8.16;

uint256 constant MAX_SMT_DEPTH = 64;

interface IState {
/**
* @dev Struct for public interfaces to represent a state information.
* @param id identity.
* @param id An identity.
* @param state A state.
* @param replacedByState A state, which replaced this state for the identity.
* @param createdAtTimestamp A time when the state was created.
* @param replacedAtTimestamp A time when the state was replaced by the next identity state.
Expand All @@ -19,16 +23,17 @@ interface IState {
uint256 createdAtBlock;
uint256 replacedAtBlock;
}

/**
* @dev Struct for public interfaces to represent SMT root info.
* @param root This SMT root.
* @dev Struct for public interfaces to represent GIST root information.
* @param root This GIST root.
* @param replacedByRoot A root, which replaced this root.
* @param createdAtTimestamp A time, when the root was saved to blockchain.
* @param replacedAtTimestamp A time, when the root was replaced by the next root in blockchain.
* @param createdAtBlock A number of block, when the root was saved to blockchain.
* @param replacedAtBlock A number of block, when the root was replaced by the next root in blockchain.
*/
struct RootInfo {
struct GistRootInfo {
uint256 root;
uint256 replacedByRoot;
uint256 createdAtTimestamp;
Expand All @@ -37,25 +42,50 @@ interface IState {
uint256 replacedAtBlock;
}

function getStateInfoById(
uint256 id
) external view returns (StateInfo memory);
/**
* @dev Struct for public interfaces to represent GIST proof information.
* @param root This GIST root.
* @param existence A flag, which shows if the leaf index exists in the GIST.
* @param siblings An array of GIST sibling node hashes.
* @param index An index of the leaf in the GIST.
* @param value A value of the leaf in the GIST.
* @param auxExistence A flag, which shows if the auxiliary leaf exists in the GIST.
* @param auxIndex An index of the auxiliary leaf in the GIST.
* @param auxValue An value of the auxiliary leaf in the GIST.
*/
struct GistProof {
uint256 root;
bool existence;
uint256[MAX_SMT_DEPTH] siblings;
uint256 index;
uint256 value;
bool auxExistence;
uint256 auxIndex;
uint256 auxValue;
}

/**
* @dev Retrieve the specific GIST root information.
* @param root GIST root
* @return The GIST root info
* @dev Retrieve last state information of specific id.
* @param id An identity.
* @return The state info.
*/
function getGISTRootInfo(
uint256 root
) external view returns (RootInfo memory);
function getStateInfoById(uint256 id) external view returns (StateInfo memory);

/**
* @dev Retrieve state information by state.
* @param state A state
* @return The state info
* @dev Retrieve state information by id and state.
* @param id An identity.
* @param state A state.
* @return The state info.
*/
function getStateInfoByState(
function getStateInfoByIdAndState(
uint256 id,
uint256 state
) external view returns (StateInfo memory);
}

/**
* @dev Retrieve the specific GIST root information.
* @param root GIST root.
* @return The GIST root info.
*/
function getGISTRootInfo(uint256 root) external view returns (GistRootInfo memory);
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,16 +56,16 @@ contract CredentialAtomicQueryMTPValidator is OwnableUpgradeable, ICircuitValida
uint256 issuerClaimIdenState = inputs[7];
uint256 issuerClaimNonRevState = inputs[9];

IState.RootInfo memory rootInfo = state.getGISTRootInfo(gistRoot);
IState.GistRootInfo memory rootInfo = state.getGISTRootInfo(gistRoot);

require(rootInfo.root == gistRoot, "Gist root state isn't in state contract");

// 2. Issuer state must be registered in state contracts or be genesis
bool isIssuerStateGenesis = GenesisUtils.isGenesisState(issuerId, issuerClaimIdenState);

if (!isIssuerStateGenesis) {
IState.StateInfo memory issuerStateInfo = state.getStateInfoByState(
issuerClaimIdenState
IState.StateInfo memory issuerStateInfo = state.getStateInfoByIdAndState(
issuerId, issuerClaimIdenState
);
require(issuerId == issuerStateInfo.id, "Issuer state doesn't exist in state contract");
}
Expand All @@ -80,9 +80,9 @@ contract CredentialAtomicQueryMTPValidator is OwnableUpgradeable, ICircuitValida
} else {
// The non-empty state is returned, and it's not equal to the state that the user has provided.
if (issuerClaimNonRevStateInfo.state != issuerClaimNonRevState) {
// Get the time of the latest state and compare it to the transition time of state provided by the user.
// Get the time of the latest state and compare it to the transition time of state provided by the user.
IState.StateInfo memory issuerClaimNonRevLatestStateInfo = state
.getStateInfoByState(issuerClaimNonRevState);
.getStateInfoByIdAndState(issuerId,issuerClaimNonRevState);

if (
issuerClaimNonRevLatestStateInfo.id == 0 ||
Expand All @@ -106,4 +106,4 @@ contract CredentialAtomicQueryMTPValidator is OwnableUpgradeable, ICircuitValida

return (true);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -54,16 +54,16 @@ contract CredentialAtomicQuerySigValidator is OwnableUpgradeable, ICircuitValida
uint256 issuerId = inputs[7];
uint256 issuerClaimNonRevState = inputs[9];

IState.RootInfo memory rootInfo = state.getGISTRootInfo(gistRoot);
IState.GistRootInfo memory rootInfo = state.getGISTRootInfo(gistRoot);

require(rootInfo.root == gistRoot, "Gist root state isn't in state contract");

// 2. Issuer state must be registered in state contracts or be genesis
bool isIssuerStateGenesis = GenesisUtils.isGenesisState(issuerId, issuerClaimAuthState);

if (!isIssuerStateGenesis) {
IState.StateInfo memory issuerStateInfo = state.getStateInfoByState(
issuerClaimAuthState
IState.StateInfo memory issuerStateInfo = state.getStateInfoByIdAndState(
issuerId, issuerClaimAuthState
);
require(issuerId == issuerStateInfo.id, "Issuer state doesn't exist in state contract");
}
Expand All @@ -80,7 +80,7 @@ contract CredentialAtomicQuerySigValidator is OwnableUpgradeable, ICircuitValida
if (issuerClaimNonRevStateInfo.state != issuerClaimNonRevState) {
// Get the time of the latest state and compare it to the transition time of state provided by the user.
IState.StateInfo memory issuerClaimNonRevLatestStateInfo = state
.getStateInfoByState(issuerClaimNonRevState);
.getStateInfoByIdAndState(issuerId, issuerClaimNonRevState);

if (
issuerClaimNonRevLatestStateInfo.id == 0 ||
Expand Down
1 change: 0 additions & 1 deletion on-chain-verification/contracts/verifiers/ZKPVerifier.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "../lib/GenesisUtils.sol";
import "../lib/SpongePoseidon.sol";
Expand Down

0 comments on commit 2cbfd65

Please sign in to comment.