From 4463c4afabc09b2cd09f1ea64b0fa59957457954 Mon Sep 17 00:00:00 2001 From: "fryorcraken.eth" Date: Wed, 28 Sep 2022 13:05:10 +1000 Subject: [PATCH] refactor: extract helper functions --- src/byte_utils.ts | 39 +++++++++++++++++++++++++++++++++ src/epoch.ts | 11 ++++++++++ src/rln.ts | 56 ++++------------------------------------------- 3 files changed, 54 insertions(+), 52 deletions(-) create mode 100644 src/byte_utils.ts create mode 100644 src/epoch.ts diff --git a/src/byte_utils.ts b/src/byte_utils.ts new file mode 100644 index 0000000..99c9671 --- /dev/null +++ b/src/byte_utils.ts @@ -0,0 +1,39 @@ +// Adapted from https://github.com/feross/buffer + +function checkInt( + buf: Uint8Array, + value: number, + offset: number, + ext: number, + max: number, + min: number +): void { + if (value > max || value < min) + throw new RangeError('"value" argument is out of bounds'); + if (offset + ext > buf.length) throw new RangeError("Index out of range"); +} + +export function writeUIntLE( + buf: Uint8Array, + value: number, + offset: number, + byteLength: number, + noAssert?: boolean +): Uint8Array { + value = +value; + offset = offset >>> 0; + byteLength = byteLength >>> 0; + if (!noAssert) { + const maxBytes = Math.pow(2, 8 * byteLength) - 1; + checkInt(buf, value, offset, byteLength, maxBytes, 0); + } + + let mul = 1; + let i = 0; + buf[offset] = value & 0xff; + while (++i < byteLength && (mul *= 0x100)) { + buf[offset + i] = (value / mul) & 0xff; + } + + return buf; +} diff --git a/src/epoch.ts b/src/epoch.ts new file mode 100644 index 0000000..3a14bd2 --- /dev/null +++ b/src/epoch.ts @@ -0,0 +1,11 @@ +import { writeUIntLE } from "./byte_utils.js"; + +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); +} diff --git a/src/rln.ts b/src/rln.ts index 1140daf..d82931d 100644 --- a/src/rln.ts +++ b/src/rln.ts @@ -1,6 +1,8 @@ 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 verificationKey from "./resources/verification_key.js"; import * as wc from "./witness_calculator.js"; import { WitnessCalculator } from "./witness_calculator.js"; @@ -67,56 +69,6 @@ export class MembershipKey { } } -// Adapted from https://github.com/feross/buffer - -function checkInt( - buf: Uint8Array, - value: number, - offset: number, - ext: number, - max: number, - min: number -): void { - if (value > max || value < min) - throw new RangeError('"value" argument is out of bounds'); - if (offset + ext > buf.length) throw new RangeError("Index out of range"); -} - -const writeUIntLE = function writeUIntLE( - buf: Uint8Array, - value: number, - offset: number, - byteLength: number, - noAssert?: boolean -): Uint8Array { - value = +value; - offset = offset >>> 0; - byteLength = byteLength >>> 0; - if (!noAssert) { - const maxBytes = Math.pow(2, 8 * byteLength) - 1; - checkInt(buf, value, offset, byteLength, maxBytes, 0); - } - - let mul = 1; - let i = 0; - buf[offset] = value & 0xff; - while (++i < byteLength && (mul *= 0x100)) { - buf[offset + i] = (value / mul) & 0xff; - } - - return buf; -}; - -const DefaultEpochUnitSeconds = 10; // the rln-relay epoch length in seconds - -export function toEpoch( - timestamp: Date, - epochUnitSeconds: number = DefaultEpochUnitSeconds -): Uint8Array { - const unix = Math.floor(timestamp.getTime() / 1000 / epochUnitSeconds); - return writeUIntLE(new Uint8Array(32), unix, 0, 8); -} - const proofOffset = 128; const rootOffset = proofOffset + 32; const epochOffset = rootOffset + 32; @@ -200,9 +152,9 @@ export class RLNInstance { idKey: Uint8Array ): Promise { if (epoch == undefined) { - epoch = toEpoch(new Date()); + epoch = dateToEpoch(new Date()); } else if (epoch instanceof Date) { - epoch = toEpoch(epoch); + epoch = dateToEpoch(epoch); } if (epoch.length != 32) throw "invalid epoch";