@waku/rln 0.0.2-c86e056.0 → 0.0.2-ebd7523.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/rln.js +29 -11
- package/bundle/packages/rln/dist/zerokit.js +22 -16
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/checksum.js +1 -1
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/pbkdf2.js +1 -1
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +2 -2
- package/bundle/packages/rln/node_modules/@noble/hashes/scrypt.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/sha256.js +1 -1
- 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/index.d.ts +2 -2
- package/dist/index.js +2 -2
- package/dist/index.js.map +1 -1
- package/dist/rln.js +29 -10
- 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/index.ts +2 -2
- package/src/rln.ts +42 -10
- package/src/zerokit.ts +45 -16
- 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
@@ -5,7 +5,7 @@ import { hexToBytes } from '../../../utils/dist/bytes/index.js';
|
|
5
5
|
import { Logger } from '../../../utils/dist/logger/index.js';
|
6
6
|
import { MerkleRootTracker } from '../root_tracker.js';
|
7
7
|
import { zeroPadLE } from '../utils/bytes.js';
|
8
|
-
import {
|
8
|
+
import { RLN_ABI } from './abi.js';
|
9
9
|
import { RATE_LIMIT_PARAMS, DEFAULT_RATE_LIMIT } from './constants.js';
|
10
10
|
import { Contract } from '../../../../node_modules/@ethersproject/contracts/lib.esm/index.js';
|
11
11
|
import { BigNumber } from '../../../../node_modules/@ethersproject/bignumber/lib.esm/bignumber.js';
|
@@ -45,8 +45,7 @@ class RLNContract {
|
|
45
45
|
this.rateLimit = rateLimit;
|
46
46
|
const initialRoot = rlnInstance.zerokit.getMerkleRoot();
|
47
47
|
// Use the injected contract if provided; otherwise, instantiate a new one.
|
48
|
-
this.contract =
|
49
|
-
contract || new Contract(address, RLN_V2_ABI, signer);
|
48
|
+
this.contract = contract || new Contract(address, RLN_ABI, signer);
|
50
49
|
this.merkleRootTracker = new MerkleRootTracker(5, initialRoot);
|
51
50
|
// Initialize event filters for MembershipRegistered and MembershipRemoved
|
52
51
|
this._membersFilter = this.contract.filters.MembershipRegistered();
|
@@ -58,6 +57,18 @@ class RLNContract {
|
|
58
57
|
getRateLimit() {
|
59
58
|
return this.rateLimit;
|
60
59
|
}
|
60
|
+
/**
|
61
|
+
* Gets the contract address
|
62
|
+
*/
|
63
|
+
get address() {
|
64
|
+
return this.contract.address;
|
65
|
+
}
|
66
|
+
/**
|
67
|
+
* Gets the contract provider
|
68
|
+
*/
|
69
|
+
get provider() {
|
70
|
+
return this.contract.provider;
|
71
|
+
}
|
61
72
|
/**
|
62
73
|
* Gets the minimum allowed rate limit from the contract
|
63
74
|
* @returns Promise<number> The minimum rate limit in messages per epoch
|
@@ -21,8 +21,8 @@ import { Logger } from '../../utils/dist/logger/index.js';
|
|
21
21
|
import '../../core/dist/lib/metadata/metadata.js';
|
22
22
|
import __wbg_init, { init_panic_hook, newRLN } from '../../../node_modules/@waku/zerokit-rln-wasm/rln_wasm.js';
|
23
23
|
import { createRLNEncoder, createRLNDecoder } from './codec.js';
|
24
|
+
import { DEFAULT_RATE_LIMIT, SEPOLIA_CONTRACT } from './contract/constants.js';
|
24
25
|
import { RLNContract } from './contract/rln_contract.js';
|
25
|
-
import { SEPOLIA_CONTRACT } from './contract/constants.js';
|
26
26
|
import { Keystore } from './keystore/keystore.js';
|
27
27
|
import verificationKey from './resources/verification_key.js';
|
28
28
|
import { builder } from './resources/witness_calculator.js';
|
@@ -32,14 +32,32 @@ import { Zerokit } from './zerokit.js';
|
|
32
32
|
|
33
33
|
const log = new Logger("waku:rln");
|
34
34
|
async function loadWitnessCalculator() {
|
35
|
-
|
36
|
-
|
37
|
-
|
35
|
+
try {
|
36
|
+
const url = new URL("./resources/rln.wasm", import.meta.url);
|
37
|
+
const response = await fetch(url);
|
38
|
+
if (!response.ok) {
|
39
|
+
throw new Error(`Failed to fetch witness calculator: ${response.status} ${response.statusText}`);
|
40
|
+
}
|
41
|
+
return await builder(new Uint8Array(await response.arrayBuffer()), false);
|
42
|
+
}
|
43
|
+
catch (error) {
|
44
|
+
log.error("Error loading witness calculator:", error);
|
45
|
+
throw new Error(`Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`);
|
46
|
+
}
|
38
47
|
}
|
39
48
|
async function loadZkey() {
|
40
|
-
|
41
|
-
|
42
|
-
|
49
|
+
try {
|
50
|
+
const url = new URL("./resources/rln_final.zkey", import.meta.url);
|
51
|
+
const response = await fetch(url);
|
52
|
+
if (!response.ok) {
|
53
|
+
throw new Error(`Failed to fetch zkey: ${response.status} ${response.statusText}`);
|
54
|
+
}
|
55
|
+
return new Uint8Array(await response.arrayBuffer());
|
56
|
+
}
|
57
|
+
catch (error) {
|
58
|
+
log.error("Error loading zkey:", error);
|
59
|
+
throw new Error(`Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`);
|
60
|
+
}
|
43
61
|
}
|
44
62
|
/**
|
45
63
|
* Create an instance of RLN
|
@@ -56,7 +74,7 @@ async function create() {
|
|
56
74
|
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
|
57
75
|
const DEPTH = 20;
|
58
76
|
const zkRLN = newRLN(DEPTH, zkey, vkey);
|
59
|
-
const zerokit = new Zerokit(zkRLN, witnessCalculator);
|
77
|
+
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
|
60
78
|
return new RLNInstance(zerokit);
|
61
79
|
}
|
62
80
|
catch (error) {
|
@@ -97,7 +115,7 @@ class RLNInstance {
|
|
97
115
|
this._contract = await RLNContract.init(this, {
|
98
116
|
address: address,
|
99
117
|
signer: signer,
|
100
|
-
rateLimit: options.rateLimit
|
118
|
+
rateLimit: options.rateLimit ?? this.zerokit.getRateLimit
|
101
119
|
});
|
102
120
|
this.started = true;
|
103
121
|
}
|
@@ -180,12 +198,12 @@ class RLNInstance {
|
|
180
198
|
throw Error("Failed to verify chain coordinates: no contract initialized.");
|
181
199
|
}
|
182
200
|
const registryAddress = credentials.membership.address;
|
183
|
-
const currentRegistryAddress = this._contract.
|
201
|
+
const currentRegistryAddress = this._contract.address;
|
184
202
|
if (registryAddress !== currentRegistryAddress) {
|
185
203
|
throw Error(`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`);
|
186
204
|
}
|
187
205
|
const chainId = credentials.membership.chainId;
|
188
|
-
const network = await this._contract.
|
206
|
+
const network = await this._contract.provider.getNetwork();
|
189
207
|
const currentChainId = network.chainId;
|
190
208
|
if (chainId !== currentChainId) {
|
191
209
|
throw Error(`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`);
|
@@ -8,9 +8,20 @@ import { epochIntToBytes, dateToEpoch } from './utils/epoch.js';
|
|
8
8
|
class Zerokit {
|
9
9
|
zkRLN;
|
10
10
|
witnessCalculator;
|
11
|
-
|
11
|
+
rateLimit;
|
12
|
+
constructor(zkRLN, witnessCalculator, rateLimit = DEFAULT_RATE_LIMIT) {
|
12
13
|
this.zkRLN = zkRLN;
|
13
14
|
this.witnessCalculator = witnessCalculator;
|
15
|
+
this.rateLimit = rateLimit;
|
16
|
+
}
|
17
|
+
get getZkRLN() {
|
18
|
+
return this.zkRLN;
|
19
|
+
}
|
20
|
+
get getWitnessCalculator() {
|
21
|
+
return this.witnessCalculator;
|
22
|
+
}
|
23
|
+
get getRateLimit() {
|
24
|
+
return this.rateLimit;
|
14
25
|
}
|
15
26
|
generateIdentityCredentials() {
|
16
27
|
const memKeys = generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
|
@@ -39,32 +50,33 @@ class Zerokit {
|
|
39
50
|
getMerkleRoot() {
|
40
51
|
return getRoot(this.zkRLN);
|
41
52
|
}
|
42
|
-
serializeMessage(uint8Msg, memIndex, epoch, idKey, rateLimit
|
53
|
+
serializeMessage(uint8Msg, memIndex, epoch, idKey, rateLimit) {
|
43
54
|
// calculate message length
|
44
55
|
const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
|
45
56
|
const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
|
46
|
-
const rateLimitBytes = writeUIntLE(new Uint8Array(8), rateLimit, 0, 8);
|
57
|
+
const rateLimitBytes = writeUIntLE(new Uint8Array(8), rateLimit ?? this.rateLimit, 0, 8);
|
47
58
|
// [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> | rate_limit<8> ]
|
48
59
|
return concatenate(idKey, memIndexBytes, epoch, msgLen, uint8Msg, rateLimitBytes);
|
49
60
|
}
|
50
|
-
async generateRLNProof(msg, index, epoch, idSecretHash, rateLimit
|
61
|
+
async generateRLNProof(msg, index, epoch, idSecretHash, rateLimit) {
|
51
62
|
if (epoch === undefined) {
|
52
63
|
epoch = epochIntToBytes(dateToEpoch(new Date()));
|
53
64
|
}
|
54
65
|
else if (epoch instanceof Date) {
|
55
66
|
epoch = epochIntToBytes(dateToEpoch(epoch));
|
56
67
|
}
|
68
|
+
const effectiveRateLimit = rateLimit ?? this.rateLimit;
|
57
69
|
if (epoch.length !== 32)
|
58
70
|
throw new Error("invalid epoch");
|
59
71
|
if (idSecretHash.length !== 32)
|
60
72
|
throw new Error("invalid id secret hash");
|
61
73
|
if (index < 0)
|
62
74
|
throw new Error("index must be >= 0");
|
63
|
-
if (
|
64
|
-
|
75
|
+
if (effectiveRateLimit < RATE_LIMIT_PARAMS.MIN_RATE ||
|
76
|
+
effectiveRateLimit > RATE_LIMIT_PARAMS.MAX_RATE) {
|
65
77
|
throw new Error(`Rate limit must be between ${RATE_LIMIT_PARAMS.MIN_RATE} and ${RATE_LIMIT_PARAMS.MAX_RATE}`);
|
66
78
|
}
|
67
|
-
const serialized_msg = this.serializeMessage(msg, index, epoch, idSecretHash,
|
79
|
+
const serialized_msg = this.serializeMessage(msg, index, epoch, idSecretHash, effectiveRateLimit);
|
68
80
|
const rlnWitness = getSerializedRLNWitness(this.zkRLN, serialized_msg);
|
69
81
|
const inputs = RLNWitnessToJson(this.zkRLN, rlnWitness);
|
70
82
|
const calculatedWitness = await this.witnessCalculator.calculateWitness(inputs, false);
|
@@ -81,9 +93,7 @@ class Zerokit {
|
|
81
93
|
}
|
82
94
|
// calculate message length
|
83
95
|
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
84
|
-
const rateLimitBytes = rateLimit
|
85
|
-
? writeUIntLE(new Uint8Array(8), rateLimit, 0, 8)
|
86
|
-
: new Uint8Array(8); // Zero if not specified
|
96
|
+
const rateLimitBytes = writeUIntLE(new Uint8Array(8), rateLimit ?? this.rateLimit, 0, 8);
|
87
97
|
return verifyRLNProof(this.zkRLN, concatenate(pBytes, msgLen, msg, rateLimitBytes));
|
88
98
|
}
|
89
99
|
verifyWithRoots(proof, msg, roots, rateLimit) {
|
@@ -96,9 +106,7 @@ class Zerokit {
|
|
96
106
|
}
|
97
107
|
// calculate message length
|
98
108
|
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
99
|
-
const rateLimitBytes = rateLimit
|
100
|
-
? writeUIntLE(new Uint8Array(8), rateLimit, 0, 8)
|
101
|
-
: new Uint8Array(8); // Zero if not specified
|
109
|
+
const rateLimitBytes = writeUIntLE(new Uint8Array(8), rateLimit ?? this.rateLimit, 0, 8);
|
102
110
|
const rootsBytes = concatenate(...roots);
|
103
111
|
return verifyWithRoots(this.zkRLN, concatenate(pBytes, msgLen, msg, rateLimitBytes), rootsBytes);
|
104
112
|
}
|
@@ -112,9 +120,7 @@ class Zerokit {
|
|
112
120
|
}
|
113
121
|
// calculate message length
|
114
122
|
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
115
|
-
const rateLimitBytes = rateLimit
|
116
|
-
? writeUIntLE(new Uint8Array(8), rateLimit, 0, 8)
|
117
|
-
: new Uint8Array(8); // Zero if not specified
|
123
|
+
const rateLimitBytes = writeUIntLE(new Uint8Array(8), rateLimit ?? this.rateLimit, 0, 8);
|
118
124
|
return verifyWithRoots(this.zkRLN, concatenate(pBytes, msgLen, msg, rateLimitBytes), new Uint8Array());
|
119
125
|
}
|
120
126
|
}
|
@@ -2,7 +2,7 @@ import { commonjsGlobal } from '../../../../../../_virtual/_commonjsHelpers.js';
|
|
2
2
|
import { __exports as checksum$1 } from '../../../../../../_virtual/checksum.js';
|
3
3
|
import '../node_modules/ethereum-cryptography/sha256.js';
|
4
4
|
import { u as utilsExports } from '../node_modules/ethereum-cryptography/utils.js';
|
5
|
-
import { __exports as sha256 } from '../../../../../../_virtual/
|
5
|
+
import { __exports as sha256 } from '../../../../../../_virtual/sha2562.js';
|
6
6
|
|
7
7
|
var __awaiter = (commonjsGlobal && commonjsGlobal.__awaiter) || function (thisArg, _arguments, P, generator) {
|
8
8
|
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
@@ -4,7 +4,7 @@ import '../../../../@noble/hashes/sha256.js';
|
|
4
4
|
import '../../../../@noble/hashes/sha512.js';
|
5
5
|
import { u as utilsExports } from './utils.js';
|
6
6
|
import { __exports as pbkdf2$2 } from '../../../../../../../_virtual/pbkdf22.js';
|
7
|
-
import { __exports as sha256 } from '../../../../../../../_virtual/
|
7
|
+
import { __exports as sha256 } from '../../../../../../../_virtual/sha256.js';
|
8
8
|
import { __exports as sha512 } from '../../../../../../../_virtual/sha512.js';
|
9
9
|
|
10
10
|
Object.defineProperty(pbkdf2$1, "__esModule", { value: true });
|
@@ -1,7 +1,7 @@
|
|
1
|
-
import { __exports as sha256 } from '../../../../../../../_virtual/
|
1
|
+
import { __exports as sha256 } from '../../../../../../../_virtual/sha2562.js';
|
2
2
|
import '../../../../@noble/hashes/sha256.js';
|
3
3
|
import { u as utilsExports } from './utils.js';
|
4
|
-
import { __exports as sha256$1 } from '../../../../../../../_virtual/
|
4
|
+
import { __exports as sha256$1 } from '../../../../../../../_virtual/sha256.js';
|
5
5
|
|
6
6
|
Object.defineProperty(sha256, "__esModule", { value: true });
|
7
7
|
sha256.sha256 = undefined;
|
@@ -5,7 +5,7 @@ import './pbkdf2.js';
|
|
5
5
|
import './utils.js';
|
6
6
|
import { __exports as utils } from '../../../../../_virtual/utils.js';
|
7
7
|
import { __exports as _assert } from '../../../../../_virtual/_assert.js';
|
8
|
-
import { __exports as sha256 } from '../../../../../_virtual/
|
8
|
+
import { __exports as sha256 } from '../../../../../_virtual/sha256.js';
|
9
9
|
import { __exports as pbkdf2 } from '../../../../../_virtual/pbkdf22.js';
|
10
10
|
|
11
11
|
Object.defineProperty(scrypt$1, "__esModule", { value: true });
|