mirror of
https://github.com/logos-messaging/js-rln.git
synced 2026-01-09 00:53:07 +00:00
address comments
This commit is contained in:
parent
b262257134
commit
ff218aa1a7
@ -1,7 +1,7 @@
|
||||
import {
|
||||
createDecoder,
|
||||
createEncoder,
|
||||
DecodedMessage,
|
||||
Decoder,
|
||||
Encoder,
|
||||
} from "@waku/core/lib/message/version_0";
|
||||
import {
|
||||
generatePrivateKey,
|
||||
@ -18,13 +18,19 @@ import {
|
||||
} from "@waku/message-encryption/symmetric";
|
||||
import { expect } from "chai";
|
||||
|
||||
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
||||
import {
|
||||
createRLNDecoder,
|
||||
createRLNEncoder,
|
||||
RLNDecoder,
|
||||
RLNEncoder,
|
||||
} from "./codec.js";
|
||||
import { epochBytesToInt } from "./epoch.js";
|
||||
import { RlnMessage } from "./message.js";
|
||||
|
||||
import * as rln from "./index.js";
|
||||
|
||||
const TestContentTopic = "/test/1/waku-message/utf8";
|
||||
const EMPTY_PUBSUB_TOPIC = "";
|
||||
|
||||
describe("RLN codec with version 0", () => {
|
||||
it("toWire", async function () {
|
||||
@ -35,23 +41,26 @@ describe("RLN codec with version 0", () => {
|
||||
|
||||
rlnInstance.insertMember(memKeys.IDCommitment);
|
||||
|
||||
const rlnEncoder = new RLNEncoder(
|
||||
new Encoder(TestContentTopic),
|
||||
const rlnEncoder = createRLNEncoder({
|
||||
encoder: createEncoder({ contentTopic: TestContentTopic }),
|
||||
rlnInstance,
|
||||
index,
|
||||
memKeys
|
||||
);
|
||||
const rlnDecoder = new RLNDecoder(
|
||||
membershipKey: memKeys,
|
||||
});
|
||||
const rlnDecoder = createRLNDecoder({
|
||||
rlnInstance,
|
||||
new Decoder(TestContentTopic)
|
||||
);
|
||||
decoder: createDecoder(TestContentTopic),
|
||||
});
|
||||
|
||||
const bytes = await rlnEncoder.toWire({ payload });
|
||||
|
||||
expect(bytes).to.not.be.undefined;
|
||||
const protoResult = await rlnDecoder.fromWireToProtoObj(bytes!);
|
||||
expect(protoResult).to.not.be.undefined;
|
||||
const msg = (await rlnDecoder.fromProtoObj("", protoResult!))!;
|
||||
const msg = (await rlnDecoder.fromProtoObj(
|
||||
EMPTY_PUBSUB_TOPIC,
|
||||
protoResult!
|
||||
))!;
|
||||
|
||||
expect(msg.rateLimitProof).to.not.be.undefined;
|
||||
expect(msg.verify()).to.be.true;
|
||||
@ -74,21 +83,21 @@ describe("RLN codec with version 0", () => {
|
||||
rlnInstance.insertMember(memKeys.IDCommitment);
|
||||
|
||||
const rlnEncoder = new RLNEncoder(
|
||||
new Encoder(TestContentTopic),
|
||||
createEncoder({ contentTopic: TestContentTopic }),
|
||||
rlnInstance,
|
||||
index,
|
||||
memKeys
|
||||
);
|
||||
const rlnDecoder = new RLNDecoder(
|
||||
rlnInstance,
|
||||
new Decoder(TestContentTopic)
|
||||
createDecoder(TestContentTopic)
|
||||
);
|
||||
|
||||
const proto = await rlnEncoder.toProtoObj({ payload });
|
||||
|
||||
expect(proto).to.not.be.undefined;
|
||||
const msg = (await rlnDecoder.fromProtoObj(
|
||||
"",
|
||||
EMPTY_PUBSUB_TOPIC,
|
||||
proto!
|
||||
)) as RlnMessage<DecodedMessage>;
|
||||
|
||||
@ -138,7 +147,10 @@ describe("RLN codec with version 1", () => {
|
||||
const protoResult = await rlnDecoder.fromWireToProtoObj(bytes!);
|
||||
|
||||
expect(protoResult).to.not.be.undefined;
|
||||
const msg = (await rlnDecoder.fromProtoObj("", protoResult!))!;
|
||||
const msg = (await rlnDecoder.fromProtoObj(
|
||||
EMPTY_PUBSUB_TOPIC,
|
||||
protoResult!
|
||||
))!;
|
||||
|
||||
expect(msg.rateLimitProof).to.not.be.undefined;
|
||||
expect(msg.verify()).to.be.true;
|
||||
@ -180,7 +192,7 @@ describe("RLN codec with version 1", () => {
|
||||
|
||||
expect(proto).to.not.be.undefined;
|
||||
const msg = (await rlnDecoder.fromProtoObj(
|
||||
"",
|
||||
EMPTY_PUBSUB_TOPIC,
|
||||
proto!
|
||||
)) as RlnMessage<DecodedMessage>;
|
||||
|
||||
@ -229,7 +241,10 @@ describe("RLN codec with version 1", () => {
|
||||
const protoResult = await rlnDecoder.fromWireToProtoObj(bytes!);
|
||||
|
||||
expect(protoResult).to.not.be.undefined;
|
||||
const msg = (await rlnDecoder.fromProtoObj("", protoResult!))!;
|
||||
const msg = (await rlnDecoder.fromProtoObj(
|
||||
EMPTY_PUBSUB_TOPIC,
|
||||
protoResult!
|
||||
))!;
|
||||
|
||||
expect(msg.rateLimitProof).to.not.be.undefined;
|
||||
expect(msg.verify()).to.be.true;
|
||||
@ -272,7 +287,7 @@ describe("RLN codec with version 1", () => {
|
||||
|
||||
expect(proto).to.not.be.undefined;
|
||||
const msg = (await rlnDecoder.fromProtoObj(
|
||||
"",
|
||||
EMPTY_PUBSUB_TOPIC,
|
||||
proto!
|
||||
)) as RlnMessage<DecodedMessage>;
|
||||
|
||||
@ -301,21 +316,21 @@ describe("RLN Codec - epoch", () => {
|
||||
rlnInstance.insertMember(memKeys.IDCommitment);
|
||||
|
||||
const rlnEncoder = new RLNEncoder(
|
||||
new Encoder(TestContentTopic),
|
||||
createEncoder({ contentTopic: TestContentTopic }),
|
||||
rlnInstance,
|
||||
index,
|
||||
memKeys
|
||||
);
|
||||
const rlnDecoder = new RLNDecoder(
|
||||
rlnInstance,
|
||||
new Decoder(TestContentTopic)
|
||||
createDecoder(TestContentTopic)
|
||||
);
|
||||
|
||||
const proto = await rlnEncoder.toProtoObj({ payload });
|
||||
|
||||
expect(proto).to.not.be.undefined;
|
||||
const msg = (await rlnDecoder.fromProtoObj(
|
||||
"",
|
||||
EMPTY_PUBSUB_TOPIC,
|
||||
proto!
|
||||
)) as RlnMessage<DecodedMessage>;
|
||||
|
||||
|
||||
40
src/codec.ts
40
src/codec.ts
@ -14,8 +14,6 @@ import { MembershipKey, RLNInstance } from "./rln.js";
|
||||
const log = debug("waku:rln:encoder");
|
||||
|
||||
export class RLNEncoder implements IEncoder {
|
||||
public contentTopic: string;
|
||||
public ephemeral = false;
|
||||
private readonly idKey: Uint8Array;
|
||||
|
||||
constructor(
|
||||
@ -26,7 +24,6 @@ export class RLNEncoder implements IEncoder {
|
||||
) {
|
||||
if (index < 0) throw "invalid membership index";
|
||||
this.idKey = membershipKey.IDKey;
|
||||
this.contentTopic = encoder.contentTopic;
|
||||
}
|
||||
|
||||
async toWire(message: IMessage): Promise<Uint8Array | undefined> {
|
||||
@ -39,7 +36,7 @@ export class RLNEncoder implements IEncoder {
|
||||
const protoMessage = await this.encoder.toProtoObj(message);
|
||||
if (!protoMessage) return;
|
||||
|
||||
protoMessage.contentTopic = this.encoder.contentTopic;
|
||||
protoMessage.contentTopic = this.contentTopic;
|
||||
protoMessage.rateLimitProof = await this.generateProof(message);
|
||||
log("Proof generated", protoMessage.rateLimitProof);
|
||||
return protoMessage;
|
||||
@ -58,8 +55,32 @@ export class RLNEncoder implements IEncoder {
|
||||
console.timeEnd("proof_gen_timer");
|
||||
return proof;
|
||||
}
|
||||
|
||||
get contentTopic(): string {
|
||||
return this.encoder.contentTopic;
|
||||
}
|
||||
|
||||
get ephemeral(): boolean {
|
||||
return this.encoder.ephemeral;
|
||||
}
|
||||
}
|
||||
|
||||
type RLNEncoderOptions = {
|
||||
encoder: IEncoder;
|
||||
rlnInstance: RLNInstance;
|
||||
index: number;
|
||||
membershipKey: MembershipKey;
|
||||
};
|
||||
|
||||
export const createRLNEncoder = (options: RLNEncoderOptions): RLNEncoder => {
|
||||
return new RLNEncoder(
|
||||
options.encoder,
|
||||
options.rlnInstance,
|
||||
options.index,
|
||||
options.membershipKey
|
||||
);
|
||||
};
|
||||
|
||||
export class RLNDecoder<T extends IDecodedMessage>
|
||||
implements IDecoder<RlnMessage<T>>
|
||||
{
|
||||
@ -87,3 +108,14 @@ export class RLNDecoder<T extends IDecodedMessage>
|
||||
return new RlnMessage(this.rlnInstance, msg, proto.rateLimitProof);
|
||||
}
|
||||
}
|
||||
|
||||
type RLNDecoderOptions<T extends IDecodedMessage> = {
|
||||
decoder: IDecoder<T>;
|
||||
rlnInstance: RLNInstance;
|
||||
};
|
||||
|
||||
export const createRLNDecoder = <T extends IDecodedMessage>(
|
||||
options: RLNDecoderOptions<T>
|
||||
): RLNDecoder<T> => {
|
||||
return new RLNDecoder(options.rlnInstance, options.decoder);
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user