@waku/rln 0.1.2 → 0.1.3-f6d5deb
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 +40916 -39697
- package/dist/codec.d.ts +2 -1
- package/dist/codec.js +7 -1
- package/dist/codec.js.map +1 -1
- package/dist/{constants.js → contract/constants.js} +3 -3
- package/dist/contract/constants.js.map +1 -0
- package/dist/contract/index.d.ts +2 -0
- package/dist/contract/index.js +3 -0
- package/dist/contract/index.js.map +1 -0
- package/dist/{rln_contract.d.ts → contract/rln_contract.d.ts} +3 -2
- package/dist/{rln_contract.js → contract/rln_contract.js} +24 -18
- package/dist/contract/rln_contract.js.map +1 -0
- package/dist/identity.d.ts +9 -0
- package/dist/identity.js +24 -0
- package/dist/identity.js.map +1 -0
- package/dist/index.d.ts +7 -5
- package/dist/index.js +7 -5
- package/dist/index.js.map +1 -1
- package/dist/keystore/cipher.js +1 -1
- package/dist/keystore/cipher.js.map +1 -1
- package/dist/keystore/credential_validation_generated.js.map +1 -1
- package/dist/keystore/keystore.js +21 -18
- package/dist/keystore/keystore.js.map +1 -1
- package/dist/keystore/keystore_validation_generated.js.map +1 -1
- package/dist/keystore/schema_validator.js.map +1 -1
- package/dist/keystore/types.d.ts +1 -1
- package/dist/message.js +7 -4
- package/dist/message.js.map +1 -1
- package/dist/proof.d.ts +21 -0
- package/dist/proof.js +49 -0
- package/dist/proof.js.map +1 -0
- package/dist/resources/verification_key.d.ts +9 -9
- package/dist/resources/witness_calculator.js.map +1 -0
- package/dist/rln.d.ts +6 -48
- package/dist/rln.js +25 -194
- package/dist/rln.js.map +1 -1
- package/dist/root_tracker.js +5 -2
- package/dist/root_tracker.js.map +1 -1
- package/dist/{byte_utils.d.ts → utils/bytes.d.ts} +7 -1
- package/dist/{byte_utils.js → utils/bytes.js} +21 -3
- package/dist/utils/bytes.js.map +1 -0
- package/dist/utils/epoch.js.map +1 -0
- package/dist/utils/hash.d.ts +2 -0
- package/dist/utils/hash.js +13 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/index.d.ts +4 -0
- package/dist/utils/index.js +5 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/{metamask.js → utils/metamask.js} +1 -0
- package/dist/utils/metamask.js.map +1 -0
- package/dist/zerokit.d.ts +19 -0
- package/dist/zerokit.js +105 -0
- package/dist/zerokit.js.map +1 -0
- package/package.json +18 -23
- package/src/codec.ts +8 -4
- package/src/identity.ts +27 -0
- package/src/index.ts +8 -12
- package/src/message.ts +4 -4
- package/src/proof.ts +67 -0
- package/src/rln.ts +32 -308
- package/src/root_tracker.ts +4 -1
- package/src/zerokit.ts +181 -0
- package/dist/.tsbuildinfo +0 -1
- package/dist/byte_utils.js.map +0 -1
- package/dist/constants.js.map +0 -1
- package/dist/epoch.js.map +0 -1
- package/dist/metamask.js.map +0 -1
- package/dist/rln_contract.js.map +0 -1
- package/dist/witness_calculator.js.map +0 -1
- package/src/byte_utils.ts +0 -63
- package/src/constants.ts +0 -68
- package/src/epoch.ts +0 -30
- package/src/metamask.ts +0 -16
- package/src/rln_contract.ts +0 -350
- package/src/witness_calculator.d.ts +0 -8
- package/src/witness_calculator.js +0 -335
- /package/dist/{constants.d.ts → contract/constants.d.ts} +0 -0
- /package/dist/{witness_calculator.d.ts → resources/witness_calculator.d.ts} +0 -0
- /package/dist/{witness_calculator.js → resources/witness_calculator.js} +0 -0
- /package/dist/{epoch.d.ts → utils/epoch.d.ts} +0 -0
- /package/dist/{epoch.js → utils/epoch.js} +0 -0
- /package/dist/{metamask.d.ts → utils/metamask.d.ts} +0 -0
@@ -0,0 +1,13 @@
|
|
1
|
+
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
2
|
+
import { concatenate, writeUIntLE } from "./bytes.js";
|
3
|
+
export function poseidonHash(...input) {
|
4
|
+
const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
|
5
|
+
const lenPrefixedData = concatenate(inputLen, ...input);
|
6
|
+
return zerokitRLN.poseidonHash(lenPrefixedData);
|
7
|
+
}
|
8
|
+
export function sha256(input) {
|
9
|
+
const inputLen = writeUIntLE(new Uint8Array(8), input.length, 0, 8);
|
10
|
+
const lenPrefixedData = concatenate(inputLen, input);
|
11
|
+
return zerokitRLN.hash(lenPrefixedData);
|
12
|
+
}
|
13
|
+
//# sourceMappingURL=hash.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"hash.js","sourceRoot":"","sources":["../../src/utils/hash.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AAErD,OAAO,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,YAAY,CAAC;AAEtD,MAAM,UAAU,YAAY,CAAC,GAAG,KAAwB;IACtD,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACpE,MAAM,eAAe,GAAG,WAAW,CAAC,QAAQ,EAAE,GAAG,KAAK,CAAC,CAAC;IACxD,OAAO,UAAU,CAAC,YAAY,CAAC,eAAe,CAAC,CAAC;AAClD,CAAC;AAED,MAAM,UAAU,MAAM,CAAC,KAAiB;IACtC,MAAM,QAAQ,GAAG,WAAW,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,KAAK,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;IACpE,MAAM,eAAe,GAAG,WAAW,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IACrD,OAAO,UAAU,CAAC,IAAI,CAAC,eAAe,CAAC,CAAC;AAC1C,CAAC"}
|
@@ -0,0 +1,4 @@
|
|
1
|
+
export { extractMetaMaskSigner } from "./metamask.js";
|
2
|
+
export { concatenate, writeUIntLE, buildBigIntFromUint8Array, zeroPadLE } from "./bytes.js";
|
3
|
+
export { sha256, poseidonHash } from "./hash.js";
|
4
|
+
export { dateToEpoch, epochIntToBytes, epochBytesToInt } from "./epoch.js";
|
@@ -0,0 +1,5 @@
|
|
1
|
+
export { extractMetaMaskSigner } from "./metamask.js";
|
2
|
+
export { concatenate, writeUIntLE, buildBigIntFromUint8Array, zeroPadLE } from "./bytes.js";
|
3
|
+
export { sha256, poseidonHash } from "./hash.js";
|
4
|
+
export { dateToEpoch, epochIntToBytes, epochBytesToInt } from "./epoch.js";
|
5
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/utils/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,qBAAqB,EAAE,MAAM,eAAe,CAAC;AACtD,OAAO,EACL,WAAW,EACX,WAAW,EACX,yBAAyB,EACzB,SAAS,EACV,MAAM,YAAY,CAAC;AACpB,OAAO,EAAE,MAAM,EAAE,YAAY,EAAE,MAAM,WAAW,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,eAAe,EAAE,eAAe,EAAE,MAAM,YAAY,CAAC"}
|
@@ -1,5 +1,6 @@
|
|
1
1
|
import { ethers } from "ethers";
|
2
2
|
export const extractMetaMaskSigner = async () => {
|
3
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
3
4
|
const ethereum = window.ethereum;
|
4
5
|
if (!ethereum) {
|
5
6
|
throw Error("Missing or invalid Ethereum provider. Please install a Web3 wallet such as MetaMask.");
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"metamask.js","sourceRoot":"","sources":["../../src/utils/metamask.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AAEhC,MAAM,CAAC,MAAM,qBAAqB,GAAG,KAAK,IAA4B,EAAE;IACtE,8DAA8D;IAC9D,MAAM,QAAQ,GAAI,MAAc,CAAC,QAAQ,CAAC;IAE1C,IAAI,CAAC,QAAQ,EAAE,CAAC;QACd,MAAM,KAAK,CACT,sFAAsF,CACvF,CAAC;IACJ,CAAC;IAED,MAAM,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,EAAE,qBAAqB,EAAE,CAAC,CAAC;IAC1D,MAAM,QAAQ,GAAG,IAAI,MAAM,CAAC,SAAS,CAAC,YAAY,CAAC,QAAQ,EAAE,KAAK,CAAC,CAAC;IAEpE,OAAO,QAAQ,CAAC,SAAS,EAAE,CAAC;AAC9B,CAAC,CAAC"}
|
@@ -0,0 +1,19 @@
|
|
1
|
+
import type { IRateLimitProof } from "@waku/interfaces";
|
2
|
+
import { IdentityCredential } from "./identity.js";
|
3
|
+
import { WitnessCalculator } from "./resources/witness_calculator.js";
|
4
|
+
export declare class Zerokit {
|
5
|
+
private zkRLN;
|
6
|
+
private witnessCalculator;
|
7
|
+
constructor(zkRLN: number, witnessCalculator: WitnessCalculator);
|
8
|
+
generateIdentityCredentials(): IdentityCredential;
|
9
|
+
generateSeededIdentityCredential(seed: string): IdentityCredential;
|
10
|
+
insertMember(idCommitment: Uint8Array): void;
|
11
|
+
insertMembers(index: number, ...idCommitments: Array<Uint8Array>): void;
|
12
|
+
deleteMember(index: number): void;
|
13
|
+
getMerkleRoot(): Uint8Array;
|
14
|
+
serializeMessage(uint8Msg: Uint8Array, memIndex: number, epoch: Uint8Array, idKey: Uint8Array): Uint8Array;
|
15
|
+
generateRLNProof(msg: Uint8Array, index: number, epoch: Uint8Array | Date | undefined, idSecretHash: Uint8Array): Promise<IRateLimitProof>;
|
16
|
+
verifyRLNProof(proof: IRateLimitProof | Uint8Array, msg: Uint8Array): boolean;
|
17
|
+
verifyWithRoots(proof: IRateLimitProof | Uint8Array, msg: Uint8Array, ...roots: Array<Uint8Array>): boolean;
|
18
|
+
verifyWithNoRoot(proof: IRateLimitProof | Uint8Array, msg: Uint8Array): boolean;
|
19
|
+
}
|
package/dist/zerokit.js
ADDED
@@ -0,0 +1,105 @@
|
|
1
|
+
import * as zerokitRLN from "@waku/zerokit-rln-wasm";
|
2
|
+
import { IdentityCredential } from "./identity.js";
|
3
|
+
import { Proof, proofToBytes } from "./proof.js";
|
4
|
+
import { concatenate, dateToEpoch, epochIntToBytes, writeUIntLE } from "./utils/index.js";
|
5
|
+
export class Zerokit {
|
6
|
+
zkRLN;
|
7
|
+
witnessCalculator;
|
8
|
+
constructor(zkRLN, witnessCalculator) {
|
9
|
+
this.zkRLN = zkRLN;
|
10
|
+
this.witnessCalculator = witnessCalculator;
|
11
|
+
}
|
12
|
+
generateIdentityCredentials() {
|
13
|
+
const memKeys = zerokitRLN.generateExtendedMembershipKey(this.zkRLN); // TODO: rename this function in zerokit rln-wasm
|
14
|
+
return IdentityCredential.fromBytes(memKeys);
|
15
|
+
}
|
16
|
+
generateSeededIdentityCredential(seed) {
|
17
|
+
const stringEncoder = new TextEncoder();
|
18
|
+
const seedBytes = stringEncoder.encode(seed);
|
19
|
+
// TODO: rename this function in zerokit rln-wasm
|
20
|
+
const memKeys = zerokitRLN.generateSeededExtendedMembershipKey(this.zkRLN, seedBytes);
|
21
|
+
return IdentityCredential.fromBytes(memKeys);
|
22
|
+
}
|
23
|
+
insertMember(idCommitment) {
|
24
|
+
zerokitRLN.insertMember(this.zkRLN, idCommitment);
|
25
|
+
}
|
26
|
+
insertMembers(index, ...idCommitments) {
|
27
|
+
// serializes a seq of IDCommitments to a byte seq
|
28
|
+
// the order of serialization is |id_commitment_len<8>|id_commitment<var>|
|
29
|
+
const idCommitmentLen = writeUIntLE(new Uint8Array(8), idCommitments.length, 0, 8);
|
30
|
+
const idCommitmentBytes = concatenate(idCommitmentLen, ...idCommitments);
|
31
|
+
zerokitRLN.setLeavesFrom(this.zkRLN, index, idCommitmentBytes);
|
32
|
+
}
|
33
|
+
deleteMember(index) {
|
34
|
+
zerokitRLN.deleteLeaf(this.zkRLN, index);
|
35
|
+
}
|
36
|
+
getMerkleRoot() {
|
37
|
+
return zerokitRLN.getRoot(this.zkRLN);
|
38
|
+
}
|
39
|
+
serializeMessage(uint8Msg, memIndex, epoch, idKey) {
|
40
|
+
// calculate message length
|
41
|
+
const msgLen = writeUIntLE(new Uint8Array(8), uint8Msg.length, 0, 8);
|
42
|
+
// Converting index to LE bytes
|
43
|
+
const memIndexBytes = writeUIntLE(new Uint8Array(8), memIndex, 0, 8);
|
44
|
+
// [ id_key<32> | id_index<8> | epoch<32> | signal_len<8> | signal<var> ]
|
45
|
+
return concatenate(idKey, memIndexBytes, epoch, msgLen, uint8Msg);
|
46
|
+
}
|
47
|
+
async generateRLNProof(msg, index, epoch, idSecretHash) {
|
48
|
+
if (epoch == undefined) {
|
49
|
+
epoch = epochIntToBytes(dateToEpoch(new Date()));
|
50
|
+
}
|
51
|
+
else if (epoch instanceof Date) {
|
52
|
+
epoch = epochIntToBytes(dateToEpoch(epoch));
|
53
|
+
}
|
54
|
+
if (epoch.length != 32)
|
55
|
+
throw "invalid epoch";
|
56
|
+
if (idSecretHash.length != 32)
|
57
|
+
throw "invalid id secret hash";
|
58
|
+
if (index < 0)
|
59
|
+
throw "index must be >= 0";
|
60
|
+
const serialized_msg = this.serializeMessage(msg, index, epoch, idSecretHash);
|
61
|
+
const rlnWitness = zerokitRLN.getSerializedRLNWitness(this.zkRLN, serialized_msg);
|
62
|
+
const inputs = zerokitRLN.RLNWitnessToJson(this.zkRLN, rlnWitness);
|
63
|
+
const calculatedWitness = await this.witnessCalculator.calculateWitness(inputs, false); // no sanity check being used in zerokit
|
64
|
+
const proofBytes = zerokitRLN.generate_rln_proof_with_witness(this.zkRLN, calculatedWitness, rlnWitness);
|
65
|
+
return new Proof(proofBytes);
|
66
|
+
}
|
67
|
+
verifyRLNProof(proof, msg) {
|
68
|
+
let pBytes;
|
69
|
+
if (proof instanceof Uint8Array) {
|
70
|
+
pBytes = proof;
|
71
|
+
}
|
72
|
+
else {
|
73
|
+
pBytes = proofToBytes(proof);
|
74
|
+
}
|
75
|
+
// calculate message length
|
76
|
+
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
77
|
+
return zerokitRLN.verifyRLNProof(this.zkRLN, concatenate(pBytes, msgLen, msg));
|
78
|
+
}
|
79
|
+
verifyWithRoots(proof, msg, ...roots) {
|
80
|
+
let pBytes;
|
81
|
+
if (proof instanceof Uint8Array) {
|
82
|
+
pBytes = proof;
|
83
|
+
}
|
84
|
+
else {
|
85
|
+
pBytes = proofToBytes(proof);
|
86
|
+
}
|
87
|
+
// calculate message length
|
88
|
+
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
89
|
+
const rootsBytes = concatenate(...roots);
|
90
|
+
return zerokitRLN.verifyWithRoots(this.zkRLN, concatenate(pBytes, msgLen, msg), rootsBytes);
|
91
|
+
}
|
92
|
+
verifyWithNoRoot(proof, msg) {
|
93
|
+
let pBytes;
|
94
|
+
if (proof instanceof Uint8Array) {
|
95
|
+
pBytes = proof;
|
96
|
+
}
|
97
|
+
else {
|
98
|
+
pBytes = proofToBytes(proof);
|
99
|
+
}
|
100
|
+
// calculate message length
|
101
|
+
const msgLen = writeUIntLE(new Uint8Array(8), msg.length, 0, 8);
|
102
|
+
return zerokitRLN.verifyWithRoots(this.zkRLN, concatenate(pBytes, msgLen, msg), new Uint8Array());
|
103
|
+
}
|
104
|
+
}
|
105
|
+
//# sourceMappingURL=zerokit.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"zerokit.js","sourceRoot":"","sources":["../src/zerokit.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,UAAU,MAAM,wBAAwB,CAAC;AAErD,OAAO,EAAE,kBAAkB,EAAE,MAAM,eAAe,CAAC;AACnD,OAAO,EAAE,KAAK,EAAE,YAAY,EAAE,MAAM,YAAY,CAAC;AAEjD,OAAO,EACL,WAAW,EACX,WAAW,EACX,eAAe,EACf,WAAW,EACZ,MAAM,kBAAkB,CAAC;AAE1B,MAAM,OAAO,OAAO;IAER;IACA;IAFV,YACU,KAAa,EACb,iBAAoC;QADpC,UAAK,GAAL,KAAK,CAAQ;QACb,sBAAiB,GAAjB,iBAAiB,CAAmB;IAC3C,CAAC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,KAAK,EACV,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,EAChC,UAAU,CACX,CAAC;IACJ,CAAC;IAED,gBAAgB,CACd,KAAmC,EACnC,GAAe;QAEf,IAAI,MAAkB,CAAC;QACvB,IAAI,KAAK,YAAY,UAAU,EAAE,CAAC;YAChC,MAAM,GAAG,KAAK,CAAC;QACjB,CAAC;aAAM,CAAC;YACN,MAAM,GAAG,YAAY,CAAC,KAAK,CAAC,CAAC;QAC/B,CAAC;QAED,2BAA2B;QAC3B,MAAM,MAAM,GAAG,WAAW,CAAC,IAAI,UAAU,CAAC,CAAC,CAAC,EAAE,GAAG,CAAC,MAAM,EAAE,CAAC,EAAE,CAAC,CAAC,CAAC;QAEhE,OAAO,UAAU,CAAC,eAAe,CAC/B,IAAI,CAAC,KAAK,EACV,WAAW,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,EAChC,IAAI,UAAU,EAAE,CACjB,CAAC;IACJ,CAAC;CACF"}
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@waku/rln",
|
3
|
-
"version": "0.1.
|
3
|
+
"version": "0.1.3-f6d5deb",
|
4
4
|
"description": "Rate Limit Nullifier for js-waku",
|
5
5
|
"types": "./dist/index.d.ts",
|
6
6
|
"module": "./dist/index.js",
|
@@ -29,11 +29,9 @@
|
|
29
29
|
"build:bundle": "rollup --config rollup.config.js",
|
30
30
|
"size": "npm run build && size-limit",
|
31
31
|
"fix": "run-s fix:*",
|
32
|
-
"fix:prettier": "prettier \"src/**/*.ts\" \"./*.json\" \"*.*js\" \".github/**/*.yml\" --write",
|
33
32
|
"fix:lint": "eslint src --ext .ts --ext .cjs --fix",
|
34
33
|
"test": "run-s test:*",
|
35
34
|
"test:lint": "eslint src --ext .ts",
|
36
|
-
"test:prettier": "prettier \"src/**/*.ts\" \"./*.json\" \"*.*js\" \".github/**/*.yml\" --list-different",
|
37
35
|
"test:spelling": "cspell \"{*.md,.github/*.md,src/**/*.ts}\"",
|
38
36
|
"test:tsc": "tsc -p tsconfig.dev.json",
|
39
37
|
"test:browser": "karma start karma.conf.cjs",
|
@@ -70,9 +68,8 @@
|
|
70
68
|
"@types/node": "^17.0.6",
|
71
69
|
"@types/tail": "^2.0.0",
|
72
70
|
"@types/uuid": "^8.3.0",
|
73
|
-
"@typescript-eslint/eslint-plugin": "^
|
74
|
-
"@typescript-eslint/parser": "^
|
75
|
-
"@waku/core": "^0.0.25",
|
71
|
+
"@typescript-eslint/eslint-plugin": "^6.6.0",
|
72
|
+
"@typescript-eslint/parser": "^6.21.0",
|
76
73
|
"@waku/interfaces": "^0.0.20",
|
77
74
|
"@waku/message-encryption": "^0.0.23",
|
78
75
|
"@web/rollup-plugin-import-meta-assets": "^1.0.7",
|
@@ -85,12 +82,12 @@
|
|
85
82
|
"chai-subset": "^1.6.0",
|
86
83
|
"cspell": "^5.14.0",
|
87
84
|
"deep-equal-in-any-order": "^2.0.6",
|
88
|
-
"eslint": "^8.
|
89
|
-
"eslint-config-prettier": "^
|
85
|
+
"eslint": "^8.56.0",
|
86
|
+
"eslint-config-prettier": "^9.0.0",
|
90
87
|
"eslint-plugin-eslint-comments": "^3.2.0",
|
91
|
-
"eslint-plugin-functional": "^
|
92
|
-
"eslint-plugin-import": "^2.
|
93
|
-
"eslint-plugin-prettier": "^
|
88
|
+
"eslint-plugin-functional": "^6.0.0",
|
89
|
+
"eslint-plugin-import": "^2.27.5",
|
90
|
+
"eslint-plugin-prettier": "^5.0.1",
|
94
91
|
"fast-check": "^2.25.0",
|
95
92
|
"gh-pages": "^3.2.3",
|
96
93
|
"husky": "^7.0.4",
|
@@ -98,26 +95,26 @@
|
|
98
95
|
"isomorphic-fetch": "^3.0.0",
|
99
96
|
"jsdom": "^19.0.0",
|
100
97
|
"jsdom-global": "^3.0.2",
|
101
|
-
"karma": "^6.
|
102
|
-
"karma-chrome-launcher": "^3.
|
98
|
+
"karma": "^6.4.2",
|
99
|
+
"karma-chrome-launcher": "^3.2.0",
|
100
|
+
"karma-firefox-launcher": "^2.1.2",
|
103
101
|
"karma-mocha": "^2.0.1",
|
104
|
-
"karma-
|
102
|
+
"karma-webkit-launcher": "^2.4.0",
|
103
|
+
"karma-webpack": "github:codymikol/karma-webpack#2337a82beb078c0d8e25ae8333a06249b8e72828",
|
105
104
|
"lint-staged": "^13.0.3",
|
106
105
|
"mocha": "10.1.0",
|
107
106
|
"npm-run-all": "^4.1.5",
|
108
107
|
"p-timeout": "^4.1.0",
|
109
|
-
"
|
108
|
+
"playwright": "^1.40.1",
|
110
109
|
"process": "^0.11.10",
|
111
|
-
"puppeteer": "^13.0.1",
|
112
|
-
"resolve-typescript-plugin": "^1.2.0",
|
113
110
|
"rollup": "^2.75.0",
|
114
111
|
"rollup-plugin-copy": "^3.4.0",
|
115
112
|
"size-limit": "^8.0.0",
|
116
113
|
"tail": "^2.2.0",
|
117
114
|
"ts-loader": "^9.3.1",
|
118
115
|
"ts-node": "^10.9.1",
|
119
|
-
"typedoc": "^0.
|
120
|
-
"typescript": "^
|
116
|
+
"typedoc": "^0.25.7",
|
117
|
+
"typescript": "^5.3.2"
|
121
118
|
},
|
122
119
|
"files": [
|
123
120
|
"dist",
|
@@ -133,19 +130,17 @@
|
|
133
130
|
"lint-staged": {
|
134
131
|
"*.ts": [
|
135
132
|
"eslint --fix"
|
136
|
-
],
|
137
|
-
"*.{ts,md,json,conf*.*js}": [
|
138
|
-
"prettier --write"
|
139
133
|
]
|
140
134
|
},
|
141
135
|
"dependencies": {
|
142
136
|
"@chainsafe/bls-keystore": "^3.0.0",
|
137
|
+
"@waku/core": "^0.0.25",
|
143
138
|
"@waku/utils": "^0.0.13",
|
144
139
|
"@waku/zerokit-rln-wasm": "^0.0.13",
|
140
|
+
"debug": "^4.3.4",
|
145
141
|
"ethereum-cryptography": "^2.1.2",
|
146
142
|
"ethers": "^5.7.2",
|
147
143
|
"lodash": "^4.17.21",
|
148
|
-
"rlnjs": "^3.2.3",
|
149
144
|
"uuid": "^9.0.1"
|
150
145
|
}
|
151
146
|
}
|
package/src/codec.ts
CHANGED
@@ -4,12 +4,13 @@ import type {
|
|
4
4
|
IEncoder,
|
5
5
|
IMessage,
|
6
6
|
IProtoMessage,
|
7
|
-
IRateLimitProof
|
7
|
+
IRateLimitProof
|
8
8
|
} from "@waku/interfaces";
|
9
9
|
import debug from "debug";
|
10
10
|
|
11
|
+
import type { IdentityCredential } from "./identity.js";
|
11
12
|
import { RlnMessage, toRLNSignal } from "./message.js";
|
12
|
-
import {
|
13
|
+
import { RLNInstance } from "./rln.js";
|
13
14
|
|
14
15
|
const log = debug("waku:rln:encoder");
|
15
16
|
|
@@ -44,7 +45,7 @@ export class RLNEncoder implements IEncoder {
|
|
44
45
|
|
45
46
|
private async generateProof(message: IMessage): Promise<IRateLimitProof> {
|
46
47
|
const signal = toRLNSignal(this.contentTopic, message);
|
47
|
-
const proof = await this.rlnInstance.generateRLNProof(
|
48
|
+
const proof = await this.rlnInstance.zerokit.generateRLNProof(
|
48
49
|
signal,
|
49
50
|
this.index,
|
50
51
|
message.timestamp,
|
@@ -85,7 +86,10 @@ export const createRLNEncoder = (options: RLNEncoderOptions): RLNEncoder => {
|
|
85
86
|
export class RLNDecoder<T extends IDecodedMessage>
|
86
87
|
implements IDecoder<RlnMessage<T>>
|
87
88
|
{
|
88
|
-
constructor(
|
89
|
+
constructor(
|
90
|
+
private rlnInstance: RLNInstance,
|
91
|
+
private decoder: IDecoder<T>
|
92
|
+
) {}
|
89
93
|
|
90
94
|
get pubsubTopic(): string {
|
91
95
|
return this.decoder.pubsubTopic;
|
package/src/identity.ts
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
import { buildBigIntFromUint8Array } from "./utils/index.js";
|
2
|
+
|
3
|
+
export class IdentityCredential {
|
4
|
+
constructor(
|
5
|
+
public readonly IDTrapdoor: Uint8Array,
|
6
|
+
public readonly IDNullifier: Uint8Array,
|
7
|
+
public readonly IDSecretHash: Uint8Array,
|
8
|
+
public readonly IDCommitment: Uint8Array,
|
9
|
+
public readonly IDCommitmentBigInt: bigint
|
10
|
+
) {}
|
11
|
+
|
12
|
+
static fromBytes(memKeys: Uint8Array): IdentityCredential {
|
13
|
+
const idTrapdoor = memKeys.subarray(0, 32);
|
14
|
+
const idNullifier = memKeys.subarray(32, 64);
|
15
|
+
const idSecretHash = memKeys.subarray(64, 96);
|
16
|
+
const idCommitment = memKeys.subarray(96);
|
17
|
+
const idCommitmentBigInt = buildBigIntFromUint8Array(idCommitment, 96);
|
18
|
+
|
19
|
+
return new IdentityCredential(
|
20
|
+
idTrapdoor,
|
21
|
+
idNullifier,
|
22
|
+
idSecretHash,
|
23
|
+
idCommitment,
|
24
|
+
idCommitmentBigInt
|
25
|
+
);
|
26
|
+
}
|
27
|
+
}
|
package/src/index.ts
CHANGED
@@ -2,19 +2,16 @@ import { RLNDecoder, RLNEncoder } from "./codec.js";
|
|
2
2
|
import {
|
3
3
|
RLN_REGISTRY_ABI,
|
4
4
|
RLN_STORAGE_ABI,
|
5
|
-
SEPOLIA_CONTRACT
|
6
|
-
} from "./
|
5
|
+
SEPOLIA_CONTRACT
|
6
|
+
} from "./contract/index.js";
|
7
|
+
import { RLNContract } from "./contract/index.js";
|
7
8
|
import { createRLN } from "./create.js";
|
9
|
+
import { IdentityCredential } from "./identity.js";
|
8
10
|
import { Keystore } from "./keystore/index.js";
|
9
|
-
import {
|
10
|
-
import {
|
11
|
-
IdentityCredential,
|
12
|
-
Proof,
|
13
|
-
ProofMetadata,
|
14
|
-
RLNInstance,
|
15
|
-
} from "./rln.js";
|
16
|
-
import { RLNContract } from "./rln_contract.js";
|
11
|
+
import { Proof } from "./proof.js";
|
12
|
+
import { RLNInstance } from "./rln.js";
|
17
13
|
import { MerkleRootTracker } from "./root_tracker.js";
|
14
|
+
import { extractMetaMaskSigner } from "./utils/index.js";
|
18
15
|
|
19
16
|
export {
|
20
17
|
createRLN,
|
@@ -22,7 +19,6 @@ export {
|
|
22
19
|
RLNInstance,
|
23
20
|
IdentityCredential,
|
24
21
|
Proof,
|
25
|
-
ProofMetadata,
|
26
22
|
RLNEncoder,
|
27
23
|
RLNDecoder,
|
28
24
|
MerkleRootTracker,
|
@@ -30,5 +26,5 @@ export {
|
|
30
26
|
RLN_STORAGE_ABI,
|
31
27
|
RLN_REGISTRY_ABI,
|
32
28
|
SEPOLIA_CONTRACT,
|
33
|
-
extractMetaMaskSigner
|
29
|
+
extractMetaMaskSigner
|
34
30
|
};
|
package/src/message.ts
CHANGED
@@ -1,12 +1,12 @@
|
|
1
1
|
import type {
|
2
2
|
IDecodedMessage,
|
3
3
|
IMessage,
|
4
|
-
IRateLimitProof
|
4
|
+
IRateLimitProof
|
5
5
|
} from "@waku/interfaces";
|
6
6
|
import * as utils from "@waku/utils/bytes";
|
7
7
|
|
8
|
-
import { epochBytesToInt } from "./epoch.js";
|
9
8
|
import { RLNInstance } from "./rln.js";
|
9
|
+
import { epochBytesToInt } from "./utils/index.js";
|
10
10
|
|
11
11
|
export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
|
12
12
|
const contentTopicBytes = utils.utf8ToBytes(contentTopic ?? "");
|
@@ -24,7 +24,7 @@ export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
|
|
24
24
|
|
25
25
|
public verify(roots: Uint8Array[]): boolean | undefined {
|
26
26
|
return this.rateLimitProof
|
27
|
-
? this.rlnInstance.verifyWithRoots(
|
27
|
+
? this.rlnInstance.zerokit.verifyWithRoots(
|
28
28
|
this.rateLimitProof,
|
29
29
|
toRLNSignal(this.msg.contentTopic, this.msg),
|
30
30
|
...roots
|
@@ -34,7 +34,7 @@ export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
|
|
34
34
|
|
35
35
|
public verifyNoRoot(): boolean | undefined {
|
36
36
|
return this.rateLimitProof
|
37
|
-
? this.rlnInstance.verifyWithNoRoot(
|
37
|
+
? this.rlnInstance.zerokit.verifyWithNoRoot(
|
38
38
|
this.rateLimitProof,
|
39
39
|
toRLNSignal(this.msg.contentTopic, this.msg)
|
40
40
|
) // this.rlnInstance.verifyRLNProof once issue status-im/nwaku#1248 is fixed
|
package/src/proof.ts
ADDED
@@ -0,0 +1,67 @@
|
|
1
|
+
import type { IRateLimitProof } from "@waku/interfaces";
|
2
|
+
|
3
|
+
import { concatenate, poseidonHash } from "./utils/index.js";
|
4
|
+
|
5
|
+
const proofOffset = 128;
|
6
|
+
const rootOffset = proofOffset + 32;
|
7
|
+
const epochOffset = rootOffset + 32;
|
8
|
+
const shareXOffset = epochOffset + 32;
|
9
|
+
const shareYOffset = shareXOffset + 32;
|
10
|
+
const nullifierOffset = shareYOffset + 32;
|
11
|
+
const rlnIdentifierOffset = nullifierOffset + 32;
|
12
|
+
|
13
|
+
class ProofMetadata {
|
14
|
+
constructor(
|
15
|
+
public readonly nullifier: Uint8Array,
|
16
|
+
public readonly shareX: Uint8Array,
|
17
|
+
public readonly shareY: Uint8Array,
|
18
|
+
public readonly externalNullifier: Uint8Array
|
19
|
+
) {}
|
20
|
+
}
|
21
|
+
|
22
|
+
export class Proof implements IRateLimitProof {
|
23
|
+
readonly proof: Uint8Array;
|
24
|
+
readonly merkleRoot: Uint8Array;
|
25
|
+
readonly epoch: Uint8Array;
|
26
|
+
readonly shareX: Uint8Array;
|
27
|
+
readonly shareY: Uint8Array;
|
28
|
+
readonly nullifier: Uint8Array;
|
29
|
+
readonly rlnIdentifier: Uint8Array;
|
30
|
+
|
31
|
+
constructor(proofBytes: Uint8Array) {
|
32
|
+
if (proofBytes.length < rlnIdentifierOffset) throw "invalid proof";
|
33
|
+
// parse the proof as proof<128> | share_y<32> | nullifier<32> | root<32> | epoch<32> | share_x<32> | rln_identifier<32>
|
34
|
+
this.proof = proofBytes.subarray(0, proofOffset);
|
35
|
+
this.merkleRoot = proofBytes.subarray(proofOffset, rootOffset);
|
36
|
+
this.epoch = proofBytes.subarray(rootOffset, epochOffset);
|
37
|
+
this.shareX = proofBytes.subarray(epochOffset, shareXOffset);
|
38
|
+
this.shareY = proofBytes.subarray(shareXOffset, shareYOffset);
|
39
|
+
this.nullifier = proofBytes.subarray(shareYOffset, nullifierOffset);
|
40
|
+
this.rlnIdentifier = proofBytes.subarray(
|
41
|
+
nullifierOffset,
|
42
|
+
rlnIdentifierOffset
|
43
|
+
);
|
44
|
+
}
|
45
|
+
|
46
|
+
extractMetadata(): ProofMetadata {
|
47
|
+
const externalNullifier = poseidonHash(this.epoch, this.rlnIdentifier);
|
48
|
+
return new ProofMetadata(
|
49
|
+
this.nullifier,
|
50
|
+
this.shareX,
|
51
|
+
this.shareY,
|
52
|
+
externalNullifier
|
53
|
+
);
|
54
|
+
}
|
55
|
+
}
|
56
|
+
|
57
|
+
export function proofToBytes(p: IRateLimitProof): Uint8Array {
|
58
|
+
return concatenate(
|
59
|
+
p.proof,
|
60
|
+
p.merkleRoot,
|
61
|
+
p.epoch,
|
62
|
+
p.shareX,
|
63
|
+
p.shareY,
|
64
|
+
p.nullifier,
|
65
|
+
p.rlnIdentifier
|
66
|
+
);
|
67
|
+
}
|