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β
| Name | Type |
|---|---|
config | Object |
config.attesterId? | bigint | bigint[] |
config.db? | DB |
config.id | default |
config.prover | Prover |
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β
Defined inβ
packages/core/src/UserState.ts:82
Methodsβ
_checkBitβ
βΈ Private _checkBit(input, bit): void
Check if the input exceeds the maximum bit
Parametersβ
| Name | Type | Description |
|---|---|---|
input | bigint | The input value |
bit | number | The 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β
| Name | Type | Description |
|---|---|---|
epochKeyNonce | number | The 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β
| Name | Type | Description |
|---|---|---|
data | bigint[] | The latest transitioned data. |
latestTransitionedEpoch | number | The latest found transitioned epoch. |
attesterId | string | The 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β
| Name | Type | Description |
|---|---|---|
options | Object | - |
options.attesterId? | string | bigint | Indicates for which attester the proof will be used. Default: this.attesterId |
options.data? | bigint | Indicates if user wants to endorse a 253-bits data. Default: 0. |
options.epoch? | number | The specified epoch. Default: current epoch. |
options.nonce? | number | The specified epoch key nonce. Default: 0. |
options.revealNonce? | boolean | Indicates 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β
| Name | Type | Description |
|---|---|---|
options | Object | - |
options.attesterId? | string | bigint | Indicates for which attester the proof will be used. Default: this.attesterId |
options.data? | bigint | Indicates if user wants to endorse a 253-bits data. Default: 0 |
options.epoch? | number | The specified epoch. Default: current epoch. |
options.nonce? | number | The specified epoch key nonce. Default: 0. |
options.revealNonce? | boolean | Indicates 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.
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β
| Name | Type | Description |
|---|---|---|
options | Object | - |
options.attesterId? | string | bigint | attesterId is used to generate proof for certain attester. Default: this.attesterId |
options.data? | string | bigint | Indicates if user wants to endorse a 253-bits data. Default: 0. |
options.epkNonce? | number | The nonce determines the output of the epoch key. Default: 0. |
options.graffiti? | string | bigint | The graffiti that user wants to prove. It should satisfy: graffiti == (data[SUM_FIELD_COUNT] / (2 ** REPL_NONCE_BITS)). Default: 0. |
options.maxRep? | string | number | bigint | The amount of reputation that user wants to prove. It should satisfy: negRep - posRep >= maxRep. Default: 0 |
options.minRep? | string | number | bigint | The amount of reputation that user wants to prove. It should satisfy: posRep - negRep >= minRep. Default: 0 |
options.proveZeroRep? | boolean | Indicates if user wants to prove posRep - negRep == 0. Default: 0. |
options.revealNonce? | boolean | Indicates 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β
| Name | Type | Description |
|---|---|---|
options | Object | - |
options.attesterId? | string | bigint | Indicates for which attester the proof will be used. Default: this.attesterId |
options.epoch? | number | Indicates 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β
| Name | Type | Description |
|---|---|---|
options | Object | - |
options.attesterId? | string | bigint | attesterId is used to generate proof for certain attester. Default: this.attesterId. |
options.toEpoch? | number | toEpoch 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.
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β
| Name | Type | Description |
|---|---|---|
toEpoch? | number | The latest epoch that the reputation is accumulated. Default: current epoch. |
attesterId | string | bigint | The 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β
| Name | Type | Description |
|---|---|---|
epochKey | string | bigint | The epoch key to be queried. |
epoch | number | The epoch of the epoch key to be queried. |
attesterId | string | bigint | The 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β
| Name | Type | Description |
|---|---|---|
epoch | number | The epoch of the epoch key to be queried. |
epochKey | string | bigint | The epoch key to be queried. |
attesterId | string | bigint | The 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β
| Name | Type | Description |
|---|---|---|
epoch? | number | bigint | The epoch to be queried. Default: current epoch. |
nonce? | number | The specified epoch key nonce. Default: 0. |
attesterId | string | bigint | The 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β
| Name | Type | Description |
|---|---|---|
attesterId | string | bigint | The 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β
| Name | Type | Description |
|---|---|---|
attesterId | string | bigint | The 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β
| Name | Type | Description |
|---|---|---|
epoch? | number | Get the global state tree leaf index of the given epoch. Default: current epoch. |
attesterId | string | bigint | The 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β
| Name | Type | Description |
|---|---|---|
attesterId | string | bigint | The 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β
| Name | Type | Description |
|---|---|---|
replData | bigint | The raw data which is processed on-chain. |
Returnsβ
Object
The parsed data and the data index (nonce).
| Name | Type |
|---|---|
data | bigint |
nonce | bigint |
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β
| Name | Type | Description |
|---|---|---|
blockNumber? | number | The block number to be synced to. |
Returnsβ
Promise<void>