@waku/rln 0.1.6-10590da.0 → 0.1.6-27c1236.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.
Files changed (40) hide show
  1. package/bundle/index.js +1 -1
  2. package/bundle/packages/core/dist/lib/message/version_0.js +1 -4
  3. package/bundle/packages/rln/dist/contract/constants.js +7 -1
  4. package/bundle/packages/rln/dist/contract/rln_base_contract.js +12 -5
  5. package/bundle/packages/rln/dist/credentials_manager.js +12 -12
  6. package/bundle/packages/rln/dist/identity.js +2 -2
  7. package/bundle/packages/rln/dist/keystore/keystore.js +19 -11
  8. package/bundle/packages/rln/dist/message.js +11 -0
  9. package/bundle/packages/rln/dist/utils/bytes.js +3 -13
  10. package/dist/.tsbuildinfo +1 -1
  11. package/dist/contract/constants.d.ts +6 -0
  12. package/dist/contract/constants.js +6 -0
  13. package/dist/contract/constants.js.map +1 -1
  14. package/dist/contract/rln_base_contract.d.ts +6 -1
  15. package/dist/contract/rln_base_contract.js +12 -5
  16. package/dist/contract/rln_base_contract.js.map +1 -1
  17. package/dist/credentials_manager.js +12 -12
  18. package/dist/credentials_manager.js.map +1 -1
  19. package/dist/identity.js +2 -2
  20. package/dist/identity.js.map +1 -1
  21. package/dist/keystore/keystore.js +19 -11
  22. package/dist/keystore/keystore.js.map +1 -1
  23. package/dist/message.d.ts +5 -4
  24. package/dist/message.js +2 -0
  25. package/dist/message.js.map +1 -1
  26. package/dist/utils/bytes.d.ts +1 -6
  27. package/dist/utils/bytes.js +2 -12
  28. package/dist/utils/bytes.js.map +1 -1
  29. package/dist/utils/index.d.ts +1 -1
  30. package/dist/utils/index.js +1 -1
  31. package/dist/utils/index.js.map +1 -1
  32. package/package.json +1 -1
  33. package/src/contract/constants.ts +9 -0
  34. package/src/contract/rln_base_contract.ts +19 -5
  35. package/src/credentials_manager.ts +20 -14
  36. package/src/identity.ts +2 -2
  37. package/src/keystore/keystore.ts +33 -18
  38. package/src/message.ts +7 -4
  39. package/src/utils/bytes.ts +5 -15
  40. package/src/utils/index.ts +1 -1
@@ -1,5 +1,5 @@
1
1
  import { hmac } from "@noble/hashes/hmac";
2
- import { sha256 } from "@noble/hashes/sha256";
2
+ import { sha256 } from "@noble/hashes/sha2";
3
3
  import { Logger } from "@waku/utils";
4
4
  import { ethers } from "ethers";
5
5
 
@@ -14,7 +14,7 @@ import type {
14
14
  import { KeystoreEntity, Password } from "./keystore/types.js";
15
15
  import { RegisterMembershipOptions, StartRLNOptions } from "./types.js";
16
16
  import {
17
- buildBigIntFromUint8Array,
17
+ buildBigIntFromUint8ArrayLE,
18
18
  extractMetaMaskSigner
19
19
  } from "./utils/index.js";
20
20
  import { Zerokit } from "./zerokit.js";
@@ -116,7 +116,9 @@ export class RLNCredentialsManager {
116
116
  );
117
117
  } else {
118
118
  log.info("Using local implementation to generate identity");
119
- identity = this.generateSeededIdentityCredential(options.signature);
119
+ identity = await this.generateSeededIdentityCredential(
120
+ options.signature
121
+ );
120
122
  }
121
123
  }
122
124
 
