@waku/rln 0.1.4-d27db21.0 → 0.1.5-053bb95.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/CHANGELOG.md +19 -0
- package/README.md +5 -0
- package/bundle/_virtual/utils.js +2 -2
- package/bundle/_virtual/utils2.js +2 -2
- package/bundle/index.js +3 -1
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/abstract-coder.js +1 -1
- package/bundle/node_modules/@ethersproject/abi/lib.esm/interface.js +1 -1
- package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/index.js +1 -1
- package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/index.js +11 -11
- package/bundle/node_modules/@ethersproject/bytes/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +7 -7
- package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/lib.js +2 -2
- package/bundle/node_modules/@ethersproject/hash/lib.esm/typed-data.js +2 -2
- package/bundle/node_modules/@ethersproject/keccak256/node_modules/js-sha3/src/sha3.js +1 -1
- package/bundle/node_modules/@ethersproject/logger/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/properties/lib.esm/index.js +1 -1
- package/bundle/node_modules/@ethersproject/providers/lib.esm/base-provider.js +44 -44
- package/bundle/node_modules/@ethersproject/providers/lib.esm/json-rpc-provider.js +8 -8
- package/bundle/node_modules/@ethersproject/rlp/lib.esm/_version.js +1 -1
- package/bundle/node_modules/@ethersproject/transactions/lib.esm/index.js +1 -1
- package/bundle/node_modules/@ethersproject/web/lib.esm/geturl.js +1 -1
- package/bundle/node_modules/@ethersproject/web/lib.esm/index.js +2 -2
- package/bundle/node_modules/@multiformats/multiaddr/dist/src/multiaddr.js +1 -0
- package/bundle/node_modules/@noble/hashes/esm/sha3.js +1 -1
- package/bundle/node_modules/bn.js/lib/bn.js +1 -1
- package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +6 -6
- package/bundle/packages/rln/dist/contract/abi.js +502 -248
- package/bundle/packages/rln/dist/contract/constants.js +5 -9
- package/bundle/packages/rln/dist/contract/errors.js +62 -0
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +347 -0
- package/bundle/packages/rln/dist/contract/rln_contract.js +81 -392
- package/bundle/packages/rln/dist/contract/types.js +9 -0
- package/bundle/packages/rln/dist/create.js +1 -1
- package/bundle/packages/rln/dist/credentials_manager.js +215 -0
- package/bundle/packages/rln/dist/identity.js +8 -0
- package/bundle/packages/rln/dist/keystore/keystore.js +20 -28
- package/bundle/packages/rln/dist/rln.js +56 -166
- package/bundle/packages/rln/dist/zerokit.js +5 -5
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/checksum.js +2 -2
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/cipher.js +3 -3
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/class.js +4 -4
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/functional.js +4 -4
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/kdf.js +4 -4
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/password.js +1 -1
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/schema-validation-generated.js +1 -1
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/lib/schema-validation.js +1 -1
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/aes.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/random.js +2 -2
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/scrypt.js +1 -1
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +1 -1
- package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +3 -3
- package/bundle/packages/rln/node_modules/@noble/hashes/_assert.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/_sha2.js +2 -2
- package/bundle/packages/rln/node_modules/@noble/hashes/_u64.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/cryptoBrowser.js +1 -1
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +43 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +116 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/hmac.js +79 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/sha256.js +126 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +43 -0
- package/bundle/packages/rln/node_modules/@noble/hashes/hmac.js +2 -2
- package/bundle/packages/rln/node_modules/@noble/hashes/pbkdf2.js +2 -2
- package/bundle/packages/rln/node_modules/@noble/hashes/scrypt.js +3 -3
- package/bundle/packages/rln/node_modules/@noble/hashes/sha256.js +2 -2
- package/bundle/packages/rln/node_modules/@noble/hashes/sha512.js +3 -3
- package/bundle/packages/rln/node_modules/@noble/hashes/utils.js +2 -2
- package/dist/.tsbuildinfo +1 -1
- package/dist/codec.test-utils.d.ts +36 -0
- package/dist/codec.test-utils.js +56 -0
- package/dist/codec.test-utils.js.map +1 -0
- package/dist/contract/abi.d.ts +21 -17
- package/dist/contract/abi.js +502 -248
- package/dist/contract/abi.js.map +1 -1
- package/dist/contract/constants.d.ts +23 -19
- package/dist/contract/constants.js +3 -3
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/errors.d.ts +30 -0
- package/dist/contract/errors.js +61 -0
- package/dist/contract/errors.js.map +1 -0
- package/dist/contract/rln_base_contract.d.ts +88 -0
- package/dist/contract/rln_base_contract.js +330 -0
- package/dist/contract/rln_base_contract.js.map +1 -0
- package/dist/contract/rln_contract.d.ts +19 -109
- package/dist/contract/rln_contract.js +80 -390
- package/dist/contract/rln_contract.js.map +1 -1
- package/dist/contract/test-setup.d.ts +26 -0
- package/dist/contract/test-setup.js +56 -0
- package/dist/contract/test-setup.js.map +1 -0
- package/dist/contract/test-utils.d.ts +39 -0
- package/dist/contract/test-utils.js +118 -0
- package/dist/contract/test-utils.js.map +1 -0
- package/dist/contract/types.d.ts +40 -0
- package/dist/contract/types.js +8 -0
- package/dist/contract/types.js.map +1 -0
- package/dist/create.js +1 -1
- package/dist/create.js.map +1 -1
- package/dist/credentials_manager.d.ts +44 -0
- package/dist/credentials_manager.js +197 -0
- package/dist/credentials_manager.js.map +1 -0
- package/dist/identity.d.ts +1 -0
- package/dist/identity.js +8 -0
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +5 -2
- package/dist/index.js +4 -2
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.d.ts +0 -1
- package/dist/keystore/keystore.js +20 -28
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +2 -1
- package/dist/rln.d.ts +9 -52
- package/dist/rln.js +54 -163
- package/dist/rln.js.map +1 -1
- package/dist/types.d.ts +27 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/zerokit.d.ts +3 -3
- package/dist/zerokit.js +5 -5
- package/dist/zerokit.js.map +1 -1
- package/package.json +1 -1
- package/src/codec.test-utils.ts +80 -0
- package/src/contract/abi.ts +502 -248
- package/src/contract/constants.ts +3 -3
- package/src/contract/errors.ts +75 -0
- package/src/contract/rln_base_contract.ts +500 -0
- package/src/contract/rln_contract.ts +102 -619
- package/src/contract/test-setup.ts +86 -0
- package/src/contract/test-utils.ts +179 -0
- package/src/contract/types.ts +48 -0
- package/src/create.ts +1 -1
- package/src/credentials_manager.ts +282 -0
- package/src/identity.ts +9 -0
- package/src/index.ts +17 -2
- package/src/keystore/keystore.ts +32 -46
- package/src/keystore/types.ts +2 -1
- package/src/rln.ts +67 -258
- package/src/types.ts +31 -0
- package/src/zerokit.ts +3 -3
package/src/keystore/keystore.ts
CHANGED
@@ -14,6 +14,7 @@ import {
|
|
14
14
|
import _ from "lodash";
|
15
15
|
import { v4 as uuidV4 } from "uuid";
|
16
16
|
|
17
|
+
import { IdentityCredential } from "../identity.js";
|
17
18
|
import { buildBigIntFromUint8Array } from "../utils/bytes.js";
|
18
19
|
|
19
20
|
import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
|
@@ -250,31 +251,32 @@ export class Keystore {
|
|
250
251
|
const str = bytesToUtf8(bytes);
|
251
252
|
const obj = JSON.parse(str);
|
252
253
|
|
253
|
-
//
|
254
|
+
// Get identity fields from named object
|
255
|
+
const { idTrapdoor, idNullifier, idSecretHash, idCommitment } = _.get(
|
256
|
+
obj,
|
257
|
+
"identityCredential",
|
258
|
+
{}
|
259
|
+
);
|
260
|
+
|
261
|
+
const idTrapdoorArray = new Uint8Array(idTrapdoor || []);
|
262
|
+
const idNullifierArray = new Uint8Array(idNullifier || []);
|
263
|
+
const idSecretHashArray = new Uint8Array(idSecretHash || []);
|
264
|
+
const idCommitmentArray = new Uint8Array(idCommitment || []);
|
265
|
+
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitmentArray);
|
266
|
+
|
254
267
|
return {
|
255
|
-
identity:
|
256
|
-
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
IDNullifier: Keystore.fromArraylikeToBytes(
|
263
|
-
_.get(obj, "identityCredential.idNullifier", [])
|
264
|
-
),
|
265
|
-
IDCommitmentBigInt: buildBigIntFromUint8Array(
|
266
|
-
Keystore.fromArraylikeToBytes(
|
267
|
-
_.get(obj, "identityCredential.idCommitment", [])
|
268
|
-
)
|
269
|
-
),
|
270
|
-
IDSecretHash: Keystore.fromArraylikeToBytes(
|
271
|
-
_.get(obj, "identityCredential.idSecretHash", [])
|
272
|
-
)
|
273
|
-
},
|
268
|
+
identity: new IdentityCredential(
|
269
|
+
idTrapdoorArray,
|
270
|
+
idNullifierArray,
|
271
|
+
idSecretHashArray,
|
272
|
+
idCommitmentArray,
|
273
|
+
idCommitmentBigInt
|
274
|
+
),
|
274
275
|
membership: {
|
275
276
|
treeIndex: _.get(obj, "treeIndex"),
|
276
277
|
chainId: _.get(obj, "membershipContract.chainId"),
|
277
|
-
address: _.get(obj, "membershipContract.address")
|
278
|
+
address: _.get(obj, "membershipContract.address"),
|
279
|
+
rateLimit: _.get(obj, "userMessageLimit")
|
278
280
|
}
|
279
281
|
};
|
280
282
|
} catch (err) {
|
@@ -283,23 +285,6 @@ export class Keystore {
|
|
283
285
|
}
|
284
286
|
}
|
285
287
|
|
286
|
-
private static fromArraylikeToBytes(obj: {
|
287
|
-
[key: number]: number;
|
288
|
-
}): Uint8Array {
|
289
|
-
const bytes = [];
|
290
|
-
|
291
|
-
let index = 0;
|
292
|
-
let lastElement = obj[index];
|
293
|
-
|
294
|
-
while (lastElement !== undefined) {
|
295
|
-
bytes.push(lastElement);
|
296
|
-
index += 1;
|
297
|
-
lastElement = obj[index];
|
298
|
-
}
|
299
|
-
|
300
|
-
return new Uint8Array(bytes);
|
301
|
-
}
|
302
|
-
|
303
288
|
// follows nwaku implementation
|
304
289
|
// https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L111
|
305
290
|
private static computeMembershipHash(info: MembershipInfo): MembershipHash {
|
@@ -313,17 +298,18 @@ export class Keystore {
|
|
313
298
|
private static fromIdentityToBytes(options: KeystoreEntity): Uint8Array {
|
314
299
|
return utf8ToBytes(
|
315
300
|
JSON.stringify({
|
316
|
-
treeIndex: options.membership.treeIndex,
|
317
|
-
identityCredential: {
|
318
|
-
idCommitment: options.identity.IDCommitment,
|
319
|
-
idNullifier: options.identity.IDNullifier,
|
320
|
-
idSecretHash: options.identity.IDSecretHash,
|
321
|
-
idTrapdoor: options.identity.IDTrapdoor
|
322
|
-
},
|
323
301
|
membershipContract: {
|
324
302
|
chainId: options.membership.chainId,
|
325
303
|
address: options.membership.address
|
326
|
-
}
|
304
|
+
},
|
305
|
+
treeIndex: options.membership.treeIndex,
|
306
|
+
identityCredential: {
|
307
|
+
idTrapdoor: Array.from(options.identity.IDTrapdoor),
|
308
|
+
idNullifier: Array.from(options.identity.IDNullifier),
|
309
|
+
idSecretHash: Array.from(options.identity.IDSecretHash),
|
310
|
+
idCommitment: Array.from(options.identity.IDCommitment)
|
311
|
+
},
|
312
|
+
userMessageLimit: options.membership.rateLimit
|
327
313
|
})
|
328
314
|
);
|
329
315
|
}
|
package/src/keystore/types.ts
CHANGED
@@ -8,9 +8,10 @@ export type Password = string | Uint8Array;
|
|
8
8
|
// see reference
|
9
9
|
// https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L111
|
10
10
|
export type MembershipInfo = {
|
11
|
-
chainId:
|
11
|
+
chainId: string;
|
12
12
|
address: string;
|
13
13
|
treeIndex: number;
|
14
|
+
rateLimit: number;
|
14
15
|
};
|
15
16
|
|
16
17
|
export type KeystoreEntity = {
|
package/src/rln.ts
CHANGED
@@ -7,7 +7,6 @@ import type {
|
|
7
7
|
import { Logger } from "@waku/utils";
|
8
8
|
import init from "@waku/zerokit-rln-wasm";
|
9
9
|
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
10
|
-
import { ethers } from "ethers";
|
11
10
|
|
12
11
|
import {
|
13
12
|
createRLNDecoder,
|
@@ -16,258 +15,52 @@ import {
|
|
16
15
|
type RLNEncoder
|
17
16
|
} from "./codec.js";
|
18
17
|
import { DEFAULT_RATE_LIMIT } from "./contract/constants.js";
|
19
|
-
import {
|
20
|
-
import { IdentityCredential } from "./identity.js";
|
21
|
-
import { Keystore } from "./keystore/index.js";
|
18
|
+
import { RLNCredentialsManager } from "./credentials_manager.js";
|
22
19
|
import type {
|
23
20
|
DecryptedCredentials,
|
24
21
|
EncryptedCredentials
|
25
22
|
} from "./keystore/index.js";
|
26
|
-
import { KeystoreEntity, Password } from "./keystore/types.js";
|
27
23
|
import verificationKey from "./resources/verification_key";
|
28
24
|
import * as wc from "./resources/witness_calculator";
|
29
25
|
import { WitnessCalculator } from "./resources/witness_calculator";
|
30
|
-
import { extractMetaMaskSigner } from "./utils/index.js";
|
31
26
|
import { Zerokit } from "./zerokit.js";
|
32
27
|
|
33
28
|
const log = new Logger("waku:rln");
|
34
29
|
|
35
|
-
async function loadWitnessCalculator(): Promise<WitnessCalculator> {
|
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
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
async function loadZkey(): Promise<Uint8Array> {
|
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
|
-
}
|
76
|
-
}
|
77
|
-
|
78
|
-
/**
|
79
|
-
* Create an instance of RLN
|
80
|
-
* @returns RLNInstance
|
81
|
-
*/
|
82
|
-
export async function create(): Promise<RLNInstance> {
|
83
|
-
try {
|
84
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
85
|
-
await (init as any)?.();
|
86
|
-
zerokitRLN.init_panic_hook();
|
87
|
-
|
88
|
-
const witnessCalculator = await loadWitnessCalculator();
|
89
|
-
const zkey = await loadZkey();
|
90
|
-
|
91
|
-
const stringEncoder = new TextEncoder();
|
92
|
-
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
|
93
|
-
|
94
|
-
const DEPTH = 20;
|
95
|
-
const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
|
96
|
-
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
|
97
|
-
|
98
|
-
return new RLNInstance(zerokit);
|
99
|
-
} catch (error) {
|
100
|
-
log.error("Failed to initialize RLN:", error);
|
101
|
-
throw error;
|
102
|
-
}
|
103
|
-
}
|
104
|
-
|
105
|
-
type StartRLNOptions = {
|
106
|
-
/**
|
107
|
-
* If not set - will extract MetaMask account and get signer from it.
|
108
|
-
*/
|
109
|
-
signer?: ethers.Signer;
|
110
|
-
/**
|
111
|
-
* If not set - will use default SEPOLIA_CONTRACT address.
|
112
|
-
*/
|
113
|
-
address?: string;
|
114
|
-
/**
|
115
|
-
* Credentials to use for generating proofs and connecting to the contract and network.
|
116
|
-
* If provided used for validating the network chainId and connecting to registry contract.
|
117
|
-
*/
|
118
|
-
credentials?: EncryptedCredentials | DecryptedCredentials;
|
119
|
-
/**
|
120
|
-
* Rate limit for the member.
|
121
|
-
*/
|
122
|
-
rateLimit?: number;
|
123
|
-
};
|
124
|
-
|
125
|
-
type RegisterMembershipOptions =
|
126
|
-
| { signature: string }
|
127
|
-
| { identity: IdentityCredential };
|
128
|
-
|
129
30
|
type WakuRLNEncoderOptions = WakuEncoderOptions & {
|
130
31
|
credentials: EncryptedCredentials | DecryptedCredentials;
|
131
32
|
};
|
132
33
|
|
133
|
-
export class RLNInstance {
|
134
|
-
|
135
|
-
|
136
|
-
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
private keystore = Keystore.create();
|
141
|
-
private _credentials: undefined | DecryptedCredentials;
|
142
|
-
|
143
|
-
public constructor(public zerokit: Zerokit) {}
|
144
|
-
|
145
|
-
public get contract(): undefined | RLNContract {
|
146
|
-
return this._contract;
|
147
|
-
}
|
148
|
-
|
149
|
-
public get signer(): undefined | ethers.Signer {
|
150
|
-
return this._signer;
|
151
|
-
}
|
152
|
-
|
153
|
-
public async start(options: StartRLNOptions = {}): Promise<void> {
|
154
|
-
if (this.started || this.starting) {
|
155
|
-
return;
|
156
|
-
}
|
157
|
-
|
158
|
-
this.starting = true;
|
159
|
-
|
34
|
+
export class RLNInstance extends RLNCredentialsManager {
|
35
|
+
/**
|
36
|
+
* Create an instance of RLN
|
37
|
+
* @returns RLNInstance
|
38
|
+
*/
|
39
|
+
public static async create(): Promise<RLNInstance> {
|
160
40
|
try {
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
options,
|
165
|
-
credentials
|
166
|
-
);
|
167
|
-
|
168
|
-
if (keystore) {
|
169
|
-
this.keystore = keystore;
|
170
|
-
}
|
171
|
-
|
172
|
-
this._credentials = credentials;
|
173
|
-
this._signer = signer!;
|
174
|
-
this._contract = await RLNContract.init(this, {
|
175
|
-
address: address!,
|
176
|
-
signer: signer!,
|
177
|
-
rateLimit: options.rateLimit ?? this.zerokit.getRateLimit
|
178
|
-
});
|
179
|
-
this.started = true;
|
180
|
-
} finally {
|
181
|
-
this.starting = false;
|
182
|
-
}
|
183
|
-
}
|
184
|
-
|
185
|
-
private async determineStartOptions(
|
186
|
-
options: StartRLNOptions,
|
187
|
-
credentials: KeystoreEntity | undefined
|
188
|
-
): Promise<StartRLNOptions> {
|
189
|
-
let chainId = credentials?.membership.chainId;
|
190
|
-
const address =
|
191
|
-
credentials?.membership.address ||
|
192
|
-
options.address ||
|
193
|
-
SEPOLIA_CONTRACT.address;
|
194
|
-
|
195
|
-
if (address === SEPOLIA_CONTRACT.address) {
|
196
|
-
chainId = SEPOLIA_CONTRACT.chainId;
|
197
|
-
}
|
41
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
42
|
+
await (init as any)?.();
|
43
|
+
zerokitRLN.init_panic_hook();
|
198
44
|
|
199
|
-
|
200
|
-
|
45
|
+
const witnessCalculator = await RLNInstance.loadWitnessCalculator();
|
46
|
+
const zkey = await RLNInstance.loadZkey();
|
201
47
|
|
202
|
-
|
203
|
-
|
204
|
-
`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`
|
205
|
-
);
|
206
|
-
}
|
207
|
-
|
208
|
-
return {
|
209
|
-
signer,
|
210
|
-
address
|
211
|
-
};
|
212
|
-
}
|
213
|
-
|
214
|
-
private static async decryptCredentialsIfNeeded(
|
215
|
-
credentials?: EncryptedCredentials | DecryptedCredentials
|
216
|
-
): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
|
217
|
-
if (!credentials) {
|
218
|
-
return {};
|
219
|
-
}
|
220
|
-
|
221
|
-
if ("identity" in credentials) {
|
222
|
-
return { credentials };
|
223
|
-
}
|
224
|
-
|
225
|
-
const keystore = Keystore.fromString(credentials.keystore);
|
226
|
-
|
227
|
-
if (!keystore) {
|
228
|
-
return {};
|
229
|
-
}
|
230
|
-
|
231
|
-
const decryptedCredentials = await keystore.readCredential(
|
232
|
-
credentials.id,
|
233
|
-
credentials.password
|
234
|
-
);
|
48
|
+
const stringEncoder = new TextEncoder();
|
49
|
+
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
|
235
50
|
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
};
|
240
|
-
}
|
241
|
-
|
242
|
-
public async registerMembership(
|
243
|
-
options: RegisterMembershipOptions
|
244
|
-
): Promise<undefined | DecryptedCredentials> {
|
245
|
-
if (!this.contract) {
|
246
|
-
throw Error("RLN Contract is not initialized.");
|
247
|
-
}
|
248
|
-
|
249
|
-
let identity = "identity" in options && options.identity;
|
250
|
-
|
251
|
-
if ("signature" in options) {
|
252
|
-
identity = this.zerokit.generateSeededIdentityCredential(
|
253
|
-
options.signature
|
254
|
-
);
|
255
|
-
}
|
51
|
+
const DEPTH = 20;
|
52
|
+
const zkRLN = zerokitRLN.newRLN(DEPTH, zkey, vkey);
|
53
|
+
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
|
256
54
|
|
257
|
-
|
258
|
-
|
55
|
+
return new RLNInstance(zerokit);
|
56
|
+
} catch (error) {
|
57
|
+
log.error("Failed to initialize RLN:", error);
|
58
|
+
throw error;
|
259
59
|
}
|
260
|
-
|
261
|
-
return this.contract.registerWithIdentity(identity);
|
262
60
|
}
|
263
61
|
|
264
|
-
|
265
|
-
|
266
|
-
* @param id: string, hash of credentials to select from Keystore
|
267
|
-
* @param password: string or bytes to use to decrypt credentials from Keystore
|
268
|
-
*/
|
269
|
-
public async useCredentials(id: string, password: Password): Promise<void> {
|
270
|
-
this._credentials = await this.keystore?.readCredential(id, password);
|
62
|
+
private constructor(public zerokit: Zerokit) {
|
63
|
+
super(zerokit);
|
271
64
|
}
|
272
65
|
|
273
66
|
public async createEncoder(
|
@@ -275,7 +68,7 @@ export class RLNInstance {
|
|
275
68
|
): Promise<RLNEncoder> {
|
276
69
|
const { credentials: decryptedCredentials } =
|
277
70
|
await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
|
278
|
-
const credentials = decryptedCredentials || this.
|
71
|
+
const credentials = decryptedCredentials || this.credentials;
|
279
72
|
|
280
73
|
if (!credentials) {
|
281
74
|
throw Error(
|
@@ -293,33 +86,6 @@ export class RLNInstance {
|
|
293
86
|
});
|
294
87
|
}
|
295
88
|
|
296
|
-
private async verifyCredentialsAgainstContract(
|
297
|
-
credentials: KeystoreEntity
|
298
|
-
): Promise<void> {
|
299
|
-
if (!this._contract) {
|
300
|
-
throw Error(
|
301
|
-
"Failed to verify chain coordinates: no contract initialized."
|
302
|
-
);
|
303
|
-
}
|
304
|
-
|
305
|
-
const registryAddress = credentials.membership.address;
|
306
|
-
const currentRegistryAddress = this._contract.address;
|
307
|
-
if (registryAddress !== currentRegistryAddress) {
|
308
|
-
throw Error(
|
309
|
-
`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`
|
310
|
-
);
|
311
|
-
}
|
312
|
-
|
313
|
-
const chainId = credentials.membership.chainId;
|
314
|
-
const network = await this._contract.provider.getNetwork();
|
315
|
-
const currentChainId = network.chainId;
|
316
|
-
if (chainId !== currentChainId) {
|
317
|
-
throw Error(
|
318
|
-
`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`
|
319
|
-
);
|
320
|
-
}
|
321
|
-
}
|
322
|
-
|
323
89
|
public createDecoder(
|
324
90
|
contentTopic: ContentTopic
|
325
91
|
): RLNDecoder<IDecodedMessage> {
|
@@ -328,4 +94,47 @@ export class RLNInstance {
|
|
328
94
|
decoder: createDecoder(contentTopic)
|
329
95
|
});
|
330
96
|
}
|
97
|
+
|
98
|
+
public static async loadWitnessCalculator(): Promise<WitnessCalculator> {
|
99
|
+
try {
|
100
|
+
const url = new URL("./resources/rln.wasm", import.meta.url);
|
101
|
+
const response = await fetch(url);
|
102
|
+
|
103
|
+
if (!response.ok) {
|
104
|
+
throw new Error(
|
105
|
+
`Failed to fetch witness calculator: ${response.status} ${response.statusText}`
|
106
|
+
);
|
107
|
+
}
|
108
|
+
|
109
|
+
return await wc.builder(
|
110
|
+
new Uint8Array(await response.arrayBuffer()),
|
111
|
+
false
|
112
|
+
);
|
113
|
+
} catch (error) {
|
114
|
+
log.error("Error loading witness calculator:", error);
|
115
|
+
throw new Error(
|
116
|
+
`Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`
|
117
|
+
);
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
public static async loadZkey(): Promise<Uint8Array> {
|
122
|
+
try {
|
123
|
+
const url = new URL("./resources/rln_final.zkey", import.meta.url);
|
124
|
+
const response = await fetch(url);
|
125
|
+
|
126
|
+
if (!response.ok) {
|
127
|
+
throw new Error(
|
128
|
+
`Failed to fetch zkey: ${response.status} ${response.statusText}`
|
129
|
+
);
|
130
|
+
}
|
131
|
+
|
132
|
+
return new Uint8Array(await response.arrayBuffer());
|
133
|
+
} catch (error) {
|
134
|
+
log.error("Error loading zkey:", error);
|
135
|
+
throw new Error(
|
136
|
+
`Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`
|
137
|
+
);
|
138
|
+
}
|
139
|
+
}
|
331
140
|
}
|
package/src/types.ts
ADDED
@@ -0,0 +1,31 @@
|
|
1
|
+
import { ethers } from "ethers";
|
2
|
+
|
3
|
+
import { IdentityCredential } from "./identity.js";
|
4
|
+
import {
|
5
|
+
DecryptedCredentials,
|
6
|
+
EncryptedCredentials
|
7
|
+
} from "./keystore/types.js";
|
8
|
+
|
9
|
+
export type StartRLNOptions = {
|
10
|
+
/**
|
11
|
+
* If not set - will extract MetaMask account and get signer from it.
|
12
|
+
*/
|
13
|
+
signer?: ethers.Signer;
|
14
|
+
/**
|
15
|
+
* If not set - will use default SEPOLIA_CONTRACT address.
|
16
|
+
*/
|
17
|
+
address?: string;
|
18
|
+
/**
|
19
|
+
* Credentials to use for generating proofs and connecting to the contract and network.
|
20
|
+
* If provided used for validating the network chainId and connecting to registry contract.
|
21
|
+
*/
|
22
|
+
credentials?: EncryptedCredentials | DecryptedCredentials;
|
23
|
+
/**
|
24
|
+
* Rate limit for the member.
|
25
|
+
*/
|
26
|
+
rateLimit?: number;
|
27
|
+
};
|
28
|
+
|
29
|
+
export type RegisterMembershipOptions =
|
30
|
+
| { signature: string }
|
31
|
+
| { identity: IdentityCredential };
|
package/src/zerokit.ts
CHANGED
@@ -16,7 +16,7 @@ export class Zerokit {
|
|
16
16
|
public constructor(
|
17
17
|
private readonly zkRLN: number,
|
18
18
|
private readonly witnessCalculator: WitnessCalculator,
|
19
|
-
private readonly
|
19
|
+
private readonly _rateLimit: number = DEFAULT_RATE_LIMIT
|
20
20
|
) {}
|
21
21
|
|
22
22
|
public get getZkRLN(): number {
|
@@ -27,8 +27,8 @@ export class Zerokit {
|
|
27
27
|
return this.witnessCalculator;
|
28
28
|
}
|
29
29
|
|
30
|
-
public get
|
31
|
-
return this.
|
30
|
+
public get rateLimit(): number {
|
31
|
+
return this._rateLimit;
|
32
32
|
}
|
33
33
|
|
34
34
|
public generateIdentityCredentials(): IdentityCredential {
|