2022-09-25 11:40:49 -04:00
|
|
|
import { Encoder, Message, ProtoMessage } from "js-waku/lib/interfaces";
|
2022-09-26 10:09:39 +10:00
|
|
|
import { proto_message } from "js-waku";
|
2022-09-25 11:40:49 -04:00
|
|
|
|
|
|
|
|
import { RLNInstance } from "./rln.js";
|
|
|
|
|
|
|
|
|
|
export class RLNEncoder implements Encoder {
|
|
|
|
|
constructor(
|
|
|
|
|
public contentTopic: string,
|
|
|
|
|
private encoder: Encoder,
|
|
|
|
|
private rlnInstance: RLNInstance,
|
|
|
|
|
private index: number,
|
|
|
|
|
private idKey: Uint8Array
|
|
|
|
|
) {
|
|
|
|
|
if (idKey.length != 32) throw "invalid id key"; // TODO: use proper err message
|
|
|
|
|
if (index < 0) throw "invalid membership index";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async encode(message: Message): Promise<Uint8Array | undefined> {
|
|
|
|
|
const protoMessage = await this.encodeProto(message);
|
|
|
|
|
if (!protoMessage) return;
|
|
|
|
|
|
2022-09-26 10:09:39 +10:00
|
|
|
return proto_message.WakuMessage.encode(protoMessage);
|
2022-09-25 11:40:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
toRLNSignal(msg: Message): Uint8Array {
|
|
|
|
|
const contentTopic = msg.contentTopic ?? "";
|
|
|
|
|
const contentTopicBytes = Uint8Array.from(
|
|
|
|
|
contentTopic.split("").map((x: string) => x.charCodeAt(0))
|
|
|
|
|
);
|
|
|
|
|
return new Uint8Array([...(msg.payload ?? []), ...contentTopicBytes]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async encodeProto(message: Message): Promise<ProtoMessage | undefined> {
|
|
|
|
|
const protoMessage = await this.encoder.encodeProto(message);
|
|
|
|
|
if (!protoMessage) return;
|
|
|
|
|
|
|
|
|
|
const signal = this.toRLNSignal(message);
|
|
|
|
|
|
|
|
|
|
console.time("proof_gen_timer");
|
|
|
|
|
const proof = await this.rlnInstance.generateProof(
|
|
|
|
|
signal,
|
|
|
|
|
this.index,
|
|
|
|
|
message.timestamp,
|
|
|
|
|
this.idKey
|
|
|
|
|
);
|
|
|
|
|
console.timeEnd("proof_gen_timer");
|
|
|
|
|
|
|
|
|
|
protoMessage.rateLimitProof = proof;
|
|
|
|
|
|
|
|
|
|
return protoMessage;
|
|
|
|
|
}
|
|
|
|
|
}
|