@waku/rln 0.1.5-861a776.0 → 0.1.5-9901863.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 +2 -2
- package/bundle/packages/rln/dist/contract/{rln_light_contract.js → rln_base_contract.js} +173 -173
- package/bundle/packages/rln/dist/contract/rln_contract.js +9 -419
- package/bundle/packages/rln/dist/contract/types.js +9 -0
- package/bundle/packages/rln/dist/create.js +1 -1
- package/bundle/packages/rln/dist/{rln_light.js → credentials_manager.js} +122 -38
- package/bundle/packages/rln/dist/identity.js +6 -7
- package/bundle/packages/rln/dist/keystore/keystore.js +12 -11
- package/bundle/packages/rln/dist/rln.js +56 -166
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/{rln_light_contract.d.ts → rln_base_contract.d.ts} +25 -53
- package/dist/contract/{rln_light_contract.js → rln_base_contract.js} +173 -173
- package/dist/contract/rln_base_contract.js.map +1 -0
- package/dist/contract/rln_contract.d.ts +5 -122
- package/dist/contract/rln_contract.js +8 -417
- package/dist/contract/rln_contract.js.map +1 -1
- 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 +50 -0
- package/dist/credentials_manager.js +215 -0
- package/dist/credentials_manager.js.map +1 -0
- package/dist/identity.d.ts +1 -1
- package/dist/identity.js +6 -7
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +3 -3
- package/dist/index.js +3 -3
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.js +12 -11
- package/dist/keystore/keystore.js.map +1 -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/package.json +1 -1
- package/src/contract/{rln_light_contract.ts → rln_base_contract.ts} +290 -308
- package/src/contract/rln_contract.ts +9 -663
- package/src/contract/types.ts +48 -0
- package/src/create.ts +1 -1
- package/src/credentials_manager.ts +306 -0
- package/src/identity.ts +7 -8
- package/src/index.ts +4 -4
- package/src/keystore/keystore.ts +13 -12
- package/src/rln.ts +67 -259
- package/src/types.ts +31 -0
- package/dist/contract/rln_light_contract.js.map +0 -1
- package/dist/rln_light.d.ts +0 -64
- package/dist/rln_light.js +0 -144
- package/dist/rln_light.js.map +0 -1
- package/src/rln_light.ts +0 -235
@@ -16,54 +16,127 @@ import '../../../node_modules/multiformats/dist/src/bases/identity.js';
|
|
16
16
|
import '../../../node_modules/multiformats/dist/src/codecs/json.js';
|
17
17
|
import { Logger } from '../../utils/dist/logger/index.js';
|
18
18
|
import { LINEA_CONTRACT } from './contract/constants.js';
|
19
|
-
import {
|
19
|
+
import { RLNBaseContract } from './contract/rln_base_contract.js';
|
20
20
|
import { IdentityCredential } from './identity.js';
|
21
21
|
import { Keystore } from './keystore/keystore.js';
|
22
22
|
import { extractMetaMaskSigner } from './utils/metamask.js';
|
23
23
|
import { buildBigIntFromUint8Array } from './utils/bytes.js';
|
24
24
|
import './utils/epoch.js';
|
25
25
|
|
26
|
-
new Logger("waku:
|
27
|
-
|
26
|
+
const log = new Logger("waku:credentials");
|
27
|
+
/**
|
28
|
+
* Manages credentials for RLN
|
29
|
+
* This is a lightweight implementation of the RLN contract that doesn't require Zerokit
|
30
|
+
* It is used to register membership and generate identity credentials
|
31
|
+
*/
|
32
|
+
class RLNCredentialsManager {
|
28
33
|
started = false;
|
29
34
|
starting = false;
|
30
35
|
_contract;
|
31
36
|
_signer;
|
32
37
|
keystore = Keystore.create();
|
33
38
|
_credentials;
|
34
|
-
|
39
|
+
zerokit;
|
40
|
+
constructor(zerokit) {
|
41
|
+
log.info("RLNCredentialsManager initialized");
|
42
|
+
this.zerokit = zerokit;
|
43
|
+
}
|
35
44
|
get contract() {
|
36
45
|
return this._contract;
|
37
46
|
}
|
47
|
+
set contract(contract) {
|
48
|
+
this._contract = contract;
|
49
|
+
}
|
38
50
|
get signer() {
|
39
51
|
return this._signer;
|
40
52
|
}
|
53
|
+
set signer(signer) {
|
54
|
+
this._signer = signer;
|
55
|
+
}
|
56
|
+
get credentials() {
|
57
|
+
return this._credentials;
|
58
|
+
}
|
59
|
+
set credentials(credentials) {
|
60
|
+
this._credentials = credentials;
|
61
|
+
}
|
62
|
+
get provider() {
|
63
|
+
return this.contract?.provider;
|
64
|
+
}
|
41
65
|
async start(options = {}) {
|
42
66
|
if (this.started || this.starting) {
|
67
|
+
log.info("RLNCredentialsManager already started or starting");
|
43
68
|
return;
|
44
69
|
}
|
70
|
+
log.info("Starting RLNCredentialsManager");
|
45
71
|
this.starting = true;
|
46
72
|
try {
|
47
|
-
const { credentials, keystore } = await
|
73
|
+
const { credentials, keystore } = await RLNCredentialsManager.decryptCredentialsIfNeeded(options.credentials);
|
74
|
+
if (credentials) {
|
75
|
+
log.info("Credentials successfully decrypted");
|
76
|
+
}
|
48
77
|
const { signer, address, rateLimit } = await this.determineStartOptions(options, credentials);
|
78
|
+
log.info(`Using contract address: ${address}`);
|
49
79
|
if (keystore) {
|
50
80
|
this.keystore = keystore;
|
81
|
+
log.info("Using provided keystore");
|
51
82
|
}
|
52
83
|
this._credentials = credentials;
|
53
84
|
this._signer = signer;
|
54
|
-
this._contract =
|
85
|
+
this._contract = new RLNBaseContract({
|
55
86
|
address: address,
|
56
87
|
signer: signer,
|
57
|
-
rateLimit: rateLimit
|
88
|
+
rateLimit: rateLimit ?? this.zerokit?.getRateLimit
|
58
89
|
});
|
90
|
+
log.info("RLNCredentialsManager successfully started");
|
59
91
|
this.started = true;
|
60
92
|
}
|
93
|
+
catch (error) {
|
94
|
+
log.error("Failed to start RLNCredentialsManager", error);
|
95
|
+
throw error;
|
96
|
+
}
|
61
97
|
finally {
|
62
98
|
this.starting = false;
|
63
99
|
}
|
64
100
|
}
|
65
|
-
|
66
|
-
|
101
|
+
async registerMembership(options) {
|
102
|
+
if (!this.contract) {
|
103
|
+
log.error("RLN Contract is not initialized");
|
104
|
+
throw Error("RLN Contract is not initialized.");
|
105
|
+
}
|
106
|
+
log.info("Registering membership");
|
107
|
+
let identity = "identity" in options && options.identity;
|
108
|
+
if ("signature" in options) {
|
109
|
+
log.info("Generating identity from signature");
|
110
|
+
if (this.zerokit) {
|
111
|
+
log.info("Using Zerokit to generate identity");
|
112
|
+
identity = this.zerokit.generateSeededIdentityCredential(options.signature);
|
113
|
+
}
|
114
|
+
else {
|
115
|
+
log.info("Using local implementation to generate identity");
|
116
|
+
identity = this.generateSeededIdentityCredential(options.signature);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
if (!identity) {
|
120
|
+
log.error("Missing signature or identity to register membership");
|
121
|
+
throw Error("Missing signature or identity to register membership.");
|
122
|
+
}
|
123
|
+
log.info("Registering identity with contract");
|
124
|
+
return this.contract.registerWithIdentity(identity);
|
125
|
+
}
|
126
|
+
/**
|
127
|
+
* Changes credentials in use by relying on provided Keystore earlier in rln.start
|
128
|
+
* @param id: string, hash of credentials to select from Keystore
|
129
|
+
* @param password: string or bytes to use to decrypt credentials from Keystore
|
130
|
+
*/
|
131
|
+
async useCredentials(id, password) {
|
132
|
+
log.info(`Attempting to use credentials with ID: ${id}`);
|
133
|
+
this._credentials = await this.keystore?.readCredential(id, password);
|
134
|
+
if (this._credentials) {
|
135
|
+
log.info("Successfully loaded credentials");
|
136
|
+
}
|
137
|
+
else {
|
138
|
+
log.warn("Failed to load credentials");
|
139
|
+
}
|
67
140
|
}
|
68
141
|
async determineStartOptions(options, credentials) {
|
69
142
|
let chainId = credentials?.membership.chainId;
|
@@ -72,10 +145,13 @@ class RLNLightInstance {
|
|
72
145
|
LINEA_CONTRACT.address;
|
73
146
|
if (address === LINEA_CONTRACT.address) {
|
74
147
|
chainId = LINEA_CONTRACT.chainId;
|
148
|
+
log.info(`Using Linea contract with chainId: ${chainId}`);
|
75
149
|
}
|
76
150
|
const signer = options.signer || (await extractMetaMaskSigner());
|
77
|
-
const currentChainId =
|
78
|
-
|
151
|
+
const currentChainId = await signer.getChainId();
|
152
|
+
log.info(`Current chain ID: ${currentChainId}`);
|
153
|
+
if (chainId && chainId !== currentChainId.toString()) {
|
154
|
+
log.error(`Chain ID mismatch: contract=${chainId}, current=${currentChainId}`);
|
79
155
|
throw Error(`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`);
|
80
156
|
}
|
81
157
|
return {
|
@@ -85,20 +161,47 @@ class RLNLightInstance {
|
|
85
161
|
}
|
86
162
|
static async decryptCredentialsIfNeeded(credentials) {
|
87
163
|
if (!credentials) {
|
164
|
+
log.info("No credentials provided");
|
88
165
|
return {};
|
89
166
|
}
|
90
167
|
if ("identity" in credentials) {
|
168
|
+
log.info("Using already decrypted credentials");
|
91
169
|
return { credentials };
|
92
170
|
}
|
171
|
+
log.info("Attempting to decrypt credentials");
|
93
172
|
const keystore = Keystore.fromString(credentials.keystore);
|
94
173
|
if (!keystore) {
|
174
|
+
log.warn("Failed to create keystore from string");
|
95
175
|
return {};
|
96
176
|
}
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
177
|
+
try {
|
178
|
+
const decryptedCredentials = await keystore.readCredential(credentials.id, credentials.password);
|
179
|
+
log.info(`Successfully decrypted credentials with ID: ${credentials.id}`);
|
180
|
+
return {
|
181
|
+
keystore,
|
182
|
+
credentials: decryptedCredentials
|
183
|
+
};
|
184
|
+
}
|
185
|
+
catch (error) {
|
186
|
+
log.error("Failed to decrypt credentials", error);
|
187
|
+
throw error;
|
188
|
+
}
|
189
|
+
}
|
190
|
+
async verifyCredentialsAgainstContract(credentials) {
|
191
|
+
if (!this.contract) {
|
192
|
+
throw Error("Failed to verify chain coordinates: no contract initialized.");
|
193
|
+
}
|
194
|
+
const registryAddress = credentials.membership.address;
|
195
|
+
const currentRegistryAddress = this.contract.address;
|
196
|
+
if (registryAddress !== currentRegistryAddress) {
|
197
|
+
throw Error(`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`);
|
198
|
+
}
|
199
|
+
const chainId = credentials.membership.chainId;
|
200
|
+
const network = await this.contract.provider.getNetwork();
|
201
|
+
const currentChainId = network.chainId;
|
202
|
+
if (chainId !== currentChainId.toString()) {
|
203
|
+
throw Error(`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`);
|
204
|
+
}
|
102
205
|
}
|
103
206
|
/**
|
104
207
|
* Generates an identity credential from a seed string
|
@@ -107,6 +210,7 @@ class RLNLightInstance {
|
|
107
210
|
* @returns IdentityCredential
|
108
211
|
*/
|
109
212
|
generateSeededIdentityCredential(seed) {
|
213
|
+
log.info("Generating seeded identity credential");
|
110
214
|
// Convert the seed to bytes
|
111
215
|
const encoder = new TextEncoder();
|
112
216
|
const seedBytes = encoder.encode(seed);
|
@@ -121,29 +225,9 @@ class RLNLightInstance {
|
|
121
225
|
const idCommitment = sha256(idSecretHash);
|
122
226
|
// Convert IDCommitment to BigInt
|
123
227
|
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
|
228
|
+
log.info("Successfully generated identity credential");
|
124
229
|
return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
|
125
230
|
}
|
126
|
-
async registerMembership(options) {
|
127
|
-
if (!this.contract) {
|
128
|
-
throw Error("RLN Contract is not initialized.");
|
129
|
-
}
|
130
|
-
let identity = "identity" in options && options.identity;
|
131
|
-
if ("signature" in options) {
|
132
|
-
identity = this.generateSeededIdentityCredential(options.signature);
|
133
|
-
}
|
134
|
-
if (!identity) {
|
135
|
-
throw Error("Missing signature or identity to register membership.");
|
136
|
-
}
|
137
|
-
return this.contract.registerWithIdentity(identity);
|
138
|
-
}
|
139
|
-
/**
|
140
|
-
* Changes credentials in use by relying on provided Keystore earlier in rln.start
|
141
|
-
* @param id: string, hash of credentials to select from Keystore
|
142
|
-
* @param password: string or bytes to use to decrypt credentials from Keystore
|
143
|
-
*/
|
144
|
-
async useCredentials(id, password) {
|
145
|
-
this._credentials = await this.keystore?.readCredential(id, password);
|
146
|
-
}
|
147
231
|
}
|
148
232
|
|
149
|
-
export {
|
233
|
+
export { RLNCredentialsManager };
|
@@ -26,13 +26,12 @@ class IdentityCredential {
|
|
26
26
|
return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
|
27
27
|
}
|
28
28
|
toJSON() {
|
29
|
-
return
|
30
|
-
Array.from(this.IDTrapdoor),
|
31
|
-
Array.from(this.IDNullifier),
|
32
|
-
Array.from(this.IDSecretHash),
|
33
|
-
Array.from(this.IDCommitment)
|
34
|
-
|
35
|
-
];
|
29
|
+
return {
|
30
|
+
idTrapdoor: Array.from(this.IDTrapdoor),
|
31
|
+
idNullifier: Array.from(this.IDNullifier),
|
32
|
+
idSecretHash: Array.from(this.IDSecretHash),
|
33
|
+
idCommitment: Array.from(this.IDCommitment)
|
34
|
+
};
|
36
35
|
}
|
37
36
|
}
|
38
37
|
|
@@ -165,8 +165,8 @@ class Keystore {
|
|
165
165
|
try {
|
166
166
|
const str = bytesToUtf8(bytes);
|
167
167
|
const obj = JSON.parse(str);
|
168
|
-
// Get identity
|
169
|
-
const
|
168
|
+
// Get identity fields from named object
|
169
|
+
const { idTrapdoor, idNullifier, idSecretHash, idCommitment } = _.get(obj, "identityCredential", {});
|
170
170
|
const idTrapdoorArray = new Uint8Array(idTrapdoor || []);
|
171
171
|
const idNullifierArray = new Uint8Array(idNullifier || []);
|
172
172
|
const idSecretHashArray = new Uint8Array(idSecretHash || []);
|
@@ -178,7 +178,7 @@ class Keystore {
|
|
178
178
|
treeIndex: _.get(obj, "treeIndex"),
|
179
179
|
chainId: _.get(obj, "membershipContract.chainId"),
|
180
180
|
address: _.get(obj, "membershipContract.address"),
|
181
|
-
rateLimit: _.get(obj, "
|
181
|
+
rateLimit: _.get(obj, "userMessageLimit")
|
182
182
|
}
|
183
183
|
};
|
184
184
|
}
|
@@ -196,17 +196,18 @@ class Keystore {
|
|
196
196
|
// https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
|
197
197
|
static fromIdentityToBytes(options) {
|
198
198
|
return utf8ToBytes(JSON.stringify({
|
199
|
-
treeIndex: options.membership.treeIndex,
|
200
|
-
identityCredential: [
|
201
|
-
Array.from(options.identity.IDTrapdoor),
|
202
|
-
Array.from(options.identity.IDNullifier),
|
203
|
-
Array.from(options.identity.IDSecretHash),
|
204
|
-
Array.from(options.identity.IDCommitment)
|
205
|
-
],
|
206
199
|
membershipContract: {
|
207
200
|
chainId: options.membership.chainId,
|
208
201
|
address: options.membership.address
|
209
|
-
}
|
202
|
+
},
|
203
|
+
treeIndex: options.membership.treeIndex,
|
204
|
+
identityCredential: {
|
205
|
+
idTrapdoor: Array.from(options.identity.IDTrapdoor),
|
206
|
+
idNullifier: Array.from(options.identity.IDNullifier),
|
207
|
+
idSecretHash: Array.from(options.identity.IDSecretHash),
|
208
|
+
idCommitment: Array.from(options.identity.IDCommitment)
|
209
|
+
},
|
210
|
+
userMessageLimit: options.membership.rateLimit
|
210
211
|
}));
|
211
212
|
}
|
212
213
|
}
|
@@ -21,167 +21,45 @@ 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 {
|
25
|
-
import {
|
26
|
-
import { Keystore } from './keystore/keystore.js';
|
24
|
+
import { DEFAULT_RATE_LIMIT } from './contract/constants.js';
|
25
|
+
import { RLNCredentialsManager } from './credentials_manager.js';
|
27
26
|
import verificationKey from './resources/verification_key.js';
|
28
27
|
import { builder } from './resources/witness_calculator.js';
|
29
|
-
import { extractMetaMaskSigner } from './utils/metamask.js';
|
30
|
-
import './utils/epoch.js';
|
31
28
|
import { Zerokit } from './zerokit.js';
|
32
29
|
|
33
30
|
const log = new Logger("waku:rln");
|
34
|
-
|
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
|
-
}
|
47
|
-
}
|
48
|
-
async function loadZkey() {
|
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
|
-
}
|
61
|
-
}
|
62
|
-
/**
|
63
|
-
* Create an instance of RLN
|
64
|
-
* @returns RLNInstance
|
65
|
-
*/
|
66
|
-
async function create() {
|
67
|
-
try {
|
68
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
69
|
-
await __wbg_init?.();
|
70
|
-
init_panic_hook();
|
71
|
-
const witnessCalculator = await loadWitnessCalculator();
|
72
|
-
const zkey = await loadZkey();
|
73
|
-
const stringEncoder = new TextEncoder();
|
74
|
-
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
|
75
|
-
const DEPTH = 20;
|
76
|
-
const zkRLN = newRLN(DEPTH, zkey, vkey);
|
77
|
-
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
|
78
|
-
return new RLNInstance(zerokit);
|
79
|
-
}
|
80
|
-
catch (error) {
|
81
|
-
log.error("Failed to initialize RLN:", error);
|
82
|
-
throw error;
|
83
|
-
}
|
84
|
-
}
|
85
|
-
class RLNInstance {
|
31
|
+
class RLNInstance extends RLNCredentialsManager {
|
86
32
|
zerokit;
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
_credentials;
|
93
|
-
constructor(zerokit) {
|
94
|
-
this.zerokit = zerokit;
|
95
|
-
}
|
96
|
-
get contract() {
|
97
|
-
return this._contract;
|
98
|
-
}
|
99
|
-
get signer() {
|
100
|
-
return this._signer;
|
101
|
-
}
|
102
|
-
async start(options = {}) {
|
103
|
-
if (this.started || this.starting) {
|
104
|
-
return;
|
105
|
-
}
|
106
|
-
this.starting = true;
|
33
|
+
/**
|
34
|
+
* Create an instance of RLN
|
35
|
+
* @returns RLNInstance
|
36
|
+
*/
|
37
|
+
static async create() {
|
107
38
|
try {
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
39
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
40
|
+
await __wbg_init?.();
|
41
|
+
init_panic_hook();
|
42
|
+
const witnessCalculator = await RLNInstance.loadWitnessCalculator();
|
43
|
+
const zkey = await RLNInstance.loadZkey();
|
44
|
+
const stringEncoder = new TextEncoder();
|
45
|
+
const vkey = stringEncoder.encode(JSON.stringify(verificationKey));
|
46
|
+
const DEPTH = 20;
|
47
|
+
const zkRLN = newRLN(DEPTH, zkey, vkey);
|
48
|
+
const zerokit = new Zerokit(zkRLN, witnessCalculator, DEFAULT_RATE_LIMIT);
|
49
|
+
return new RLNInstance(zerokit);
|
50
|
+
}
|
51
|
+
catch (error) {
|
52
|
+
log.error("Failed to initialize RLN:", error);
|
53
|
+
throw error;
|
121
54
|
}
|
122
|
-
finally {
|
123
|
-
this.starting = false;
|
124
|
-
}
|
125
|
-
}
|
126
|
-
async determineStartOptions(options, credentials) {
|
127
|
-
let chainId = credentials?.membership.chainId;
|
128
|
-
const address = credentials?.membership.address ||
|
129
|
-
options.address ||
|
130
|
-
LINEA_CONTRACT.address;
|
131
|
-
if (address === LINEA_CONTRACT.address) {
|
132
|
-
chainId = LINEA_CONTRACT.chainId;
|
133
|
-
}
|
134
|
-
const signer = options.signer || (await extractMetaMaskSigner());
|
135
|
-
const currentChainId = (await signer.getChainId()).toString();
|
136
|
-
if (chainId && chainId !== currentChainId) {
|
137
|
-
throw Error(`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`);
|
138
|
-
}
|
139
|
-
return {
|
140
|
-
signer,
|
141
|
-
address
|
142
|
-
};
|
143
55
|
}
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
}
|
148
|
-
if ("identity" in credentials) {
|
149
|
-
return { credentials };
|
150
|
-
}
|
151
|
-
const keystore = Keystore.fromString(credentials.keystore);
|
152
|
-
if (!keystore) {
|
153
|
-
return {};
|
154
|
-
}
|
155
|
-
const decryptedCredentials = await keystore.readCredential(credentials.id, credentials.password);
|
156
|
-
return {
|
157
|
-
keystore,
|
158
|
-
credentials: decryptedCredentials
|
159
|
-
};
|
160
|
-
}
|
161
|
-
async registerMembership(options) {
|
162
|
-
if (!this.contract) {
|
163
|
-
throw Error("RLN Contract is not initialized.");
|
164
|
-
}
|
165
|
-
let identity = "identity" in options && options.identity;
|
166
|
-
if ("signature" in options) {
|
167
|
-
identity = this.zerokit.generateSeededIdentityCredential(options.signature);
|
168
|
-
}
|
169
|
-
if (!identity) {
|
170
|
-
throw Error("Missing signature or identity to register membership.");
|
171
|
-
}
|
172
|
-
return this.contract.registerWithIdentity(identity);
|
173
|
-
}
|
174
|
-
/**
|
175
|
-
* Changes credentials in use by relying on provided Keystore earlier in rln.start
|
176
|
-
* @param id: string, hash of credentials to select from Keystore
|
177
|
-
* @param password: string or bytes to use to decrypt credentials from Keystore
|
178
|
-
*/
|
179
|
-
async useCredentials(id, password) {
|
180
|
-
this._credentials = await this.keystore?.readCredential(id, password);
|
56
|
+
constructor(zerokit) {
|
57
|
+
super(zerokit);
|
58
|
+
this.zerokit = zerokit;
|
181
59
|
}
|
182
60
|
async createEncoder(options) {
|
183
61
|
const { credentials: decryptedCredentials } = await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
|
184
|
-
const credentials = decryptedCredentials || this.
|
62
|
+
const credentials = decryptedCredentials || this.credentials;
|
185
63
|
if (!credentials) {
|
186
64
|
throw Error("Failed to create Encoder: missing RLN credentials. Use createRLNEncoder directly.");
|
187
65
|
}
|
@@ -193,28 +71,40 @@ class RLNInstance {
|
|
193
71
|
credential: credentials.identity
|
194
72
|
});
|
195
73
|
}
|
196
|
-
async verifyCredentialsAgainstContract(credentials) {
|
197
|
-
if (!this._contract) {
|
198
|
-
throw Error("Failed to verify chain coordinates: no contract initialized.");
|
199
|
-
}
|
200
|
-
const registryAddress = credentials.membership.address;
|
201
|
-
const currentRegistryAddress = this._contract.address;
|
202
|
-
if (registryAddress !== currentRegistryAddress) {
|
203
|
-
throw Error(`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`);
|
204
|
-
}
|
205
|
-
const chainId = credentials.membership.chainId;
|
206
|
-
const network = await this._contract.provider.getNetwork();
|
207
|
-
const currentChainId = network.chainId.toString();
|
208
|
-
if (chainId !== currentChainId) {
|
209
|
-
throw Error(`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`);
|
210
|
-
}
|
211
|
-
}
|
212
74
|
createDecoder(contentTopic) {
|
213
75
|
return createRLNDecoder({
|
214
76
|
rlnInstance: this,
|
215
77
|
decoder: createDecoder(contentTopic)
|
216
78
|
});
|
217
79
|
}
|
80
|
+
static async loadWitnessCalculator() {
|
81
|
+
try {
|
82
|
+
const url = new URL("./resources/rln.wasm", import.meta.url);
|
83
|
+
const response = await fetch(url);
|
84
|
+
if (!response.ok) {
|
85
|
+
throw new Error(`Failed to fetch witness calculator: ${response.status} ${response.statusText}`);
|
86
|
+
}
|
87
|
+
return await builder(new Uint8Array(await response.arrayBuffer()), false);
|
88
|
+
}
|
89
|
+
catch (error) {
|
90
|
+
log.error("Error loading witness calculator:", error);
|
91
|
+
throw new Error(`Failed to load witness calculator: ${error instanceof Error ? error.message : String(error)}`);
|
92
|
+
}
|
93
|
+
}
|
94
|
+
static async loadZkey() {
|
95
|
+
try {
|
96
|
+
const url = new URL("./resources/rln_final.zkey", import.meta.url);
|
97
|
+
const response = await fetch(url);
|
98
|
+
if (!response.ok) {
|
99
|
+
throw new Error(`Failed to fetch zkey: ${response.status} ${response.statusText}`);
|
100
|
+
}
|
101
|
+
return new Uint8Array(await response.arrayBuffer());
|
102
|
+
}
|
103
|
+
catch (error) {
|
104
|
+
log.error("Error loading zkey:", error);
|
105
|
+
throw new Error(`Failed to load zkey: ${error instanceof Error ? error.message : String(error)}`);
|
106
|
+
}
|
107
|
+
}
|
218
108
|
}
|
219
109
|
|
220
|
-
export { RLNInstance
|
110
|
+
export { RLNInstance };
|