mirror of
https://github.com/logos-messaging/js-rln.git
synced 2026-01-04 06:33:09 +00:00
refactor: extract helper functions
This commit is contained in:
parent
84803d77e5
commit
4463c4afab
39
src/byte_utils.ts
Normal file
39
src/byte_utils.ts
Normal 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
11
src/epoch.ts
Normal 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);
|
||||||
|
}
|
||||||
56
src/rln.ts
56
src/rln.ts
@ -1,6 +1,8 @@
|
|||||||
import init, * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
import init, * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
||||||
import { RateLimitProof } from "js-waku/lib/interfaces";
|
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 verificationKey from "./resources/verification_key.js";
|
||||||
import * as wc from "./witness_calculator.js";
|
import * as wc from "./witness_calculator.js";
|
||||||
import { WitnessCalculator } 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 proofOffset = 128;
|
||||||
const rootOffset = proofOffset + 32;
|
const rootOffset = proofOffset + 32;
|
||||||
const epochOffset = rootOffset + 32;
|
const epochOffset = rootOffset + 32;
|
||||||
@ -200,9 +152,9 @@ export class RLNInstance {
|
|||||||
idKey: Uint8Array
|
idKey: Uint8Array
|
||||||
): Promise<RateLimitProof> {
|
): Promise<RateLimitProof> {
|
||||||
if (epoch == undefined) {
|
if (epoch == undefined) {
|
||||||
epoch = toEpoch(new Date());
|
epoch = dateToEpoch(new Date());
|
||||||
} else if (epoch instanceof Date) {
|
} else if (epoch instanceof Date) {
|
||||||
epoch = toEpoch(epoch);
|
epoch = dateToEpoch(epoch);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (epoch.length != 32) throw "invalid epoch";
|
if (epoch.length != 32) throw "invalid epoch";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user