@waku/rln 0.1.5-9901863.0 → 0.1.5-a8ff776.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 +0 -2
- package/bundle/packages/rln/dist/contract/constants.js +1 -1
- package/bundle/packages/rln/dist/contract/rln_contract.js +419 -9
- package/bundle/packages/rln/dist/create.js +1 -1
- package/bundle/packages/rln/dist/identity.js +0 -8
- package/bundle/packages/rln/dist/keystore/keystore.js +28 -19
- package/bundle/packages/rln/dist/rln.js +166 -56
- 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/rln_contract.d.ts +122 -5
- package/dist/contract/rln_contract.js +417 -8
- package/dist/contract/rln_contract.js.map +1 -1
- package/dist/create.js +1 -1
- package/dist/create.js.map +1 -1
- package/dist/identity.d.ts +0 -1
- package/dist/identity.js +0 -8
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +1 -3
- package/dist/index.js +1 -3
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.d.ts +1 -0
- package/dist/keystore/keystore.js +28 -19
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +1 -1
- package/dist/rln.d.ts +52 -9
- package/dist/rln.js +163 -54
- package/dist/rln.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/constants.ts +1 -1
- package/src/contract/rln_contract.ts +663 -9
- package/src/create.ts +1 -1
- package/src/identity.ts +0 -9
- package/src/index.ts +0 -4
- package/src/keystore/keystore.ts +46 -31
- package/src/keystore/types.ts +1 -1
- package/src/rln.ts +259 -67
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +0 -477
- package/bundle/packages/rln/dist/contract/types.js +0 -9
- package/bundle/packages/rln/dist/credentials_manager.js +0 -233
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +0 -43
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +0 -116
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/hmac.js +0 -79
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/sha256.js +0 -126
- package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +0 -43
- package/dist/contract/rln_base_contract.d.ts +0 -96
- package/dist/contract/rln_base_contract.js +0 -460
- package/dist/contract/rln_base_contract.js.map +0 -1
- package/dist/contract/types.d.ts +0 -40
- package/dist/contract/types.js +0 -8
- package/dist/contract/types.js.map +0 -1
- package/dist/credentials_manager.d.ts +0 -50
- package/dist/credentials_manager.js +0 -215
- package/dist/credentials_manager.js.map +0 -1
- package/dist/types.d.ts +0 -27
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/src/contract/rln_base_contract.ts +0 -707
- package/src/contract/types.ts +0 -48
- package/src/credentials_manager.ts +0 -306
- package/src/types.ts +0 -31
@@ -21,45 +21,167 @@ 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 } from './contract/constants.js';
|
25
|
-
import {
|
24
|
+
import { LINEA_CONTRACT, DEFAULT_RATE_LIMIT } from './contract/constants.js';
|
25
|
+
import { RLNContract } from './contract/rln_contract.js';
|
26
|
+
import { Keystore } from './keystore/keystore.js';
|
26
27
|
import verificationKey from './resources/verification_key.js';
|
27
28
|
import { builder } from './resources/witness_calculator.js';
|
29
|
+
import { extractMetaMaskSigner } from './utils/metamask.js';
|
30
|
+
import './utils/epoch.js';
|
28
31
|
import { Zerokit } from './zerokit.js';
|
29
32
|
|
30
33
|
const log = new Logger("waku:rln");
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
static async create() {
|
38
|
-
try {
|
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;
|
34
|
+
async function loadWitnessCalculator() {
|
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}`);
|
54
40
|
}
|
41
|
+
return await builder(new Uint8Array(await response.arrayBuffer()), false);
|
55
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 {
|
86
|
+
zerokit;
|
87
|
+
started = false;
|
88
|
+
starting = false;
|
89
|
+
_contract;
|
90
|
+
_signer;
|
91
|
+
keystore = Keystore.create();
|
92
|
+
_credentials;
|
56
93
|
constructor(zerokit) {
|
57
|
-
super(zerokit);
|
58
94
|
this.zerokit = zerokit;
|
59
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;
|
107
|
+
try {
|
108
|
+
const { credentials, keystore } = await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
|
109
|
+
const { signer, address } = await this.determineStartOptions(options, credentials);
|
110
|
+
if (keystore) {
|
111
|
+
this.keystore = keystore;
|
112
|
+
}
|
113
|
+
this._credentials = credentials;
|
114
|
+
this._signer = signer;
|
115
|
+
this._contract = await RLNContract.init(this, {
|
116
|
+
address: address,
|
117
|
+
signer: signer,
|
118
|
+
rateLimit: options.rateLimit ?? this.zerokit.getRateLimit
|
119
|
+
});
|
120
|
+
this.started = true;
|
121
|
+
}
|
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();
|
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
|
+
}
|
144
|
+
static async decryptCredentialsIfNeeded(credentials) {
|
145
|
+
if (!credentials) {
|
146
|
+
return {};
|
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);
|
181
|
+
}
|
60
182
|
async createEncoder(options) {
|
61
183
|
const { credentials: decryptedCredentials } = await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
|
62
|
-
const credentials = decryptedCredentials || this.
|
184
|
+
const credentials = decryptedCredentials || this._credentials;
|
63
185
|
if (!credentials) {
|
64
186
|
throw Error("Failed to create Encoder: missing RLN credentials. Use createRLNEncoder directly.");
|
65
187
|
}
|
@@ -71,40 +193,28 @@ class RLNInstance extends RLNCredentialsManager {
|
|
71
193
|
credential: credentials.identity
|
72
194
|
});
|
73
195
|
}
|
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;
|
208
|
+
if (chainId !== currentChainId) {
|
209
|
+
throw Error(`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`);
|
210
|
+
}
|
211
|
+
}
|
74
212
|
createDecoder(contentTopic) {
|
75
213
|
return createRLNDecoder({
|
76
214
|
rlnInstance: this,
|
77
215
|
decoder: createDecoder(contentTopic)
|
78
216
|
});
|
79
217
|
}
|
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
|
-
}
|
108
218
|
}
|
109
219
|
|
110
|
-
export { RLNInstance };
|
220
|
+
export { RLNInstance, create };
|