@waku/rln 0.1.5-861a776.0 → 0.1.5-a824fff.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 +4 -3
- package/bundle/packages/rln/dist/contract/constants.js +2 -1
- package/bundle/packages/rln/dist/contract/{rln_light_contract.js → rln_base_contract.js} +191 -186
- package/bundle/packages/rln/dist/contract/rln_contract.js +10 -420
- 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} +114 -48
- package/bundle/packages/rln/dist/identity.js +0 -9
- package/bundle/packages/rln/dist/keystore/keystore.js +31 -17
- package/bundle/packages/rln/dist/rln.js +56 -166
- package/bundle/packages/rln/dist/utils/metamask.js +2 -2
- package/bundle/packages/rln/dist/zerokit.js +5 -5
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/constants.d.ts +1 -1
- package/dist/contract/constants.js +1 -1
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.js +1 -0
- package/dist/contract/index.js.map +1 -1
- package/dist/contract/{rln_light_contract.d.ts → rln_base_contract.d.ts} +24 -58
- package/dist/contract/{rln_light_contract.js → rln_base_contract.js} +189 -184
- 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 +45 -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 +0 -1
- package/dist/identity.js +0 -9
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +5 -4
- package/dist/index.js +4 -3
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.d.ts +1 -0
- package/dist/keystore/keystore.js +31 -17
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +2 -2
- 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/contract/constants.ts +1 -1
- package/src/contract/index.ts +1 -0
- package/src/contract/{rln_light_contract.ts → rln_base_contract.ts} +312 -323
- package/src/contract/rln_contract.ts +9 -663
- package/src/contract/types.ts +53 -0
- package/src/create.ts +1 -1
- package/src/credentials_manager.ts +282 -0
- package/src/identity.ts +0 -10
- package/src/index.ts +7 -5
- package/src/keystore/keystore.ts +57 -31
- package/src/keystore/types.ts +2 -2
- package/src/rln.ts +67 -258
- package/src/types.ts +31 -0
- package/src/zerokit.ts +3 -3
- package/bundle/_virtual/__node-resolve_empty.js +0 -6
- package/bundle/_virtual/_node-resolve_empty.js +0 -3
- package/bundle/_virtual/bn.js +0 -3
- package/bundle/_virtual/common.js +0 -3
- package/bundle/_virtual/common2.js +0 -3
- package/bundle/_virtual/hash.js +0 -3
- package/bundle/_virtual/inherits_browser.js +0 -3
- package/bundle/_virtual/ripemd.js +0 -3
- package/bundle/_virtual/sha.js +0 -3
- package/bundle/_virtual/sha3.js +0 -3
- package/bundle/_virtual/utils3.js +0 -3
- package/bundle/node_modules/@ethersproject/abi/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/abi/lib.esm/abi-coder.js +0 -96
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/abstract-coder.js +0 -148
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/address.js +0 -26
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/anonymous.js +0 -20
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/array.js +0 -210
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/boolean.js +0 -18
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/bytes.js +0 -30
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/fixed-bytes.js +0 -26
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/null.js +0 -22
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/number.js +0 -43
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/string.js +0 -19
- package/bundle/node_modules/@ethersproject/abi/lib.esm/coders/tuple.js +0 -58
- package/bundle/node_modules/@ethersproject/abi/lib.esm/fragments.js +0 -854
- package/bundle/node_modules/@ethersproject/abi/lib.esm/interface.js +0 -609
- package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/index.js +0 -66
- package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/index.js +0 -302
- package/bundle/node_modules/@ethersproject/address/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/address/lib.esm/index.js +0 -110
- package/bundle/node_modules/@ethersproject/base64/lib.esm/base64.js +0 -20
- package/bundle/node_modules/@ethersproject/basex/lib.esm/index.js +0 -120
- package/bundle/node_modules/@ethersproject/bignumber/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/bignumber/lib.esm/bignumber.js +0 -287
- package/bundle/node_modules/@ethersproject/bytes/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/bytes/lib.esm/index.js +0 -402
- package/bundle/node_modules/@ethersproject/constants/lib.esm/addresses.js +0 -3
- package/bundle/node_modules/@ethersproject/constants/lib.esm/bignumbers.js +0 -8
- package/bundle/node_modules/@ethersproject/constants/lib.esm/hashes.js +0 -3
- package/bundle/node_modules/@ethersproject/contracts/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +0 -893
- package/bundle/node_modules/@ethersproject/hash/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/decoder.js +0 -256
- package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/include.js +0 -36
- package/bundle/node_modules/@ethersproject/hash/lib.esm/ens-normalize/lib.js +0 -135
- package/bundle/node_modules/@ethersproject/hash/lib.esm/id.js +0 -8
- package/bundle/node_modules/@ethersproject/hash/lib.esm/namehash.js +0 -64
- package/bundle/node_modules/@ethersproject/hash/lib.esm/typed-data.js +0 -443
- package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +0 -8
- package/bundle/node_modules/@ethersproject/keccak256/node_modules/js-sha3/src/sha3.js +0 -660
- package/bundle/node_modules/@ethersproject/logger/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/logger/lib.esm/index.js +0 -352
- package/bundle/node_modules/@ethersproject/networks/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/networks/lib.esm/index.js +0 -248
- package/bundle/node_modules/@ethersproject/properties/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/properties/lib.esm/index.js +0 -127
- package/bundle/node_modules/@ethersproject/providers/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/providers/lib.esm/base-provider.js +0 -2007
- package/bundle/node_modules/@ethersproject/providers/lib.esm/formatter.js +0 -422
- package/bundle/node_modules/@ethersproject/providers/lib.esm/json-rpc-provider.js +0 -674
- package/bundle/node_modules/@ethersproject/providers/lib.esm/web3-provider.js +0 -132
- package/bundle/node_modules/@ethersproject/rlp/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/rlp/lib.esm/index.js +0 -120
- package/bundle/node_modules/@ethersproject/sha2/lib.esm/sha2.js +0 -8
- package/bundle/node_modules/@ethersproject/signing-key/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/signing-key/lib.esm/elliptic.js +0 -2430
- package/bundle/node_modules/@ethersproject/signing-key/lib.esm/index.js +0 -76
- package/bundle/node_modules/@ethersproject/strings/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/strings/lib.esm/utf8.js +0 -219
- package/bundle/node_modules/@ethersproject/transactions/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/transactions/lib.esm/index.js +0 -279
- package/bundle/node_modules/@ethersproject/web/lib.esm/_version.js +0 -3
- package/bundle/node_modules/@ethersproject/web/lib.esm/geturl.js +0 -69
- package/bundle/node_modules/@ethersproject/web/lib.esm/index.js +0 -404
- package/bundle/node_modules/bech32/index.js +0 -187
- package/bundle/node_modules/bn.js/lib/bn.js +0 -3361
- package/bundle/node_modules/hash.js/lib/hash/common.js +0 -97
- package/bundle/node_modules/hash.js/lib/hash/hmac.js +0 -51
- package/bundle/node_modules/hash.js/lib/hash/ripemd.js +0 -152
- package/bundle/node_modules/hash.js/lib/hash/sha/1.js +0 -81
- package/bundle/node_modules/hash.js/lib/hash/sha/224.js +0 -33
- package/bundle/node_modules/hash.js/lib/hash/sha/256.js +0 -113
- package/bundle/node_modules/hash.js/lib/hash/sha/384.js +0 -39
- package/bundle/node_modules/hash.js/lib/hash/sha/512.js +0 -336
- package/bundle/node_modules/hash.js/lib/hash/sha/common.js +0 -53
- package/bundle/node_modules/hash.js/lib/hash/sha.js +0 -14
- package/bundle/node_modules/hash.js/lib/hash/utils.js +0 -282
- package/bundle/node_modules/hash.js/lib/hash.js +0 -33
- package/bundle/node_modules/inherits/inherits_browser.js +0 -33
- package/bundle/node_modules/minimalistic-assert/index.js +0 -13
- 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
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 LINEA_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: 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
|
-
LINEA_CONTRACT.address;
|
194
|
-
|
195
|
-
if (address === LINEA_CONTRACT.address) {
|
196
|
-
chainId = LINEA_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.toString();
|
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 {
|
package/bundle/_virtual/bn.js
DELETED
package/bundle/_virtual/hash.js
DELETED
package/bundle/_virtual/sha.js
DELETED
package/bundle/_virtual/sha3.js
DELETED
@@ -1,96 +0,0 @@
|
|
1
|
-
import { arrayify } from '../../bytes/lib.esm/index.js';
|
2
|
-
import { defineReadOnly } from '../../properties/lib.esm/index.js';
|
3
|
-
import { Logger } from '../../logger/lib.esm/index.js';
|
4
|
-
import { version } from './_version.js';
|
5
|
-
import { Reader, Writer } from './coders/abstract-coder.js';
|
6
|
-
import { AddressCoder } from './coders/address.js';
|
7
|
-
import { ArrayCoder } from './coders/array.js';
|
8
|
-
import { BooleanCoder } from './coders/boolean.js';
|
9
|
-
import { BytesCoder } from './coders/bytes.js';
|
10
|
-
import { FixedBytesCoder } from './coders/fixed-bytes.js';
|
11
|
-
import { NullCoder } from './coders/null.js';
|
12
|
-
import { NumberCoder } from './coders/number.js';
|
13
|
-
import { StringCoder } from './coders/string.js';
|
14
|
-
import { TupleCoder } from './coders/tuple.js';
|
15
|
-
import { ParamType } from './fragments.js';
|
16
|
-
|
17
|
-
const logger = new Logger(version);
|
18
|
-
const paramTypeBytes = new RegExp(/^bytes([0-9]*)$/);
|
19
|
-
const paramTypeNumber = new RegExp(/^(u?int)([0-9]*)$/);
|
20
|
-
class AbiCoder {
|
21
|
-
constructor(coerceFunc) {
|
22
|
-
defineReadOnly(this, "coerceFunc", coerceFunc || null);
|
23
|
-
}
|
24
|
-
_getCoder(param) {
|
25
|
-
switch (param.baseType) {
|
26
|
-
case "address":
|
27
|
-
return new AddressCoder(param.name);
|
28
|
-
case "bool":
|
29
|
-
return new BooleanCoder(param.name);
|
30
|
-
case "string":
|
31
|
-
return new StringCoder(param.name);
|
32
|
-
case "bytes":
|
33
|
-
return new BytesCoder(param.name);
|
34
|
-
case "array":
|
35
|
-
return new ArrayCoder(this._getCoder(param.arrayChildren), param.arrayLength, param.name);
|
36
|
-
case "tuple":
|
37
|
-
return new TupleCoder((param.components || []).map((component) => {
|
38
|
-
return this._getCoder(component);
|
39
|
-
}), param.name);
|
40
|
-
case "":
|
41
|
-
return new NullCoder(param.name);
|
42
|
-
}
|
43
|
-
// u?int[0-9]*
|
44
|
-
let match = param.type.match(paramTypeNumber);
|
45
|
-
if (match) {
|
46
|
-
let size = parseInt(match[2] || "256");
|
47
|
-
if (size === 0 || size > 256 || (size % 8) !== 0) {
|
48
|
-
logger.throwArgumentError("invalid " + match[1] + " bit length", "param", param);
|
49
|
-
}
|
50
|
-
return new NumberCoder(size / 8, (match[1] === "int"), param.name);
|
51
|
-
}
|
52
|
-
// bytes[0-9]+
|
53
|
-
match = param.type.match(paramTypeBytes);
|
54
|
-
if (match) {
|
55
|
-
let size = parseInt(match[1]);
|
56
|
-
if (size === 0 || size > 32) {
|
57
|
-
logger.throwArgumentError("invalid bytes length", "param", param);
|
58
|
-
}
|
59
|
-
return new FixedBytesCoder(size, param.name);
|
60
|
-
}
|
61
|
-
return logger.throwArgumentError("invalid type", "type", param.type);
|
62
|
-
}
|
63
|
-
_getWordSize() { return 32; }
|
64
|
-
_getReader(data, allowLoose) {
|
65
|
-
return new Reader(data, this._getWordSize(), this.coerceFunc, allowLoose);
|
66
|
-
}
|
67
|
-
_getWriter() {
|
68
|
-
return new Writer(this._getWordSize());
|
69
|
-
}
|
70
|
-
getDefaultValue(types) {
|
71
|
-
const coders = types.map((type) => this._getCoder(ParamType.from(type)));
|
72
|
-
const coder = new TupleCoder(coders, "_");
|
73
|
-
return coder.defaultValue();
|
74
|
-
}
|
75
|
-
encode(types, values) {
|
76
|
-
if (types.length !== values.length) {
|
77
|
-
logger.throwError("types/values length mismatch", Logger.errors.INVALID_ARGUMENT, {
|
78
|
-
count: { types: types.length, values: values.length },
|
79
|
-
value: { types: types, values: values }
|
80
|
-
});
|
81
|
-
}
|
82
|
-
const coders = types.map((type) => this._getCoder(ParamType.from(type)));
|
83
|
-
const coder = (new TupleCoder(coders, "_"));
|
84
|
-
const writer = this._getWriter();
|
85
|
-
coder.encode(writer, values);
|
86
|
-
return writer.data;
|
87
|
-
}
|
88
|
-
decode(types, data, loose) {
|
89
|
-
const coders = types.map((type) => this._getCoder(ParamType.from(type)));
|
90
|
-
const coder = new TupleCoder(coders, "_");
|
91
|
-
return coder.decode(this._getReader(arrayify(data), loose));
|
92
|
-
}
|
93
|
-
}
|
94
|
-
const defaultAbiCoder = new AbiCoder();
|
95
|
-
|
96
|
-
export { AbiCoder, defaultAbiCoder };
|