Skip to content

Commit

Permalink
create a new id counter if not present when create proof
Browse files Browse the repository at this point in the history
  • Loading branch information
mhchia committed Sep 18, 2023
1 parent f63c138 commit ccbecfd
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
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 @@ export class RLN implements IRLN {

/**
* 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 @@ export class RLN implements IRLN {
`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 @@ export class RLN implements IRLN {
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

0 comments on commit ccbecfd

Please sign in to comment.