@waku/rln 0.0.2-3ab8023.0 → 0.0.2-9094860.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 (37) hide show
  1. package/bundle/_virtual/utils.js +2 -2
  2. package/bundle/_virtual/utils2.js +2 -2
  3. package/bundle/index.js +2 -0
  4. package/bundle/packages/rln/dist/contract/rln_contract.js +30 -150
  5. package/bundle/packages/rln/dist/identity_generator.js +75 -0
  6. package/bundle/packages/rln/dist/rln.js +0 -2
  7. package/bundle/packages/rln/dist/rln_light.js +228 -0
  8. package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/random.js +1 -1
  9. package/bundle/packages/rln/node_modules/@chainsafe/bls-keystore/node_modules/ethereum-cryptography/utils.js +2 -2
  10. package/bundle/packages/rln/node_modules/@noble/hashes/_sha2.js +1 -1
  11. package/bundle/packages/rln/node_modules/@noble/hashes/hmac.js +1 -1
  12. package/bundle/packages/rln/node_modules/@noble/hashes/pbkdf2.js +1 -1
  13. package/bundle/packages/rln/node_modules/@noble/hashes/scrypt.js +1 -1
  14. package/bundle/packages/rln/node_modules/@noble/hashes/sha256.js +1 -1
  15. package/bundle/packages/rln/node_modules/@noble/hashes/sha512.js +1 -1
  16. package/bundle/packages/rln/node_modules/@noble/hashes/utils.js +1 -1
  17. package/dist/.tsbuildinfo +1 -1
  18. package/dist/contract/rln_contract.d.ts +4 -10
  19. package/dist/contract/rln_contract.js +30 -150
  20. package/dist/contract/rln_contract.js.map +1 -1
  21. package/dist/identity_generator.d.ts +19 -0
  22. package/dist/identity_generator.js +72 -0
  23. package/dist/identity_generator.js.map +1 -0
  24. package/dist/index.d.ts +4 -1
  25. package/dist/index.js +3 -1
  26. package/dist/index.js.map +1 -1
  27. package/dist/rln.js +0 -2
  28. package/dist/rln.js.map +1 -1
  29. package/dist/rln_light.d.ts +95 -0
  30. package/dist/rln_light.js +210 -0
  31. package/dist/rln_light.js.map +1 -0
  32. package/package.json +1 -1
  33. package/src/contract/rln_contract.ts +36 -218
  34. package/src/identity_generator.ts +108 -0
  35. package/src/index.ts +15 -0
  36. package/src/rln.ts +0 -3
  37. package/src/rln_light.ts +304 -0
