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

Merkle provider poc #106

Draft
wants to merge 4 commits into
base: master
Choose a base branch
from
Draft
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
121 changes: 107 additions & 14 deletions package-lock.json

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

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@
},
"dependencies": {
"@chainsafe/bls-keystore": "^3.0.0",
"@noble/curves": "^1.4.0",
"@waku/core": "^0.0.25",
"@waku/utils": "^0.0.13",
"@waku/zerokit-rln-wasm": "^0.0.13",
Expand All @@ -143,4 +144,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
4 changes: 2 additions & 2 deletions src/identity.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { buildBigIntFromUint8Array } from "./utils/index.js";
import { uint256FromBytes } from "./utils/bytes.js";

export class IdentityCredential {
constructor(
Expand All @@ -14,7 +14,7 @@ export class IdentityCredential {
const idNullifier = memKeys.subarray(32, 64);
const idSecretHash = memKeys.subarray(64, 96);
const idCommitment = memKeys.subarray(96);
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
const idCommitmentBigInt = uint256FromBytes(idCommitment);

return new IdentityCredential(
idTrapdoor,
Expand Down
4 changes: 2 additions & 2 deletions src/keystore/keystore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
import _ from "lodash";
import { v4 as uuidV4 } from "uuid";

import { buildBigIntFromUint8Array } from "../utils/bytes.js";
import { uint256FromBytes } from "../utils/bytes.js";

import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
Expand Down Expand Up @@ -262,7 +262,7 @@ export class Keystore {
IDNullifier: Keystore.fromArraylikeToBytes(
_.get(obj, "identityCredential.idNullifier", [])
),
IDCommitmentBigInt: buildBigIntFromUint8Array(
IDCommitmentBigInt: uint256FromBytes(
Keystore.fromArraylikeToBytes(
_.get(obj, "identityCredential.idCommitment", [])
)
Expand Down
2 changes: 1 addition & 1 deletion src/proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Proof implements IRateLimitProof {

constructor(proofBytes: Uint8Array) {
if (proofBytes.length < rlnIdentifierOffset) throw "invalid proof";
// parse the proof as proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32>
// parse the proof as proof<128> | root<32> | epoch<32> | share_x<32> | share_y<32> | nullifier<32> | rln_identifier<32>
this.proof = proofBytes.subarray(0, proofOffset);
this.merkleRoot = proofBytes.subarray(proofOffset, rootOffset);
this.epoch = proofBytes.subarray(rootOffset, epochOffset);
Expand Down
Loading