Skip to main content
Version: 1.0.1

@unirep/core

Github license NPM version Downloads Linter eslint Code style prettier

🛠 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 Unirep state

import { Synchronizer } from '@unirep/core'

const genUnirepState = async (
provider: ethers.providers.Provider,
address: string,
_db?: DB
) => {
const unirepContract: Unirep = await getUnirepContract(address, provider)
let synchronizer: Synchronizer
let db: DB = _db ?? (await SQLiteConnector.create(schema, ':memory:'))
synchronizer = new Synchronizer(db, defaultProver, unirepContract)
await synchronizer.start()
await synchronizer.waitForSync()
return synchronizer
}

Example: use the synchronizer to generate unirep state

const epoch = 1
const globalStateTree = await synchronizer.genGSTree(epoch)

UserState 👤

Construct a Unirep user state

import { UserState } from '@unirep/core'

const genUserState = async (
provider: ethers.providers.Provider,
address: string,
userIdentity: ZkIdentity,
_db?: DB
) => {
const unirepContract: Unirep = getUnirepContract(address, provider)
let db: DB = _db ?? (await SQLiteConnector.create(schema, ':memory:'))
const userState = new UserState(
db,
defaultProver,
unirepContract,
userIdentity
)
await userState.start()
await userState.waitForSync()
return userState
}

Example: use the user state to generate proofs

const nonce = 1
const epochKeyProof = await userState.genVerifyEpochKeyProof(nonce)
// Then the attester can call `submitAttestation` on Unirep contract
// to send attestation to the epoch key

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
)