@@ -0,0 +1,228 @@
1
+ import '../../interfaces/dist/protocols.js';
2
+ import '../../interfaces/dist/connection_manager.js';
3
+ import '../../interfaces/dist/health_indicator.js';
4
+ import '../../../node_modules/multiformats/dist/src/bases/base10.js';
5
+ import '../../../node_modules/multiformats/dist/src/bases/base16.js';
6
+ import '../../../node_modules/multiformats/dist/src/bases/base2.js';
7
+ import '../../../node_modules/multiformats/dist/src/bases/base256emoji.js';
8
+ import '../../../node_modules/multiformats/dist/src/bases/base32.js';
9
+ import '../../../node_modules/multiformats/dist/src/bases/base36.js';
10
+ import '../../../node_modules/multiformats/dist/src/bases/base58.js';
11
+ import '../../../node_modules/multiformats/dist/src/bases/base64.js';
12
+ import '../../../node_modules/multiformats/dist/src/bases/base8.js';
13
+ import '../../../node_modules/multiformats/dist/src/bases/identity.js';
14
+ import '../../../node_modules/multiformats/dist/src/codecs/json.js';
15
+ import { Logger } from '../../utils/dist/logger/index.js';
16
+ import { RLNContract } from './contract/rln_contract.js';
17
+ import { SEPOLIA_CONTRACT } from './contract/constants.js';
18
+ import { IdentityGenerator } from './identity_generator.js';
19
+ import { Keystore } from './keystore/keystore.js';
20
+ import { extractMetaMaskSigner } from './utils/metamask.js';
21
+ import './utils/epoch.js';
22
+
23
+ const log = new Logger("waku:rln-light");
24
+ /**
25
+ * Lightweight RLN implementation without Zerokit
26
+ * Only supports Keystore and membership registration
27
+ */
28
+ class RLNLight {
29
+ started = false;
30
+ starting = false;
31
+ _contract;
32
+ _signer;
33
+ keystore = Keystore.create();
34
+ _credentials;
35
+ identityGenerator = new IdentityGenerator();
36
+ get contract() {
37
+ return this._contract;
38
+ }
39
+ get signer() {
40
+ return this._signer;
41
+ }
42
+ /**
43
+ * Get the current credentials
44
+ */
45
+ get credentials() {
46
+ return this._credentials;
47
+ }
48
+ /**
49
+ * Start the RLNLight instance
50
+ * @param options Configuration options
51
+ */
52
+ async start(options = {}) {
53
+ if (this.started || this.starting) {
54
+ log.warn("RLNLight already started or starting");
55
+ return;
56
+ }
57
+ this.starting = true;
58
+ try {
59
+ // Determine correct options based on credentials, if any
60
+ const keystoreCredentials = options.address
61
+ ? await this.findCredentialsByAddress(options.address)
62
+ : undefined;
63
+ const startOptions = await this.determineStartOptions(options, keystoreCredentials);
64
+ // Set the signer
65
+ if (startOptions.signer) {
66
+ this._signer = startOptions.signer;
67
+ }
68
+ else {
69
+ this._signer = await extractMetaMaskSigner();
70
+ }
71
+ // Initialize the contract with the correct address format
72
+ const finalAddress = typeof startOptions.address === "string"
73
+ ? startOptions.address
74
+ : SEPOLIA_CONTRACT.address;
75
+ const contractOptions = {
76
+ signer: this._signer,
77
+ address: finalAddress,
78
+ rateLimit: startOptions.rateLimit
79
+ };
80
+ // We need to create a minimal compatible object with RLNInstance interface
81
+ // but we only need it for contract initialization
82
+ const compatibleRLN = {
83
+ zerokit: {
84
+ getMerkleRoot: () => new Uint8Array(32),
85
+ insertMember: (_idCommitment) => { },
86
+ insertMembers: (_index, ..._idCommitments) => { },
87
+ deleteMember: (_index) => { }
88
+ }
89
+ };
90
+ this._contract = await RLNContract.init(
91
+ // Type assertion needed for compatibility with RLNInstance interface
92
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
+ compatibleRLN, contractOptions);
94
+ this.started = true;
95
+ }
96
+ catch (error) {
97
+ log.error("Failed to start RLNLight:", error);
98
+ throw error;
99
+ }
100
+ finally {
101
+ this.starting = false;
102
+ }
103
+ }
104
+ /**
105
+ * Find credentials in the keystore by contract address
106
+ */
107
+ async findCredentialsByAddress(address) {
108
+ // Since Keystore doesn't have a direct method to find by address,
109
+ // we need to iterate through all credentials
110
+ const hashes = this.keystore.keys();
111
+ for (const hash of hashes) {
112
+ try {
113
+ // Try with an empty password - this won't work but lets us check schema
114
+ const credential = await this.keystore.readCredential(hash, new Uint8Array(0));
115
+ if (credential?.membership.address === address) {
116
+ return credential;
117
+ }
118
+ }
119
+ catch (e) {
120
+ // Ignore errors, just means we couldn't read this credential
121
+ }
122
+ }
123
+ return undefined;
124
+ }
125
+ /**
126
+ * Determine the correct options for starting RLNLight
127
+ */
128
+ async determineStartOptions(options, credentials) {
129
+ if (options.credentials) {
130
+ const { credentials: decrypted } = await RLNLight.decryptCredentialsIfNeeded(options.credentials);
131
+ if (decrypted?.membership) {
132
+ this._credentials = decrypted;
133
+ return {
134
+ ...options,
135
+ address: decrypted.membership.address
136
+ };
137
+ }
138
+ }
139
+ else if (credentials) {
140
+ return {
141
+ ...options,
142
+ address: credentials.membership.address
143
+ };
144
+ }
145
+ return options;
146
+ }
147
+ /**
148
+ * Decrypt credentials if they are encrypted
149
+ */
150
+ static async decryptCredentialsIfNeeded(credentials) {
151
+ if (!credentials) {
152
+ return {};
153
+ }
154
+ if ("identity" in credentials) {
155
+ return { credentials };
156
+ }
157
+ else {
158
+ const keystore = Keystore.create();
159
+ try {
160
+ const decrypted = await keystore.readCredential(credentials.id, credentials.password);
161
+ if (!decrypted) {
162
+ return {};
163
+ }
164
+ return {
165
+ credentials: {
166
+ identity: decrypted.identity,
167
+ membership: decrypted.membership
168
+ },
169
+ keystore
170
+ };
171
+ }
172
+ catch (e) {
173
+ log.error("Failed to decrypt credentials", e);
174
+ return {};
175
+ }
176
+ }
177
+ }
178
+ /**
179
+ * Register a membership using an identity or signature
180
+ * @param options Options including identity or signature
181
+ * @returns Decrypted credentials if successful
182
+ */
183
+ async registerMembership(options) {
184
+ if (!this.contract) {
185
+ throw Error("RLN Contract is not initialized.");
186
+ }
187
+ let identity = "identity" in options && options.identity;
188
+ if ("signature" in options) {
189
+ identity = this.identityGenerator.generateSeededIdentityCredential(options.signature);
190
+ }
191
+ if (!identity) {
192
+ throw Error("Missing signature or identity to register membership.");
193
+ }
194
+ return this.contract.registerWithIdentity(identity);
195
+ }
196
+ /**
197
+ * Changes credentials in use by relying on provided Keystore
198
+ * @param id Hash of credentials to select from Keystore
199
+ * @param password Password to decrypt credentials from Keystore
200
+ */
201
+ async useCredentials(id, password) {
202
+ const decrypted = await this.keystore.readCredential(id, password);
203
+ if (!decrypted) {
204
+ throw new Error("Credentials not found or incorrect password");
205
+ }
206
+ this._credentials = {
207
+ identity: decrypted.identity,
208
+ membership: decrypted.membership
209
+ };
210
+ }
211
+ /**
212
+ * Generate a new identity credential
213
+ * @returns New identity credential
214
+ */
215
+ generateIdentityCredential() {
216
+ return this.identityGenerator.generateIdentityCredentials();
217
+ }
218
+ /**
219
+ * Generate a seeded identity credential
220
+ * @param seed Seed string to generate deterministic identity
221
+ * @returns Seeded identity credential
222
+ */
223
+ generateSeededIdentityCredential(seed) {
224
+ return this.identityGenerator.generateSeededIdentityCredential(seed);
225
+ }
226
+ }
227
+
228
+ export { RLNLight };
@@ -1,6 +1,6 @@
1
1
  import { __exports as random } from '../../../../../../../_virtual/random.js';
