@waku/rln 0.1.5-9901863.0 → 0.1.5-aaa7a0c.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.
@@ -1,7 +1,7 @@
1
1
  import { RLN_ABI } from './abi.js';
2
2
 
3
3
  const LINEA_CONTRACT = {
4
- chainId: "59141",
4
+ chainId: 59141,
5
5
  address: "0xb9cd878c90e49f797b4431fbf4fb333108cb90e6",
6
6
  abi: RLN_ABI
7
7
  };
@@ -356,12 +356,12 @@ class RLNBaseContract {
356
356
  `and rate limit ${decodedData.membershipRateLimit}`);
357
357
  const network = await this.contract.provider.getNetwork();
358
358
  const address = this.contract.address;
359
- const membershipId = decodedData.index.toString();
359
+ const membershipId = Number(decodedData.index);
360
360
  return {
361
361
  identity,
362
362
  membership: {
363
363
  address,
364
- treeIndex: parseInt(membershipId),
364
+ treeIndex: membershipId,
365
365
  chainId: network.chainId.toString(),
366
366
  rateLimit: decodedData.membershipRateLimit.toNumber()
367
367
  }
@@ -415,12 +415,12 @@ class RLNBaseContract {
415
415
  `Rate limit: ${decodedData.membershipRateLimit}, Erased ${idCommitmentsToErase.length} commitments`);
416
416
  const network = await this.contract.provider.getNetwork();
417
417
  const address = this.contract.address;
418
- const membershipId = decodedData.index.toString();
418
+ const membershipId = Number(decodedData.index);
419
419
  return {
420
420
  identity,
421
421
  membership: {
422
422
  address,
423
- treeIndex: parseInt(membershipId),
423
+ treeIndex: membershipId,
424
424
  chainId: network.chainId.toString(),
425
425
  rateLimit: decodedData.membershipRateLimit.toNumber()
426
426
  }
@@ -32,33 +32,15 @@ const log = new Logger("waku:credentials");
32
32
  class RLNCredentialsManager {
33
33
  started = false;
34
34
  starting = false;
35
- _contract;
36
- _signer;
35
+ contract;
36
+ signer;
37
37
  keystore = Keystore.create();
38
- _credentials;
38
+ credentials;
39
39
  zerokit;
40
40
  constructor(zerokit) {
41
41
  log.info("RLNCredentialsManager initialized");
42
42
  this.zerokit = zerokit;
43
43
  }
44
- get contract() {
45
- return this._contract;
46
- }
47
- set contract(contract) {
48
- this._contract = contract;
49
- }
50
- get signer() {
51
- return this._signer;
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
44
  get provider() {
63
45
  return this.contract?.provider;
64
46
  }
@@ -80,12 +62,12 @@ class RLNCredentialsManager {
80
62
  this.keystore = keystore;
81
63
  log.info("Using provided keystore");
82
64
  }
83
- this._credentials = credentials;
84
- this._signer = signer;
85
- this._contract = new RLNBaseContract({
65
+ this.credentials = credentials;
66
+ this.signer = signer;
67
+ this.contract = new RLNBaseContract({
86
68
  address: address,
87
69
  signer: signer,
88
- rateLimit: rateLimit ?? this.zerokit?.getRateLimit
70
+ rateLimit: rateLimit ?? this.zerokit?.rateLimit
89
71
  });
90
72
  log.info("RLNCredentialsManager successfully started");
91
73
  this.started = true;
@@ -130,8 +112,8 @@ class RLNCredentialsManager {
130
112
  */
131
113
  async useCredentials(id, password) {
132
114
  log.info(`Attempting to use credentials with ID: ${id}`);
133
- this._credentials = await this.keystore?.readCredential(id, password);
134
- if (this._credentials) {
115
+ this.credentials = await this.keystore?.readCredential(id, password);
116
+ if (this.credentials) {
135
117
  log.info("Successfully loaded credentials");
136
118
  }
137
119
  else {
@@ -144,7 +126,7 @@ class RLNCredentialsManager {
144
126
  options.address ||
145
127
  LINEA_CONTRACT.address;
146
128
  if (address === LINEA_CONTRACT.address) {
147
- chainId = LINEA_CONTRACT.chainId;
129
+ chainId = LINEA_CONTRACT.chainId.toString();
148
130
  log.info(`Using Linea contract with chainId: ${chainId}`);
149
131
  }
150
132
  const signer = options.signer || (await extractMetaMaskSigner());
@@ -25,14 +25,6 @@ class IdentityCredential {
25
25
  const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment, 32);
26
26
  return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
27
27
  }
28
- toJSON() {
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
- };
35
- }
36
28
  }
37
29
 
38
30
  export { IdentityCredential };
@@ -17,7 +17,6 @@ import { Logger } from '../../../utils/dist/logger/index.js';
17
17
  import { sha256 } from '../../../../node_modules/ethereum-cryptography/esm/sha256.js';
18
18
  import { bytesToUtf8 } from '../../../../node_modules/ethereum-cryptography/esm/utils.js';
19
19
  import _ from '../../../../node_modules/lodash/lodash.js';
20
- import { IdentityCredential } from '../identity.js';
21
20
  import { buildBigIntFromUint8Array } from '../utils/bytes.js';
22
21
  import { keccak256Checksum, decryptEipKeystore } from './cipher.js';
23
22
  import { isKeystoreValid, isCredentialValid } from './schema_validator.js';
@@ -165,20 +164,20 @@ class Keystore {
165
164
  try {
166
165
  const str = bytesToUtf8(bytes);
167
166
  const obj = JSON.parse(str);
168
- // Get identity fields from named object
169
- const { idTrapdoor, idNullifier, idSecretHash, idCommitment } = _.get(obj, "identityCredential", {});
170
- const idTrapdoorArray = new Uint8Array(idTrapdoor || []);
171
- const idNullifierArray = new Uint8Array(idNullifier || []);
172
- const idSecretHashArray = new Uint8Array(idSecretHash || []);
173
- const idCommitmentArray = new Uint8Array(idCommitment || []);
174
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitmentArray);
167
+ // TODO: add runtime validation of nwaku credentials
175
168
  return {
176
- identity: new IdentityCredential(idTrapdoorArray, idNullifierArray, idSecretHashArray, idCommitmentArray, idCommitmentBigInt),
169
+ identity: {
170
+ IDCommitment: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", [])),
171
+ IDTrapdoor: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idTrapdoor", [])),
172
+ IDNullifier: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idNullifier", [])),
173
+ IDCommitmentBigInt: buildBigIntFromUint8Array(Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", []))),
174
+ IDSecretHash: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idSecretHash", []))
175
+ },
177
176
  membership: {
178
177
  treeIndex: _.get(obj, "treeIndex"),
179
178
  chainId: _.get(obj, "membershipContract.chainId"),
180
179
  address: _.get(obj, "membershipContract.address"),
181
- rateLimit: _.get(obj, "userMessageLimit")
180
+ rateLimit: _.get(obj, "membershipContract.rateLimit")
182
181
  }
183
182
  };
184
183
  }
@@ -187,6 +186,20 @@ class Keystore {
187
186
  return;
188
187
  }
189
188
  }
189
+ static fromArraylikeToBytes(obj) {
190
+ if (Array.isArray(obj)) {
191
+ return new Uint8Array(obj);
192
+ }
193
+ const bytes = [];
194
+ let index = 0;
195
+ let lastElement = obj[index];
196
+ while (lastElement !== undefined) {
197
+ bytes.push(lastElement);
198
+ index += 1;
199
+ lastElement = obj[index];
200
+ }
201
+ return new Uint8Array(bytes);
202
+ }
190
203
  // follows nwaku implementation
191
204
  // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L111
192
205
  static computeMembershipHash(info) {
@@ -196,18 +209,17 @@ class Keystore {
196
209
  // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
197
210
  static fromIdentityToBytes(options) {
198
211
  return utf8ToBytes(JSON.stringify({
199
- membershipContract: {
200
- chainId: options.membership.chainId,
201
- address: options.membership.address
202
- },
203
212
  treeIndex: options.membership.treeIndex,
204
213
  identityCredential: {
205
- idTrapdoor: Array.from(options.identity.IDTrapdoor),
214
+ idCommitment: Array.from(options.identity.IDCommitment),
206
215
  idNullifier: Array.from(options.identity.IDNullifier),
207
216
  idSecretHash: Array.from(options.identity.IDSecretHash),
208
- idCommitment: Array.from(options.identity.IDCommitment)
217
+ idTrapdoor: Array.from(options.identity.IDTrapdoor)
209
218
  },
210
- userMessageLimit: options.membership.rateLimit
219
+ membershipContract: {
220
+ chainId: options.membership.chainId,
221
+ address: options.membership.address
222
+ }
211
223
  }));
212
224
  }
213
225
  }
@@ -8,11 +8,11 @@ import { epochIntToBytes, dateToEpoch } from './utils/epoch.js';
8
8
  class Zerokit {
9
9
  zkRLN;
10
10
  witnessCalculator;
11
- rateLimit;
12
- constructor(zkRLN, witnessCalculator, rateLimit = DEFAULT_RATE_LIMIT) {
11
+ _rateLimit;
12
+ constructor(zkRLN, witnessCalculator, _rateLimit = DEFAULT_RATE_LIMIT) {
13
13
  this.zkRLN = zkRLN;
14
14
  this.witnessCalculator = witnessCalculator;
15
- this.rateLimit = rateLimit;
15
+ this._rateLimit = _rateLimit;
16
16
  }
17
17
  get getZkRLN() {
18
18
  return this.zkRLN;
@@ -20,8 +20,8 @@ class Zerokit {
20
20
  get getWitnessCalculator() {
21
21
  return this.witnessCalculator;
22
22
  }
23
- get getRateLimit() {
24
- return this.rateLimit;
23
+ get rateLimit() {
24
+ return this._rateLimit;
25
25
  }
26
26
  generateIdentityCredentials() {
27
27
  const memKeys = generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm