@waku/rln 0.1.5-ff0222a.0 → 0.1.6-b7e9b08.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 (158) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/bundle/_virtual/index2.js +1 -1
  3. package/bundle/index.js +2 -2
  4. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/checksum.js +3 -3
  5. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/cipher.js +4 -4
  6. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/class.js +7 -7
  7. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/functional.js +7 -7
  8. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/index.js +6 -6
  9. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/kdf.js +5 -5
  10. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/password.js +1 -1
  11. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/schema-validation-generated.js +1 -1
  12. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/schema-validation.js +2 -2
  13. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/lib/types.js +1 -1
  14. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_assert.js +1 -1
  15. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_sha2.js +3 -3
  16. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/_u64.js +1 -1
  17. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/cryptoBrowser.js +1 -1
  18. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/hmac.js +3 -3
  19. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/pbkdf2.js +4 -4
  20. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/scrypt.js +5 -5
  21. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/sha256.js +3 -3
  22. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/sha512.js +4 -4
  23. package/bundle/{packages/rln → node_modules/@chainsafe/bls-keystore}/node_modules/@noble/hashes/utils.js +2 -2
  24. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/aes.js +3 -3
  25. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/pbkdf2.js +7 -7
  26. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +3 -3
  27. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/scrypt.js +3 -3
  28. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/sha256.js +3 -3
  29. package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +7 -7
  30. package/bundle/node_modules/@ethersproject/abi/lib.esm/_version.js +1 -1
  31. package/bundle/node_modules/@ethersproject/abstract-provider/lib.esm/_version.js +1 -1
  32. package/bundle/node_modules/@ethersproject/abstract-signer/lib.esm/_version.js +1 -1
  33. package/bundle/node_modules/@ethersproject/address/lib.esm/_version.js +1 -1
  34. package/bundle/node_modules/@ethersproject/bignumber/lib.esm/_version.js +1 -1
  35. package/bundle/node_modules/@ethersproject/contracts/lib.esm/_version.js +1 -1
  36. package/bundle/node_modules/@ethersproject/contracts/lib.esm/index.js +1 -1
  37. package/bundle/node_modules/@ethersproject/hash/lib.esm/_version.js +1 -1
  38. package/bundle/node_modules/@ethersproject/networks/lib.esm/_version.js +1 -1
  39. package/bundle/node_modules/@ethersproject/networks/lib.esm/index.js +19 -1
  40. package/bundle/node_modules/@ethersproject/properties/lib.esm/_version.js +1 -1
  41. package/bundle/node_modules/@ethersproject/providers/lib.esm/_version.js +1 -1
  42. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/_version.js +1 -1
  43. package/bundle/node_modules/@ethersproject/signing-key/lib.esm/elliptic.js +58 -10
  44. package/bundle/node_modules/@ethersproject/strings/lib.esm/_version.js +1 -1
  45. package/bundle/node_modules/@ethersproject/transactions/lib.esm/_version.js +1 -1
  46. package/bundle/node_modules/@ethersproject/web/lib.esm/_version.js +1 -1
  47. package/bundle/node_modules/@noble/hashes/esm/_assert.js +6 -32
  48. package/bundle/node_modules/@noble/hashes/esm/_md.js +22 -11
  49. package/bundle/node_modules/@noble/hashes/esm/_u64.js +4 -3
  50. package/bundle/{packages/rln/node_modules → node_modules}/@noble/hashes/esm/hmac.js +19 -10
  51. package/bundle/{packages/rln/node_modules/@noble/hashes/esm/sha256.js → node_modules/@noble/hashes/esm/sha2.js} +36 -50
  52. package/bundle/node_modules/@noble/hashes/esm/sha256.js +5 -102
  53. package/bundle/node_modules/@noble/hashes/esm/sha3.js +30 -24
  54. package/bundle/node_modules/@noble/hashes/esm/utils.js +69 -18
  55. package/bundle/node_modules/bn.js/lib/bn.js +1 -0
  56. package/bundle/node_modules/ethereum-cryptography/esm/sha256.js +1 -1
  57. package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +3 -0
  58. package/bundle/packages/core/dist/lib/connection_manager/keep_alive_manager.js +3 -3
  59. package/bundle/packages/core/dist/lib/filter/filter.js +3 -0
  60. package/bundle/packages/core/dist/lib/light_push/light_push.js +3 -0
  61. package/bundle/packages/core/dist/lib/metadata/metadata.js +3 -0
  62. package/bundle/packages/core/dist/lib/store/store.js +3 -3
  63. package/bundle/packages/proto/dist/generated/sds_message.js +59 -3
  64. package/bundle/packages/rln/dist/codec.js +3 -3
  65. package/bundle/packages/rln/dist/contract/constants.js +1 -1
  66. package/bundle/packages/rln/dist/contract/{rln_light_contract.js → rln_base_contract.js} +180 -180
  67. package/bundle/packages/rln/dist/contract/rln_contract.js +12 -422
  68. package/bundle/packages/rln/dist/contract/types.js +9 -0
  69. package/bundle/packages/rln/dist/create.js +1 -1
  70. package/bundle/packages/rln/dist/credentials_manager.js +215 -0
  71. package/bundle/packages/rln/dist/identity.js +0 -9
  72. package/bundle/packages/rln/dist/keystore/cipher.js +3 -3
  73. package/bundle/packages/rln/dist/keystore/keystore.js +32 -18
  74. package/bundle/packages/rln/dist/rln.js +59 -169
  75. package/bundle/packages/rln/dist/utils/epoch.js +3 -3
  76. package/bundle/packages/rln/dist/zerokit.js +5 -5
  77. package/bundle/packages/utils/dist/common/sharding/index.js +4 -4
  78. package/bundle/packages/utils/node_modules/@waku/interfaces/dist/connection_manager.js +19 -0
  79. package/bundle/packages/utils/node_modules/@waku/interfaces/dist/health_indicator.js +12 -0
  80. package/bundle/packages/utils/node_modules/@waku/interfaces/dist/protocols.js +92 -0
  81. package/dist/.tsbuildinfo +1 -1
  82. package/dist/contract/constants.d.ts +1 -1
  83. package/dist/contract/constants.js +1 -1
  84. package/dist/contract/constants.js.map +1 -1
  85. package/dist/contract/{rln_light_contract.d.ts → rln_base_contract.d.ts} +25 -53
  86. package/dist/contract/{rln_light_contract.js → rln_base_contract.js} +177 -177
  87. package/dist/contract/rln_base_contract.js.map +1 -0
  88. package/dist/contract/rln_contract.d.ts +5 -122
  89. package/dist/contract/rln_contract.js +8 -417
  90. package/dist/contract/rln_contract.js.map +1 -1
  91. package/dist/contract/types.d.ts +40 -0
  92. package/dist/contract/types.js +8 -0
  93. package/dist/contract/types.js.map +1 -0
  94. package/dist/create.js +1 -1
  95. package/dist/create.js.map +1 -1
  96. package/dist/credentials_manager.d.ts +44 -0
  97. package/dist/credentials_manager.js +197 -0
  98. package/dist/credentials_manager.js.map +1 -0
  99. package/dist/identity.d.ts +0 -1
  100. package/dist/identity.js +0 -9
  101. package/dist/identity.js.map +1 -1
  102. package/dist/index.d.ts +3 -3
  103. package/dist/index.js +3 -3
  104. package/dist/index.js.map +1 -1
  105. package/dist/keystore/keystore.d.ts +1 -0
  106. package/dist/keystore/keystore.js +27 -13
  107. package/dist/keystore/keystore.js.map +1 -1
  108. package/dist/rln.d.ts +9 -52
  109. package/dist/rln.js +54 -163
  110. package/dist/rln.js.map +1 -1
  111. package/dist/types.d.ts +27 -0
  112. package/dist/types.js +2 -0
  113. package/dist/types.js.map +1 -0
  114. package/dist/zerokit.d.ts +3 -3
  115. package/dist/zerokit.js +5 -5
  116. package/dist/zerokit.js.map +1 -1
  117. package/package.json +1 -1
  118. package/src/contract/constants.ts +1 -1
  119. package/src/contract/{rln_light_contract.ts → rln_base_contract.ts} +294 -312
  120. package/src/contract/rln_contract.ts +9 -663
  121. package/src/contract/types.ts +48 -0
  122. package/src/create.ts +1 -1
  123. package/src/credentials_manager.ts +282 -0
  124. package/src/identity.ts +0 -10
  125. package/src/index.ts +4 -4
  126. package/src/keystore/keystore.ts +49 -25
  127. package/src/rln.ts +67 -258
  128. package/src/types.ts +31 -0
  129. package/src/zerokit.ts +3 -3
  130. package/bundle/packages/rln/dist/rln_light.js +0 -149
  131. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_assert.js +0 -43
  132. package/bundle/packages/rln/node_modules/@noble/hashes/esm/_sha2.js +0 -116
  133. package/bundle/packages/rln/node_modules/@noble/hashes/esm/utils.js +0 -43
  134. package/dist/contract/rln_light_contract.js.map +0 -1
  135. package/dist/rln_light.d.ts +0 -64
  136. package/dist/rln_light.js +0 -144
  137. package/dist/rln_light.js.map +0 -1
  138. package/src/rln_light.ts +0 -235
  139. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/index.js +0 -0
  140. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/md5.js +0 -0
  141. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/nil.js +0 -0
  142. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/parse.js +0 -0
  143. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/regex.js +0 -0
  144. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/rng.js +0 -0
  145. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/sha1.js +0 -0
  146. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/stringify.js +0 -0
  147. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v1.js +0 -0
  148. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v3.js +0 -0
  149. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v35.js +0 -0
  150. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v4.js +0 -0
  151. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/v5.js +0 -0
  152. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/validate.js +0 -0
  153. /package/bundle/{packages/rln/node_modules → node_modules}/@chainsafe/bls-keystore/node_modules/uuid/dist/esm-browser/version.js +0 -0
  154. /package/bundle/{packages/rln/node_modules → node_modules}/uuid/dist/esm-browser/native.js +0 -0
  155. /package/bundle/{packages/rln/node_modules → node_modules}/uuid/dist/esm-browser/rng.js +0 -0
  156. /package/bundle/{packages/rln/node_modules → node_modules}/uuid/dist/esm-browser/stringify.js +0 -0
  157. /package/bundle/{packages/rln/node_modules → node_modules}/uuid/dist/esm-browser/v4.js +0 -0
  158. /package/bundle/packages/{interfaces → utils/node_modules/@waku/interfaces}/dist/constants.js +0 -0
