2022-09-26 12:08:28 -04:00
|
|
|
import { expect } from "chai";
|
2022-09-28 14:23:10 +10:00
|
|
|
import { DecoderV0, EncoderV0 } from "js-waku/lib/waku_message/version_0";
|
2022-09-26 12:08:28 -04:00
|
|
|
|
2022-09-28 12:54:04 +10:00
|
|
|
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
2022-09-26 14:58:03 -04:00
|
|
|
|
|
|
|
|
import * as rln from "./index.js";
|
2022-09-26 12:08:28 -04:00
|
|
|
|
|
|
|
|
const TestContentTopic = "/test/1/waku-message/utf8";
|
2022-09-25 11:40:49 -04:00
|
|
|
|
|
|
|
|
describe("js-rln: encoder", () => {
|
|
|
|
|
it("should attach a proof to a waku message", async function () {
|
2022-09-26 12:08:28 -04:00
|
|
|
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
|
|
|
|
|
);
|
2022-09-28 14:23:10 +10:00
|
|
|
const rlnDecoder = new RLNDecoder(
|
|
|
|
|
rlnInstance,
|
|
|
|
|
new DecoderV0(TestContentTopic)
|
|
|
|
|
);
|
2022-09-26 12:08:28 -04:00
|
|
|
|
|
|
|
|
const bytes = await rlnEncoder.encode({ payload });
|
|
|
|
|
const protoResult = await rlnDecoder.decodeProto(bytes!);
|
|
|
|
|
|
2022-09-26 14:58:03 -04:00
|
|
|
const msg = (await rlnDecoder.decode(protoResult!))!;
|
|
|
|
|
|
|
|
|
|
// Validate proof
|
2022-09-28 14:23:10 +10:00
|
|
|
expect(msg.verify()).to.be.true;
|
|
|
|
|
|
|
|
|
|
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;
|
2022-09-25 11:40:49 -04:00
|
|
|
});
|
|
|
|
|
});
|