refactor: extract helper functions

This commit is contained in:
fryorcraken.eth 2022-09-28 13:05:10 +10:00
parent 84803d77e5
commit 4463c4afab
No known key found for this signature in database
GPG Key ID: A82ED75A8DFC50A4
3 changed files with 54 additions and 52 deletions

39
src/byte_utils.ts Normal file
View File

@ -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;
}

11
src/epoch.ts Normal file
View File

@ -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);
}

View File

@ -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<RateLimitProof> {
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";