From da8c4b2204fb7c9235e97b9ee146e1acd4fd9a82 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Mon, 10 Oct 2022 08:48:26 -0500 Subject: [PATCH 1/2] fix: epoch decoding --- src/codec.spec.ts | 47 ++++++++++++++++++++++++++++++++++++++++++++++- src/epoch.ts | 2 +- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/src/codec.spec.ts b/src/codec.spec.ts index 8a95688..ba2e012 100644 --- a/src/codec.spec.ts +++ b/src/codec.spec.ts @@ -17,6 +17,7 @@ import { } from "js-waku/lib/waku_message/version_1"; import { RLNDecoder, RLNEncoder } from "./codec.js"; +import { epochBytesToInt } from "./epoch.js"; import { RlnMessage } from "./message.js"; import * as rln from "./index.js"; @@ -47,7 +48,6 @@ describe("RLN codec with version 0", () => { expect(bytes).to.not.be.undefined; const protoResult = await rlnDecoder.fromWireToProtoObj(bytes!); - expect(protoResult).to.not.be.undefined; const msg = (await rlnDecoder.fromProtoObj(protoResult!))!; @@ -267,3 +267,48 @@ describe("RLN codec with version 1", () => { expect(msg.timestamp).to.not.be.undefined; }); }); + +describe("RLN Codec - epoch", () => { + it("toProtoObj", async function () { + const rlnInstance = await rln.create(); + const memKeys = rlnInstance.generateMembershipKey(); + const index = 0; + const payload = new Uint8Array([1, 2, 3, 4, 5]); + + rlnInstance.insertMember(memKeys.IDCommitment); + + const rlnEncoder = new RLNEncoder( + new EncoderV0(TestContentTopic), + rlnInstance, + index, + memKeys + ); + const rlnDecoder = new RLNDecoder( + rlnInstance, + new DecoderV0(TestContentTopic) + ); + + const proto = await rlnEncoder.toProtoObj({ payload }); + + expect(proto).to.not.be.undefined; + const msg = (await rlnDecoder.fromProtoObj( + proto! + )) as RlnMessage; + + const epochBytes = proto!.rateLimitProof!.epoch; + const epoch = epochBytesToInt(epochBytes); + + expect(msg).to.not.be.undefined; + expect(msg.rateLimitProof).to.not.be.undefined; + + expect(msg.verify()).to.be.true; + expect(msg.epoch).to.not.be.undefined; + expect(msg.epoch!.toString(10).length).to.eq(9); + expect(msg.epoch).to.eq(epoch); + + expect(msg.contentTopic).to.eq(TestContentTopic); + expect(msg.msg.version).to.eq(0); + expect(msg.payload).to.deep.eq(payload); + expect(msg.timestamp).to.not.be.undefined; + }); +}); diff --git a/src/epoch.ts b/src/epoch.ts index af4fe8e..bb2d964 100644 --- a/src/epoch.ts +++ b/src/epoch.ts @@ -23,7 +23,7 @@ export function epochIntToBytes(epoch: number): Uint8Array { } export function epochBytesToInt(bytes: Uint8Array): number { - const dv = new DataView(bytes.buffer); + const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength); const epoch = dv.getUint32(0, true); log("decoded epoch", epoch, bytes); return epoch; From 8eb7779ecdb25b8e35af286995d32b72ca89e3c1 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Mon, 10 Oct 2022 08:50:50 -0500 Subject: [PATCH 2/2] fix: proof generated log --- src/codec.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/codec.ts b/src/codec.ts index 7614a8f..2d9682a 100644 --- a/src/codec.ts +++ b/src/codec.ts @@ -42,7 +42,7 @@ export class RLNEncoder implements Encoder { if (!protoMessage) return; protoMessage.rateLimitProof = await this.generateProof(message); - log("Proof generated", message.rateLimitProof); + log("Proof generated", protoMessage.rateLimitProof); return protoMessage; }