@@ -0,0 +1,215 @@
1
+ import { hmac } from '../../../node_modules/@noble/hashes/esm/hmac.js';
2
+ import { sha256 } from '../../../node_modules/@noble/hashes/esm/sha256.js';
3
+ import '../../utils/node_modules/@waku/interfaces/dist/protocols.js';
4
+ import '../../utils/node_modules/@waku/interfaces/dist/connection_manager.js';
5
+ import '../../utils/node_modules/@waku/interfaces/dist/health_indicator.js';
6
+ import '../../../node_modules/multiformats/dist/src/bases/base10.js';
7
+ import '../../../node_modules/multiformats/dist/src/bases/base16.js';
8
+ import '../../../node_modules/multiformats/dist/src/bases/base2.js';
9
+ import '../../../node_modules/multiformats/dist/src/bases/base256emoji.js';
10
+ import '../../../node_modules/multiformats/dist/src/bases/base32.js';
11
+ import '../../../node_modules/multiformats/dist/src/bases/base36.js';
12
+ import '../../../node_modules/multiformats/dist/src/bases/base58.js';
13
+ import '../../../node_modules/multiformats/dist/src/bases/base64.js';
14
+ import '../../../node_modules/multiformats/dist/src/bases/base8.js';
15
+ import '../../../node_modules/multiformats/dist/src/bases/identity.js';
16
+ import '../../../node_modules/multiformats/dist/src/codecs/json.js';
17
+ import { Logger } from '../../utils/dist/logger/index.js';
18
+ import { LINEA_CONTRACT } from './contract/constants.js';
19
+ import { RLNBaseContract } from './contract/rln_base_contract.js';
20
+ import { IdentityCredential } from './identity.js';
21
+ import { Keystore } from './keystore/keystore.js';
22
+ import { extractMetaMaskSigner } from './utils/metamask.js';
23
+ import { buildBigIntFromUint8Array } from './utils/bytes.js';
24
+ import './utils/epoch.js';
25
+
26
+ const log = new Logger("waku:credentials");
27
+ /**
28
+ * Manages credentials for RLN
29
+ * This is a lightweight implementation of the RLN contract that doesn't require Zerokit
30
+ * It is used to register membership and generate identity credentials
31
+ */
32
+ class RLNCredentialsManager {
33
+ started = false;
34
+ starting = false;
35
+ contract;
36
+ signer;
37
+ keystore = Keystore.create();
38
+ credentials;
39
+ zerokit;
40
+ constructor(zerokit) {
41
+ log.info("RLNCredentialsManager initialized");
42
+ this.zerokit = zerokit;
43
+ }
44
+ get provider() {
45
+ return this.contract?.provider;
46
+ }
47
+ async start(options = {}) {
48
+ if (this.started || this.starting) {
49
+ log.info("RLNCredentialsManager already started or starting");
50
+ return;
51
+ }
52
+ log.info("Starting RLNCredentialsManager");
53
+ this.starting = true;
54
+ try {
55
+ const { credentials, keystore } = await RLNCredentialsManager.decryptCredentialsIfNeeded(options.credentials);
56
+ if (credentials) {
57
+ log.info("Credentials successfully decrypted");
58
+ }
59
+ const { signer, address, rateLimit } = await this.determineStartOptions(options, credentials);
60
+ log.info(`Using contract address: ${address}`);
61
+ if (keystore) {
62
+ this.keystore = keystore;
63
+ log.info("Using provided keystore");
64
+ }
65
+ this.credentials = credentials;
66
+ this.signer = signer;
67
+ this.contract = new RLNBaseContract({
68
+ address: address,
69
+ signer: signer,
70
+ rateLimit: rateLimit ?? this.zerokit?.rateLimit
71
+ });
72
+ log.info("RLNCredentialsManager successfully started");
73
+ this.started = true;
74
+ }
75
+ catch (error) {
76
+ log.error("Failed to start RLNCredentialsManager", error);
77
+ throw error;
78
+ }
79
+ finally {
80
+ this.starting = false;
81
+ }
82
+ }
83
+ async registerMembership(options) {
84
+ if (!this.contract) {
85
+ log.error("RLN Contract is not initialized");
86
+ throw Error("RLN Contract is not initialized.");
87
+ }
88
+ log.info("Registering membership");
89
+ let identity = "identity" in options && options.identity;
90
+ if ("signature" in options) {
91
+ log.info("Generating identity from signature");
92
+ if (this.zerokit) {
93
+ log.info("Using Zerokit to generate identity");
94
+ identity = this.zerokit.generateSeededIdentityCredential(options.signature);
95
+ }
96
+ else {
97
+ log.info("Using local implementation to generate identity");
98
+ identity = this.generateSeededIdentityCredential(options.signature);
99
+ }
100
+ }
101
+ if (!identity) {
102
+ log.error("Missing signature or identity to register membership");
103
+ throw Error("Missing signature or identity to register membership.");
104
+ }
105
+ log.info("Registering identity with contract");
106
+ return this.contract.registerWithIdentity(identity);
107
+ }
108
+ /**
109
+ * Changes credentials in use by relying on provided Keystore earlier in rln.start
110
+ * @param id: string, hash of credentials to select from Keystore
111
+ * @param password: string or bytes to use to decrypt credentials from Keystore
112
+ */
113
+ async useCredentials(id, password) {
114
+ log.info(`Attempting to use credentials with ID: ${id}`);
115
+ this.credentials = await this.keystore?.readCredential(id, password);
116
+ if (this.credentials) {
117
+ log.info("Successfully loaded credentials");
118
+ }
119
+ else {
120
+ log.warn("Failed to load credentials");
121
+ }
122
+ }
123
+ async determineStartOptions(options, credentials) {
124
+ let chainId = credentials?.membership.chainId;
125
+ const address = credentials?.membership.address ||
126
+ options.address ||
127
+ LINEA_CONTRACT.address;
128
+ if (address === LINEA_CONTRACT.address) {
129
+ chainId = LINEA_CONTRACT.chainId.toString();
130
+ log.info(`Using Linea contract with chainId: ${chainId}`);
131
+ }
132
+ const signer = options.signer || (await extractMetaMaskSigner());
133
+ const currentChainId = await signer.getChainId();
134
+ log.info(`Current chain ID: ${currentChainId}`);
135
+ if (chainId && chainId !== currentChainId.toString()) {
136
+ log.error(`Chain ID mismatch: contract=${chainId}, current=${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
+ log.info("No credentials provided");
147
+ return {};
148
+ }
149
+ if ("identity" in credentials) {
150
+ log.info("Using already decrypted credentials");
151
+ return { credentials };
152
+ }
153
+ log.info("Attempting to decrypt credentials");
154
+ const keystore = Keystore.fromString(credentials.keystore);
155
+ if (!keystore) {
156
+ log.warn("Failed to create keystore from string");
157
+ return {};
158
+ }
159
+ try {
160
+ const decryptedCredentials = await keystore.readCredential(credentials.id, credentials.password);
161
+ log.info(`Successfully decrypted credentials with ID: ${credentials.id}`);
162
+ return {
163
+ keystore,
164
+ credentials: decryptedCredentials
165
+ };
166
+ }
167
+ catch (error) {
168
+ log.error("Failed to decrypt credentials", error);
169
+ throw error;
170
+ }
171
+ }
172
+ async verifyCredentialsAgainstContract(credentials) {
173
+ if (!this.contract) {
174
+ throw Error("Failed to verify chain coordinates: no contract initialized.");
175
+ }
176
+ const registryAddress = credentials.membership.address;
177
+ const currentRegistryAddress = this.contract.address;
178
+ if (registryAddress !== currentRegistryAddress) {
179
+ throw Error(`Failed to verify chain coordinates: credentials contract address=${registryAddress} is not equal to registryContract address=${currentRegistryAddress}`);
180
+ }
181
+ const chainId = credentials.membership.chainId;
182
+ const network = await this.contract.provider.getNetwork();
183
+ const currentChainId = network.chainId;
184
+ if (chainId !== currentChainId.toString()) {
185
+ throw Error(`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`);
186
+ }
187
+ }
188
+ /**
189
+ * Generates an identity credential from a seed string
190
+ * This is a pure implementation that doesn't rely on Zerokit
191
+ * @param seed A string seed to generate the identity from
192
+ * @returns IdentityCredential
193
+ */
194
+ generateSeededIdentityCredential(seed) {
195
+ log.info("Generating seeded identity credential");
196
+ // Convert the seed to bytes
197
+ const encoder = new TextEncoder();
198
+ const seedBytes = encoder.encode(seed);
199
+ // Generate deterministic values using HMAC-SHA256
200
+ // We use different context strings for each component to ensure they're different
201
+ const idTrapdoor = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
202
+ const idNullifier = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
203
+ // Generate IDSecretHash as a hash of IDTrapdoor and IDNullifier
204
+ const combinedBytes = new Uint8Array([...idTrapdoor, ...idNullifier]);
205
+ const idSecretHash = sha256(combinedBytes);
206
+ // Generate IDCommitment as a hash of IDSecretHash
207
+ const idCommitment = sha256(idSecretHash);
208
+ // Convert IDCommitment to BigInt
209
+ const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment);
210
+ log.info("Successfully generated identity credential");
211
+ return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
212
+ }
213
+ }
214
+
215
+ export { RLNCredentialsManager };
@@ -25,15 +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
- Array.from(this.IDTrapdoor),
31
- Array.from(this.IDNullifier),
32
- Array.from(this.IDSecretHash),
33
- Array.from(this.IDCommitment),
34
- this.IDCommitmentBigInt.toString()
35
- ];
36
- }
37
28
  }
38
29
 
39
30
  export { IdentityCredential };
@@ -1,6 +1,6 @@
1
- import { cipherDecrypt as cipherDecrypt_1 } from '../../node_modules/@chainsafe/bls-keystore/lib/cipher.js';
2
- import { kdf as kdf_2 } from '../../node_modules/@chainsafe/bls-keystore/lib/kdf.js';
3
- import { normalizePassword as normalizePassword_1 } from '../../node_modules/@chainsafe/bls-keystore/lib/password.js';
1
+ import { cipherDecrypt as cipherDecrypt_1 } from '../../../../node_modules/@chainsafe/bls-keystore/lib/cipher.js';
2
+ import { kdf as kdf_2 } from '../../../../node_modules/@chainsafe/bls-keystore/lib/kdf.js';
3
+ import { normalizePassword as normalizePassword_1 } from '../../../../node_modules/@chainsafe/bls-keystore/lib/password.js';
4
4
  import { keccak256 } from '../../../../node_modules/ethereum-cryptography/esm/keccak.js';
5
5
  import { hexToBytes } from '../../../../node_modules/ethereum-cryptography/esm/utils.js';
6
6
  import { concatBytes, bytesToHex } from '../../../../node_modules/@noble/hashes/esm/utils.js';
@@ -1,7 +1,7 @@
1
- import '../../node_modules/@chainsafe/bls-keystore/lib/index.js';
2
- import '../../../interfaces/dist/protocols.js';
3
- import '../../../interfaces/dist/connection_manager.js';
4
- import '../../../interfaces/dist/health_indicator.js';
1
+ import '../../../../node_modules/@chainsafe/bls-keystore/lib/index.js';
2
+ import '../../../utils/node_modules/@waku/interfaces/dist/protocols.js';
3
+ import '../../../utils/node_modules/@waku/interfaces/dist/connection_manager.js';
4
+ import '../../../utils/node_modules/@waku/interfaces/dist/health_indicator.js';
5
5
  import '../../../../node_modules/multiformats/dist/src/bases/base10.js';
6
6
  import '../../../../node_modules/multiformats/dist/src/bases/base16.js';
7
7
  import '../../../../node_modules/multiformats/dist/src/bases/base2.js';
@@ -17,12 +17,11 @@ 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';
24
23
  import { __exports as lib } from '../../../../_virtual/index.js';
25
- import v4 from '../../node_modules/uuid/dist/esm-browser/v4.js';
24
+ import v4 from '../../../../node_modules/uuid/dist/esm-browser/v4.js';
26
25
  import { bytesToHex, utf8ToBytes } from '../../../../node_modules/@noble/hashes/esm/utils.js';
27
26
 
28
27
  const log = new Logger("waku:rln:keystore");
@@ -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, "membershipContract.rateLimit")
180
+ rateLimit: _.get(obj, "userMessageLimit")
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) {
@@ -198,15 +211,16 @@ class Keystore {
198
211
  return utf8ToBytes(JSON.stringify({
199
212
  treeIndex: options.membership.treeIndex,
200
213
  identityCredential: {
201
- idTrapdoor: Array.from(options.identity.IDTrapdoor),
214
+ idCommitment: Array.from(options.identity.IDCommitment),
202
215
  idNullifier: Array.from(options.identity.IDNullifier),
203
216
  idSecretHash: Array.from(options.identity.IDSecretHash),
204
- idCommitment: Array.from(options.identity.IDCommitment)
217
+ idTrapdoor: Array.from(options.identity.IDTrapdoor)
205
218
  },
206
219
  membershipContract: {
207
220
  chainId: options.membership.chainId,
208
221
  address: options.membership.address
209
- }
222
+ },
223
+ userMessageLimit: options.membership.rateLimit
210
224
  }));
211
225
  }
212
226
  }
@@ -3,9 +3,9 @@ import '../../core/dist/lib/filter/filter.js';
3
3
  import '../../core/dist/lib/light_push/light_push.js';
4
4
  import '../../core/dist/lib/store/store.js';
5
5
  import '../../core/dist/lib/connection_manager/connection_manager.js';
6
- import '../../interfaces/dist/protocols.js';
7
- import '../../interfaces/dist/connection_manager.js';
8
- import '../../interfaces/dist/health_indicator.js';
6
+ import '../../utils/node_modules/@waku/interfaces/dist/protocols.js';
7
+ import '../../utils/node_modules/@waku/interfaces/dist/connection_manager.js';
8
+ import '../../utils/node_modules/@waku/interfaces/dist/health_indicator.js';
9
9
  import '../../../node_modules/multiformats/dist/src/bases/base10.js';
10
10
  import '../../../node_modules/multiformats/dist/src/bases/base16.js';
11
11
  import '../../../node_modules/multiformats/dist/src/bases/base2.js';
@@ -21,167 +21,45 @@ 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 { LINEA_CONTRACT, DEFAULT_RATE_LIMIT } from './contract/constants.js';
25
- import { RLNContract } from './contract/rln_contract.js';
26
- import { Keystore } from './keystore/keystore.js';
24
+ import { DEFAULT_RATE_LIMIT } from './contract/constants.js';
25
+ import { RLNCredentialsManager } from './credentials_manager.js';
27
26
  import verificationKey from './resources/verification_key.js';
28
27
  import { builder } from './resources/witness_calculator.js';
29
- import { extractMetaMaskSigner } from './utils/metamask.js';
30
- import './utils/epoch.js';
31
28
  import { Zerokit } from './zerokit.js';
32
29
 
33
30
  const log = new Logger("waku:rln");
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}`);
40
- }
41
- return await builder(new Uint8Array(await response.arrayBuffer()), false);
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 {
31
+ class RLNInstance extends RLNCredentialsManager {
86
32
  zerokit;
87
- started = false;
88
- starting = false;
89
- _contract;
90
- _signer;
91
- keystore = Keystore.create();
92
- _credentials;
93
- constructor(zerokit) {
94
- this.zerokit = zerokit;
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;
33
+ /**
34
+ * Create an instance of RLN
35
+ * @returns RLNInstance
36
+ */
37
+ static async create() {
107
38
  try {
108
- const { credentials, keystore } = await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
109
- const { signer, address, rateLimit } = 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: rateLimit ?? this.zerokit.getRateLimit
119
- });
120
- this.started = true;
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;
121
54
  }
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()).toString();
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
55
  }
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);
56
+ constructor(zerokit) {
57
+ super(zerokit);
58
+ this.zerokit = zerokit;
181
59
  }
182
60
  async createEncoder(options) {
183
61
  const { credentials: decryptedCredentials } = await RLNInstance.decryptCredentialsIfNeeded(options.credentials);
184
- const credentials = decryptedCredentials || this._credentials;
62
+ const credentials = decryptedCredentials || this.credentials;
185
63
  if (!credentials) {
186
64
  throw Error("Failed to create Encoder: missing RLN credentials. Use createRLNEncoder directly.");
187
65
  }
@@ -193,28 +71,40 @@ class RLNInstance {
193
71
  credential: credentials.identity
194
72
  });
195
73
  }
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.toString();
208
- if (chainId !== currentChainId) {
209
- throw Error(`Failed to verify chain coordinates: credentials chainID=${chainId} is not equal to registryContract chainID=${currentChainId}`);
210
- }
211
- }
212
74
  createDecoder(contentTopic) {
213
75
  return createRLNDecoder({
214
76
  rlnInstance: this,
215
77
  decoder: createDecoder(contentTopic)
216
78
  });
217
79
  }
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
+ }
218
108
  }
