@waku/rln 0.0.2-c41b319.0 → 0.0.2-caeafce.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/bundle/index.js +1 -1
- package/bundle/packages/interfaces/dist/protocols.js +40 -45
- package/bundle/packages/rln/dist/contract/abi.js +648 -0
- package/bundle/packages/rln/dist/contract/constants.js +8 -13
- package/bundle/packages/rln/dist/contract/rln_contract.js +14 -3
- package/bundle/packages/rln/dist/identity.js +0 -24
- package/bundle/packages/rln/dist/rln.js +39 -15
- package/bundle/packages/rln/dist/zerokit.js +22 -16
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/{abi/rlnv2.d.ts → abi.d.ts} +22 -18
- package/dist/contract/abi.js +647 -0
- package/dist/contract/abi.js.map +1 -0
- package/dist/contract/constants.d.ts +22 -23
- package/dist/contract/constants.js +7 -12
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/rln_contract.d.ts +8 -0
- package/dist/contract/rln_contract.js +14 -3
- package/dist/contract/rln_contract.js.map +1 -1
- package/dist/identity.d.ts +0 -1
- package/dist/identity.js +0 -24
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/rln.js +39 -14
- package/dist/rln.js.map +1 -1
- package/dist/zerokit.d.ts +5 -1
- package/dist/zerokit.js +22 -16
- package/dist/zerokit.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/abi.ts +646 -0
- package/src/contract/constants.ts +8 -14
- package/src/contract/rln_contract.ts +16 -3
- package/src/identity.ts +0 -42
- package/src/index.ts +2 -2
- package/src/rln.ts +54 -14
- package/src/zerokit.ts +45 -16
- package/bundle/node_modules/@iden3/js-crypto/dist/browser/esm/index.js +0 -7
- package/bundle/node_modules/@stablelib/binary/lib/binary.js +0 -22
- package/bundle/node_modules/@stablelib/chacha/lib/chacha.js +0 -245
- package/bundle/node_modules/@stablelib/wipe/lib/wipe.js +0 -26
- package/bundle/packages/rln/dist/contract/abi/rlnv2.js +0 -394
- package/dist/contract/abi/rlnv2.js +0 -393
- package/dist/contract/abi/rlnv2.js.map +0 -1
- package/src/contract/abi/rlnv2.ts +0 -392
@@ -8,7 +8,7 @@ import type { RLNInstance } from "../rln.js";
|
|
8
8
|
import { MerkleRootTracker } from "../root_tracker.js";
|
9
9
|
import { zeroPadLE } from "../utils/bytes.js";
|
10
10
|
|
11
|
-
import {
|
11
|
+
import { RLN_ABI } from "./abi.js";
|
12
12
|
import { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from "./constants.js";
|
13
13
|
|
14
14
|
const log = new Logger("waku:rln:contract");
|
@@ -108,8 +108,7 @@ export class RLNContract {
|
|
108
108
|
const initialRoot = rlnInstance.zerokit.getMerkleRoot();
|
109
109
|
|
110
110
|
// Use the injected contract if provided; otherwise, instantiate a new one.
|
111
|
-
this.contract =
|
112
|
-
contract || new ethers.Contract(address, RLN_V2_ABI, signer);
|
111
|
+
this.contract = contract || new ethers.Contract(address, RLN_ABI, signer);
|
113
112
|
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
114
113
|
|
115
114
|
// Initialize event filters for MembershipRegistered and MembershipRemoved
|
@@ -124,6 +123,20 @@ export class RLNContract {
|
|
124
123
|
return this.rateLimit;
|
125
124
|
}
|
126
125
|
|
126
|
+
/**
|
127
|
+
* Gets the contract address
|
128
|
+
*/
|
129
|
+
public get address(): string {
|
130
|
+
return this.contract.address;
|
131
|
+
}
|
132
|
+
|
133
|
+
/**
|
134
|
+
* Gets the contract provider
|
135
|
+
*/
|
136
|
+
public get provider(): ethers.providers.Provider {
|
137
|
+
return this.contract.provider;
|
138
|
+
}
|
139
|
+
|
127
140
|
/**
|
128
141
|
* Gets the minimum allowed rate limit from the contract
|
129
142
|
* @returns Promise<number> The minimum rate limit in messages per epoch
|
package/src/identity.ts
CHANGED
@@ -1,8 +1,3 @@
|
|
1
|
-
import { arrayify } from "@ethersproject/bytes";
|
2
|
-
import { keccak256 } from "@ethersproject/keccak256";
|
3
|
-
import { Poseidon } from "@iden3/js-crypto";
|
4
|
-
import { streamXOR } from "@stablelib/chacha";
|
5
|
-
|
6
1
|
import { buildBigIntFromUint8Array } from "./utils/index.js";
|
7
2
|
|
8
3
|
export class IdentityCredential {
|
@@ -33,41 +28,4 @@ export class IdentityCredential {
|
|
33
28
|
idCommitmentBigInt
|
34
29
|
);
|
35
30
|
}
|
36
|
-
|
37
|
-
public static generateSeeded(signature: Uint8Array): IdentityCredential {
|
38
|
-
// Generate deterministic seed from signature
|
39
|
-
const seed = arrayify(keccak256(signature));
|
40
|
-
|
41
|
-
// Use ChaCha for deterministic randomness (as in Rust code)
|
42
|
-
const nonce = new Uint8Array(12);
|
43
|
-
const idSecretHash = new Uint8Array(32);
|
44
|
-
streamXOR(seed, nonce, idSecretHash, idSecretHash);
|
45
|
-
|
46
|
-
// Convert to bigint for Poseidon
|
47
|
-
const secretBigInt = BigInt(
|
48
|
-
"0x" + Buffer.from(idSecretHash).toString("hex")
|
49
|
-
);
|
50
|
-
|
51
|
-
// Generate commitment using Poseidon
|
52
|
-
const idCommitmentBigInt = Poseidon.hash([secretBigInt]);
|
53
|
-
|
54
|
-
// Convert commitment back to Uint8Array
|
55
|
-
const idCommitment = arrayify(
|
56
|
-
"0x" + idCommitmentBigInt.toString(16).padStart(64, "0")
|
57
|
-
);
|
58
|
-
|
59
|
-
// Generate deterministic trapdoor and nullifier from the secret hash
|
60
|
-
const idTrapdoor = new Uint8Array(32);
|
61
|
-
const idNullifier = new Uint8Array(32);
|
62
|
-
streamXOR(idSecretHash, nonce, idTrapdoor, idTrapdoor);
|
63
|
-
streamXOR(idTrapdoor, nonce, idNullifier, idNullifier);
|
64
|
-
|
65
|
-
return new IdentityCredential(
|
66
|
-
idTrapdoor,
|
67
|
-
idNullifier,
|
68
|
-
idSecretHash,
|
69
|
-
idCommitment,
|
70
|
-
idCommitmentBigInt
|
71
|
-
);
|
72
|
-
}
|
73
31
|
}
|
package/src/index.ts
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
2
|
-
import {
|
2
|
+
import { RLN_ABI } from "./contract/abi.js";
|
3
3
|
import { RLNContract, SEPOLIA_CONTRACT } from "./contract/index.js";
|
4
4
|
import { createRLN } from "./create.js";
|
5
5
|
import { IdentityCredential } from "./identity.js";
|
@@ -21,5 +21,5 @@ export {
|
|
21
21
|
RLNContract,
|
22
22
|
SEPOLIA_CONTRACT,
|
23
23
|
extractMetaMaskSigner,
|
24
|
-
|
24
|
+
RLN_ABI
|
25
25
|
};
|
package/src/rln.ts
CHANGED
@@ -15,6 +15,7 @@ import {
|
|
15
15
|
type RLNDecoder,
|
16
16
|
type RLNEncoder
|
17
17
|
} from "./codec.js";
|
18
|
+
import { DEFAULT_RATE_LIMIT } from "./contract/constants.js";
|
18
19
|
import { RLNContract, SEPOLIA_CONTRACT } from "./contract/index.js";
|
19
20
|
import { IdentityCredential } from "./identity.js";
|
20
21
|
import { Keystore } from "./keystore/index.js";
|
@@ -32,15 +33,46 @@ import { Zerokit } from "./zerokit.js";
|
|
32
33
|
const log = new Logger("waku:rln");
|
33
34
|
|
34
35
|
async function loadWitnessCalculator(): Promise<WitnessCalculator> {
|
35
|
-
|
36
|
-
|
37
|
-
|
36
|
+
try {
|
37
|
+
const url = new URL("./resources/rln.wasm", import.meta.url);
|
38
|
+
const response = await fetch(url);
|
39
|
+
|
40
|
+
if (!response.ok) {
|
41
|
+
throw new Error(
|
42
|
+
`Failed to fetch witness calculator: ${response.status} ${response.statusText}`
|
43
|
+
);
|
44
|
+
}
|
45
|
+
|
46
|
+
return await wc.builder(
|
47
|
+
new Uint8Array(await response.arrayBuffer()),
|
48
|
+
false
|
49
|
+
);
|
50
|
+
} catch (error) {
|
51
|
+
log.error("Error loading witness calculator:", error);
|
52
|
+
throw new Error(
|
53
|
+
`Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`
|
54
|
+
);
|
55
|
+
}
|
38
56
|
}
|
39
57
|
|
40
58
|
async function loadZkey(): Promise<Uint8Array> {
|
41
|
-
|
42
|
-
|
43
|
-
|
59
|
+
try {
|
60
|
+
const url = new URL("./resources/rln_final.zkey", import.meta.url);
|
61
|
+
const response = await fetch(url);
|
62
|
+
|
63
|
+
if (!response.ok) {
|
64
|
+
throw new Error(
|
65
|
+
`Failed to fetch zkey: ${response.status} ${response.statusText}`
|
66
|
+
);
|
67
|
+
}
|
68
|
+
|
69
|
+
return new Uint8Array(await response.arrayBuffer());
|
70
|
+
} catch (error) {
|
71
|
+
log.error("Error loading zkey:", error);
|
72
|
+
throw new Error(
|
73
|
+
`Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`
|
74
|
+
);
|
75
|
+
}
|
44
76
|
}
|
45
77
|
|
46
78
|
/**
|
@@ -61,7 +93,7 @@ export async function create(): Promise<RLNInstance> {
|
|
61
93
|
|
62
94
|
const DEPTH = 20;
|
63
95
|
const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
|
64
|
-
const zerokit = new Zerokit(zkRLN, witnessCalculator);
|
96
|
+
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
|
65
97
|
|
66
98
|
return new RLNInstance(zerokit);
|
67
99
|
} catch (error) {
|
@@ -119,6 +151,8 @@ export class RLNInstance {
|
|
119
151
|
}
|
120
152
|
|
121
153
|
public async start(options: StartRLNOptions = {}): Promise<void> {
|
154
|
+
// eslint-disable-next-line no-console
|
155
|
+
console.log("starting", options);
|
122
156
|
if (this.started || this.starting) {
|
123
157
|
return;
|
124
158
|
}
|
@@ -142,7 +176,7 @@ export class RLNInstance {
|
|
142
176
|
this._contract = await RLNContract.init(this, {
|
143
177
|
address: address!,
|
144
178
|
signer: signer!,
|
145
|
-
rateLimit: options.rateLimit
|
179
|
+
rateLimit: options.rateLimit ?? this.zerokit.getRateLimit
|
146
180
|
});
|
147
181
|
this.started = true;
|
148
182
|
} finally {
|
@@ -164,6 +198,13 @@ export class RLNInstance {
|
|
164
198
|
chainId = SEPOLIA_CONTRACT.chainId;
|
165
199
|
}
|
166
200
|
|
201
|
+
// eslint-disable-next-line no-console
|
202
|
+
console.log({
|
203
|
+
chainId,
|
204
|
+
address,
|
205
|
+
SEPOLIA_CONTRACT
|
206
|
+
});
|
207
|
+
|
167
208
|
const signer = options.signer || (await extractMetaMaskSigner());
|
168
209
|
const currentChainId = await signer.getChainId();
|
169
210
|
|
@@ -214,12 +255,11 @@ export class RLNInstance {
|
|
214
255
|
throw Error("RLN Contract is not initialized.");
|
215
256
|
}
|
216
257
|
|
217
|
-
let identity
|
218
|
-
"identity" in options ? options.identity : undefined;
|
258
|
+
let identity = "identity" in options && options.identity;
|
219
259
|
|
220
260
|
if ("signature" in options) {
|
221
|
-
identity =
|
222
|
-
|
261
|
+
identity = this.zerokit.generateSeededIdentityCredential(
|
262
|
+
options.signature
|
223
263
|
);
|
224
264
|
}
|
225
265
|
|
@@ -272,7 +312,7 @@ export class RLNInstance {
|
|
272
312
|
}
|
273
313
|
|
274
314
|
const registryAddress = credentials.membership.address;
|
275
|
-
const currentRegistryAddress = this._contract.
|
315
|
+
const currentRegistryAddress = this._contract.address;
|
276
316
|
if (registryAddress !== currentRegistryAddress) {
|
277
317
|
throw Error(
|
278
318
|
`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`
|
@@ -280,7 +320,7 @@ export class RLNInstance {
|
|
280
320
|
}
|
281
321
|
|
282
322
|
const chainId = credentials.membership.chainId;
|
283
|
-
const network = await this._contract.
|
323
|
+
const network = await this._contract.provider.getNetwork();
|
284
324
|
const currentChainId = network.chainId;
|
285
325
|
if (chainId !== currentChainId) {
|
286
326
|
throw Error(
|
package/src/zerokit.ts
CHANGED
@@ -15,9 +15,22 @@ import {
|
|
15
15
|
export class Zerokit {
|
16
16
|
public constructor(
|
17
17
|
private readonly zkRLN: number,
|
18
|
-
private readonly witnessCalculator: WitnessCalculator
|
18
|
+
private readonly witnessCalculator: WitnessCalculator,
|
19
|
+
private readonly rateLimit: number = DEFAULT_RATE_LIMIT
|
19
20
|
) {}
|
20
21
|
|
22
|
+
public get getZkRLN(): number {
|
23
|
+
return this.zkRLN;
|
24
|
+
}
|
25
|
+
|
26
|
+
public get getWitnessCalculator(): WitnessCalculator {
|
27
|
+
return this.witnessCalculator;
|
28
|
+
}
|
29
|
+
|
30
|
+
public get getRateLimit(): number {
|
31
|
+
return this.rateLimit;
|
32
|
+
}
|
33
|
+
|
21
34
|
public generateIdentityCredentials(): IdentityCredential {
|
22
35
|
const memKeys = zerokitRLN.generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
|
23
36
|
return IdentityCredential.fromBytes(memKeys);
|
@@ -67,12 +80,17 @@ export class Zerokit {
|
|
67
80
|
memIndex: number,
|
68
81
|
epoch: Uint8Array,
|
69
82
|
idKey: Uint8Array,
|
70
|
-
rateLimit
|
83
|
+
rateLimit?: number
|
71
84
|
): Uint8Array {
|
72
85
|
// calculate message length
|
73
86
|
const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
|
74
87
|
const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
|
75
|
-
const rateLimitBytes = writeUIntLE(
|
88
|
+
const rateLimitBytes = writeUIntLE(
|
89
|
+
new Uint8Array(8),
|
90
|
+
rateLimit ?? this.rateLimit,
|
91
|
+
0,
|
92
|
+
8
|
93
|
+
);
|
76
94
|
|
77
95
|
// [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> | rate_limit<8> ]
|
78
96
|
return concatenate(
|
@@ -90,7 +108,7 @@ export class Zerokit {
|
|
90
108
|
index: number,
|
91
109
|
epoch: Uint8Array | Date | undefined,
|
92
110
|
idSecretHash: Uint8Array,
|
93
|
-
rateLimit
|
111
|
+
rateLimit?: number
|
94
112
|
): Promise<IRateLimitProof> {
|
95
113
|
if (epoch === undefined) {
|
96
114
|
epoch = epochIntToBytes(dateToEpoch(new Date()));
|
@@ -98,12 +116,14 @@ export class Zerokit {
|
|
98
116
|
epoch = epochIntToBytes(dateToEpoch(epoch));
|
99
117
|
}
|
100
118
|
|
119
|
+
const effectiveRateLimit = rateLimit ?? this.rateLimit;
|
120
|
+
|
101
121
|
if (epoch.length !== 32) throw new Error("invalid epoch");
|
102
122
|
if (idSecretHash.length !== 32) throw new Error("invalid id secret hash");
|
103
123
|
if (index < 0) throw new Error("index must be >= 0");
|
104
124
|
if (
|
105
|
-
|
106
|
-
|
125
|
+
effectiveRateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
|
126
|
+
effectiveRateLimit > RATE_LIMIT_PARAMS.MAX_RATE
|
107
127
|
) {
|
108
128
|
throw new Error(
|
109
129
|
`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`
|
@@ -115,7 +135,7 @@ export class Zerokit {
|
|
115
135
|
index,
|
116
136
|
epoch,
|
117
137
|
idSecretHash,
|
118
|
-
|
138
|
+
effectiveRateLimit
|
119
139
|
);
|
120
140
|
const rlnWitness = zerokitRLN.getSerializedRLNWitness(
|
121
141
|
this.zkRLN,
|
@@ -150,9 +170,12 @@ export class Zerokit {
|
|
150
170
|
|
151
171
|
// calculate message length
|
152
172
|
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
153
|
-
const rateLimitBytes =
|
154
|
-
|
155
|
-
|
173
|
+
const rateLimitBytes = writeUIntLE(
|
174
|
+
new Uint8Array(8),
|
175
|
+
rateLimit ?? this.rateLimit,
|
176
|
+
0,
|
177
|
+
8
|
178
|
+
);
|
156
179
|
|
157
180
|
return zerokitRLN.verifyRLNProof(
|
158
181
|
this.zkRLN,
|
@@ -174,9 +197,12 @@ export class Zerokit {
|
|
174
197
|
}
|
175
198
|
// calculate message length
|
176
199
|
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
177
|
-
const rateLimitBytes =
|
178
|
-
|
179
|
-
|
200
|
+
const rateLimitBytes = writeUIntLE(
|
201
|
+
new Uint8Array(8),
|
202
|
+
rateLimit ?? this.rateLimit,
|
203
|
+
0,
|
204
|
+
8
|
205
|
+
);
|
180
206
|
|
181
207
|
const rootsBytes = concatenate(...roots);
|
182
208
|
|
@@ -201,9 +227,12 @@ export class Zerokit {
|
|
201
227
|
|
202
228
|
// calculate message length
|
203
229
|
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
204
|
-
const rateLimitBytes =
|
205
|
-
|
206
|
-
|
230
|
+
const rateLimitBytes = writeUIntLE(
|
231
|
+
new Uint8Array(8),
|
232
|
+
rateLimit ?? this.rateLimit,
|
233
|
+
0,
|
234
|
+
8
|
235
|
+
);
|
207
236
|
|
208
237
|
return zerokitRLN.verifyWithRoots(
|
209
238
|
this.zkRLN,
|