Skip to main content
Version: 2.1.x

Class: UserState

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

Example

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,
prover: defaultProver, // a circuit prover
})

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

// stop the synchronizer deamon
state.stop()

Constructors​

constructor​

β€’ new UserState(config)

Parameters​

NameType
configObject
config.attesterId?bigint | bigint[]
config.db?DB
config.iddefault
config.proverProver
config.provider?Provider
config.synchronizer?Synchronizer
config.unirepAddress?string

Defined in​

packages/core/src/UserState.ts:107

Properties​

_chainId​

β€’ Private _chainId: number

Defined in​

packages/core/src/UserState.ts:62


_db​

β€’ Private _db: DB

Defined in​

packages/core/src/UserState.ts:63


_id​

β€’ Private _id: default

Defined in​

packages/core/src/UserState.ts:60


_prover​

β€’ Private _prover: Prover

Defined in​

packages/core/src/UserState.ts:59


_sync​

β€’ Private _sync: Synchronizer

Defined in​

packages/core/src/UserState.ts:61

Accessors​

chainId​

β€’ get chainId(): number

The current chain ID of UniRep contract.

Returns​

number

Defined in​

packages/core/src/UserState.ts:96


commitment​

β€’ get commitment(): bigint

The Semaphore identity commitment of the user.

Returns​

bigint

Defined in​

packages/core/src/UserState.ts:68


db​

β€’ get db(): DB

Read the database object.

Returns​

DB

Defined in​

packages/core/src/UserState.ts:103


id​

β€’ get id(): default

The Semaphore identity of the user.

Returns​

default

Defined in​

packages/core/src/UserState.ts:75


prover​

β€’ get prover(): Prover

The prover object.

Returns​

Prover

Defined in​

packages/core/src/UserState.ts:89


sync​

β€’ get sync(): Synchronizer

The underlying synchronizer object.

Returns​

Synchronizer

Defined in​

packages/core/src/UserState.ts:82

Methods​

_checkBit​

β–Έ Private _checkBit(input, bit): void

Check if the input exceeds the maximum bit

Parameters​

NameTypeDescription
inputbigintThe input value
bitnumberThe bit number

Returns​

void

Defined in​

packages/core/src/UserState.ts:660


_checkChainId​

β–Έ Private _checkChainId(): Promise<void>

Check if a chain ID is set. If a chain ID is not set, it queries the provider and sets chain ID.

Returns​

Promise<void>

Defined in​

packages/core/src/UserState.ts:647


_checkEpkNonce​

β–Έ Private _checkEpkNonce(epochKeyNonce): void

Check if epoch key nonce is valid. Throws an error if the epoch key nonce is invalid.

Parameters​

NameTypeDescription
epochKeyNoncenumberThe input epoch key nonce to be checked.

Returns​

void

Defined in​

packages/core/src/UserState.ts:624


_checkSync​

β–Έ Private _checkSync(): void

Check if a synchronizer is set. Throws an error if a synchronizer is not set.

Returns​

void

Defined in​

packages/core/src/UserState.ts:638


_updateData​

β–Έ Private _updateData(data, latestTransitionedEpoch, attesterId): Promise<void>

Update user state data.

Parameters​

NameTypeDescription
databigint[]The latest transitioned data.
latestTransitionedEpochnumberThe latest found transitioned epoch.
attesterIdstringThe attester ID.

Returns​

Promise<void>

Defined in​

packages/core/src/UserState.ts:376


genEpochKeyLiteProof​

β–Έ genEpochKeyLiteProof(options?): Promise<EpochKeyLiteProof>

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.

Parameters​

NameTypeDescription
optionsObject-
options.attesterId?string | bigintIndicates for which attester the proof will be used. Default: this.attesterId
options.data?bigintIndicates if user wants to endorse a 253-bits data. Default: 0.
options.epoch?numberThe specified epoch. Default: current epoch.
options.nonce?numberThe specified epoch key nonce. Default: 0.
options.revealNonce?booleanIndicates if user wants to reveal epoch key nonce. Default: false.

Returns​

Promise<EpochKeyLiteProof>