219
109
 
220
- export { RLNInstance, create };
110
+ export { RLNInstance };
@@ -1,6 +1,6 @@
1
- import '../../../interfaces/dist/protocols.js';
2
- import '../../../interfaces/dist/connection_manager.js';
3
- import '../../../interfaces/dist/health_indicator.js';
1
+ import '../../../utils/node_modules/@waku/interfaces/dist/protocols.js';
2
+ import '../../../utils/node_modules/@waku/interfaces/dist/connection_manager.js';
3
+ import '../../../utils/node_modules/@waku/interfaces/dist/health_indicator.js';
4
4
  import '../../../../node_modules/multiformats/dist/src/bases/base10.js';
5
5
  import '../../../../node_modules/multiformats/dist/src/bases/base16.js';
6
6
  import '../../../../node_modules/multiformats/dist/src/bases/base2.js';
@@ -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
@@ -1,8 +1,8 @@
1
1
  import { sha256 } from '../../../../../node_modules/@noble/hashes/esm/sha256.js';
2
- import '../../../../interfaces/dist/protocols.js';
3
- import '../../../../interfaces/dist/connection_manager.js';
4
- import { DEFAULT_CLUSTER_ID } from '../../../../interfaces/dist/constants.js';
5
- import '../../../../interfaces/dist/health_indicator.js';
2
+ import '../../../node_modules/@waku/interfaces/dist/protocols.js';
3
+ import '../../../node_modules/@waku/interfaces/dist/connection_manager.js';
4
+ import { DEFAULT_CLUSTER_ID } from '../../../node_modules/@waku/interfaces/dist/constants.js';
5
+ import '../../../node_modules/@waku/interfaces/dist/health_indicator.js';
6
6
  import { concat, utf8ToBytes } from '../../bytes/index.js';
7
7
 
8
8
  const singleShardInfoToPubsubTopic = (shardInfo) => {