UNPKG

@waku/rln

Version:

RLN (Rate Limiting Nullifier) implementation for Waku

77 lines 2.65 kB
import { Logger } from "@waku/utils"; import { RlnMessage, toRLNSignal } from "./message.js"; const log = new Logger("waku:rln:encoder"); export class RLNEncoder { encoder; rlnInstance; index; idSecretHash; constructor(encoder, rlnInstance, index, identityCredential) { this.encoder = encoder; this.rlnInstance = rlnInstance; this.index = index; if (index < 0) throw new Error("Invalid membership index"); this.idSecretHash = identityCredential.IDSecretHash; } async toWire(message) { message.rateLimitProof = await this.generateProof(message); log.info("Proof generated", message.rateLimitProof); return this.encoder.toWire(message); } async toProtoObj(message) { const protoMessage = await this.encoder.toProtoObj(message); if (!protoMessage) return; protoMessage.contentTopic = this.contentTopic; protoMessage.rateLimitProof = await this.generateProof(message); log.info("Proof generated", protoMessage.rateLimitProof); return protoMessage; } async generateProof(message) { const signal = toRLNSignal(this.contentTopic, message); const proof = await this.rlnInstance.zerokit.generateRLNProof(signal, this.index, message.timestamp, this.idSecretHash); return proof; } get pubsubTopic() { return this.encoder.pubsubTopic; } get contentTopic() { return this.encoder.contentTopic; } get ephemeral() { return this.encoder.ephemeral; } } export const createRLNEncoder = (options) => { return new RLNEncoder(options.encoder, options.rlnInstance, options.index, options.credential); }; export class RLNDecoder { rlnInstance; decoder; constructor(rlnInstance, decoder) { this.rlnInstance = rlnInstance; this.decoder = decoder; } get pubsubTopic() { return this.decoder.pubsubTopic; } get contentTopic() { return this.decoder.contentTopic; } fromWireToProtoObj(bytes) { const protoMessage = this.decoder.fromWireToProtoObj(bytes); log.info("Message decoded", protoMessage); return Promise.resolve(protoMessage); } async fromProtoObj(pubsubTopic, proto) { const msg = await this.decoder.fromProtoObj(pubsubTopic, proto); if (!msg) return; return new RlnMessage(this.rlnInstance, msg, proto.rateLimitProof); } } export const createRLNDecoder = (options) => { return new RLNDecoder(options.rlnInstance, options.decoder); }; //# sourceMappingURL=codec.js.map