Skip to main content
Version: 2.0.0-beta-4

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 } from '@unirep/core'
import { defaultProver } from '@unirep/circuits/provers/defaultProver'
import { Identity } from '@semaphore-protocol/identity'

const id = new Identity()
const state = new UserState({
prover: defaultProver, // a circuit prover
unirepAddress: '0xaabbccaabbccaabbccaabbccaabbccaabbccaaaa',
provider, // an ethers.js provider
id,
})

// or, initialize with an existing synchronizer object
const state = new UserState({
synchronizer,
id
})

// start the synchoronizer deamon
await state.start()
await state.waitForSync()

// stop the synchronizer deamon
state.stop()

constructor​

Can be constructed using an existing synchronizer, or by initializing a new synchronizer.

constructor(
config: {
synchronizer?: Synchronizer
db?: DB
attesterId?: bigint | bigint[]
unirepAddress?: string
provider?: ethers.providers.Provider
id: Identity
prover: Prover
}
)

commitment​

The Semaphore identity commitment of the user.

state.commitment: bigint

id​

The Semaphore identity of the user.

state.id: Identity

sync​

The underlying synchronizer object.

state.sync: Synchronizer

prover​

The prover object.

state.prover: Prover

start​

Convenience accessor for synchronizer start.

state.start(): Promise<void>

waitForSync​

Convenience accessor for synchronizer waitForSync.

state.waitForSync(blockNumber?: number): Promise<void>

stop​

Convenience accessor for synchronizer stop.

state.stop(): void

hasSignedUp​

Query the current database if the Semaphore identity commitment is stored.

state.hasSignedUp(attesterId?: bigint | string): Promise<boolean>

latestTransitionedEpoch​

Query the current database for a user's signup event or latest user state transition nullifier.

state.latestTransitionedEpoch(attesterId?: bigint | string): Promise<number>

latestStateTreeLeafIndex​

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

state.latestStateTreeLeafIndex(epoch?: number, attesterId?: bigint | string): Promise<number>

getEpochKeys​

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, attesterId?: bigint | string): bigint | bigint[]

parseReplData​

This function is used to parse replacement data field to be index and data. See replacement data field

state.parseReplData(replData: bigint): <{data: bigint, nonce: bigint}>

getData​

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

tip

If you want to make a proof of data make sure to use getProvableData. Data can only be proven once it has been included in a state tree leaf. Learn more about reputation proofs here.

state.getData(toEpoch?: number, attesterId?: bigint | string): Promise<bigint[]>

getProvableData​

Get the data that can be proven by the user using a state tree leaf. This is the data up to, but not including, the epoch the user has transitioned into.

state.getProvableData(attesterId?: bigint | string): Promise<bigint[]>

getDataByEpochKey​

Get the pending changes to the data owned by an epoch key.

state.getDataByEpochKey(
epochKey: bigint | string,
epoch: number,
attesterId?: bigint | string
): Promise<bigint[]>

getEpochKeyIndex​

Get the index of epoch key among all attestations.

state.getEpochKeyIndex(
epoch: number,
epochKey: bigint | string,
attesterId?: bigint | string
): Promise<number>

genUserStateTransitionProof​

Generate a user state transition proof. Returns a UserStateTransitionProof.

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

genProveReputationProof​

Generate a proof of reputation. Returns a ReputationProof.

danger

Please avoid assigning the minRep = data[0] - data[1] or maxRep = data[1] - data[0].
The proof could allow a user to accidentally publish their overall reputation (i.e. data[0]-data[1]). Depending on the circumstances (such as the length of the attestation history) this could reveal a user’s epoch key(s) as well.

state.genProveReputationProof(options: {
epkNonce?: number
minRep?: number
maxRep?: number
graffiti?: bigint | string
proveZeroRep?: boolean
revealNonce?: boolean
data?: bigint | string
attesterId?: bigint | string
}): Promise<ReputationProof>

genUserSignUpProof​

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

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

genEpochKeyProof​

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,
attesterId?: bigint | string
}): Promise<EpochKeyProof>

genEpochKeyLiteProof​

Generate a proof that a user controls an epoch key in a certain epoch. Optionally provide a data value to sign. Returns an EpochKeyLiteProof. This proof will not include a merkle tree proof which makes the proof size smaller than an EpochKeyProof. It can be used to prove a seen and valid epoch key.

state.genEpochKeyLiteProof(options: {
revealNonce?: boolean,
nonce?: number,
epoch?: number,
data?: bigint,
attesterId?: bigint | string
}): Promise<EpochKeyLiteProof>