@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
@@ -0,0 +1,48 @@
|
|
1
|
+
import { ethers } from "ethers";
|
2
|
+
|
3
|
+
export interface CustomQueryOptions extends FetchMembersOptions {
|
4
|
+
membersFilter: ethers.EventFilter;
|
5
|
+
}
|
6
|
+
|
7
|
+
export type Member = {
|
8
|
+
idCommitment: string;
|
9
|
+
index: ethers.BigNumber;
|
10
|
+
};
|
11
|
+
|
12
|
+
export interface RLNContractOptions {
|
13
|
+
signer: ethers.Signer;
|
14
|
+
address: string;
|
15
|
+
rateLimit?: number;
|
16
|
+
}
|
17
|
+
|
18
|
+
export interface RLNContractInitOptions extends RLNContractOptions {
|
19
|
+
contract?: ethers.Contract;
|
20
|
+
}
|
21
|
+
|
22
|
+
export interface MembershipRegisteredEvent {
|
23
|
+
idCommitment: string;
|
24
|
+
membershipRateLimit: ethers.BigNumber;
|
25
|
+
index: ethers.BigNumber;
|
26
|
+
}
|
27
|
+
|
28
|
+
export type FetchMembersOptions = {
|
29
|
+
fromBlock?: number;
|
30
|
+
fetchRange?: number;
|
31
|
+
fetchChunks?: number;
|
32
|
+
};
|
33
|
+
|
34
|
+
export interface MembershipInfo {
|
35
|
+
index: ethers.BigNumber;
|
36
|
+
idCommitment: string;
|
37
|
+
rateLimit: number;
|
38
|
+
startBlock: number;
|
39
|
+
endBlock: number;
|
40
|
+
state: MembershipState;
|
41
|
+
}
|
42
|
+
|
43
|
+
export enum MembershipState {
|
44
|
+
Active = "Active",
|
45
|
+
GracePeriod = "GracePeriod",
|
46
|
+
Expired = "Expired",
|
47
|
+
ErasedAwaitsWithdrawal = "ErasedAwaitsWithdrawal"
|
48
|
+
}
|
package/src/create.ts
CHANGED
@@ -5,5 +5,5 @@ export async function createRLN(): Promise<RLNInstance> {
|
|
5
5
|
// asynchronously. This file does the single async import, so
|
6
6
|
// that no one else needs to worry about it again.
|
7
7
|
const rlnModule = await import("./rln.js");
|
8
|
-
return rlnModule.create();
|
8
|
+
return rlnModule.RLNInstance.create();
|
9
9
|
}
|
@@ -0,0 +1,306 @@
|
|
1
|
+
import { hmac } from "@noble/hashes/hmac";
|
2
|
+
import { sha256 } from "@noble/hashes/sha256";
|
3
|
+
import { Logger } from "@waku/utils";
|
4
|
+
import { ethers } from "ethers";
|
5
|
+
|
6
|
+
import { LINEA_CONTRACT } from "./contract/constants.js";
|
7
|
+
import { RLNBaseContract } from "./contract/rln_base_contract.js";
|
8
|
+
import { IdentityCredential } from "./identity.js";
|
9
|
+
import { Keystore } from "./keystore/index.js";
|
10
|
+
import type {
|
11
|
+
DecryptedCredentials,
|
12
|
+
EncryptedCredentials
|
13
|
+
} from "./keystore/index.js";
|
14
|
+
import { KeystoreEntity, Password } from "./keystore/types.js";
|
15
|
+
import { RegisterMembershipOptions, StartRLNOptions } from "./types.js";
|
16
|
+
import {
|
17
|
+
buildBigIntFromUint8Array,
|
18
|
+
extractMetaMaskSigner
|
19
|
+
} from "./utils/index.js";
|
20
|
+
import { Zerokit } from "./zerokit.js";
|
21
|
+
|
22
|
+
const log = new Logger("waku:credentials");
|
23
|
+
|
24
|
+
/**
|
25
|
+
* Manages credentials for RLN
|
26
|
+
* This is a lightweight implementation of the RLN contract that doesn't require Zerokit
|
27
|
+
* It is used to register membership and generate identity credentials
|
28
|
+
*/
|
29
|
+
export class RLNCredentialsManager {
|
30
|
+
protected started = false;
|
31
|
+
protected starting = false;
|
32
|
+
|
33
|
+
private _contract: undefined | RLNBaseContract;
|
34
|
+
private _signer: undefined | ethers.Signer;
|
35
|
+
|
36
|
+
protected keystore = Keystore.create();
|
37
|
+
private _credentials: undefined | DecryptedCredentials;
|
38
|
+
|
39
|
+
public zerokit: undefined | Zerokit;
|
40
|
+
|
41
|
+
public constructor(zerokit?: Zerokit) {
|
42
|
+
log.info("RLNCredentialsManager initialized");
|
43
|
+
this.zerokit = zerokit;
|
44
|
+
}
|
45
|
+
|
46
|
+
public get contract(): undefined | RLNBaseContract {
|
47
|
+
return this._contract;
|
48
|
+
}
|
49
|
+
|
50
|
+
public set contract(contract: RLNBaseContract | undefined) {
|
51
|
+
this._contract = contract;
|
52
|
+
}
|
53
|
+
|
54
|
+
public get signer(): undefined | ethers.Signer {
|
55
|
+
return this._signer;
|
56
|
+
}
|
57
|
+
|
58
|
+
public set signer(signer: ethers.Signer | undefined) {
|
59
|
+
this._signer = signer;
|
60
|
+
}
|
61
|
+
|
62
|
+
public get credentials(): undefined | DecryptedCredentials {
|
63
|
+
return this._credentials;
|
64
|
+
}
|
65
|
+
|
66
|
+
public set credentials(credentials: DecryptedCredentials | undefined) {
|
67
|
+
this._credentials = credentials;
|
68
|
+
}
|
69
|
+
|
70
|
+
public get provider(): undefined | ethers.providers.Provider {
|
71
|
+
return this.contract?.provider;
|
72
|
+
}
|
73
|
+
|
74
|
+
public async start(options: StartRLNOptions = {}): Promise<void> {
|
75
|
+
if (this.started || this.starting) {
|
76
|
+
log.info("RLNCredentialsManager already started or starting");
|
77
|
+
return;
|
78
|
+
}
|
79
|
+
|
80
|
+
log.info("Starting RLNCredentialsManager");
|
81
|
+
this.starting = true;
|
82
|
+
|
83
|
+
try {
|
84
|
+
const { credentials, keystore } =
|
85
|
+
await RLNCredentialsManager.decryptCredentialsIfNeeded(
|
86
|
+
options.credentials
|
87
|
+
);
|
88
|
+
|
89
|
+
if (credentials) {
|
90
|
+
log.info("Credentials successfully decrypted");
|
91
|
+
}
|
92
|
+
|
93
|
+
const { signer, address, rateLimit } = await this.determineStartOptions(
|
94
|
+
options,
|
95
|
+
credentials
|
96
|
+
);
|
97
|
+
|
98
|
+
log.info(`Using contract address: ${address}`);
|
99
|
+
|
100
|
+
if (keystore) {
|
101
|
+
this.keystore = keystore;
|
102
|
+
log.info("Using provided keystore");
|
103
|
+
}
|
104
|
+
|
105
|
+
this._credentials = credentials;
|
106
|
+
this._signer = signer!;
|
107
|
+
this._contract = new RLNBaseContract({
|
108
|
+
address: address!,
|
109
|
+
signer: signer!,
|
110
|
+
rateLimit: rateLimit ?? this.zerokit?.getRateLimit
|
111
|
+
});
|
112
|
+
|
113
|
+
log.info("RLNCredentialsManager successfully started");
|
114
|
+
this.started = true;
|
115
|
+
} catch (error) {
|
116
|
+
log.error("Failed to start RLNCredentialsManager", error);
|
117
|
+
throw error;
|
118
|
+
} finally {
|
119
|
+
this.starting = false;
|
120
|
+
}
|
121
|
+
}
|
122
|
+
|
123
|
+
public async registerMembership(
|
124
|
+
options: RegisterMembershipOptions
|
125
|
+
): Promise<undefined | DecryptedCredentials> {
|
126
|
+
if (!this.contract) {
|
127
|
+
log.error("RLN Contract is not initialized");
|
128
|
+
throw Error("RLN Contract is not initialized.");
|
129
|
+
}
|
130
|
+
|
131
|
+
log.info("Registering membership");
|
132
|
+
let identity = "identity" in options && options.identity;
|
133
|
+
|
134
|
+
if ("signature" in options) {
|
135
|
+
log.info("Generating identity from signature");
|
136
|
+
if (this.zerokit) {
|
137
|
+
log.info("Using Zerokit to generate identity");
|
138
|
+
identity = this.zerokit.generateSeededIdentityCredential(
|
139
|
+
options.signature
|
140
|
+
);
|
141
|
+
} else {
|
142
|
+
log.info("Using local implementation to generate identity");
|
143
|
+
identity = this.generateSeededIdentityCredential(options.signature);
|
144
|
+
}
|
145
|
+
}
|
146
|
+
|
147
|
+
if (!identity) {
|
148
|
+
log.error("Missing signature or identity to register membership");
|
149
|
+
throw Error("Missing signature or identity to register membership.");
|
150
|
+
}
|
151
|
+
|
152
|
+
log.info("Registering identity with contract");
|
153
|
+
return this.contract.registerWithIdentity(identity);
|
154
|
+
}
|
155
|
+
|
156
|
+
/**
|
157
|
+
* Changes credentials in use by relying on provided Keystore earlier in rln.start
|
158
|
+
* @param id: string, hash of credentials to select from Keystore
|
159
|
+
* @param password: string or bytes to use to decrypt credentials from Keystore
|
160
|
+
*/
|
161
|
+
public async useCredentials(id: string, password: Password): Promise<void> {
|
162
|
+
log.info(`Attempting to use credentials with ID: ${id}`);
|
163
|
+
this._credentials = await this.keystore?.readCredential(id, password);
|
164
|
+
if (this._credentials) {
|
165
|
+
log.info("Successfully loaded credentials");
|
166
|
+
} else {
|
167
|
+
log.warn("Failed to load credentials");
|
168
|
+
}
|
169
|
+
}
|
170
|
+
|
171
|
+
protected async determineStartOptions(
|
172
|
+
options: StartRLNOptions,
|
173
|
+
credentials: KeystoreEntity | undefined
|
174
|
+
): Promise<StartRLNOptions> {
|
175
|
+
let chainId = credentials?.membership.chainId;
|
176
|
+
const address =
|
177
|
+
credentials?.membership.address ||
|
178
|
+
options.address ||
|
179
|
+
LINEA_CONTRACT.address;
|
180
|
+
|
181
|
+
if (address === LINEA_CONTRACT.address) {
|
182
|
+
chainId = LINEA_CONTRACT.chainId;
|
183
|
+
log.info(`Using Linea contract with chainId: ${chainId}`);
|
184
|
+
}
|
185
|
+
|
186
|
+
const signer = options.signer || (await extractMetaMaskSigner());
|
187
|
+
const currentChainId = await signer.getChainId();
|
188
|
+
log.info(`Current chain ID: ${currentChainId}`);
|
189
|
+
|
190
|
+
if (chainId && chainId !== currentChainId.toString()) {
|
191
|
+
log.error(
|
192
|
+
`Chain ID mismatch: contract=${chainId}, current=${currentChainId}`
|
193
|
+
);
|
194
|
+
throw Error(
|
195
|
+
`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`
|
196
|
+
);
|
197
|
+
}
|
198
|
+
|
199
|
+
return {
|
200
|
+
signer,
|
201
|
+
address
|
202
|
+
};
|
203
|
+
}
|
204
|
+
|
205
|
+
protected static async decryptCredentialsIfNeeded(
|
206
|
+
credentials?: EncryptedCredentials | DecryptedCredentials
|
207
|
+
): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
|
208
|
+
if (!credentials) {
|
209
|
+
log.info("No credentials provided");
|
210
|
+
return {};
|
211
|
+
}
|
212
|
+
|
213
|
+
if ("identity" in credentials) {
|
214
|
+
log.info("Using already decrypted credentials");
|
215
|
+
return { credentials };
|
216
|
+
}
|
217
|
+
|
218
|
+
log.info("Attempting to decrypt credentials");
|
219
|
+
const keystore = Keystore.fromString(credentials.keystore);
|
220
|
+
|
221
|
+
if (!keystore) {
|
222
|
+
log.warn("Failed to create keystore from string");
|
223
|
+
return {};
|
224
|
+
}
|
225
|
+
|
226
|
+
try {
|
227
|
+
const decryptedCredentials = await keystore.readCredential(
|
228
|
+
credentials.id,
|
229
|
+
credentials.password
|
230
|
+
);
|
231
|
+
log.info(`Successfully decrypted credentials with ID: ${credentials.id}`);
|
232
|
+
|
233
|
+
return {
|
234
|
+
keystore,
|
235
|
+
credentials: decryptedCredentials
|
236
|
+
};
|
237
|
+
} catch (error) {
|
238
|
+
log.error("Failed to decrypt credentials", error);
|
239
|
+
throw error;
|
240
|
+
}
|
241
|
+
}
|
242
|
+
|
243
|
+
protected async verifyCredentialsAgainstContract(
|
244
|
+
credentials: KeystoreEntity
|
245
|
+
): Promise<void> {
|
246
|
+
if (!this.contract) {
|
247
|
+
throw Error(
|
248
|
+
"Failed to verify chain coordinates: no contract initialized."
|
249
|
+
);
|
250
|
+
}
|
251
|
+
|
252
|
+
const registryAddress = credentials.membership.address;
|
253
|
+
const currentRegistryAddress = this.contract.address;
|
254
|
+
if (registryAddress !== currentRegistryAddress) {
|
255
|
+
throw Error(
|
256
|
+
`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`
|
257
|
+
);
|
258
|
+
}
|
259
|
+
|
260
|
+
const chainId = credentials.membership.chainId;
|
261
|
+
const network = await this.contract.provider.getNetwork();
|
262
|
+
const currentChainId = network.chainId;
|
263
|
+
if (chainId !== currentChainId.toString()) {
|
264
|
+
throw Error(
|
265
|
+
`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`
|
266
|
+
);
|
267
|
+
}
|
268
|
+
}
|
269
|
+
|
270
|
+
/**
|
271
|
+
* Generates an identity credential from a seed string
|
272
|
+
* This is a pure implementation that doesn't rely on Zerokit
|
273
|
+
* @param seed A string seed to generate the identity from
|
274
|
+
* @returns IdentityCredential
|
275
|
+
*/
|
276
|
+
private generateSeededIdentityCredential(seed: string): IdentityCredential {
|
277
|
+
log.info("Generating seeded identity credential");
|
278
|
+
// Convert the seed to bytes
|
279
|
+
const encoder = new TextEncoder();
|
280
|
+
const seedBytes = encoder.encode(seed);
|
281
|
+
|
282
|
+
// Generate deterministic values using HMAC-SHA256
|
283
|
+
// We use different context strings for each component to ensure they're different
|
284
|
+
const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
|
285
|
+
const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
|
286
|
+
|
287
|
+
// Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
|
288
|
+
const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
|
289
|
+
const idSecretHash = sha256(combinedBytes);
|
290
|
+
|
291
|
+
// Generate IDCommitment as a hash of IDSecretHash
|
292
|
+
const idCommitment = sha256(idSecretHash);
|
293
|
+
|
294
|
+
// Convert IDCommitment to BigInt
|
295
|
+
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
|
296
|
+
|
297
|
+
log.info("Successfully generated identity credential");
|
298
|
+
return new IdentityCredential(
|
299
|
+
idTrapdoor,
|
300
|
+
idNullifier,
|
301
|
+
idSecretHash,
|
302
|
+
idCommitment,
|
303
|
+
idCommitmentBigInt
|
304
|
+
);
|
305
|
+
}
|
306
|
+
}
|
package/src/identity.ts
CHANGED
@@ -29,13 +29,12 @@ export class IdentityCredential {
|
|
29
29
|
);
|
30
30
|
}
|
31
31
|
|
32
|
-
public toJSON():
|
33
|
-
return
|
34
|
-
Array.from(this.IDTrapdoor),
|
35
|
-
Array.from(this.IDNullifier),
|
36
|
-
Array.from(this.IDSecretHash),
|
37
|
-
Array.from(this.IDCommitment)
|
38
|
-
|
39
|
-
];
|
32
|
+
public toJSON(): Record<string, number[]> {
|
33
|
+
return {
|
34
|
+
idTrapdoor: Array.from(this.IDTrapdoor),
|
35
|
+
idNullifier: Array.from(this.IDNullifier),
|
36
|
+
idSecretHash: Array.from(this.IDSecretHash),
|
37
|
+
idCommitment: Array.from(this.IDCommitment)
|
38
|
+
};
|
40
39
|
}
|
41
40
|
}
|
package/src/index.ts
CHANGED
@@ -1,19 +1,19 @@
|
|
1
1
|
import { RLNDecoder, RLNEncoder } from "./codec.js";
|
2
2
|
import { RLN_ABI } from "./contract/abi.js";
|
3
3
|
import { LINEA_CONTRACT, RLNContract } from "./contract/index.js";
|
4
|
-
import {
|
4
|
+
import { RLNBaseContract } from "./contract/rln_base_contract.js";
|
5
5
|
import { createRLN } from "./create.js";
|
6
|
+
import { RLNCredentialsManager } from "./credentials_manager.js";
|
6
7
|
import { IdentityCredential } from "./identity.js";
|
7
8
|
import { Keystore } from "./keystore/index.js";
|
8
9
|
import { Proof } from "./proof.js";
|
9
10
|
import { RLNInstance } from "./rln.js";
|
10
|
-
import { RLNLightInstance } from "./rln_light.js";
|
11
11
|
import { MerkleRootTracker } from "./root_tracker.js";
|
12
12
|
import { extractMetaMaskSigner } from "./utils/index.js";
|
13
13
|
|
14
14
|
export {
|
15
|
-
|
16
|
-
|
15
|
+
RLNCredentialsManager,
|
16
|
+
RLNBaseContract,
|
17
17
|
createRLN,
|
18
18
|
Keystore,
|
19
19
|
RLNInstance,
|
package/src/keystore/keystore.ts
CHANGED
@@ -251,11 +251,11 @@ export class Keystore {
|
|
251
251
|
const str = bytesToUtf8(bytes);
|
252
252
|
const obj = JSON.parse(str);
|
253
253
|
|
254
|
-
// Get identity
|
255
|
-
const
|
254
|
+
// Get identity fields from named object
|
255
|
+
const { idTrapdoor, idNullifier, idSecretHash, idCommitment } = _.get(
|
256
256
|
obj,
|
257
257
|
"identityCredential",
|
258
|
-
|
258
|
+
{}
|
259
259
|
);
|
260
260
|
|
261
261
|
const idTrapdoorArray = new Uint8Array(idTrapdoor || []);
|
@@ -276,7 +276,7 @@ export class Keystore {
|
|
276
276
|
treeIndex: _.get(obj, "treeIndex"),
|
277
277
|
chainId: _.get(obj, "membershipContract.chainId"),
|
278
278
|
address: _.get(obj, "membershipContract.address"),
|
279
|
-
rateLimit: _.get(obj, "
|
279
|
+
rateLimit: _.get(obj, "userMessageLimit")
|
280
280
|
}
|
281
281
|
};
|
282
282
|
} catch (err) {
|
@@ -298,17 +298,18 @@ export class Keystore {
|
|
298
298
|
private static fromIdentityToBytes(options: KeystoreEntity): Uint8Array {
|
299
299
|
return utf8ToBytes(
|
300
300
|
JSON.stringify({
|
301
|
-
treeIndex: options.membership.treeIndex,
|
302
|
-
identityCredential: [
|
303
|
-
Array.from(options.identity.IDTrapdoor),
|
304
|
-
Array.from(options.identity.IDNullifier),
|
305
|
-
Array.from(options.identity.IDSecretHash),
|
306
|
-
Array.from(options.identity.IDCommitment)
|
307
|
-
],
|
308
301
|
membershipContract: {
|
309
302
|
chainId: options.membership.chainId,
|
310
303
|
address: options.membership.address
|
311
|
-
}
|
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
|
312
313
|
})
|
313
314
|
);
|
314
315
|
}
|