@waku/rln 0.1.6-a8ca168.0 → 0.1.6-acc9100.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.
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +21 -6
- package/bundle/packages/rln/dist/credentials_manager.js +13 -16
- package/bundle/packages/rln/dist/identity.js +5 -8
- package/bundle/packages/rln/dist/keystore/keystore.js +15 -11
- package/bundle/packages/rln/dist/utils/bytes.js +14 -10
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/rln_base_contract.d.ts +1 -0
- package/dist/contract/rln_base_contract.js +21 -6
- package/dist/contract/rln_base_contract.js.map +1 -1
- package/dist/credentials_manager.js +13 -16
- package/dist/credentials_manager.js.map +1 -1
- package/dist/identity.d.ts +4 -2
- package/dist/identity.js +5 -6
- package/dist/identity.js.map +1 -1
- package/dist/keystore/keystore.js +15 -11
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/utils/bytes.d.ts +2 -1
- package/dist/utils/bytes.js +13 -9
- package/dist/utils/bytes.js.map +1 -1
- package/dist/utils/index.d.ts +1 -1
- package/dist/utils/index.js +1 -1
- package/dist/utils/index.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/rln_base_contract.ts +29 -15
- package/src/credentials_manager.ts +20 -27
- package/src/identity.ts +5 -7
- package/src/keystore/keystore.ts +28 -24
- package/src/utils/bytes.ts +21 -19
- package/src/utils/index.ts +1 -1
- package/dist/contract/test-utils.d.ts +0 -39
- package/dist/contract/test-utils.js +0 -118
- package/dist/contract/test-utils.js.map +0 -1
- package/src/contract/test-utils.ts +0 -179
@@ -13,6 +13,7 @@ import '../../../../node_modules/multiformats/dist/src/bases/base8.js';
|
|
13
13
|
import '../../../../node_modules/multiformats/dist/src/bases/identity.js';
|
14
14
|
import '../../../../node_modules/multiformats/dist/src/codecs/json.js';
|
15
15
|
import { Logger } from '../../../utils/dist/logger/index.js';
|
16
|
+
import { buildBigIntFromUint8ArrayBE } from '../utils/bytes.js';
|
16
17
|
import { RLN_ABI } from './abi.js';
|
17
18
|
import { DEFAULT_Q, DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from './constants.js';
|
18
19
|
import { MembershipState } from './types.js';
|
@@ -69,14 +70,13 @@ class RLNBaseContract {
|
|
69
70
|
*/
|
70
71
|
static async create(options) {
|
71
72
|
const instance = new RLNBaseContract(options);
|
72
|
-
const [min, max
|
73
|
+
const [min, max] = await Promise.all([
|
73
74
|
instance.contract.minMembershipRateLimit(),
|
74
75
|
instance.contract.maxMembershipRateLimit(),
|
75
76
|
instance.contract.Q()
|
76
77
|
]);
|
77
78
|
instance.minRateLimit = BigNumber.from(min).toNumber();
|
78
79
|
instance.maxRateLimit = BigNumber.from(max).toNumber();
|
79
|
-
instance.idCommitmentBigIntLimit = BigInt(idCommitmentBigIntLimit.toString());
|
80
80
|
instance.validateRateLimit(instance.rateLimit);
|
81
81
|
return instance;
|
82
82
|
}
|
@@ -344,11 +344,24 @@ class RLNBaseContract {
|
|
344
344
|
log.error(`Error in withdraw: ${error.message}`);
|
345
345
|
}
|
346
346
|
}
|
347
|
+
getIdCommitmentBigInt(bytes) {
|
348
|
+
let idCommitmentBigIntBE = buildBigIntFromUint8ArrayBE(bytes);
|
349
|
+
if (!this.contract) {
|
350
|
+
throw Error("RLN contract is not initialized");
|
351
|
+
}
|
352
|
+
if (idCommitmentBigIntBE >= this.idCommitmentBigIntLimit) {
|
353
|
+
log.warn(`ID commitment is greater than Q, reducing it by Q(idCommitmentBigIntLimit): ${idCommitmentBigIntBE} % ${this.idCommitmentBigIntLimit}`);
|
354
|
+
idCommitmentBigIntBE =
|
355
|
+
idCommitmentBigIntBE % this.idCommitmentBigIntLimit;
|
356
|
+
}
|
357
|
+
return idCommitmentBigIntBE;
|
358
|
+
}
|
347
359
|
async registerWithIdentity(identity) {
|
348
360
|
try {
|
349
361
|
log.info(`Registering identity with rate limit: ${this.rateLimit} messages/epoch`);
|
362
|
+
const idCommitmentBigInt = this.getIdCommitmentBigInt(identity.IDCommitment);
|
350
363
|
// Check if the ID commitment is already registered
|
351
|
-
const existingIndex = await this.getMemberIndex(
|
364
|
+
const existingIndex = await this.getMemberIndex(idCommitmentBigInt);
|
352
365
|
if (existingIndex) {
|
353
366
|
throw new Error(`ID commitment is already registered with index ${existingIndex}`);
|
354
367
|
}
|
@@ -357,9 +370,11 @@ class RLNBaseContract {
|
|
357
370
|
if (remainingRateLimit < this.rateLimit) {
|
358
371
|
throw new Error(`Not enough remaining rate limit. Requested: ${this.rateLimit}, Available: ${remainingRateLimit}`);
|
359
372
|
}
|
360
|
-
const estimatedGas = await this.contract.estimateGas.register(
|
373
|
+
const estimatedGas = await this.contract.estimateGas.register(idCommitmentBigInt, this.rateLimit, []);
|
361
374
|
const gasLimit = estimatedGas.add(10000);
|
362
|
-
const txRegisterResponse = await this.contract.register(
|
375
|
+
const txRegisterResponse = await this.contract.register(idCommitmentBigInt, this.rateLimit, [], {
|
376
|
+
gasLimit
|
377
|
+
});
|
363
378
|
const txRegisterReceipt = await txRegisterResponse.wait();
|
364
379
|
if (txRegisterReceipt.status === 0) {
|
365
380
|
throw new Error("Transaction failed on-chain");
|
@@ -421,7 +436,7 @@ class RLNBaseContract {
|
|
421
436
|
async registerWithPermitAndErase(identity, permit, idCommitmentsToErase) {
|
422
437
|
try {
|
423
438
|
log.info(`Registering identity with permit and rate limit: ${this.rateLimit} messages/epoch`);
|
424
|
-
const txRegisterResponse = await this.contract.registerWithPermit(permit.owner, permit.deadline, permit.v, permit.r, permit.s, identity.
|
439
|
+
const txRegisterResponse = await this.contract.registerWithPermit(permit.owner, permit.deadline, permit.v, permit.r, permit.s, this.getIdCommitmentBigInt(identity.IDCommitment), this.rateLimit, idCommitmentsToErase.map((id) => BigNumber.from(id)));
|
425
440
|
const txRegisterReceipt = await txRegisterResponse.wait();
|
426
441
|
const memberRegistered = txRegisterReceipt.events?.find((event) => event.event === "MembershipRegistered");
|
427
442
|
if (!memberRegistered || !memberRegistered.args) {
|
@@ -20,7 +20,7 @@ import { RLNBaseContract } from './contract/rln_base_contract.js';
|
|
20
20
|
import { IdentityCredential } from './identity.js';
|
21
21
|
import { Keystore } from './keystore/keystore.js';
|
22
22
|
import { extractMetaMaskSigner } from './utils/metamask.js';
|
23
|
-
import {
|
23
|
+
import { switchEndianness } from './utils/bytes.js';
|
24
24
|
import './utils/epoch.js';
|
25
25
|
|
26
26
|
const log = new Logger("waku:credentials");
|
@@ -198,22 +198,19 @@ class RLNCredentialsManager {
|
|
198
198
|
const seedBytes = encoder.encode(seed);
|
199
199
|
// Generate deterministic values using HMAC-SHA256
|
200
200
|
// We use different context strings for each component to ensure they're different
|
201
|
-
const
|
202
|
-
const
|
203
|
-
const combinedBytes = new Uint8Array([...
|
204
|
-
const
|
205
|
-
const
|
206
|
-
|
207
|
-
|
208
|
-
|
209
|
-
|
210
|
-
const
|
211
|
-
|
212
|
-
log.warn(`ID commitment is greater than Q, reducing it by Q(idCommitmentBigIntLimit): ${idCommitmentBigInt} % ${idCommitmentBigIntLimit}`);
|
213
|
-
idCommitmentBigInt = idCommitmentBigInt % idCommitmentBigIntLimit;
|
214
|
-
}
|
201
|
+
const idTrapdoorBE = hmac(sha256, seedBytes, encoder.encode("IDTrapdoor"));
|
202
|
+
const idNullifierBE = hmac(sha256, seedBytes, encoder.encode("IDNullifier"));
|
203
|
+
const combinedBytes = new Uint8Array([...idTrapdoorBE, ...idNullifierBE]);
|
204
|
+
const idSecretHashBE = sha256(combinedBytes);
|
205
|
+
const idCommitmentBE = sha256(idSecretHashBE);
|
206
|
+
// All hashing functions return big-endian bytes
|
207
|
+
// We need to switch to little-endian for the identity credential
|
208
|
+
const idTrapdoorLE = switchEndianness(idTrapdoorBE);
|
209
|
+
const idNullifierLE = switchEndianness(idNullifierBE);
|
210
|
+
const idSecretHashLE = switchEndianness(idSecretHashBE);
|
211
|
+
const idCommitmentLE = switchEndianness(idCommitmentBE);
|
215
212
|
log.info("Successfully generated identity credential");
|
216
|
-
return new IdentityCredential(
|
213
|
+
return new IdentityCredential(idTrapdoorLE, idNullifierLE, idSecretHashLE, idCommitmentLE);
|
217
214
|
}
|
218
215
|
}
|
219
216
|
|
@@ -1,18 +1,16 @@
|
|
1
|
-
import { buildBigIntFromUint8ArrayLE } from './utils/bytes.js';
|
2
|
-
import './utils/epoch.js';
|
3
|
-
|
4
1
|
class IdentityCredential {
|
5
2
|
IDTrapdoor;
|
6
3
|
IDNullifier;
|
7
4
|
IDSecretHash;
|
8
5
|
IDCommitment;
|
9
|
-
|
10
|
-
|
6
|
+
/**
|
7
|
+
* All variables are in little-endian format
|
8
|
+
*/
|
9
|
+
constructor(IDTrapdoor, IDNullifier, IDSecretHash, IDCommitment) {
|
11
10
|
this.IDTrapdoor = IDTrapdoor;
|
12
11
|
this.IDNullifier = IDNullifier;
|
13
12
|
this.IDSecretHash = IDSecretHash;
|
14
13
|
this.IDCommitment = IDCommitment;
|
15
|
-
this.IDCommitmentBigInt = IDCommitmentBigInt;
|
16
14
|
}
|
17
15
|
static fromBytes(memKeys) {
|
18
16
|
if (memKeys.length < 128) {
|
@@ -22,8 +20,7 @@ class IdentityCredential {
|
|
22
20
|
const idNullifier = memKeys.subarray(32, 64);
|
23
21
|
const idSecretHash = memKeys.subarray(64, 96);
|
24
22
|
const idCommitment = memKeys.subarray(96, 128);
|
25
|
-
|
26
|
-
return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment, idCommitmentBigInt);
|
23
|
+
return new IdentityCredential(idTrapdoor, idNullifier, idSecretHash, idCommitment);
|
27
24
|
}
|
28
25
|
}
|
29
26
|
|
@@ -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 { buildBigIntFromUint8ArrayLE } from '../utils/bytes.js';
|
21
20
|
import { keccak256Checksum, decryptEipKeystore } from './cipher.js';
|
22
21
|
import { isKeystoreValid, isCredentialValid } from './schema_validator.js';
|
23
22
|
import { __exports as lib } from '../../../../_virtual/index.js';
|
@@ -164,14 +163,16 @@ class Keystore {
|
|
164
163
|
try {
|
165
164
|
const str = bytesToUtf8(bytes);
|
166
165
|
const obj = JSON.parse(str);
|
167
|
-
|
166
|
+
const idCommitmentLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idCommitment", []));
|
167
|
+
const idTrapdoorLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idTrapdoor", []));
|
168
|
+
const idNullifierLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idNullifier", []));
|
169
|
+
const idSecretHashLE = Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idSecretHash", []));
|
168
170
|
return {
|
169
171
|
identity: {
|
170
|
-
IDCommitment:
|
171
|
-
IDTrapdoor:
|
172
|
-
IDNullifier:
|
173
|
-
|
174
|
-
IDSecretHash: Keystore.fromArraylikeToBytes(_.get(obj, "identityCredential.idSecretHash", []))
|
172
|
+
IDCommitment: idCommitmentLE,
|
173
|
+
IDTrapdoor: idTrapdoorLE,
|
174
|
+
IDNullifier: idNullifierLE,
|
175
|
+
IDSecretHash: idSecretHashLE
|
175
176
|
},
|
176
177
|
membership: {
|
177
178
|
treeIndex: _.get(obj, "treeIndex"),
|
@@ -208,13 +209,16 @@ class Keystore {
|
|
208
209
|
// follows nwaku implementation
|
209
210
|
// https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
|
210
211
|
static fromIdentityToBytes(options) {
|
212
|
+
function toLittleEndian(bytes) {
|
213
|
+
return new Uint8Array(bytes).reverse();
|
214
|
+
}
|
211
215
|
return utf8ToBytes(JSON.stringify({
|
212
216
|
treeIndex: options.membership.treeIndex,
|
213
217
|
identityCredential: {
|
214
|
-
idCommitment: Array.from(options.identity.IDCommitment),
|
215
|
-
idNullifier: Array.from(options.identity.IDNullifier),
|
216
|
-
idSecretHash: Array.from(options.identity.IDSecretHash),
|
217
|
-
idTrapdoor: Array.from(options.identity.IDTrapdoor)
|
218
|
+
idCommitment: Array.from(toLittleEndian(options.identity.IDCommitment)),
|
219
|
+
idNullifier: Array.from(toLittleEndian(options.identity.IDNullifier)),
|
220
|
+
idSecretHash: Array.from(toLittleEndian(options.identity.IDSecretHash)),
|
221
|
+
idTrapdoor: Array.from(toLittleEndian(options.identity.IDTrapdoor))
|
218
222
|
},
|
219
223
|
membershipContract: {
|
220
224
|
chainId: options.membership.chainId,
|
@@ -16,12 +16,12 @@ function concatenate(...input) {
|
|
16
16
|
}
|
17
17
|
return result;
|
18
18
|
}
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
19
|
+
function switchEndianness(bytes) {
|
20
|
+
return new Uint8Array(bytes.reverse());
|
21
|
+
}
|
22
|
+
function buildBigIntFromUint8ArrayBE(bytes) {
|
23
|
+
// Interpret bytes as big-endian
|
24
|
+
return bytes.reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
|
25
25
|
}
|
26
26
|
function writeUIntLE(buf, value, offset, byteLength, noAssert) {
|
27
27
|
value = +value;
|
@@ -39,9 +39,6 @@ function writeUIntLE(buf, value, offset, byteLength, noAssert) {
|
|
39
39
|
}
|
40
40
|
return buf;
|
41
41
|
}
|
42
|
-
function buildBigIntFromUint8ArrayLE(bytes) {
|
43
|
-
return bytes.reduce((acc, byte, i) => acc + BigInt(byte) * (1n << (8n * BigInt(i))), 0n);
|
44
|
-
}
|
45
42
|
/**
|
46
43
|
* Fills with zeros to set length
|
47
44
|
* @param array little endian Uint8Array
|
@@ -55,5 +52,12 @@ function zeroPadLE(array, length) {
|
|
55
52
|
}
|
56
53
|
return result;
|
57
54
|
}
|
55
|
+
// Adapted from https://github.com/feross/buffer
|
56
|
+
function checkInt(buf, value, offset, ext, max, min) {
|
57
|
+
if (value > max || value < min)
|
58
|
+
throw new RangeError('"value" argument is out of bounds');
|
59
|
+
if (offset + ext > buf.length)
|
60
|
+
throw new RangeError("Index out of range");
|
61
|
+
}
|
58
62
|
|
59
|
-
export {
|
63
|
+
export { buildBigIntFromUint8ArrayBE, concatenate, switchEndianness, writeUIntLE, zeroPadLE };
|