Skip to main content
Version: 2.0.0-alpha-3

User State

The user state object is used to manage user state for an attester. The state is backed by an anondb instance.

import { UserState, schema } from '@unirep/core'
import { SQLiteConnector } from 'anondb/node'
import { defaultProver } from '@unirep/circuits/provers/defaultProver'

const db = new SQLiteConnector(schema, ':memory:')
const state = new UserState({
prover: defaultProver, // a circuit prover
unirepAddress: '0xaabbccaabbccaabbccaabbccaabbccaabbccaaaa',
provider, // an ethers.js provider
_id: identity, // a user identity that will be used for making proofs

The UserState class is a subclass of Synchronizer.


constructor(config: {
db: DB
prover: Prover
unirepContract: ethers.Contract
_id: ZkIdentity
attesterId: bigint
}) {


state.hasSignedUp(): Promise<boolean>


state.latestTransitionedEpoch(): Promise<number>


Get the latest state tree leaf index for either the latest transitioned epoch, or the epoch specified.

state.latestStateTreeLeafIndex(epoch?: number): Promise<number>


Get epoch keys for the current user, for an epoch. If a nonce value is supplied the return value will be a single epoch key. Otherwise an array of all epoch keys will be returned.

If no epoch is supplied the current epoch will be used (as determined by calcCurrentEpoch).

state.getEpochKeys(epoch?: number, nonce?: number): bigint | bigint[]


Get the reputation balance for the user up to and including the provided epoch. By default reputation up to and including the current epoch is returned.


If you want to make a proof of reputation make sure to use getProvableRep to show the user balance. Reputation can only be proven once it has been included in a state tree leaf. Learn more about reputation proofs here.

state.getRep(toEpoch?: number): Promise<Reputation>


Get the reputation balance that can be proven by the user in a reputation proof. This is the balance up to, but not including, the epoch the user has transitioned into.

state.getProvableRep(): Promise<Reputation>


Get the rep owed to an epoch key in a certain epoch.

state.getRepByEpochKey(epochKey: bigint, epoch: number): Promise<Reputation>


Generate a user state transition proof. Returns a UserStateTransitionProof.

state.genUserStateTransitionProof(options?: {
toEpoch?: bigint | number
}): Promise<UserStateTransitionProof>


Generate a proof of reputation. Returns a ReputationProof.

state.genProveReputationProof(options: {
epkNonce: number,
minRep?: number
graffitiPreImage?: bigint | string
}): Promise<ReputationProof>


Generate a proof that can be used to signup. Returns a SignupProof.

state.genUserSignUpProof(options: {
epoch?: bigint | number
}): Promise<SignupProof>


Generate a proof that a user controls an epoch key in a certain epoch. Optionally provide a data value to sign. Returns an EpochKeyProof.

state.genEpochKeyProof(options: {
revealNonce?: boolean,
nonce?: number,
epoch?: number,
data?: bigint,
}): Promise<EpochKeyProof>


Generate a proof that can be used to update the epoch tree root on chain.


TODO: See the epoch tree section for more info


Users shouldn't be generating this proof.

state.genAggregateEpochKeysProof(options: {
epochKeys: bigint[],
newBalances: Reputation[],
hashchainIndex: bigint,
epoch?: number
}): Promise<AggregateEpochKeysProof>