The epoch key lite proof of type EpochKeyLiteProof.

Example

const { publicSignals, proof } = await userState.genEpochKeyLiteProof({
nonce: 1
})

Defined in​

packages/core/src/UserState.ts:1026


genEpochKeyProof​

β–Έ genEpochKeyProof(options?): Promise<EpochKeyProof>

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

Parameters​

NameTypeDescription
optionsObject-
options.attesterId?string | bigintIndicates for which attester the proof will be used. Default: this.attesterId
options.data?bigintIndicates if user wants to endorse a 253-bits data. Default: 0
options.epoch?numberThe specified epoch. Default: current epoch.
options.nonce?numberThe specified epoch key nonce. Default: 0.
options.revealNonce?booleanIndicates if user wants to reveal epoch key nonce. Default: false.

Returns​

Promise<EpochKeyProof>

The epoch key proof of type EpochKeyProof.

Example

const { publicSignals, proof } = await userState.genEpochKeyProof({
nonce: 1
})

Defined in​

packages/core/src/UserState.ts:963


genProveReputationProof​

β–Έ genProveReputationProof(options): Promise<ReputationProof>

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.

Parameters​

NameTypeDescription
optionsObject-
options.attesterId?string | bigintattesterId is used to generate proof for certain attester. Default: this.attesterId
options.data?string | bigintIndicates if user wants to endorse a 253-bits data. Default: 0.
options.epkNonce?numberThe nonce determines the output of the epoch key. Default: 0.
options.graffiti?string | bigintThe graffiti that user wants to prove. It should satisfy: graffiti == (data[SUM_FIELD_COUNT] / (2 ** REPL_NONCE_BITS)). Default: 0.
options.maxRep?string | number | bigintThe amount of reputation that user wants to prove. It should satisfy: negRep - posRep >= maxRep. Default: 0
options.minRep?string | number | bigintThe amount of reputation that user wants to prove. It should satisfy: posRep - negRep >= minRep. Default: 0
options.proveZeroRep?booleanIndicates if user wants to prove posRep - negRep == 0. Default: 0.
options.revealNonce?booleanIndicates if user wants to reveal epoch key nonce. Default: false.

Returns​

Promise<ReputationProof>

The reputation proof of type ReputationProof.

Example

const {publicSignals, proof} = await userState.genProveReputationProof({
minRep: 3,
graffiti: '1234',
})

Defined in​

packages/core/src/UserState.ts:854


genUserSignUpProof​

β–Έ genUserSignUpProof(options?): Promise<SignupProof>

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

Parameters​

NameTypeDescription
optionsObject-
options.attesterId?string | bigintIndicates for which attester the proof will be used. Default: this.attesterId
options.epoch?numberIndicates in which epoch the proof will be used. Default: current epoch.

Returns​

Promise<SignupProof>

The sign up proof of type SignUpProof.

Example

const { publicSignals, proof } = await userState.genUserSignUpProof()

Defined in​

packages/core/src/UserState.ts:921


genUserStateTransitionProof​

β–Έ genUserStateTransitionProof(options?): Promise<UserStateTransitionProof>

Generate a user state transition proof. Returns a UserStateTransitionProof.

Parameters​

NameTypeDescription
optionsObject-
options.attesterId?string | bigintattesterId is used to generate proof for certain attester. Default: this.attesterId.
options.toEpoch?numbertoEpoch is used to indicate in which epoch the proof will be used. Default: current epoch.

Returns​

Promise<UserStateTransitionProof>

The UserStateTransitionProof object.

Example

const { publicSignals, proof } = await userState.genUserStateTransitionProof()

Defined in​

packages/core/src/UserState.ts:714


getData​

β–Έ getData(toEpoch?, attesterId?): Promise<bigint[]>

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.

Parameters​

NameTypeDescription
toEpoch?numberThe latest epoch that the reputation is accumulated. Default: current epoch.
attesterIdstring | bigintThe attester to be queried. Default: this.sync.attesterId

Returns​

Promise<bigint[]>

The data object

Defined in​

packages/core/src/UserState.ts:434