2
2
  import '../../../../@noble/hashes/utils.js';
3
- import { __exports as utils } from '../../../../../../../_virtual/utils2.js';
3
+ import { __exports as utils } from '../../../../../../../_virtual/utils.js';
4
4
 
5
5
  Object.defineProperty(random, "__esModule", { value: true });
6
6
  random.getRandomBytes = random.getRandomBytesSync = undefined;
@@ -1,10 +1,10 @@
1
1
  import { commonjsGlobal } from '../../../../../../../_virtual/_commonjsHelpers.js';
2
2
  import { commonjsRequire } from '../../../../../../../_virtual/_commonjs-dynamic-modules.js';
3
- import { __module as utils } from '../../../../../../../_virtual/utils.js';
3
+ import { __module as utils } from '../../../../../../../_virtual/utils2.js';
4
4
  import '../../../../@noble/hashes/_assert.js';
5
5
  import '../../../../@noble/hashes/utils.js';
6
6
  import { __exports as _assert } from '../../../../../../../_virtual/_assert.js';
7
- import { __exports as utils$1 } from '../../../../../../../_virtual/utils2.js';
7
+ import { __exports as utils$1 } from '../../../../../../../_virtual/utils.js';
8
8
 
9
9
  utils.exports;
10
10
 
@@ -2,7 +2,7 @@ import { __exports as _sha2 } from '../../../../../_virtual/_sha2.js';
2
2
  import './_assert.js';
