@waku/rln 0.1.5-861a776.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 +8 -8
- package/bundle/packages/rln/dist/identity.js +0 -9
- package/bundle/packages/rln/dist/keystore/keystore.js +25 -15
- package/bundle/packages/rln/dist/rln.js +4 -4
- 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.js +8 -8
- package/dist/contract/rln_contract.js.map +1 -1
- 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 +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 +25 -15
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +1 -1
- package/dist/rln.js +4 -4
- 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 +8 -8
- package/src/identity.ts +0 -10
- package/src/index.ts +0 -4
- package/src/keystore/keystore.ts +43 -27
- package/src/keystore/types.ts +1 -1
- package/src/rln.ts +4 -4
- package/bundle/packages/rln/dist/contract/rln_light_contract.js +0 -477
- package/bundle/packages/rln/dist/rln_light.js +0 -149
- 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_light_contract.d.ts +0 -124
- package/dist/contract/rln_light_contract.js +0 -460
- 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/contract/rln_light_contract.ts +0 -725
- package/src/rln_light.ts +0 -235
package/src/rln_light.ts
DELETED
@@ -1,235 +0,0 @@
|
|
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 { RLNLightContract } from "./contract/rln_light_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 {
|
16
|
-
buildBigIntFromUint8Array,
|
17
|
-
extractMetaMaskSigner
|
18
|
-
} from "./utils/index.js";
|
19
|
-
|
20
|
-
const log = new Logger("waku:rln");
|
21
|
-
|
22
|
-
/**
|
23
|
-
* Create an instance of RLN
|
24
|
-
* @returns RLNInstance
|
25
|
-
*/
|
26
|
-
export async function create(): Promise<RLNLightInstance> {
|
27
|
-
try {
|
28
|
-
return new RLNLightInstance();
|
29
|
-
} catch (error) {
|
30
|
-
log.error("Failed to initialize RLN:", error);
|
31
|
-
throw error;
|
32
|
-
}
|
33
|
-
}
|
34
|
-
|
35
|
-
type StartRLNOptions = {
|
36
|
-
/**
|
37
|
-
* If not set - will extract MetaMask account and get signer from it.
|
38
|
-
*/
|
39
|
-
signer?: ethers.Signer;
|
40
|
-
/**
|
41
|
-
* If not set - will use default SEPOLIA_CONTRACT address.
|
42
|
-
*/
|
43
|
-
address?: string;
|
44
|
-
/**
|
45
|
-
* Credentials to use for generating proofs and connecting to the contract and network.
|
46
|
-
* If provided used for validating the network chainId and connecting to registry contract.
|
47
|
-
*/
|
48
|
-
credentials?: EncryptedCredentials | DecryptedCredentials;
|
49
|
-
/**
|
50
|
-
* Rate limit for the member.
|
51
|
-
*/
|
52
|
-
rateLimit?: number;
|
53
|
-
};
|
54
|
-
|
55
|
-
type RegisterMembershipOptions =
|
56
|
-
| { signature: string }
|
57
|
-
| { identity: IdentityCredential };
|
58
|
-
|
59
|
-
export class RLNLightInstance {
|
60
|
-
private started = false;
|
61
|
-
private starting = false;
|
62
|
-
|
63
|
-
private _contract: undefined | RLNLightContract;
|
64
|
-
private _signer: undefined | ethers.Signer;
|
65
|
-
|
66
|
-
private keystore = Keystore.create();
|
67
|
-
private _credentials: undefined | DecryptedCredentials;
|
68
|
-
|
69
|
-
public constructor() {}
|
70
|
-
|
71
|
-
public get contract(): undefined | RLNLightContract {
|
72
|
-
return this._contract;
|
73
|
-
}
|
74
|
-
|
75
|
-
public get signer(): undefined | ethers.Signer {
|
76
|
-
return this._signer;
|
77
|
-
}
|
78
|
-
|
79
|
-
public async start(options: StartRLNOptions = {}): Promise<void> {
|
80
|
-
if (this.started || this.starting) {
|
81
|
-
return;
|
82
|
-
}
|
83
|
-
|
84
|
-
this.starting = true;
|
85
|
-
|
86
|
-
try {
|
87
|
-
const { credentials, keystore } =
|
88
|
-
await RLNLightInstance.decryptCredentialsIfNeeded(options.credentials);
|
89
|
-
const { signer, address, rateLimit } = await this.determineStartOptions(
|
90
|
-
options,
|
91
|
-
credentials
|
92
|
-
);
|
93
|
-
|
94
|
-
if (keystore) {
|
95
|
-
this.keystore = keystore;
|
96
|
-
}
|
97
|
-
|
98
|
-
this._credentials = credentials;
|
99
|
-
this._signer = signer!;
|
100
|
-
this._contract = await RLNLightContract.init({
|
101
|
-
address: address!,
|
102
|
-
signer: signer!,
|
103
|
-
rateLimit: rateLimit
|
104
|
-
});
|
105
|
-
this.started = true;
|
106
|
-
} finally {
|
107
|
-
this.starting = false;
|
108
|
-
}
|
109
|
-
}
|
110
|
-
|
111
|
-
public get credentials(): DecryptedCredentials | undefined {
|
112
|
-
return this._credentials;
|
113
|
-
}
|
114
|
-
|
115
|
-
private async determineStartOptions(
|
116
|
-
options: StartRLNOptions,
|
117
|
-
credentials: KeystoreEntity | undefined
|
118
|
-
): Promise<StartRLNOptions> {
|
119
|
-
let chainId = credentials?.membership.chainId;
|
120
|
-
const address =
|
121
|
-
credentials?.membership.address ||
|
122
|
-
options.address ||
|
123
|
-
LINEA_CONTRACT.address;
|
124
|
-
|
125
|
-
if (address === LINEA_CONTRACT.address) {
|
126
|
-
chainId = LINEA_CONTRACT.chainId;
|
127
|
-
}
|
128
|
-
|
129
|
-
const signer = options.signer || (await extractMetaMaskSigner());
|
130
|
-
const currentChainId = (await signer.getChainId()).toString();
|
131
|
-
|
132
|
-
if (chainId && chainId !== currentChainId) {
|
133
|
-
throw Error(
|
134
|
-
`Failed to start RLN contract, chain ID of contract is different from current one: contract-${chainId}, current network-${currentChainId}`
|
135
|
-
);
|
136
|
-
}
|
137
|
-
|
138
|
-
return {
|
139
|
-
signer,
|
140
|
-
address
|
141
|
-
};
|
142
|
-
}
|
143
|
-
|
144
|
-
private static async decryptCredentialsIfNeeded(
|
145
|
-
credentials?: EncryptedCredentials | DecryptedCredentials
|
146
|
-
): Promise<{ credentials?: DecryptedCredentials; keystore?: Keystore }> {
|
147
|
-
if (!credentials) {
|
148
|
-
return {};
|
149
|
-
}
|
150
|
-
|
151
|
-
if ("identity" in credentials) {
|
152
|
-
return { credentials };
|
153
|
-
}
|
154
|
-
|
155
|
-
const keystore = Keystore.fromString(credentials.keystore);
|
156
|
-
|
157
|
-
if (!keystore) {
|
158
|
-
return {};
|
159
|
-
}
|
160
|
-
|
161
|
-
const decryptedCredentials = await keystore.readCredential(
|
162
|
-
credentials.id,
|
163
|
-
credentials.password
|
164
|
-
);
|
165
|
-
|
166
|
-
return {
|
167
|
-
keystore,
|
168
|
-
credentials: decryptedCredentials
|
169
|
-
};
|
170
|
-
}
|
171
|
-
|
172
|
-
/**
|
173
|
-
* Generates an identity credential from a seed string
|
174
|
-
* This is a pure implementation that doesn't rely on Zerokit
|
175
|
-
* @param seed A string seed to generate the identity from
|
176
|
-
* @returns IdentityCredential
|
177
|
-
*/
|
178
|
-
private generateSeededIdentityCredential(seed: string): IdentityCredential {
|
179
|
-
// Convert the seed to bytes
|
180
|
-
const encoder = new TextEncoder();
|
181
|
-
const seedBytes = encoder.encode(seed);
|
182
|
-
|
183
|
-
// Generate deterministic values using HMAC-SHA256
|
184
|
-
// We use different context strings for each component to ensure they're different
|
185
|
-
const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
|
186
|
-
const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
|
187
|
-
|
188
|
-
// Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
|
189
|
-
const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
|
190
|
-
const idSecretHash = sha256(combinedBytes);
|
191
|
-
|
192
|
-
// Generate IDCommitment as a hash of IDSecretHash
|
193
|
-
const idCommitment = sha256(idSecretHash);
|
194
|
-
|
195
|
-
// Convert IDCommitment to BigInt
|
196
|
-
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
|
197
|
-
|
198
|
-
return new IdentityCredential(
|
199
|
-
idTrapdoor,
|
200
|
-
idNullifier,
|
201
|
-
idSecretHash,
|
202
|
-
idCommitment,
|
203
|
-
idCommitmentBigInt
|
204
|
-
);
|
205
|
-
}
|
206
|
-
|
207
|
-
public async registerMembership(
|
208
|
-
options: RegisterMembershipOptions
|
209
|
-
): Promise<undefined | DecryptedCredentials> {
|
210
|
-
if (!this.contract) {
|
211
|
-
throw Error("RLN Contract is not initialized.");
|
212
|
-
}
|
213
|
-
|
214
|
-
let identity = "identity" in options && options.identity;
|
215
|
-
|
216
|
-
if ("signature" in options) {
|
217
|
-
identity = this.generateSeededIdentityCredential(options.signature);
|
218
|
-
}
|
219
|
-
|
220
|
-
if (!identity) {
|
221
|
-
throw Error("Missing signature or identity to register membership.");
|
222
|
-
}
|
223
|
-
|
224
|
-
return this.contract.registerWithIdentity(identity);
|
225
|
-
}
|
226
|
-
|
227
|
-
/**
|
228
|
-
* Changes credentials in use by relying on provided Keystore earlier in rln.start
|
229
|
-
* @param id: string, hash of credentials to select from Keystore
|
230
|
-
* @param password: string or bytes to use to decrypt credentials from Keystore
|
231
|
-
*/
|
232
|
-
public async useCredentials(id: string, password: Password): Promise<void> {
|
233
|
-
this._credentials = await this.keystore?.readCredential(id, password);
|
234
|
-
}
|
235
|
-
}
|