@rarimo/zkp-gen-iden3
TypeScript icon, indicating that this package has built-in type declarations

2.0.0-rc.34 • Public • Published

@rarimo/zkp-gen-iden3

Tools that generate zero-knowledge proofs for the Iden3 protocol.

version (scoped package) types tree-shaking checks

Getting Started

Installing

yarn add @rarimo/zkp-gen-iden3

Usage

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)
}

For more specific use cases, e.g. when we need to ensure of states actuality:

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 ...
}

Known Issues

Read here

License

This project is licensed under the MIT License - see the LICENSE.md file for details

Readme

Keywords

none

Package Sidebar

Install

npm i @rarimo/zkp-gen-iden3

Weekly Downloads

770

Version

2.0.0-rc.34

License

MIT

Unpacked Size

314 MB

Total Files

75

Last publish

Collaborators

  • lukachi
  • ihor.diachenko
  • electr1xx
  • arvolear
  • volendi
  • semen_rarify