feat: decode epoch to int

This commit is contained in:
fryorcraken.eth 2022-09-28 13:51:53 +10:00
parent 4463c4afab
commit 50c30f1332
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
5 changed files with 38 additions and 9 deletions

7
package-lock.json generated
View File

@ -36,7 +36,7 @@
"eslint-plugin-functional": "^4.0.2",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"fast-check": "^2.14.0",
"fast-check": "^2.25.0",
"gh-pages": "^3.2.3",
"husky": "^7.0.4",
"ignore-loader": "^0.1.2",
@ -5052,8 +5052,9 @@
},
"node_modules/fast-check": {
"version": "2.25.0",
"resolved": "https://registry.npmjs.org/fast-check/-/fast-check-2.25.0.tgz",
"integrity": "sha512-wRUT2KD2lAmT75WNIJIHECawoUUMHM0I5jrlLXGtGeqmPL8jl/EldUDjY1VCp6fDY8yflyfUeIOsOBrIbIiArg==",
"dev": true,
"license": "MIT",
"dependencies": {
"pure-rand": "^5.0.1"
},
@ -14684,6 +14685,8 @@
},
"fast-check": {
"version": "2.25.0",
"resolved": "https://registry.npmjs.org/fast-check/-/fast-check-2.25.0.tgz",
"integrity": "sha512-wRUT2KD2lAmT75WNIJIHECawoUUMHM0I5jrlLXGtGeqmPL8jl/EldUDjY1VCp6fDY8yflyfUeIOsOBrIbIiArg==",
"dev": true,
"requires": {
"pure-rand": "^5.0.1"

View File

@ -76,7 +76,7 @@
"eslint-plugin-functional": "^4.0.2",
"eslint-plugin-import": "^2.25.3",
"eslint-plugin-prettier": "^4.0.0",
"fast-check": "^2.14.0",
"fast-check": "^2.25.0",
"gh-pages": "^3.2.3",
"husky": "^7.0.4",
"ignore-loader": "^0.1.2",

17
src/epoch.spec.ts Normal file
View File

@ -0,0 +1,17 @@
import { expect } from "chai";
import fc from "fast-check";
import { epochBytesToInt, epochIntToBytes } from "./epoch.js";
describe("epoch serialization", () => {
it("Round trip", async function () {
await fc.assert(
fc.asyncProperty(fc.integer({ min: 0 }), async (date) => {
const bytes = epochIntToBytes(date);
const _date = epochBytesToInt(bytes);
expect(_date.valueOf()).to.eq(date.valueOf());
})
);
});
});

View File

@ -5,7 +5,16 @@ const DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds
export function dateToEpoch(
timestamp: Date,
epochUnitSeconds: number = DefaultEpochUnitSeconds
): Uint8Array {
const unix = Math.floor(timestamp.getTime() / 1000 / epochUnitSeconds);
return writeUIntLE(new Uint8Array(32), unix, 0, 8);
): number {
const time = timestamp.getTime();
return Math.floor(time / 1000 / epochUnitSeconds);
}
export function epochIntToBytes(epoch: number): Uint8Array {
return writeUIntLE(new Uint8Array(32), epoch, 0, 8);
}
export function epochBytesToInt(bytes: Uint8Array): number {
const dv = new DataView(bytes.buffer);
return dv.getUint32(0, true);
}

View File

@ -2,7 +2,7 @@ import init, * as zerokitRLN from "@waku/zerokit-rln-wasm";
import { RateLimitProof } from "js-waku/lib/interfaces";
import { writeUIntLE } from "./byte_utils.js";
import { dateToEpoch } from "./epoch.js";
import { dateToEpoch, epochIntToBytes } from "./epoch.js";
import verificationKey from "./resources/verification_key.js";
import * as wc from "./witness_calculator.js";
import { WitnessCalculator } from "./witness_calculator.js";
@ -152,9 +152,9 @@ export class RLNInstance {
idKey: Uint8Array
): Promise<RateLimitProof> {
if (epoch == undefined) {
epoch = dateToEpoch(new Date());
epoch = epochIntToBytes(dateToEpoch(new Date()));
} else if (epoch instanceof Date) {
epoch = dateToEpoch(epoch);
epoch = epochIntToBytes(dateToEpoch(epoch));
}
if (epoch.length != 32) throw "invalid epoch";