Tools that generate zero-knowledge proofs for the Iden3 protocol.
yarn add @rarimo/zkp-gen-iden3
import { CircuitId, ZkpGen, ZkpOperators } from '@rarimo/zkp-gen-iden3';
import { type Identity } from '@rarimo/identity-gen-iden3';
import { type VerifiableCredentials } from '@rarimo/auth-zkp-iden3';
/**
* define type with variable to proof,
* verifiableCredentials response will keep this variable in credentialSubject
*/
type QueryVariableName = { isNatural: number };
const getZkProof = async (
identity: Identity,
verifiableCredentials: VerifiableCredentials,
providerAddress: string,
) => {
ZkpGen.setConfig({
// rpc url where statev2 contract is deployed
RPC_URL: 'https://matic-mumbai.chainstacklabs.com',
// statev2 contract address
STATE_V2_ADDRESS: '0x134B1BE34911E39A8397ec6289782989729807a4',
// api url of issuer svc
ISSUER_API_URL: 'http://127.0.0.1:8000/',
});
const zkProof = new ZkpGen<QueryVariableName>({
/**
* In zkp environment, every prove request should have a unique requestId
* by using this requestId, issuer svc can identify the request
*/
requestId: '1',
/**
* Identity generated by @rarimo/identity-gen-iden3
*/
identity: identity,
/**
* Verifiable credentials generated by @rarimo/auth-zkp-iden3
*/
verifiableCredentials: verifiableCredentials,
/**
* Challenge is a random string for example user metamask address without '0x'
*/
challenge: String(providerAddress).substring(2),
/**
* Query is a json object with variable to proof
*/
query: {
/**
* variableName is the name of variable to proof
*/
variableName: 'isNatural',
/**
* operator is the operator to compare the variable with value
*/
operator: ZkpOperators.Equals,
/**
* value is the value to compare with variable
*/
value: ['1'],
circuitId: CircuitId.AtomicQueryMTPV2OnChain,
issuerId: config.ISSUER_ID,
},
});
await zkProof.generateProof();
setIsNaturalProof(zkProof);
};
import { makeRarimoQuerier } from '@rarimo/client';
import { getTransitStateTxBody } from '@rarimo/shared-zkp-iden3';
import { FetcherError, HTTP_STATUS_CODES } from '@distributedlab/jac';
import { sleep } from '@rarimo/shared';
import { config } from '@/config';
const querier = makeRarimoQuerier({
apiUrl: config.RARIMO_CORE_RPC_API_URL,
});
await Promise.all([
zkProof.loadStatesDetails(querier),
zkProof.loadMerkleProof(querier, config.ISSUER_ID),
]);
await zkProof.loadOperation(
querier,
zkProof.coreStateDetails.lastUpdateOperationIndex,
);
do {
try {
await zkProof.loadOperationProof(
querier,
zkProof.coreStateDetails.lastUpdateOperationIndex,
);
} catch (error) {
if (
error instanceof FetcherError &&
error.response.status === HTTP_STATUS_CODES.BAD_REQUEST
) {
await sleep(30 * 1000);
} else {
throw error;
}
}
} while (!zkProof.operationProof);
if (zkProof.isStatesActual()) {
const transitParams = await isNaturalZkp?.loadParamsForTransitState(querier);
// use
const txBody = getTransitStateTxBody(
config.LIGHTWEIGHT_STATE_V2_CONTRACT_ADDRESS,
transitParams.newIdentitiesStatesRoot,
transitParams.gistData,
transitParams.proof,
);
// send tx with txBody ...
}
This project is licensed under the MIT License - see the LICENSE.md file for details