@waku/rln 0.1.6-b7e9b08.0 → 0.1.6-c8b7131.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/index.js +2 -1
- package/bundle/node_modules/@ethersproject/keccak256/lib.esm/index.js +1 -1
- package/bundle/node_modules/{@ethersproject/keccak256/node_modules/js-sha3 → js-sha3}/src/sha3.js +2 -2
- package/bundle/packages/core/dist/lib/connection_manager/connection_manager.js +0 -3
- package/bundle/packages/core/dist/lib/connection_manager/keep_alive_manager.js +3 -3
- package/bundle/packages/core/dist/lib/filter/filter.js +0 -3
- package/bundle/packages/core/dist/lib/light_push/light_push.js +0 -3
- package/bundle/packages/core/dist/lib/message/version_0.js +1 -4
- package/bundle/packages/core/dist/lib/metadata/metadata.js +0 -3
- package/bundle/packages/core/dist/lib/store/store.js +3 -3
- package/bundle/packages/rln/dist/codec.js +3 -3
- package/bundle/packages/rln/dist/contract/constants.js +8 -1
- package/bundle/packages/rln/dist/contract/rln_base_contract.js +138 -77
- package/bundle/packages/rln/dist/contract/rln_contract.js +5 -5
- package/bundle/packages/rln/dist/credentials_manager.js +28 -20
- package/bundle/packages/rln/dist/identity.js +8 -6
- package/bundle/packages/rln/dist/keystore/keystore.js +18 -14
- package/bundle/packages/rln/dist/message.js +11 -0
- package/bundle/packages/rln/dist/proof.js +2 -2
- package/bundle/packages/rln/dist/rln.js +3 -3
- package/bundle/packages/rln/dist/utils/bytes.js +103 -58
- package/bundle/packages/rln/dist/utils/epoch.js +3 -3
- package/bundle/packages/rln/dist/utils/hash.js +3 -3
- package/bundle/packages/rln/dist/zerokit.js +17 -17
- package/bundle/packages/utils/dist/common/sharding/index.js +4 -4
- package/dist/.tsbuildinfo +1 -1
- package/dist/contract/constants.d.ts +6 -0
- package/dist/contract/constants.js +6 -0
- package/dist/contract/constants.js.map +1 -1
- package/dist/contract/index.d.ts +1 -0
- package/dist/contract/index.js +1 -0
- package/dist/contract/index.js.map +1 -1
- package/dist/contract/rln_base_contract.d.ts +32 -22
- package/dist/contract/rln_base_contract.js +135 -74
- 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/contract/types.d.ts +5 -0
- package/dist/contract/types.js.map +1 -1
- package/dist/credentials_manager.d.ts +4 -0
- package/dist/credentials_manager.js +26 -17
- package/dist/credentials_manager.js.map +1 -1
- package/dist/identity.d.ts +5 -2
- package/dist/identity.js +8 -5
- package/dist/identity.js.map +1 -1
- package/dist/index.d.ts +2 -1
- package/dist/index.js +1 -0
- package/dist/index.js.map +1 -1
- package/dist/keystore/keystore.js +14 -10
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/types.d.ts +2 -2
- package/dist/message.d.ts +5 -4
- package/dist/message.js +2 -0
- package/dist/message.js.map +1 -1
- package/dist/proof.js +2 -2
- package/dist/proof.js.map +1 -1
- package/dist/utils/bytes.d.ts +42 -20
- package/dist/utils/bytes.js +102 -57
- 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/constants.ts +9 -0
- package/src/contract/index.ts +1 -0
- package/src/contract/rln_base_contract.ts +177 -113
- package/src/contract/rln_contract.ts +5 -2
- package/src/contract/types.ts +5 -0
- package/src/credentials_manager.ts +47 -25
- package/src/identity.ts +11 -7
- package/src/index.ts +3 -1
- package/src/keystore/keystore.ts +34 -24
- package/src/keystore/types.ts +2 -2
- package/src/message.ts +7 -4
- package/src/proof.ts +2 -2
- package/src/utils/bytes.ts +118 -72
- package/src/utils/hash.ts +15 -5
- package/src/utils/index.ts +1 -6
- package/src/zerokit.ts +30 -22
- package/bundle/packages/utils/node_modules/@waku/interfaces/dist/connection_manager.js +0 -19
- package/bundle/packages/utils/node_modules/@waku/interfaces/dist/health_indicator.js +0 -12
- package/bundle/packages/utils/node_modules/@waku/interfaces/dist/protocols.js +0 -92
- 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
- /package/bundle/packages/{utils/node_modules/@waku/interfaces → interfaces}/dist/constants.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/uuid/dist/esm-browser/native.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/uuid/dist/esm-browser/rng.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/uuid/dist/esm-browser/stringify.js +0 -0
- /package/bundle/{node_modules → packages/rln/node_modules}/uuid/dist/esm-browser/v4.js +0 -0
package/src/keystore/keystore.ts
CHANGED
@@ -14,15 +14,15 @@ import {
|
|
14
14
|
import _ from "lodash";
|
15
15
|
import { v4 as uuidV4 } from "uuid";
|
16
16
|
|
17
|
-
import {
|
17
|
+
import { BytesUtils } from "../utils/bytes.js";
|
18
18
|
|
19
19
|
import { decryptEipKeystore, keccak256Checksum } from "./cipher.js";
|
20
20
|
import { isCredentialValid, isKeystoreValid } from "./schema_validator.js";
|
21
21
|
import type {
|
22
22
|
Keccak256Hash,
|
23
23
|
KeystoreEntity,
|
24
|
+
KeystoreMembershipInfo,
|
24
25
|
MembershipHash,
|
25
|
-
MembershipInfo,
|
26
26
|
Password,
|
27
27
|
Sha256Hash
|
28
28
|
} from "./types.js";
|
@@ -250,26 +250,27 @@ export class Keystore {
|
|
250
250
|
const str = bytesToUtf8(bytes);
|
251
251
|
const obj = JSON.parse(str);
|
252
252
|
|
253
|
-
|
253
|
+
const idCommitmentLE = Keystore.fromArraylikeToBytes(
|
254
|
+
_.get(obj, "identityCredential.idCommitment", [])
|
255
|
+
);
|
256
|
+
const idTrapdoorLE = Keystore.fromArraylikeToBytes(
|
257
|
+
_.get(obj, "identityCredential.idTrapdoor", [])
|
258
|
+
);
|
259
|
+
const idNullifierLE = Keystore.fromArraylikeToBytes(
|
260
|
+
_.get(obj, "identityCredential.idNullifier", [])
|
261
|
+
);
|
262
|
+
const idSecretHashLE = Keystore.fromArraylikeToBytes(
|
263
|
+
_.get(obj, "identityCredential.idSecretHash", [])
|
264
|
+
);
|
265
|
+
|
254
266
|
return {
|
255
267
|
identity: {
|
256
|
-
IDCommitment:
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
IDNullifier: Keystore.fromArraylikeToBytes(
|
263
|
-
_.get(obj, "identityCredential.idNullifier", [])
|
264
|
-
),
|
265
|
-
IDCommitmentBigInt: buildBigIntFromUint8Array(
|
266
|
-
Keystore.fromArraylikeToBytes(
|
267
|
-
_.get(obj, "identityCredential.idCommitment", [])
|
268
|
-
)
|
269
|
-
),
|
270
|
-
IDSecretHash: Keystore.fromArraylikeToBytes(
|
271
|
-
_.get(obj, "identityCredential.idSecretHash", [])
|
272
|
-
)
|
268
|
+
IDCommitment: idCommitmentLE,
|
269
|
+
IDTrapdoor: idTrapdoorLE,
|
270
|
+
IDNullifier: idNullifierLE,
|
271
|
+
IDSecretHash: idSecretHashLE,
|
272
|
+
IDCommitmentBigInt:
|
273
|
+
BytesUtils.buildBigIntFromUint8ArrayBE(idCommitmentLE)
|
273
274
|
},
|
274
275
|
membership: {
|
275
276
|
treeIndex: _.get(obj, "treeIndex"),
|
@@ -310,7 +311,9 @@ export class Keystore {
|
|
310
311
|
|
311
312
|
// follows nwaku implementation
|
312
313
|
// https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L111
|
313
|
-
private static computeMembershipHash(
|
314
|
+
private static computeMembershipHash(
|
315
|
+
info: KeystoreMembershipInfo
|
316
|
+
): MembershipHash {
|
314
317
|
return bytesToHex(
|
315
318
|
sha256(utf8ToBytes(`${info.chainId}${info.address}${info.treeIndex}`))
|
316
319
|
).toUpperCase();
|
@@ -318,14 +321,21 @@ export class Keystore {
|
|
318
321
|
|
319
322
|
// follows nwaku implementation
|
320
323
|
// https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L98
|
324
|
+
// IdentityCredential is stored in Big Endian format => switch to Little Endian
|
321
325
|
private static fromIdentityToBytes(options: KeystoreEntity): Uint8Array {
|
322
326
|
return utf8ToBytes(
|
323
327
|
JSON.stringify({
|
324
328
|
treeIndex: options.membership.treeIndex,
|
325
329
|
identityCredential: {
|
326
|
-
idCommitment: Array.from(
|
327
|
-
|
328
|
-
|
330
|
+
idCommitment: Array.from(
|
331
|
+
BytesUtils.switchEndianness(options.identity.IDCommitment)
|
332
|
+
),
|
333
|
+
idNullifier: Array.from(
|
334
|
+
BytesUtils.switchEndianness(options.identity.IDNullifier)
|
335
|
+
),
|
336
|
+
idSecretHash: Array.from(
|
337
|
+
BytesUtils.switchEndianness(options.identity.IDSecretHash)
|
338
|
+
),
|
329
339
|
idTrapdoor: Array.from(options.identity.IDTrapdoor)
|
330
340
|
},
|
331
341
|
membershipContract: {
|
package/src/keystore/types.ts
CHANGED
@@ -7,7 +7,7 @@ export type Password = string | Uint8Array;
|
|
7
7
|
|
8
8
|
// see reference
|
9
9
|
// https://github.com/waku-org/nwaku/blob/f05528d4be3d3c876a8b07f9bb7dfaae8aa8ec6e/waku/waku_keystore/protocol_types.nim#L111
|
10
|
-
export type
|
10
|
+
export type KeystoreMembershipInfo = {
|
11
11
|
chainId: string;
|
12
12
|
address: string;
|
13
13
|
treeIndex: number;
|
@@ -16,7 +16,7 @@ export type MembershipInfo = {
|
|
16
16
|
|
17
17
|
export type KeystoreEntity = {
|
18
18
|
identity: IdentityCredential;
|
19
|
-
membership:
|
19
|
+
membership: KeystoreMembershipInfo;
|
20
20
|
};
|
21
21
|
|
22
22
|
export type DecryptedCredentials = KeystoreEntity;
|
package/src/message.ts
CHANGED
@@ -1,7 +1,9 @@
|
|
1
|
+
import { message } from "@waku/core";
|
1
2
|
import type {
|
2
3
|
IDecodedMessage,
|
3
4
|
IMessage,
|
4
|
-
IRateLimitProof
|
5
|
+
IRateLimitProof,
|
6
|
+
IRlnMessage
|
5
7
|
} from "@waku/interfaces";
|
6
8
|
import * as utils from "@waku/utils/bytes";
|
7
9
|
|
@@ -13,12 +15,13 @@ export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
|
|
13
15
|
return new Uint8Array([...(msg.payload ?? []), ...contentTopicBytes]);
|
14
16
|
}
|
15
17
|
|
16
|
-
export class RlnMessage<T extends IDecodedMessage> implements
|
18
|
+
export class RlnMessage<T extends IDecodedMessage> implements IRlnMessage {
|
17
19
|
public pubsubTopic = "";
|
20
|
+
public version = message.version_0.Version;
|
18
21
|
|
19
22
|
public constructor(
|
20
|
-
|
21
|
-
|
23
|
+
private rlnInstance: RLNInstance,
|
24
|
+
private msg: T,
|
22
25
|
public rateLimitProof: IRateLimitProof | undefined
|
23
26
|
) {}
|
24
27
|
|
package/src/proof.ts
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
import type { IRateLimitProof } from "@waku/interfaces";
|
2
2
|
|
3
|
-
import {
|
3
|
+
import { BytesUtils, 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 concatenate(
|
60
|
+
return BytesUtils.concatenate(
|
61
61
|
p.proof,
|
62
62
|
p.merkleRoot,
|
63
63
|
p.epoch,
|
package/src/utils/bytes.ts
CHANGED
@@ -1,84 +1,130 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
let totalLength = 0;
|
8
|
-
for (const arr of input) {
|
9
|
-
totalLength += arr.length;
|
1
|
+
export class BytesUtils {
|
2
|
+
/**
|
3
|
+
* Switches endianness of a byte array
|
4
|
+
*/
|
5
|
+
public static switchEndianness(bytes: Uint8Array): Uint8Array {
|
6
|
+
return new Uint8Array(bytes.reverse());
|
10
7
|
}
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
8
|
+
|
9
|
+
/**
|
10
|
+
* Builds a BigInt from a big-endian Uint8Array
|
11
|
+
* @param bytes The big-endian bytes to convert
|
12
|
+
* @returns The resulting BigInt in big-endian format
|
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;
|
16
20
|
}
|
17
|
-
return result;
|
18
|
-
}
|
19
21
|
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
}
|
22
|
+
/**
|
23
|
+
* Switches endianness of a bigint value
|
24
|
+
* @param value The bigint value to switch endianness for
|
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
|
+
}
|
33
35
|
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
byteLength: number,
|
39
|
-
noAssert?: boolean
|
40
|
-
): Uint8Array {
|
41
|
-
value = +value;
|
42
|
-
offset = offset >>> 0;
|
43
|
-
byteLength = byteLength >>> 0;
|
44
|
-
if (!noAssert) {
|
45
|
-
const maxBytes = Math.pow(2, 8 * byteLength) - 1;
|
46
|
-
checkInt(buf, value, offset, byteLength, maxBytes, 0);
|
36
|
+
// Reverse bytes and convert back to bigint
|
37
|
+
return bytes
|
38
|
+
.reverse()
|
39
|
+
.reduce((acc, byte) => (acc << 8n) + BigInt(byte), 0n);
|
47
40
|
}
|
48
41
|
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
42
|
+
/**
|
43
|
+
* Converts a big-endian bigint to a 32-byte big-endian Uint8Array
|
44
|
+
* @param value The big-endian bigint to convert
|
45
|
+
* @returns A 32-byte big-endian Uint8Array
|
46
|
+
*/
|
47
|
+
public static bigIntToUint8Array32BE(value: bigint): Uint8Array {
|
48
|
+
const bytes = new Uint8Array(32);
|
49
|
+
for (let i = 31; i >= 0; i--) {
|
50
|
+
bytes[i] = Number(value & 0xffn);
|
51
|
+
value >>= 8n;
|
52
|
+
}
|
53
|
+
return bytes;
|
54
54
|
}
|
55
55
|
|
56
|
-
|
57
|
-
|
56
|
+
/**
|
57
|
+
* Writes an unsigned integer to a buffer in little-endian format
|
58
|
+
*/
|
59
|
+
public static writeUIntLE(
|
60
|
+
buf: Uint8Array,
|
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
|
+
}
|
58
73
|
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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;
|
82
|
+
}
|
83
|
+
|
84
|
+
/**
|
85
|
+
* Fills with zeros to set length
|
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
|
+
}
|
97
|
+
|
98
|
+
// Adapted from https://github.com/feross/buffer
|
99
|
+
public static checkInt(
|
100
|
+
buf: Uint8Array,
|
101
|
+
value: number,
|
102
|
+
offset: number,
|
103
|
+
ext: number,
|
104
|
+
max: number,
|
105
|
+
min: number
|
106
|
+
): void {
|
107
|
+
if (value > max || value < min)
|
108
|
+
throw new RangeError('"value" argument is out of bounds');
|
109
|
+
if (offset + ext > buf.length) throw new RangeError("Index out of range");
|
110
|
+
}
|
71
111
|
|
72
|
-
/**
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
112
|
+
/**
|
113
|
+
* Concatenate Uint8Arrays
|
114
|
+
* @param input
|
115
|
+
* @returns concatenation of all Uint8Array received as input
|
116
|
+
*/
|
117
|
+
public static concatenate(...input: Uint8Array[]): Uint8Array {
|
118
|
+
let totalLength = 0;
|
119
|
+
for (const arr of input) {
|
120
|
+
totalLength += arr.length;
|
121
|
+
}
|
122
|
+
const result = new Uint8Array(totalLength);
|
123
|
+
let offset = 0;
|
124
|
+
for (const arr of input) {
|
125
|
+
result.set(arr, offset);
|
126
|
+
offset += arr.length;
|
127
|
+
}
|
128
|
+
return result;
|
82
129
|
}
|
83
|
-
return result;
|
84
130
|
}
|
package/src/utils/hash.ts
CHANGED
@@ -1,15 +1,25 @@
|
|
1
1
|
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
2
2
|
|
3
|
-
import {
|
3
|
+
import { BytesUtils } from "./bytes.js";
|
4
4
|
|
5
5
|
export function poseidonHash(...input: Array<Uint8Array>): Uint8Array {
|
6
|
-
const inputLen = writeUIntLE(
|
7
|
-
|
6
|
+
const inputLen = BytesUtils.writeUIntLE(
|
7
|
+
new Uint8Array(8),
|
8
|
+
input.length,
|
9
|
+
0,
|
10
|
+
8
|
11
|
+
);
|
12
|
+
const lenPrefixedData = BytesUtils.concatenate(inputLen, ...input);
|
8
13
|
return zerokitRLN.poseidonHash(lenPrefixedData);
|
9
14
|
}
|
10
15
|
|
11
16
|
export function sha256(input: Uint8Array): Uint8Array {
|
12
|
-
const inputLen = writeUIntLE(
|
13
|
-
|
17
|
+
const inputLen = BytesUtils.writeUIntLE(
|
18
|
+
new Uint8Array(8),
|
19
|
+
input.length,
|
20
|
+
0,
|
21
|
+
8
|
22
|
+
);
|
23
|
+
const lenPrefixedData = BytesUtils.concatenate(inputLen, input);
|
14
24
|
return zerokitRLN.hash(lenPrefixedData);
|
15
25
|
}
|
package/src/utils/index.ts
CHANGED
@@ -1,9 +1,4 @@
|
|
1
1
|
export { extractMetaMaskSigner } from "./metamask.js";
|
2
|
-
export {
|
3
|
-
concatenate,
|
4
|
-
writeUIntLE,
|
5
|
-
buildBigIntFromUint8Array,
|
6
|
-
zeroPadLE
|
7
|
-
} from "./bytes.js";
|
2
|
+
export { BytesUtils } from "./bytes.js";
|
8
3
|
export { sha256, poseidonHash } from "./hash.js";
|
9
4
|
export { dateToEpoch, epochIntToBytes, epochBytesToInt } from "./epoch.js";
|
package/src/zerokit.ts
CHANGED
@@ -5,12 +5,7 @@ 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 {
|
9
|
-
concatenate,
|
10
|
-
dateToEpoch,
|
11
|
-
epochIntToBytes,
|
12
|
-
writeUIntLE
|
13
|
-
} from "./utils/index.js";
|
8
|
+
import { BytesUtils, dateToEpoch, epochIntToBytes } from "./utils/index.js";
|
14
9
|
|
15
10
|
export class Zerokit {
|
16
11
|
public constructor(
|
@@ -57,13 +52,16 @@ export class Zerokit {
|
|
57
52
|
): void {
|
58
53
|
// serializes a seq of IDCommitments to a byte seq
|
59
54
|
// the order of serialization is |id_commitment_len<8>|id_commitment<var>|
|
60
|
-
const idCommitmentLen = writeUIntLE(
|
55
|
+
const idCommitmentLen = BytesUtils.writeUIntLE(
|
61
56
|
new Uint8Array(8),
|
62
57
|
idCommitments.length,
|
63
58
|
0,
|
64
59
|
8
|
65
60
|
);
|
66
|
-
const idCommitmentBytes = concatenate(
|
61
|
+
const idCommitmentBytes = BytesUtils.concatenate(
|
62
|
+
idCommitmentLen,
|
63
|
+
...idCommitments
|
64
|
+
);
|
67
65
|
zerokitRLN.setLeavesFrom(this.zkRLN, index, idCommitmentBytes);
|
68
66
|
}
|
69
67
|
|
@@ -83,9 +81,19 @@ export class Zerokit {
|
|
83
81
|
rateLimit?: number
|
84
82
|
): Uint8Array {
|
85
83
|
// calculate message length
|
86
|
-
const msgLen = writeUIntLE(
|
87
|
-
|
88
|
-
|
84
|
+
const msgLen = BytesUtils.writeUIntLE(
|
85
|
+
new Uint8Array(8),
|
86
|
+
uint8Msg.length,
|
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(
|
89
97
|
new Uint8Array(8),
|
90
98
|
rateLimit ?? this.rateLimit,
|
91
99
|
0,
|
@@ -93,7 +101,7 @@ export class Zerokit {
|
|
93
101
|
);
|
94
102
|
|
95
103
|
// [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> | rate_limit<8> ]
|
96
|
-
return concatenate(
|
104
|
+
return BytesUtils.concatenate(
|
97
105
|
idKey,
|
98
106
|
memIndexBytes,
|
99
107
|
epoch,
|
@@ -169,8 +177,8 @@ export class Zerokit {
|
|
169
177
|
}
|
170
178
|
|
171
179
|
// calculate message length
|
172
|
-
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
173
|
-
const rateLimitBytes = writeUIntLE(
|
180
|
+
const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
181
|
+
const rateLimitBytes = BytesUtils.writeUIntLE(
|
174
182
|
new Uint8Array(8),
|
175
183
|
rateLimit ?? this.rateLimit,
|
176
184
|
0,
|
@@ -179,7 +187,7 @@ export class Zerokit {
|
|
179
187
|
|
180
188
|
return zerokitRLN.verifyRLNProof(
|
181
189
|
this.zkRLN,
|
182
|
-
concatenate(pBytes, msgLen, msg, rateLimitBytes)
|
190
|
+
BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes)
|
183
191
|
);
|
184
192
|
}
|
185
193
|
|
@@ -196,19 +204,19 @@ export class Zerokit {
|
|
196
204
|
pBytes = proofToBytes(proof);
|
197
205
|
}
|
198
206
|
// calculate message length
|
199
|
-
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
200
|
-
const rateLimitBytes = writeUIntLE(
|
207
|
+
const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
208
|
+
const rateLimitBytes = BytesUtils.writeUIntLE(
|
201
209
|
new Uint8Array(8),
|
202
210
|
rateLimit ?? this.rateLimit,
|
203
211
|
0,
|
204
212
|
8
|
205
213
|
);
|
206
214
|
|
207
|
-
const rootsBytes = concatenate(...roots);
|
215
|
+
const rootsBytes = BytesUtils.concatenate(...roots);
|
208
216
|
|
209
217
|
return zerokitRLN.verifyWithRoots(
|
210
218
|
this.zkRLN,
|
211
|
-
concatenate(pBytes, msgLen, msg, rateLimitBytes),
|
219
|
+
BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes),
|
212
220
|
rootsBytes
|
213
221
|
);
|
214
222
|
}
|
@@ -226,8 +234,8 @@ export class Zerokit {
|
|
226
234
|
}
|
227
235
|
|
228
236
|
// calculate message length
|
229
|
-
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
230
|
-
const rateLimitBytes = writeUIntLE(
|
237
|
+
const msgLen = BytesUtils.writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
238
|
+
const rateLimitBytes = BytesUtils.writeUIntLE(
|
231
239
|
new Uint8Array(8),
|
232
240
|
rateLimit ?? this.rateLimit,
|
233
241
|
0,
|
@@ -236,7 +244,7 @@ export class Zerokit {
|
|
236
244
|
|
237
245
|
return zerokitRLN.verifyWithRoots(
|
238
246
|
this.zkRLN,
|
239
|
-
concatenate(pBytes, msgLen, msg, rateLimitBytes),
|
247
|
+
BytesUtils.concatenate(pBytes, msgLen, msg, rateLimitBytes),
|
240
248
|
new Uint8Array()
|
241
249
|
);
|
242
250
|
}
|
@@ -1,19 +0,0 @@
|
|
1
|
-
var Tags;
|
2
|
-
(function (Tags) {
|
3
|
-
Tags["BOOTSTRAP"] = "bootstrap";
|
4
|
-
Tags["PEER_EXCHANGE"] = "peer-exchange";
|
5
|
-
Tags["LOCAL"] = "local-peer-cache";
|
6
|
-
})(Tags || (Tags = {}));
|
7
|
-
var EPeersByDiscoveryEvents;
|
8
|
-
(function (EPeersByDiscoveryEvents) {
|
9
|
-
EPeersByDiscoveryEvents["PEER_DISCOVERY_BOOTSTRAP"] = "peer:discovery:bootstrap";
|
10
|
-
EPeersByDiscoveryEvents["PEER_DISCOVERY_PEER_EXCHANGE"] = "peer:discovery:peer-exchange";
|
11
|
-
EPeersByDiscoveryEvents["PEER_CONNECT_BOOTSTRAP"] = "peer:connected:bootstrap";
|
12
|
-
EPeersByDiscoveryEvents["PEER_CONNECT_PEER_EXCHANGE"] = "peer:connected:peer-exchange";
|
13
|
-
})(EPeersByDiscoveryEvents || (EPeersByDiscoveryEvents = {}));
|
14
|
-
var EConnectionStateEvents;
|
15
|
-
(function (EConnectionStateEvents) {
|
16
|
-
EConnectionStateEvents["CONNECTION_STATUS"] = "waku:connection";
|
17
|
-
})(EConnectionStateEvents || (EConnectionStateEvents = {}));
|
18
|
-
|
19
|
-
export { EConnectionStateEvents, EPeersByDiscoveryEvents, Tags };
|
@@ -1,12 +0,0 @@
|
|
1
|
-
var HealthStatusChangeEvents;
|
2
|
-
(function (HealthStatusChangeEvents) {
|
3
|
-
HealthStatusChangeEvents["StatusChange"] = "health:change";
|
4
|
-
})(HealthStatusChangeEvents || (HealthStatusChangeEvents = {}));
|
5
|
-
var HealthStatus;
|
6
|
-
(function (HealthStatus) {
|
7
|
-
HealthStatus["Unhealthy"] = "Unhealthy";
|
8
|
-
HealthStatus["MinimallyHealthy"] = "MinimallyHealthy";
|
9
|
-
HealthStatus["SufficientlyHealthy"] = "SufficientlyHealthy";
|
10
|
-
})(HealthStatus || (HealthStatus = {}));
|
11
|
-
|
12
|
-
export { HealthStatus, HealthStatusChangeEvents };
|
@@ -1,92 +0,0 @@
|
|
1
|
-
var Protocols;
|
2
|
-
(function (Protocols) {
|
3
|
-
Protocols["Relay"] = "relay";
|
4
|
-
Protocols["Store"] = "store";
|
5
|
-
Protocols["LightPush"] = "lightpush";
|
6
|
-
Protocols["Filter"] = "filter";
|
7
|
-
})(Protocols || (Protocols = {}));
|
8
|
-
var ProtocolError;
|
9
|
-
(function (ProtocolError) {
|
10
|
-
//
|
11
|
-
// GENERAL ERRORS SECTION
|
12
|
-
//
|
13
|
-
/**
|
14
|
-
* Could not determine the origin of the fault. Best to check connectivity and try again
|
15
|
-
* */
|
16
|
-
ProtocolError["GENERIC_FAIL"] = "Generic error";
|
17
|
-
/**
|
18
|
-
* The remote peer rejected the message. Information provided by the remote peer
|
19
|
-
* is logged. Review message validity, or mitigation for `NO_PEER_AVAILABLE`
|
20
|
-
* or `DECODE_FAILED` can be used.
|
21
|
-
*/
|
22
|
-
ProtocolError["REMOTE_PEER_REJECTED"] = "Remote peer rejected";
|
23
|
-
/**
|
24
|
-
* Failure to protobuf decode the message. May be due to a remote peer issue,
|
25
|
-
* ensuring that messages are sent via several peer enable mitigation of this error.
|
26
|
-
*/
|
27
|
-
ProtocolError["DECODE_FAILED"] = "Failed to decode";
|
28
|
-
/**
|
29
|
-
* Failure to find a peer with suitable protocols. This may due to a connection issue.
|
30
|
-
* Mitigation can be: retrying after a given time period, display connectivity issue
|
31
|
-
* to user or listening for `peer:connected:bootstrap` or `peer:connected:peer-exchange`
|
32
|
-
* on the connection manager before retrying.
|
33
|
-
*/
|
34
|
-
ProtocolError["NO_PEER_AVAILABLE"] = "No peer available";
|
35
|
-
/**
|
36
|
-
* Failure to find a stream to the peer. This may be because the connection with the peer is not still alive.
|
37
|
-
* Mitigation can be: retrying after a given time period, or mitigation for `NO_PEER_AVAILABLE` can be used.
|
38
|
-
*/
|
39
|
-
ProtocolError["NO_STREAM_AVAILABLE"] = "No stream available";
|
40
|
-
/**
|
41
|
-
* The remote peer did not behave as expected. Mitigation for `NO_PEER_AVAILABLE`
|
42
|
-
* or `DECODE_FAILED` can be used.
|
43
|
-
*/
|
44
|
-
ProtocolError["NO_RESPONSE"] = "No response received";
|
45
|
-
//
|
46
|
-
// SEND ERRORS SECTION
|
47
|
-
//
|
48
|
-
/**
|
49
|
-
* Failure to protobuf encode the message. This is not recoverable and needs
|
50
|
-
* further investigation.
|
51
|
-
*/
|
52
|
-
ProtocolError["ENCODE_FAILED"] = "Failed to encode";
|
53
|
-
/**
|
54
|
-
* The message payload is empty, making the message invalid. Ensure that a non-empty
|
55
|
-
* payload is set on the outgoing message.
|
56
|
-
*/
|
57
|
-
ProtocolError["EMPTY_PAYLOAD"] = "Payload is empty";
|
58
|
-
/**
|
59
|
-
* The message size is above the maximum message size allowed on the Waku Network.
|
60
|
-
* Compressing the message or using an alternative strategy for large messages is recommended.
|
61
|
-
*/
|
62
|
-
ProtocolError["SIZE_TOO_BIG"] = "Size is too big";
|
63
|
-
/**
|
64
|
-
* The PubsubTopic passed to the send function is not configured on the Waku node.
|
65
|
-
* Please ensure that the PubsubTopic is used when initializing the Waku node.
|
66
|
-
*/
|
67
|
-
ProtocolError["TOPIC_NOT_CONFIGURED"] = "Topic not configured";
|
68
|
-
/**
|
69
|
-
* Fails when
|
70
|
-
*/
|
71
|
-
ProtocolError["STREAM_ABORTED"] = "Stream aborted";
|
72
|
-
/**
|
73
|
-
* General proof generation error message.
|
74
|
-
* nwaku: https://github.com/waku-org/nwaku/blob/c3cb06ac6c03f0f382d3941ea53b330f6a8dd127/waku/waku_rln_relay/group_manager/group_manager_base.nim#L201C19-L201C42
|
75
|
-
*/
|
76
|
-
ProtocolError["RLN_PROOF_GENERATION"] = "Proof generation failed";
|
77
|
-
//
|
78
|
-
// RECEIVE ERRORS SECTION
|
79
|
-
//
|
80
|
-
/**
|
81
|
-
* The pubsub topic configured on the decoder does not match the pubsub topic setup on the protocol.
|
82
|
-
* Ensure that the pubsub topic used for decoder creation is the same as the one used for protocol.
|
83
|
-
*/
|
84
|
-
ProtocolError["TOPIC_DECODER_MISMATCH"] = "Topic decoder mismatch";
|
85
|
-
/**
|
86
|
-
* The topics passed in the decoders do not match each other, or don't exist at all.
|
87
|
-
* Ensure that all the pubsub topics used in the decoders are valid and match each other.
|
88
|
-
*/
|
89
|
-
ProtocolError["INVALID_DECODER_TOPICS"] = "Invalid decoder topics";
|
90
|
-
})(ProtocolError || (ProtocolError = {}));
|
91
|
-
|
92
|
-
export { ProtocolError, Protocols };
|