UNPKG

@waku/rln

Version:

RLN (Rate Limiting Nullifier) implementation for Waku

94 lines (91 loc) 3.6 kB
import '../../interfaces/dist/protocols.js'; import '../../interfaces/dist/connection_manager.js'; import '../../interfaces/dist/health_status.js'; import '../../../node_modules/multiformats/dist/src/bases/base10.js'; import '../../../node_modules/multiformats/dist/src/bases/base16.js'; import '../../../node_modules/multiformats/dist/src/bases/base2.js'; import '../../../node_modules/multiformats/dist/src/bases/base256emoji.js'; import '../../../node_modules/multiformats/dist/src/bases/base32.js'; import '../../../node_modules/multiformats/dist/src/bases/base36.js'; import '../../../node_modules/multiformats/dist/src/bases/base58.js'; import '../../../node_modules/multiformats/dist/src/bases/base64.js'; import '../../../node_modules/multiformats/dist/src/bases/base8.js'; import '../../../node_modules/multiformats/dist/src/bases/identity.js'; import '../../../node_modules/multiformats/dist/src/codecs/json.js'; import { Logger } from '../../utils/dist/logger.js'; import { toRLNSignal, RlnMessage } from './message.js'; const log = new Logger("waku:rln:encoder"); 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; } } const createRLNEncoder = (options) => { return new RLNEncoder(options.encoder, options.rlnInstance, options.index, options.credential); }; 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); } } const createRLNDecoder = (options) => { return new RLNDecoder(options.rlnInstance, options.decoder); }; export { RLNDecoder, RLNEncoder, createRLNDecoder, createRLNEncoder };