From f63c138e9f19fbbdc3a237f9e43d9413b37a3c1f Mon Sep 17 00:00:00 2001 From: mhchia Date: Mon, 18 Sep 2023 16:14:38 +0800 Subject: [PATCH 1/2] expose default resources --- src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/src/index.ts b/src/index.ts index a7a5499..3074031 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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 From ccbecfd6fe2554140b8cb0e07573ec5b53c7ad77 Mon Sep 17 00:00:00 2001 From: mhchia Date: Mon, 18 Sep 2023 16:35:32 +0800 Subject: [PATCH 2/2] create a new id counter if not present when create proof --- src/rln.ts | 29 ++++++++++++++++++++--------- tests/circuit-wrapper.test.ts | 1 - 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/rln.ts b/src/rln.ts index 699fff6..548ca6c 100644 --- a/src/rln.ts +++ b/src/rln.ts @@ -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 { @@ -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}` + ) + } else { + console.debug( + `User has already registered before. Skip registration: this.identityCommitment=${this.identityCommitment}` + ) + } + await this.setMessageIDCounter(messageIDCounter) } /** @@ -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.' ) } + // 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. diff --git a/tests/circuit-wrapper.test.ts b/tests/circuit-wrapper.test.ts index fcbb4c9..3593c87 100644 --- a/tests/circuit-wrapper.test.ts +++ b/tests/circuit-wrapper.test.ts @@ -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