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