getDataByEpochKey​

β–Έ getDataByEpochKey(epochKey, epoch, attesterId?): Promise<any[]>

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

Parameters​

NameTypeDescription
epochKeystring | bigintThe epoch key to be queried.
epochnumberThe epoch of the epoch key to be queried.
attesterIdstring | bigintThe attester to be queried. Default: this.attesterId

Returns​

Promise<any[]>

The data object.

Defined in​

packages/core/src/UserState.ts:583


getEpochKeyIndex​

β–Έ getEpochKeyIndex(epoch, epochKey, attesterId): Promise<number>

Get the index of epoch key among all attestations.

Parameters​

NameTypeDescription
epochnumberThe epoch of the epoch key to be queried.
epochKeystring | bigintThe epoch key to be queried.
attesterIdstring | bigintThe attester to be queried.

Returns​

Promise<number>

The index of the epoch key.

Defined in​

packages/core/src/UserState.ts:675


getEpochKeys​

β–Έ getEpochKeys(epoch?, nonce?, attesterId?): bigint | bigint[]

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 synchronizer.calcCurrentEpoch).

Parameters​

NameTypeDescription
epoch?number | bigintThe epoch to be queried. Default: current epoch.
nonce?numberThe specified epoch key nonce. Default: 0.
attesterIdstring | bigintThe attester to be queried. Default: this.attesterId

Returns​

bigint | bigint[]

An epoch key or an array of epoch keys.

Defined in​

packages/core/src/UserState.ts:322


getProvableData​

β–Έ getProvableData(attesterId?): Promise<bigint[]>

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.

Parameters​

NameTypeDescription
attesterIdstring | bigintThe attester to be queried. Default: this.attesterId

Returns​

Promise<bigint[]>

The data object

Defined in​

packages/core/src/UserState.ts:569


hasSignedUp​

β–Έ hasSignedUp(attesterId?): Promise<boolean>

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

Parameters​

NameTypeDescription
attesterIdstring | bigintThe attester to be queried. Default: this.attesterId.

Returns​

Promise<boolean>

True if user has signed up in unirep contract, false otherwise.

Defined in​

packages/core/src/UserState.ts:186


latestStateTreeLeafIndex​

β–Έ latestStateTreeLeafIndex(epoch?, attesterId?): Promise<number>

Get the latest global state tree leaf index for an epoch.

Parameters​

NameTypeDescription
epoch?numberGet the global state tree leaf index of the given epoch. Default: current epoch.
attesterIdstring | bigintThe attester to be queried. Default: this.attesterId

Returns​

Promise<number>

The the latest state tree leaf index for an epoch.

Defined in​

packages/core/src/UserState.ts:264


latestTransitionedEpoch​

β–Έ latestTransitionedEpoch(attesterId?): Promise<number>

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

Parameters​

NameTypeDescription
attesterIdstring | bigintThe attester to be queried. Default: this.attesterId

Returns​

Promise<number>

The latest epoch where a user performed a user state transition.

Defined in​

packages/core/src/UserState.ts:205


parseReplData​

β–Έ parseReplData(replData): Object

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

Parameters​

NameTypeDescription
replDatabigintThe raw data which is processed on-chain.

Returns​

Object

The parsed data and the data index (nonce).

NameType
databigint
noncebigint

Defined in​

packages/core/src/UserState.ts:358


start​

β–Έ start(): Promise<void>

Start the synchronizer daemon. Start polling the blockchain for new events. If we're behind the HEAD block we'll poll many times quickly

Returns​

Promise<void>

Defined in​

packages/core/src/UserState.ts:160


stop​

β–Έ stop(): void

Stop synchronizing with Unirep contract.

Returns​

void

Defined in​

packages/core/src/UserState.ts:177


waitForSync​

β–Έ waitForSync(blockNumber?): Promise<void>

Wait for the synchronizer to sync up to a certain block. By default this will wait until the current latest known block (according to the provider).

Parameters​

NameTypeDescription
blockNumber?numberThe block number to be synced to.

Returns​

Promise<void>

Defined in​

packages/core/src/UserState.ts:170