Skip to content

Commit

Permalink
Merge pull request #104 from Rate-Limiting-Nullifier/feat/expose-defa…
Browse files Browse the repository at this point in the history
…ult-params-and-change-message-id-counter

Expose default params and create message id counter in createProof if not present
  • Loading branch information
mhchia authored Sep 18, 2023
2 parents e43ddd5 + ccbecfd commit cec08c8
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 10 deletions.
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 @@ 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 cec08c8

Please sign in to comment.