import { Synchronizer, schema } from'@unirep/core'import { getUnirepContract, Unirep } from'@unirep/contracts'import { DB, SQLiteConnector } from'anondb/node'// connect a unirep contract with the address and a providerconstunirepContract:Unirep=getUnirepContract(address, provider)// initialize a databaseconstdb:DB=awaitSQLiteConnector.create(schema,':memory:')// 1. initialize a synchronizerconstsynchronizer=newSynchronizer(db, provider, unirepContract)// 2. start listening to unriep contract eventsawaitsynchronizer.start()// 3. wait until the latest block is processedawaitsynchronizer.waitForSync()
Example: use the synchronizer to generate unirep state
import { ZkIdentity } from'@unirep/crypto'import { Synchronizer, schema } from'@unirep/core'import { getUnirepContract, Unirep } from'@unirep/contracts'import { DB, SQLiteConnector } from'anondb/node'// random generate a user identityconstidentity=newZkIdentity()// connect a unirep contract with the address and a providerconstunirepContract:Unirep=getUnirepContract(address, provider)// initialize a databaseconstdb:DB=awaitSQLiteConnector.create(schema,':memory:')// 1. initialize a user state objectconstuserState=newUserState( db, provider, unirepContract, identity)// 2. start listening to unriep contract eventsawaituserState.start()// 3. wait until the latest block is processedawaituserState.waitForSync()
Example: use the user state to generate proofs
constnonce=1constepochKeyProof=awaituserState.genVerifyEpochKeyProof(nonce)// 1. submit the epoch key proof to smart contractconsttx=awaitunirepContract.submitEpochKeyProof(epochKeyProof.publicSignals,epochKeyProof.proof)// 2. get the index of the epoch key proofconstproofHash=epochKeyProof.hash()constindex=awaitunirepContract.getProofIndex(proofHash)// Then the attester can call `submitAttestation` on Unirep contract// to send attestation to the epoch key with a proof index