@@ -249,7 +251,9 @@ export class RLNCredentialsManager {
249
251
  * @param seed A string seed to generate the identity from
250
252
  * @returns IdentityCredential
251
253
  */
252
- private generateSeededIdentityCredential(seed: string): IdentityCredential {
254
+ private async generateSeededIdentityCredential(
255
+ seed: string
256
+ ): Promise<IdentityCredential> {
253
257
  log.info("Generating seeded identity credential");
254
258
  // Convert the seed to bytes
255
259
  const encoder = new TextEncoder();
@@ -260,21 +264,23 @@ export class RLNCredentialsManager {
260
264
  const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
261
265
  const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
262
266
 
263
- // Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
264
267
  const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
265
268
  const idSecretHash = sha256(combinedBytes);
266
269
 
267
- // Generate IDCommitment as a hash of IDSecretHash
268
270
  const idCommitment = sha256(idSecretHash);
269
271
 
270
- // Convert IDCommitment to BigInt
271
- let idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
272
- const Q = BigInt(
273
- "21888242871839275222246405745257275088548364400416034343698204186575808495617"
274
- );
275
- if (idCommitmentBigInt >= Q) {
276
- log.warn("IDCommitment is greater than Q, truncating");
277
- idCommitmentBigInt = idCommitmentBigInt % Q;
272
+ let idCommitmentBigInt = buildBigIntFromUint8ArrayLE(idCommitment);
273
+ if (!this.contract) {
274
+ throw Error("RLN contract is not initialized");
275
+ }
276
+
277
+ const idCommitmentBigIntLimit = this.contract.idCommitmentBigIntLimit;
278
+
279
+ if (idCommitmentBigInt >= idCommitmentBigIntLimit) {
280
+ log.warn(
281
+ `ID commitment is greater than Q, reducing it by Q(idCommitmentBigIntLimit): ${idCommitmentBigInt} % ${idCommitmentBigIntLimit}`
282
+ );
283
+ idCommitmentBigInt = idCommitmentBigInt % idCommitmentBigIntLimit;
278
284
  }
279
285
 
280
286
  log.info("Successfully generated identity credential");
package/src/identity.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { buildBigIntFromUint8Array } from "./utils/index.js";
1
+ import { buildBigIntFromUint8ArrayLE } from "./utils/index.js";
2
2
 
3
3
  export class IdentityCredential {
4
4
  public constructor(
@@ -18,7 +18,7 @@ export class IdentityCredential {
18
18
  const idNullifier = memKeys.subarray(32, 64);
19
19
  const idSecretHash = memKeys.subarray(64, 96);
20
20
  const idCommitment = memKeys.subarray(96, 128);
21
- const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
21
+ const idCommitmentBigInt = buildBigIntFromUint8ArrayLE(idCommitment);
22
22
 
23
23
  return new IdentityCredential(
24
24
  idTrapdoor,
@@ -14,7 +14,7 @@ import {
14
14
  import _ from "lodash";
15
15
  import { v4 as uuidV4 } from "uuid";
16
16
 
17
- import { buildBigIntFromUint8Array } from "../utils/bytes.js";
17
+ import { buildBigIntFromUint8ArrayLE } from "../utils/bytes.js";
18
18
 
19
19
  import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
20
20
  import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
@@ -246,29 +246,37 @@ export class Keystore {
246
246
  private static fromBytesToIdentity(
247
247
  bytes: Uint8Array
248
248
  ): undefined | KeystoreEntity {
249
+ function fromLittleEndian(bytes: Uint8Array): Uint8Array {
250
+ return new Uint8Array(bytes).reverse();
251
+ }
249
252
  try {
250
253
  const str = bytesToUtf8(bytes);
251
254
  const obj = JSON.parse(str);
252
255
 
253
- // TODO: add runtime validation of nwaku credentials
256
+ // Use little-endian bytes directly for BigInt conversion (matches storage and contract expectation)
257
+ const idCommitmentLE = Keystore.fromArraylikeToBytes(
258
+ _.get(obj, "identityCredential.idCommitment", [])
259
+ );
260
+
254
261
  return {
255
262
  identity: {
256
- IDCommitment: Keystore.fromArraylikeToBytes(
257
- _.get(obj, "identityCredential.idCommitment", [])
258
- ),
259
- IDTrapdoor: Keystore.fromArraylikeToBytes(
260
- _.get(obj, "identityCredential.idTrapdoor", [])
261
- ),
262
- IDNullifier: Keystore.fromArraylikeToBytes(
263
- _.get(obj, "identityCredential.idNullifier", [])
263
+ IDCommitment: fromLittleEndian(idCommitmentLE),
264
+ IDTrapdoor: fromLittleEndian(
265
+ Keystore.fromArraylikeToBytes(
266
+ _.get(obj, "identityCredential.idTrapdoor", [])
267
+ )
264
268
  ),
265
- IDCommitmentBigInt: buildBigIntFromUint8Array(
269
+ IDNullifier: fromLittleEndian(
266
270
  Keystore.fromArraylikeToBytes(
267
- _.get(obj, "identityCredential.idCommitment", [])
271
+ _.get(obj, "identityCredential.idNullifier", [])
268
272
  )
269
273
  ),
270
- IDSecretHash: Keystore.fromArraylikeToBytes(
271
- _.get(obj, "identityCredential.idSecretHash", [])
274
+ // Do NOT reverse for BigInt conversion; use little-endian as stored
275
+ IDCommitmentBigInt: buildBigIntFromUint8ArrayLE(idCommitmentLE),
276
+ IDSecretHash: fromLittleEndian(
277
+ Keystore.fromArraylikeToBytes(
278
+ _.get(obj, "identityCredential.idSecretHash", [])
279
+ )
272
280
  )
273
281
  },
274
282
  membership: {
@@ -321,14 +329,21 @@ export class Keystore {
321
329
  // follows nwaku implementation
322
330
  // https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
323
331
  private static fromIdentityToBytes(options: KeystoreEntity): Uint8Array {
332
+ function toLittleEndian(bytes: Uint8Array): Uint8Array {
333
+ return new Uint8Array(bytes).reverse();
334
+ }
324
335
  return utf8ToBytes(
325
336
  JSON.stringify({
326
337
  treeIndex: options.membership.treeIndex,
327
338
  identityCredential: {
328
- idCommitment: Array.from(options.identity.IDCommitment),
329
- idNullifier: Array.from(options.identity.IDNullifier),
330
- idSecretHash: Array.from(options.identity.IDSecretHash),
331
- idTrapdoor: Array.from(options.identity.IDTrapdoor)
339
+ idCommitment: Array.from(
340
+ toLittleEndian(options.identity.IDCommitment)
341
+ ),
342
+ idNullifier: Array.from(toLittleEndian(options.identity.IDNullifier)),
343
+ idSecretHash: Array.from(
344
+ toLittleEndian(options.identity.IDSecretHash)
345
+ ),
346
+ idTrapdoor: Array.from(toLittleEndian(options.identity.IDTrapdoor))
332
347
  },
333
348
  membershipContract: {
334
349
  chainId: options.membership.chainId,
package/src/message.ts CHANGED
@@ -1,7 +1,9 @@
1
+ import { message } from "@waku/core";
1
2
  import type {
2
3
  IDecodedMessage,
3
4
  IMessage,
4
- IRateLimitProof
5
+ IRateLimitProof,
6
+ IRlnMessage
5
7
  } from "@waku/interfaces";
6
8
  import * as utils from "@waku/utils/bytes";
7
9
 
@@ -13,12 +15,13 @@ export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
13
15
  return new Uint8Array([...(msg.payload ?? []), ...contentTopicBytes]);
14
16
  }
15
17
 
16
- export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
18
+ export class RlnMessage<T extends IDecodedMessage> implements IRlnMessage {
17
19
  public pubsubTopic = "";
20
+ public version = message.version_0.Version;
18
21
 
19
22
  public constructor(
20
- public rlnInstance: RLNInstance,
21
- public msg: T,
23
+ private rlnInstance: RLNInstance,
24
+ private msg: T,
22
25
  public rateLimitProof: IRateLimitProof | undefined
23
26
  ) {}
24
27
 
@@ -56,21 +56,11 @@ export function writeUIntLE(
56
56
  return buf;
57
57
  }
58
58
 
59
- /**
60
- * Transforms Uint8Array into BigInt
61
- * @param array: Uint8Array
62
- * @returns BigInt
63
- */
64
- export function buildBigIntFromUint8Array(
65
- array: Uint8Array,
66
- byteOffset: number = 0
67
- ): bigint {
68
- // Use all bytes from byteOffset to the end, big-endian
69
- let hex = "";
70
- for (let i = byteOffset; i < array.length; i++) {
71
- hex += array[i].toString(16).padStart(2, "0");
72
- }
73
- return BigInt("0x" + hex);
59
+ export function buildBigIntFromUint8ArrayLE(bytes: Uint8Array): bigint {
60
+ return bytes.reduce(
61
+ (acc, byte, i) => acc + BigInt(byte) * (1n << (8n * BigInt(i))),
62
+ 0n
63
+ );
74
64
  }
75
65
 
76
66
  /**
@@ -2,7 +2,7 @@ export { extractMetaMaskSigner } from "./metamask.js";
2
2
  export {
3
3
  concatenate,
4
4
  writeUIntLE,
5
- buildBigIntFromUint8Array,
5
+ buildBigIntFromUint8ArrayLE,
6
6
  zeroPadLE
7
7
  } from "./bytes.js";
8
8
  export { sha256, poseidonHash } from "./hash.js";