-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathrln.ts
More file actions
83 lines (70 loc) · 2.44 KB
/
Copy pathrln.ts
File metadata and controls
83 lines (70 loc) · 2.44 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { Logger } from "@waku/utils";
import init, * as zerokitRLN from "@waku/zerokit-rln-wasm";
import initUtils from "@waku/zerokit-rln-wasm-utils";
import { DEFAULT_RATE_LIMIT } from "./contract/constants.js";
import { RLNCredentialsManager } from "./credentials_manager.js";
import * as wc from "./resources/witness_calculator";
import { WitnessCalculator } from "./resources/witness_calculator";
import { Zerokit } from "./zerokit.js";
const log = new Logger("rln");
export class RLNInstance extends RLNCredentialsManager {
/**
* Create an instance of RLN
* @returns RLNInstance
*/
public static async create(): Promise<RLNInstance> {
try {
await initUtils();
await init();
zerokitRLN.initPanicHook();
const witnessCalculator = await RLNInstance.loadWitnessCalculator();
const zkey = await RLNInstance.loadZkey();
const zkRLN = zerokitRLN.newRLN(zkey);
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
return new RLNInstance(zerokit);
} catch (error) {
log.error("Failed to initialize RLN:", error);
throw error;
}
}
private constructor(public zerokit: Zerokit) {
super(zerokit);
}
public static async loadWitnessCalculator(): Promise<WitnessCalculator> {
try {
const url = new URL("./resources/rln.wasm", import.meta.url);
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch witness calculator: ${response.status} ${response.statusText}`
);
}
return await wc.builder(
new Uint8Array(await response.arrayBuffer()),
false
);
} catch (error) {
log.error("Error loading witness calculator:", error);
throw new Error(
`Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`
);
}
}
public static async loadZkey(): Promise<Uint8Array> {
try {
const url = new URL("./resources/rln_final.zkey", import.meta.url);
const response = await fetch(url);
if (!response.ok) {
throw new Error(
`Failed to fetch zkey: ${response.status} ${response.statusText}`
);
}
return new Uint8Array(await response.arrayBuffer());
} catch (error) {
log.error("Error loading zkey:", error);
throw new Error(
`Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`
);
}
}
}