Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose default params and create message id counter in createProof if not present #104

Merged
merged 2 commits into from
Sep 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ export { IRLN, RLN } from './rln'
export { ContractRLNRegistry, IRLNRegistry, MemoryRLNRegistry } from './registry'
export { CachedProof, ICache, MemoryCache, Status } from './cache'
export { IMessageIDCounter, MemoryMessageIDCounter } from './message-id-counter'
export { getDefaultRLNParams, getDefaultWithdrawParams } from './resources'

export * from './types'
// Expose helpers
Expand Down
29 changes: 20 additions & 9 deletions src/rln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,12 +374,9 @@

/**
* Set a custom messageIDCounter
* @param messageIDCounter The custom messageIDCounter
* @param messageIDCounter The custom messageIDCounter. If undefined, a new `MemoryMessageIDCounter` is created.
*/
async setMessageIDCounter(messageIDCounter?: IMessageIDCounter) {
if (await this.isRegistered() === false) {
throw new Error('Cannot set messageIDCounter for an unregistered user.')
}
if (messageIDCounter !== undefined) {
this.messageIDCounter = messageIDCounter
} else {
Expand Down Expand Up @@ -468,8 +465,18 @@
`userMessageLimit must be in range (0, ${MAX_MESSAGE_LIMIT}]. Got ${userMessageLimit}.`,
)
}
await this.registry.register(this.identityCommitment, userMessageLimit)
this.messageIDCounter = messageIDCounter ? messageIDCounter : new MemoryMessageIDCounter(userMessageLimit)

if (await this.isRegistered() === false) {
await this.registry.register(this.identityCommitment, userMessageLimit)
console.debug(
`User has registered: this.identityCommitment=${this.identityCommitment}, userMessageLimit=${userMessageLimit}`

Check failure on line 472 in src/rln.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
)
} else {
console.debug(
`User has already registered before. Skip registration: this.identityCommitment=${this.identityCommitment}`

Check failure on line 476 in src/rln.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
)
}
await this.setMessageIDCounter(messageIDCounter)
}

/**
Expand Down Expand Up @@ -516,11 +523,15 @@
throw new Error('User has not registered before')
}
if (this.messageIDCounter === undefined) {
throw new Error(
'State is not synced with the registry. ' +
'If user is currently registered, `messageIDCounter` should be non-undefined',
await this.setMessageIDCounter()
console.warn(
'MessageIDCounter is not initialized but user has registered. Maybe the user has restarted the app? ' +
'A new counter is created automatically. If a counter has been persisted, consider setting it with ' +
'with `setMessageIDCounter`. Otherwise, it is possible for user to reuse the same message id.'

Check failure on line 530 in src/rln.ts

View workflow job for this annotation

GitHub Actions / build

Missing trailing comma
)
}
// Safely cast `this.messageIDCounter` to `IMessageIDCounter` since it must have been set.
this.messageIDCounter = this.messageIDCounter as IMessageIDCounter
const merkleProof = await this.registry.generateMerkleProof(this.identityCommitment)
// NOTE: get the message id and increment the counter.
// Even if the message is not sent, the counter is still incremented.
Expand Down
1 change: 0 additions & 1 deletion tests/circuit-wrapper.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { RLNProver, RLNVerifier, WithdrawProver, WithdrawVerifier } from '../src/circuit-wrapper';
import { rlnParams, withdrawParams } from './configs';
import { fieldFactory, generateMerkleProof } from './utils';
import poseidon from 'poseidon-lite';
import { DEFAULT_MERKLE_TREE_DEPTH, calculateIdentityCommitment } from '../src/common';

// `userMessageLimit` is at most 16 bits
Expand Down
Loading