3
3
  import './utils.js';
4
4
  import { __exports as _assert } from '../../../../../_virtual/_assert.js';
5
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
5
+ import { __exports as utils } from '../../../../../_virtual/utils.js';
6
6
 
7
7
  Object.defineProperty(_sha2, "__esModule", { value: true });
8
8
  _sha2.SHA2 = undefined;
@@ -2,7 +2,7 @@ import { __exports as hmac } from '../../../../../_virtual/hmac.js';
2
2
  import './_assert.js';
3
3
  import './utils.js';
4
4
  import { __exports as _assert } from '../../../../../_virtual/_assert.js';
5
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
5
+ import { __exports as utils } from '../../../../../_virtual/utils.js';
6
6
 
7
7
  (function (exports) {
8
8
  Object.defineProperty(exports, "__esModule", { value: true });
@@ -2,7 +2,7 @@ import { __exports as pbkdf2$1 } from '../../../../../_virtual/pbkdf22.js';
2
2
  import './_assert.js';
3
3
  import './hmac.js';
4
4
  import './utils.js';
5
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
5
+ import { __exports as utils } from '../../../../../_virtual/utils.js';
6
6
  import { __exports as _assert } from '../../../../../_virtual/_assert.js';
7
7
  import { __exports as hmac } from '../../../../../_virtual/hmac.js';
8
8
 
@@ -3,7 +3,7 @@ import './_assert.js';
3
3
  import './sha256.js';
4
4
  import './pbkdf2.js';
5
5
  import './utils.js';
6
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
6
+ import { __exports as utils } from '../../../../../_virtual/utils.js';
7
7
  import { __exports as _assert } from '../../../../../_virtual/_assert.js';
8
8
  import { __exports as sha256 } from '../../../../../_virtual/sha2562.js';
9
9
  import { __exports as pbkdf2 } from '../../../../../_virtual/pbkdf22.js';
@@ -2,7 +2,7 @@ import { __exports as sha256 } from '../../../../../_virtual/sha2562.js';
2
2
  import './_sha2.js';
3
3
  import './utils.js';
4
4
  import { __exports as _sha2 } from '../../../../../_virtual/_sha2.js';
5
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
5
+ import { __exports as utils } from '../../../../../_virtual/utils.js';
6
6
 
7
7
  Object.defineProperty(sha256, "__esModule", { value: true });
8
8
  sha256.sha224 = sha256.sha256 = undefined;
@@ -4,7 +4,7 @@ import './_u64.js';
4
4
  import './utils.js';
5
5
  import { __exports as _sha2 } from '../../../../../_virtual/_sha2.js';
6
6
  import { __exports as _u64 } from '../../../../../_virtual/_u64.js';
7
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
7
+ import { __exports as utils } from '../../../../../_virtual/utils.js';
8
8
 
9
9
  Object.defineProperty(sha512, "__esModule", { value: true });
10
10
  sha512.sha384 = sha512.sha512_256 = sha512.sha512_224 = sha512.sha512 = sha512.SHA512 = undefined;
@@ -1,4 +1,4 @@
1
- import { __exports as utils } from '../../../../../_virtual/utils2.js';
1
+ import { __exports as utils } from '../../../../../_virtual/utils.js';
2
2
  import './cryptoBrowser.js';
3
3
  import { __exports as cryptoBrowser } from '../../../../../_virtual/cryptoBrowser.js';
4
4