Skip to content

Commit

Permalink
feat: add option to use service for merkle proof in generateRLNProof
Browse files Browse the repository at this point in the history
  • Loading branch information
Arseniy Klempner authored and Arseniy Klempner committed Mar 30, 2024
1 parent 10d463a commit 6b20df5
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 33 deletions.
122 changes: 108 additions & 14 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@
},
"dependencies": {
"@chainsafe/bls-keystore": "^3.0.0",
"@noble/curves": "^1.4.0",
"@noble/hashes": "^1.4.0",
"@waku/core": "^0.0.25",
"@waku/utils": "^0.0.13",
"@waku/zerokit-rln-wasm": "^0.0.13",
Expand All @@ -143,4 +145,4 @@
"lodash": "^4.17.21",
"uuid": "^9.0.1"
}
}
}
6 changes: 4 additions & 2 deletions src/codec.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ describe("RLN codec with version 0", () => {
encoder: createEncoder({ contentTopic: TestContentTopic }),
rlnInstance,
index,
credential
credential,
fetchMembersFromService: false
});
const rlnDecoder = createRLNDecoder({
rlnInstance,
Expand Down Expand Up @@ -384,7 +385,8 @@ describe("RLN codec with version 0 and meta setter", () => {
encoder: createEncoder({ contentTopic: TestContentTopic, metaSetter }),
rlnInstance,
index,
credential
credential,
fetchMembersFromService: false
});
const rlnDecoder = createRLNDecoder({
rlnInstance,
Expand Down
13 changes: 10 additions & 3 deletions src/codec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,18 @@ const log = debug("waku:rln:encoder");

export class RLNEncoder implements IEncoder {
private readonly idSecretHash: Uint8Array;
private readonly idCommitment: bigint;

constructor(
private encoder: IEncoder,
private rlnInstance: RLNInstance,
private index: number,
identityCredential: IdentityCredential
identityCredential: IdentityCredential,
private readonly fetchMembersFromService: boolean = false
) {
if (index < 0) throw "invalid membership index";
this.idSecretHash = identityCredential.IDSecretHash;
this.idCommitment = identityCredential.IDCommitmentBigInt;
}

async toWire(message: IMessage): Promise<Uint8Array | undefined> {
Expand All @@ -49,7 +52,9 @@ export class RLNEncoder implements IEncoder {
signal,
this.index,
message.timestamp,
this.idSecretHash
this.idSecretHash,
this.idCommitment,
this.fetchMembersFromService
);
return proof;
}
Expand All @@ -72,14 +77,16 @@ type RLNEncoderOptions = {
rlnInstance: RLNInstance;
index: number;
credential: IdentityCredential;
fetchMembersFromService: boolean;
};

export const createRLNEncoder = (options: RLNEncoderOptions): RLNEncoder => {
return new RLNEncoder(
options.encoder,
options.rlnInstance,
options.index,
options.credential
options.credential,
options.fetchMembersFromService
);
};

Expand Down
6 changes: 4 additions & 2 deletions src/contract/rln_contract.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ describe("RLN Contract abstraction", () => {
const voidSigner = new ethers.VoidSigner(SEPOLIA_CONTRACT.address);
const rlnContract = new RLNContract(rlnInstance, {
registryAddress: SEPOLIA_CONTRACT.address,
signer: voidSigner
signer: voidSigner,
fetchMembersFromService: false
});

rlnContract["storageContract"] = {
Expand All @@ -43,7 +44,8 @@ describe("RLN Contract abstraction", () => {
const voidSigner = new ethers.VoidSigner(SEPOLIA_CONTRACT.address);
const rlnContract = new RLNContract(rlnInstance, {
registryAddress: SEPOLIA_CONTRACT.address,
signer: voidSigner
signer: voidSigner,
fetchMembersFromService: false
});

rlnContract["storageIndex"] = 1;
Expand Down
13 changes: 9 additions & 4 deletions src/contract/rln_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ type Signer = ethers.Signer;
type RLNContractOptions = {
signer: Signer;
registryAddress: string;
fetchMembersFromService: boolean;
};

type RLNStorageOptions = {
Expand Down Expand Up @@ -54,7 +55,9 @@ export class RLNContract {
const rlnContract = new RLNContract(rlnInstance, options);

await rlnContract.initStorageContract(options.signer);
await rlnContract.fetchMembers(rlnInstance);
if (!options.fetchMembersFromService) {
await rlnContract.fetchMembers(rlnInstance);
}
rlnContract.subscribeToMembers(rlnInstance);

return rlnContract;
Expand All @@ -80,8 +83,9 @@ export class RLNContract {
): Promise<void> {
const storageIndex = options?.storageIndex
? options.storageIndex
: await this.registryContract.usingStorageIndex();
const storageAddress = await this.registryContract.storages(storageIndex);
: await this.registryContract.callStatic.usingStorageIndex();
const storageAddress =
await this.registryContract.callStatic.storages(storageIndex);

if (!storageAddress || storageAddress === ethers.constants.AddressZero) {
throw Error("No RLN Storage initialized on registry contract.");
Expand All @@ -95,7 +99,8 @@ export class RLNContract {
);
this._membersFilter = this.storageContract.filters.MemberRegistered();

this.deployBlock = await this.storageContract.deployedBlockNumber();
this.deployBlock =
await this.storageContract.callStatic.deployedBlockNumber();
}

public get registry(): ethers.Contract {
Expand Down
8 changes: 6 additions & 2 deletions src/rln.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type StartRLNOptions = {
* If provided used for validating the network chainId and connecting to registry contract.
*/
credentials?: EncryptedCredentials | DecryptedCredentials;
fetchMembersFromService?: boolean;
};

type RegisterMembershipOptions =
Expand All @@ -84,6 +85,7 @@ type RegisterMembershipOptions =

type WakuRLNEncoderOptions = WakuEncoderOptions & {
credentials: EncryptedCredentials | DecryptedCredentials;
fetchMembersFromService: boolean;
};

export class RLNInstance {
Expand Down Expand Up @@ -129,7 +131,8 @@ export class RLNInstance {
this._signer = signer!;
this._contract = await RLNContract.init(this, {
registryAddress: registryAddress!,
signer: signer!
signer: signer!,
fetchMembersFromService: options.fetchMembersFromService ?? false
});
this.started = true;
} finally {
Expand Down Expand Up @@ -244,7 +247,8 @@ export class RLNInstance {
encoder: createEncoder(options),
rlnInstance: this,
index: credentials.membership.treeIndex,
credential: credentials.identity
credential: credentials.identity,
fetchMembersFromService: options.fetchMembersFromService
});
}

Expand Down
Loading

0 comments on commit 6b20df5

Please sign in to comment.