Unirep procotol package
Client library for protocol related functions which are used in unirep protocol.
🛠 Install
npm or yarn
Install the @unirep/core
package with npm:
npm i @unirep/core
or yarn:
yarn add @unirep/core
📔 Usage
Synchronizer ⏲
Construct a synchronizer
import { Synchronizer, schema } from '@unirep/core'
import { getUnirepContract, Unirep } from '@unirep/contracts'
import { DB, SQLiteConnector } from 'anondb/node'
const unirepContract: Unirep = getUnirepContract(address, provider)
const db: DB = await SQLiteConnector.create(schema, ':memory:')
const synchronizer = new Synchronizer(db, provider, unirepContract)
await synchronizer.start()
await synchronizer.waitForSync()
Example: use the synchronizer to generate unirep state
const epoch = 1
const globalStateTree = await synchronizer.genGSTree(epoch)
UserState 👤
Construct a user state
import { ZkIdentity } from '@unirep/crypto'
import { Synchronizer, schema } from '@unirep/core'
import { getUnirepContract, Unirep } from '@unirep/contracts'
import { DB, SQLiteConnector } from 'anondb/node'
const identity = new ZkIdentity()
const unirepContract: Unirep = getUnirepContract(address, provider)
const db: DB = await SQLiteConnector.create(schema, ':memory:')
const userState = new UserState(
db,
provider,
unirepContract,
identity
)
await userState.start()
await userState.waitForSync()
Example: use the user state to generate proofs
const nonce = 1
const epochKeyProof = await userState.genVerifyEpochKeyProof(nonce)
const tx = await unirepContract.submitEpochKeyProof(
epochKeyProof.publicSignals,
epochKeyProof.proof
)
const proofHash = epochKeyProof.hash()
const index = await unirepContract.getProofIndex(proofHash)
Utils 🧳
Example: Compute an epoch key
import { ZkIdentity } from '@unirep/crypto'
import { genEpochKey } from '@unirep/core'
const identity = new ZkIdentity()
const epoch = 1
const nonce = 0
const epochTreeDepth = 64
const epk = genEpochKey(
identity.identityNullifier,
epoch,
nonce,
epochTreeDepth
)