mirror of
https://github.com/logos-messaging/js-rln.git
synced 2026-01-07 16:13:07 +00:00
fix: epoch decoding
This commit is contained in:
parent
6ecdc598d5
commit
da8c4b2204
@ -17,6 +17,7 @@ import {
|
|||||||
} from "js-waku/lib/waku_message/version_1";
|
} from "js-waku/lib/waku_message/version_1";
|
||||||
|
|
||||||
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
||||||
|
import { epochBytesToInt } from "./epoch.js";
|
||||||
import { RlnMessage } from "./message.js";
|
import { RlnMessage } from "./message.js";
|
||||||
|
|
||||||
import * as rln from "./index.js";
|
import * as rln from "./index.js";
|
||||||
@ -47,7 +48,6 @@ describe("RLN codec with version 0", () => {
|
|||||||
|
|
||||||
expect(bytes).to.not.be.undefined;
|
expect(bytes).to.not.be.undefined;
|
||||||
const protoResult = await rlnDecoder.fromWireToProtoObj(bytes!);
|
const protoResult = await rlnDecoder.fromWireToProtoObj(bytes!);
|
||||||
|
|
||||||
expect(protoResult).to.not.be.undefined;
|
expect(protoResult).to.not.be.undefined;
|
||||||
const msg = (await rlnDecoder.fromProtoObj(protoResult!))!;
|
const msg = (await rlnDecoder.fromProtoObj(protoResult!))!;
|
||||||
|
|
||||||
@ -267,3 +267,48 @@ describe("RLN codec with version 1", () => {
|
|||||||
expect(msg.timestamp).to.not.be.undefined;
|
expect(msg.timestamp).to.not.be.undefined;
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe("RLN Codec - epoch", () => {
|
||||||
|
it("toProtoObj", async function () {
|
||||||
|
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
|
||||||
|
);
|
||||||
|
const rlnDecoder = new RLNDecoder(
|
||||||
|
rlnInstance,
|
||||||
|
new DecoderV0(TestContentTopic)
|
||||||
|
);
|
||||||
|
|
||||||
|
const proto = await rlnEncoder.toProtoObj({ payload });
|
||||||
|
|
||||||
|
expect(proto).to.not.be.undefined;
|
||||||
|
const msg = (await rlnDecoder.fromProtoObj(
|
||||||
|
proto!
|
||||||
|
)) as RlnMessage<MessageV0>;
|
||||||
|
|
||||||
|
const epochBytes = proto!.rateLimitProof!.epoch;
|
||||||
|
const epoch = epochBytesToInt(epochBytes);
|
||||||
|
|
||||||
|
expect(msg).to.not.be.undefined;
|
||||||
|
expect(msg.rateLimitProof).to.not.be.undefined;
|
||||||
|
|
||||||
|
expect(msg.verify()).to.be.true;
|
||||||
|
expect(msg.epoch).to.not.be.undefined;
|
||||||
|
expect(msg.epoch!.toString(10).length).to.eq(9);
|
||||||
|
expect(msg.epoch).to.eq(epoch);
|
||||||
|
|
||||||
|
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;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|||||||
@ -23,7 +23,7 @@ export function epochIntToBytes(epoch: number): Uint8Array {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export function epochBytesToInt(bytes: Uint8Array): number {
|
export function epochBytesToInt(bytes: Uint8Array): number {
|
||||||
const dv = new DataView(bytes.buffer);
|
const dv = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
||||||
const epoch = dv.getUint32(0, true);
|
const epoch = dv.getUint32(0, true);
|
||||||
log("decoded epoch", epoch, bytes);
|
log("decoded epoch", epoch, bytes);
|
||||||
return epoch;
|
return epoch;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user