@waku/rln 0.1.6-b133417.0 → 0.1.6-b4748fd.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 +32 -8
- package/bundle/packages/rln/dist/contract/rln_contract.js +2 -2
- package/bundle/packages/rln/dist/credentials_manager.js +11 -17
- package/bundle/packages/rln/dist/identity.js +0 -5
- package/bundle/packages/rln/dist/keystore/keystore.js +11 -18
- package/bundle/packages/rln/dist/proof.js +2 -2
- package/bundle/packages/rln/dist/utils/bytes.js +61 -108
- package/bundle/packages/rln/dist/utils/hash.js +3 -3
- package/bundle/packages/rln/dist/zerokit.js +17 -17
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/rln_base_contract.d.ts +6 -0
- package/dist/contract/rln_base_contract.js +32 -8
- package/dist/contract/rln_base_contract.js.map +1 -1
- package/dist/contract/rln_contract.js +2 -2
- package/dist/contract/rln_contract.js.map +1 -1
- package/dist/credentials_manager.d.ts +0 -4
- package/dist/credentials_manager.js +11 -18
- package/dist/credentials_manager.js.map +1 -1
- package/dist/identity.d.ts +0 -1
- package/dist/identity.js +0 -4
- package/dist/identity.js.map +1 -1
- package/dist/keystore/keystore.js +11 -18
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/proof.js +2 -2
- package/dist/proof.js.map +1 -1
- package/dist/utils/bytes.d.ts +16 -42
- package/dist/utils/bytes.js +60 -107
- package/dist/utils/bytes.js.map +1 -1
- package/dist/utils/hash.js +5 -5
- package/dist/utils/hash.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/dist/zerokit.js +17 -17
- package/dist/zerokit.js.map +1 -1
- package/package.json +1 -1
- package/src/contract/rln_base_contract.ts +55 -17
- package/src/contract/rln_contract.ts +2 -5
- package/src/credentials_manager.ts +15 -31
- package/src/identity.ts +1 -7
- package/src/keystore/keystore.ts +11 -25
- package/src/proof.ts +2 -2
- package/src/utils/bytes.ts +67 -117
- package/src/utils/hash.ts +5 -15
- package/src/utils/index.ts +6 -1
- package/src/zerokit.ts +22 -30
package/src/keystore/keystore.ts
CHANGED
@@ -14,8 +14,6 @@ import {
|
|
14
14
|
import _ from "lodash";
|
15
15
|
import { v4 as uuidV4 } from "uuid";
|
16
16
|
|
17
|
-
import { BytesUtils } from "../utils/bytes.js";
|
18
|
-
|
19
17
|
import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
|
20
18
|
import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
|
21
19
|
import type {
|
@@ -250,7 +248,6 @@ export class Keystore {
|
|
250
248
|
const str = bytesToUtf8(bytes);
|
251
249
|
const obj = JSON.parse(str);
|
252
250
|
|
253
|
-
// Little Endian
|
254
251
|
const idCommitmentLE = Keystore.fromArraylikeToBytes(
|
255
252
|
_.get(obj, "identityCredential.idCommitment", [])
|
256
253
|
);
|
@@ -264,21 +261,12 @@ export class Keystore {
|
|
264
261
|
_.get(obj, "identityCredential.idSecretHash", [])
|
265
262
|
);
|
266
263
|
|
267
|
-
// Big Endian
|
268
|
-
const idCommitmentBE = BytesUtils.switchEndianness(idCommitmentLE);
|
269
|
-
const idTrapdoorBE = BytesUtils.switchEndianness(idTrapdoorLE);
|
270
|
-
const idNullifierBE = BytesUtils.switchEndianness(idNullifierLE);
|
271
|
-
const idSecretHashBE = BytesUtils.switchEndianness(idSecretHashLE);
|
272
|
-
const idCommitmentBigInt =
|
273
|
-
BytesUtils.buildBigIntFromUint8ArrayBE(idCommitmentBE);
|
274
|
-
|
275
264
|
return {
|
276
265
|
identity: {
|
277
|
-
IDCommitment:
|
278
|
-
IDTrapdoor:
|
279
|
-
IDNullifier:
|
280
|
-
IDSecretHash:
|
281
|
-
IDCommitmentBigInt: idCommitmentBigInt
|
266
|
+
IDCommitment: idCommitmentLE,
|
267
|
+
IDTrapdoor: idTrapdoorLE,
|
268
|
+
IDNullifier: idNullifierLE,
|
269
|
+
IDSecretHash: idSecretHashLE
|
282
270
|
},
|
283
271
|
membership: {
|
284
272
|
treeIndex: _.get(obj, "treeIndex"),
|
@@ -329,24 +317,22 @@ export class Keystore {
|
|
329
317
|
|
330
318
|
// follows nwaku implementation
|
331
319
|
// https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
|
332
|
-
// IdentityCredential is stored in Big Endian format => switch to Little Endian
|
333
320
|
private static fromIdentityToBytes(options: KeystoreEntity): Uint8Array {
|
321
|
+
function toLittleEndian(bytes: Uint8Array): Uint8Array {
|
322
|
+
return new Uint8Array(bytes).reverse();
|
323
|
+
}
|
334
324
|
return utf8ToBytes(
|
335
325
|
JSON.stringify({
|
336
326
|
treeIndex: options.membership.treeIndex,
|
337
327
|
identityCredential: {
|
338
328
|
idCommitment: Array.from(
|
339
|
-
|
340
|
-
),
|
341
|
-
idNullifier: Array.from(
|
342
|
-
BytesUtils.switchEndianness(options.identity.IDNullifier)
|
329
|
+
toLittleEndian(options.identity.IDCommitment)
|
343
330
|
),
|
331
|
+
idNullifier: Array.from(toLittleEndian(options.identity.IDNullifier)),
|
344
332
|
idSecretHash: Array.from(
|
345
|
-
|
333
|
+
toLittleEndian(options.identity.IDSecretHash)
|
346
334
|
),
|
347
|
-
idTrapdoor: Array.from(
|
348
|
-
BytesUtils.switchEndianness(options.identity.IDTrapdoor)
|
349
|
-
)
|
335
|
+
idTrapdoor: Array.from(toLittleEndian(options.identity.IDTrapdoor))
|
350
336
|
},
|
351
337
|
membershipContract: {
|
352
338
|
chainId: options.membership.chainId,
|
package/src/proof.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { IRateLimitProof } from "@waku/interfaces";
|
2
2
|
|
3
|
-
import {
|
3
|
+
import { concatenate, poseidonHash } from "./utils/index.js";
|
4
4
|
|
5
5
|
const proofOffset = 128;
|
6
6
|
const rootOffset = proofOffset + 32;
|
@@ -57,7 +57,7 @@ export class Proof implements IRateLimitProof {
|
|
57
57
|
}
|
58
58
|
|
59
59
|
export function proofToBytes(p: IRateLimitProof): Uint8Array {
|
60
|
-
return
|
60
|
+
return concatenate(
|
61
61
|
p.proof,
|
62
62
|
p.merkleRoot,
|
63
63
|
p.epoch,
|
package/src/utils/bytes.ts
CHANGED
@@ -1,130 +1,80 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
/**
|
2
|
+
* Concatenate Uint8Arrays
|
3
|
+
* @param input
|
4
|
+
* @returns concatenation of all Uint8Array received as input
|
5
|
+
*/
|
6
|
+
export function concatenate(...input: Uint8Array[]): Uint8Array {
|
7
|
+
let totalLength = 0;
|
8
|
+
for (const arr of input) {
|
9
|
+
totalLength += arr.length;
|
7
10
|
}
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
*/
|
14
|
-
public static buildBigIntFromUint8ArrayBE(bytes: Uint8Array): bigint {
|
15
|
-
let result = 0n;
|
16
|
-
for (let i = 0; i < bytes.length; i++) {
|
17
|
-
result = (result << 8n) + BigInt(bytes[i]);
|
18
|
-
}
|
19
|
-
return result;
|
11
|
+
const result = new Uint8Array(totalLength);
|
12
|
+
let offset = 0;
|
13
|
+
for (const arr of input) {
|
14
|
+
result.set(arr, offset);
|
15
|
+
offset += arr.length;
|
20
16
|
}
|
17
|
+
return result;
|
18
|
+
}
|
21
19
|
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
* @returns The bigint value with reversed endianness
|
26
|
-
*/
|
27
|
-
public static switchEndiannessBigInt(value: bigint): bigint {
|
28
|
-
// Convert bigint to byte array
|
29
|
-
const bytes = [];
|
30
|
-
let tempValue = value;
|
31
|
-
while (tempValue > 0n) {
|
32
|
-
bytes.push(Number(tempValue & 0xffn));
|
33
|
-
tempValue >>= 8n;
|
34
|
-
}
|
20
|
+
export function switchEndianness(bytes: Uint8Array): Uint8Array {
|
21
|
+
return new Uint8Array(bytes.reverse());
|
22
|
+
}
|
35
23
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
}
|
24
|
+
export function buildBigIntFromUint8ArrayBE(bytes: Uint8Array): bigint {
|
25
|
+
// Interpret bytes as big-endian
|
26
|
+
return bytes.reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
|
27
|
+
}
|
41
28
|
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
29
|
+
export function writeUIntLE(
|
30
|
+
buf: Uint8Array,
|
31
|
+
value: number,
|
32
|
+
offset: number,
|
33
|
+
byteLength: number,
|
34
|
+
noAssert?: boolean
|
35
|
+
): Uint8Array {
|
36
|
+
value = +value;
|
37
|
+
offset = offset >>> 0;
|
38
|
+
byteLength = byteLength >>> 0;
|
39
|
+
if (!noAssert) {
|
40
|
+
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
|
41
|
+
checkInt(buf, value, offset, byteLength, maxBytes, 0);
|
54
42
|
}
|
55
43
|
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
buf
|
61
|
-
value: number,
|
62
|
-
offset: number,
|
63
|
-
byteLength: number,
|
64
|
-
noAssert?: boolean
|
65
|
-
): Uint8Array {
|
66
|
-
value = +value;
|
67
|
-
offset = offset >>> 0;
|
68
|
-
byteLength = byteLength >>> 0;
|
69
|
-
if (!noAssert) {
|
70
|
-
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
|
71
|
-
BytesUtils.checkInt(buf, value, offset, byteLength, maxBytes, 0);
|
72
|
-
}
|
73
|
-
|
74
|
-
let mul = 1;
|
75
|
-
let i = 0;
|
76
|
-
buf[offset] = value & 0xff;
|
77
|
-
while (++i < byteLength && (mul *= 0x100)) {
|
78
|
-
buf[offset + i] = (value / mul) & 0xff;
|
79
|
-
}
|
80
|
-
|
81
|
-
return buf;
|
44
|
+
let mul = 1;
|
45
|
+
let i = 0;
|
46
|
+
buf[offset] = value & 0xff;
|
47
|
+
while (++i < byteLength && (mul *= 0x100)) {
|
48
|
+
buf[offset + i] = (value / mul) & 0xff;
|
82
49
|
}
|
83
50
|
|
84
|
-
|
85
|
-
|
86
|
-
* @param array little endian Uint8Array
|
87
|
-
* @param length amount to pad
|
88
|
-
* @returns little endian Uint8Array padded with zeros to set length
|
89
|
-
*/
|
90
|
-
public static zeroPadLE(array: Uint8Array, length: number): Uint8Array {
|
91
|
-
const result = new Uint8Array(length);
|
92
|
-
for (let i = 0; i < length; i++) {
|
93
|
-
result[i] = array[i] || 0;
|
94
|
-
}
|
95
|
-
return result;
|
96
|
-
}
|
51
|
+
return buf;
|
52
|
+
}
|
97
53
|
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
)
|
107
|
-
|
108
|
-
throw new RangeError('"value" argument is out of bounds');
|
109
|
-
if (offset + ext > buf.length) throw new RangeError("Index out of range");
|
54
|
+
/**
|
55
|
+
* Fills with zeros to set length
|
56
|
+
* @param array little endian Uint8Array
|
57
|
+
* @param length amount to pad
|
58
|
+
* @returns little endian Uint8Array padded with zeros to set length
|
59
|
+
*/
|
60
|
+
export function zeroPadLE(array: Uint8Array, length: number): Uint8Array {
|
61
|
+
const result = new Uint8Array(length);
|
62
|
+
for (let i = 0; i < length; i++) {
|
63
|
+
result[i] = array[i] || 0;
|
110
64
|
}
|
65
|
+
return result;
|
66
|
+
}
|
111
67
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
117
|
-
|
118
|
-
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
123
|
-
|
124
|
-
for (const arr of input) {
|
125
|
-
result.set(arr, offset);
|
126
|
-
offset += arr.length;
|
127
|
-
}
|
128
|
-
return result;
|
129
|
-
}
|
68
|
+
// Adapted from https://github.com/feross/buffer
|
69
|
+
function checkInt(
|
70
|
+
buf: Uint8Array,
|
71
|
+
value: number,
|
72
|
+
offset: number,
|
73
|
+
ext: number,
|
74
|
+
max: number,
|
75
|
+
min: number
|
76
|
+
): void {
|
77
|
+
if (value > max || value < min)
|
78
|
+
throw new RangeError('"value" argument is out of bounds');
|
79
|
+
if (offset + ext > buf.length) throw new RangeError("Index out of range");
|
130
80
|
}
|
package/src/utils/hash.ts
CHANGED
@@ -1,25 +1,15 @@
|
|
1
1
|
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
2
2
|
|
3
|
-
import {
|
3
|
+
import { concatenate, writeUIntLE } from "./bytes.js";
|
4
4
|
|
5
5
|
export function poseidonHash(...input: Array<Uint8Array>): Uint8Array {
|
6
|
-
const inputLen =
|
7
|
-
|
8
|
-
input.length,
|
9
|
-
0,
|
10
|
-
8
|
11
|
-
);
|
12
|
-
const lenPrefixedData = BytesUtils.concatenate(inputLen, ...input);
|
6
|
+
const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
|
7
|
+
const lenPrefixedData = concatenate(inputLen, ...input);
|
13
8
|
return zerokitRLN.poseidonHash(lenPrefixedData);
|
14
9
|
}
|
15
10
|
|
16
11
|
export function sha256(input: Uint8Array): Uint8Array {
|
17
|
-
const inputLen =
|
18
|
-
|
19
|
-
input.length,
|
20
|
-
0,
|
21
|
-
8
|
22
|
-
);
|
23
|
-
const lenPrefixedData = BytesUtils.concatenate(inputLen, input);
|
12
|
+
const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
|
13
|
+
const lenPrefixedData = concatenate(inputLen, input);
|
24
14
|
return zerokitRLN.hash(lenPrefixedData);
|
25
15
|
}
|
package/src/utils/index.ts
CHANGED
@@ -1,4 +1,9 @@
|
|
1
1
|
export { extractMetaMaskSigner } from "./metamask.js";
|
2
|
-
export {
|
2
|
+
export {
|
3
|
+
concatenate,
|
4
|
+
writeUIntLE,
|
5
|
+
switchEndianness,
|
6
|
+
zeroPadLE
|
7
|
+
} from "./bytes.js";
|
3
8
|
export { sha256, poseidonHash } from "./hash.js";
|
4
9
|
export { dateToEpoch, epochIntToBytes, epochBytesToInt } from "./epoch.js";
|
package/src/zerokit.ts
CHANGED
@@ -5,7 +5,12 @@ import { DEFAULT_RATE_LIMIT, RATE_LIMIT_PARAMS } from "./contract/constants.js";
|
|
5
5
|
import { IdentityCredential } from "./identity.js";
|
6
6
|
import { Proof, proofToBytes } from "./proof.js";
|
7
7
|
import { WitnessCalculator } from "./resources/witness_calculator";
|
8
|
-
import {
|
8
|
+
import {
|
9
|
+
concatenate,
|
10
|
+
dateToEpoch,
|
11
|
+
epochIntToBytes,
|
12
|
+
writeUIntLE
|
13
|
+
} from "./utils/index.js";
|
9
14
|
|
10
15
|
export class Zerokit {
|
11
16
|
public constructor(
|
@@ -52,16 +57,13 @@ export class Zerokit {
|
|
52
57
|
): void {
|
53
58
|
// serializes a seq of IDCommitments to a byte seq
|
54
59
|
// the order of serialization is |id_commitment_len<8>|id_commitment<var>|
|
55
|
-
const idCommitmentLen =
|
60
|
+
const idCommitmentLen = writeUIntLE(
|
56
61
|
new Uint8Array(8),
|
57
62
|
idCommitments.length,
|
58
63
|
0,
|
59
64
|
8
|
60
65
|
);
|
61
|
-
const idCommitmentBytes =
|
62
|
-
idCommitmentLen,
|
63
|
-
...idCommitments
|
64
|
-
);
|
66
|
+
const idCommitmentBytes = concatenate(idCommitmentLen, ...idCommitments);
|
65
67
|
zerokitRLN.setLeavesFrom(this.zkRLN, index, idCommitmentBytes);
|
66
68
|
}
|
67
69
|
|
@@ -81,19 +83,9 @@ export class Zerokit {
|
|
81
83
|
rateLimit?: number
|
82
84
|
): Uint8Array {
|
83
85
|
// calculate message length
|
84
|
-
const msgLen =
|
85
|
-
|
86
|
-
|
87
|
-
0,
|
88
|
-
8
|
89
|
-
);
|
90
|
-
const memIndexBytes = BytesUtils.writeUIntLE(
|
91
|
-
new Uint8Array(8),
|
92
|
-
memIndex,
|
93
|
-
0,
|
94
|
-
8
|
95
|
-
);
|
96
|
-
const rateLimitBytes = BytesUtils.writeUIntLE(
|
86
|
+
const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
|
87
|
+
const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
|
88
|
+
const rateLimitBytes = writeUIntLE(
|
97
89
|
new Uint8Array(8),
|
98
90
|
rateLimit ?? this.rateLimit,
|
99
91
|
0,
|
@@ -101,7 +93,7 @@ export class Zerokit {
|
|
101
93
|
);
|
102
94
|
|
103
95
|
// [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> | rate_limit<8> ]
|
104
|
-
return
|
96
|
+
return concatenate(
|
105
97
|
idKey,
|
106
98
|
memIndexBytes,
|
107
99
|
epoch,
|
@@ -177,8 +169,8 @@ export class Zerokit {
|
|
177
169
|
}
|
178
170
|
|
179
171
|
// calculate message length
|
180
|
-
const msgLen =
|
181
|
-
const rateLimitBytes =
|
172
|
+
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
173
|
+
const rateLimitBytes = writeUIntLE(
|
182
174
|
new Uint8Array(8),
|
183
175
|
rateLimit ?? this.rateLimit,
|
184
176
|
0,
|
@@ -187,7 +179,7 @@ export class Zerokit {
|
|
187
179
|
|
188
180
|
return zerokitRLN.verifyRLNProof(
|
189
181
|
this.zkRLN,
|
190
|
-
|
182
|
+
concatenate(pBytes, msgLen, msg, rateLimitBytes)
|
191
183
|
);
|
192
184
|
}
|
193
185
|
|
@@ -204,19 +196,19 @@ export class Zerokit {
|
|
204
196
|
pBytes = proofToBytes(proof);
|
205
197
|
}
|
206
198
|
// calculate message length
|
207
|
-
const msgLen =
|
208
|
-
const rateLimitBytes =
|
199
|
+
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
200
|
+
const rateLimitBytes = writeUIntLE(
|
209
201
|
new Uint8Array(8),
|
210
202
|
rateLimit ?? this.rateLimit,
|
211
203
|
0,
|
212
204
|
8
|
213
205
|
);
|
214
206
|
|
215
|
-
const rootsBytes =
|
207
|
+
const rootsBytes = concatenate(...roots);
|
216
208
|
|
217
209
|
return zerokitRLN.verifyWithRoots(
|
218
210
|
this.zkRLN,
|
219
|
-
|
211
|
+
concatenate(pBytes, msgLen, msg, rateLimitBytes),
|
220
212
|
rootsBytes
|
221
213
|
);
|
222
214
|
}
|
@@ -234,8 +226,8 @@ export class Zerokit {
|
|
234
226
|
}
|
235
227
|
|
236
228
|
// calculate message length
|
237
|
-
const msgLen =
|
238
|
-
const rateLimitBytes =
|
229
|
+
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
230
|
+
const rateLimitBytes = writeUIntLE(
|
239
231
|
new Uint8Array(8),
|
240
232
|
rateLimit ?? this.rateLimit,
|
241
233
|
0,
|
@@ -244,7 +236,7 @@ export class Zerokit {
|
|
244
236
|
|
245
237
|
return zerokitRLN.verifyWithRoots(
|
246
238
|
this.zkRLN,
|
247
|
-
|
239
|
+
concatenate(pBytes, msgLen, msg, rateLimitBytes),
|
248
240
|
new Uint8Array()
|
249
241
|
);
|
250
242
|
}
|