@waku/rln 0.1.1-fa49e29 → 0.1.2-6454446

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 (92) hide show
  1. package/README.md +26 -2
  2. package/bundle/assets/rln_wasm_bg-a503e304.wasm +0 -0
  3. package/bundle/index.js +69858 -45010
  4. package/dist/codec.d.ts +7 -4
  5. package/dist/codec.js +15 -5
  6. package/dist/codec.js.map +1 -1
  7. package/dist/{constants.js → contract/constants.js} +3 -3
  8. package/dist/contract/constants.js.map +1 -0
  9. package/dist/contract/index.d.ts +2 -0
  10. package/dist/contract/index.js +3 -0
  11. package/dist/contract/index.js.map +1 -0
  12. package/dist/{rln_contract.d.ts → contract/rln_contract.d.ts} +13 -11
  13. package/dist/{rln_contract.js → contract/rln_contract.js} +47 -28
  14. package/dist/contract/rln_contract.js.map +1 -0
  15. package/dist/create.d.ts +2 -0
  16. package/dist/create.js +8 -0
  17. package/dist/create.js.map +1 -0
  18. package/dist/identity.d.ts +9 -0
  19. package/dist/identity.js +24 -0
  20. package/dist/identity.js.map +1 -0
  21. package/dist/index.d.ts +8 -5
  22. package/dist/index.js +8 -12
  23. package/dist/index.js.map +1 -1
  24. package/dist/keystore/cipher.js +1 -1
  25. package/dist/keystore/cipher.js.map +1 -1
  26. package/dist/keystore/credential_validation_generated.js.map +1 -1
  27. package/dist/keystore/index.d.ts +2 -0
  28. package/dist/keystore/index.js.map +1 -1
  29. package/dist/keystore/keystore.d.ts +11 -11
  30. package/dist/keystore/keystore.js +33 -21
  31. package/dist/keystore/keystore.js.map +1 -1
  32. package/dist/keystore/keystore_validation_generated.js.map +1 -1
  33. package/dist/keystore/schema_validator.js.map +1 -1
  34. package/dist/keystore/types.d.ts +25 -5
  35. package/dist/message.d.ts +1 -1
  36. package/dist/message.js +7 -4
  37. package/dist/message.js.map +1 -1
  38. package/dist/proof.d.ts +21 -0
  39. package/dist/proof.js +49 -0
  40. package/dist/proof.js.map +1 -0
  41. package/dist/resources/verification_key.d.ts +9 -9
  42. package/dist/resources/witness_calculator.js.map +1 -0
  43. package/dist/rln.d.ts +55 -46
  44. package/dist/rln.js +135 -174
  45. package/dist/rln.js.map +1 -1
  46. package/dist/root_tracker.js +5 -2
  47. package/dist/root_tracker.js.map +1 -1
  48. package/dist/utils/bytes.d.ts +20 -0
  49. package/dist/{byte_utils.js → utils/bytes.js} +32 -1
  50. package/dist/utils/bytes.js.map +1 -0
  51. package/dist/utils/epoch.js.map +1 -0
  52. package/dist/utils/hash.d.ts +2 -0
  53. package/dist/utils/hash.js +13 -0
  54. package/dist/utils/hash.js.map +1 -0
  55. package/dist/utils/index.d.ts +4 -0
  56. package/dist/utils/index.js +5 -0
  57. package/dist/utils/index.js.map +1 -0
  58. package/dist/utils/metamask.d.ts +2 -0
  59. package/dist/utils/metamask.js +12 -0
  60. package/dist/utils/metamask.js.map +1 -0
  61. package/dist/zerokit.d.ts +19 -0
  62. package/dist/zerokit.js +105 -0
  63. package/dist/zerokit.js.map +1 -0
  64. package/package.json +22 -27
  65. package/src/codec.ts +18 -9
  66. package/src/create.ts +9 -0
  67. package/src/identity.ts +27 -0
  68. package/src/index.ts +10 -19
  69. package/src/message.ts +5 -5
  70. package/src/proof.ts +67 -0
  71. package/src/rln.ts +219 -260
  72. package/src/root_tracker.ts +4 -1
  73. package/src/zerokit.ts +181 -0
  74. package/bundle/assets/rln_wasm_bg-6f96f821.wasm +0 -0
  75. package/dist/.tsbuildinfo +0 -1
  76. package/dist/byte_utils.d.ts +0 -7
  77. package/dist/byte_utils.js.map +0 -1
  78. package/dist/constants.js.map +0 -1
  79. package/dist/epoch.js.map +0 -1
  80. package/dist/rln_contract.js.map +0 -1
  81. package/dist/witness_calculator.js.map +0 -1
  82. package/src/byte_utils.ts +0 -49
  83. package/src/constants.ts +0 -68
  84. package/src/epoch.ts +0 -30
  85. package/src/rln_contract.ts +0 -346
  86. package/src/witness_calculator.d.ts +0 -8
  87. package/src/witness_calculator.js +0 -335
  88. /package/dist/{constants.d.ts → contract/constants.d.ts} +0 -0
  89. /package/dist/{witness_calculator.d.ts → resources/witness_calculator.d.ts} +0 -0
  90. /package/dist/{witness_calculator.js → resources/witness_calculator.js} +0 -0
  91. /package/dist/{epoch.d.ts → utils/epoch.d.ts} +0 -0
  92. /package/dist/{epoch.js → utils/epoch.js} +0 -0
@@ -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;IAEJ,2BAA2B;QACzB,MAAM,OAAO,GAAG,UAAU,CAAC,6BAA6B,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,CAAC,iDAAiD;QACvH,OAAO,kBAAkB,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;IAC/C,CAAC;IAED,gCA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AE,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.1-fa49e29",
3
+ "version": "0.1.2-6454446",
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,11 +68,10 @@
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": "^5.8.1",
74
- "@typescript-eslint/parser": "^5.8.1",
75
- "@waku/core": "^0.0.18",
76
- "@waku/interfaces": "^0.0.13",
77
- "@waku/message-encryption": "^0.0.16",
71
+ "@typescript-eslint/eslint-plugin": "^6.6.0",
72
+ "@typescript-eslint/parser": "^6.21.0",
73
+ "@waku/interfaces": "^0.0.20",
74
+ "@waku/message-encryption": "^0.0.23",
78
75
  "@web/rollup-plugin-import-meta-assets": "^1.0.7",
79
76
  "ajv": "^8.12.0",
80
77
  "ajv-formats": "^2.1.1",
@@ -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.6.0",
89
- "eslint-config-prettier": "^8.3.0",
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": "^4.0.2",
92
- "eslint-plugin-import": "^2.25.3",
93
- "eslint-plugin-prettier": "^4.0.0",
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.3.12",
102
- "karma-chrome-launcher": "^3.1.0",
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-webpack": "^5.0.0",
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
- "prettier": "^2.1.1",
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.23.10",
120
- "typescript": "^4.5.5"
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",
143
- "@waku/utils": "^0.0.11",
144
- "@waku/zerokit-rln-wasm": "^0.0.10",
137
+ "@waku/core": "^0.0.25",
138
+ "@waku/utils": "^0.0.13",
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 { IdentityCredential, RLNInstance } from "./rln.js";
13
+ import { RLNInstance } from "./rln.js";
13
14
 
14
15
  const log = debug("waku:rln:encoder");
15
16
 
@@ -44,18 +45,19 @@ 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
-
48
- console.time("proof_gen_timer");
49
- const proof = await this.rlnInstance.generateRLNProof(
48
+ const proof = await this.rlnInstance.zerokit.generateRLNProof(
50
49
  signal,
51
50
  this.index,
52
51
  message.timestamp,
53
52
  this.idSecretHash
54
53
  );
55
- console.timeEnd("proof_gen_timer");
56
54
  return proof;
57
55
  }
58
56
 
57
+ get pubsubTopic(): string {
58
+ return this.encoder.pubsubTopic;
59
+ }
60
+
59
61
  get contentTopic(): string {
60
62
  return this.encoder.contentTopic;
61
63
  }
@@ -84,7 +86,14 @@ export const createRLNEncoder = (options: RLNEncoderOptions): RLNEncoder => {
84
86
  export class RLNDecoder<T extends IDecodedMessage>
85
87
  implements IDecoder<RlnMessage<T>>
86
88
  {
87
- constructor(private rlnInstance: RLNInstance, private decoder: IDecoder<T>) {}
89
+ constructor(
90
+ private rlnInstance: RLNInstance,
91
+ private decoder: IDecoder<T>
92
+ ) {}
93
+
94
+ get pubsubTopic(): string {
95
+ return this.decoder.pubsubTopic;
96
+ }
88
97
 
89
98
  get contentTopic(): string {
90
99
  return this.decoder.contentTopic;
@@ -97,11 +106,11 @@ export class RLNDecoder<T extends IDecodedMessage>
97
106
  }
98
107
 
99
108
  async fromProtoObj(
100
- pubSubTopic: string,
109
+ pubsubTopic: string,
101
110
  proto: IProtoMessage
102
111
  ): Promise<RlnMessage<T> | undefined> {
103
112
  const msg: T | undefined = await this.decoder.fromProtoObj(
104
- pubSubTopic,
113
+ pubsubTopic,
105
114
  proto
106
115
  );
107
116
  if (!msg) return;
package/src/create.ts ADDED
@@ -0,0 +1,9 @@
1
+ import type { RLNInstance } from "./rln.js";
2
+
3
+ export async function createRLN(): Promise<RLNInstance> {
4
+ // A dependency graph that contains any wasm must all be imported
5
+ // asynchronously. This file does the single async import, so
6
+ // that no one else needs to worry about it again.
7
+ const rlnModule = await import("./rln.js");
8
+ return rlnModule.create();
9
+ }
@@ -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);
18
+
19
+ return new IdentityCredential(
20
+ idTrapdoor,
21
+ idNullifier,
22
+ idSecretHash,
23
+ idCommitment,
24
+ idCommitmentBigInt
25
+ );
26
+ }
27
+ }
package/src/index.ts CHANGED
@@ -2,33 +2,23 @@ import { RLNDecoder, RLNEncoder } from "./codec.js";
2
2
  import {
3
3
  RLN_REGISTRY_ABI,
4
4
  RLN_STORAGE_ABI,
5
- SEPOLIA_CONTRACT,
6
- } from "./constants.js";
5
+ SEPOLIA_CONTRACT
6
+ } from "./contract/index.js";
7
+ import { RLNContract } from "./contract/index.js";
8
+ import { createRLN } from "./create.js";
9
+ import { IdentityCredential } from "./identity.js";
7
10
  import { Keystore } from "./keystore/index.js";
8
- import {
9
- IdentityCredential,
10
- Proof,
11
- ProofMetadata,
12
- RLNInstance,
13
- } from "./rln.js";
14
- import { RLNContract } from "./rln_contract.js";
11
+ import { Proof } from "./proof.js";
12
+ import { RLNInstance } from "./rln.js";
15
13
  import { MerkleRootTracker } from "./root_tracker.js";
16
-
17
- // reexport the create function, dynamically imported from rln.ts
18
- export async function create(): Promise<RLNInstance> {
19
- // A dependency graph that contains any wasm must all be imported
20
- // asynchronously. This file does the single async import, so
21
- // that no one else needs to worry about it again.
22
- const rlnModule = await import("./rln.js");
23
- return await rlnModule.create();
24
- }
14
+ import { extractMetaMaskSigner } from "./utils/index.js";
25
15
 
26
16
  export {
17
+ createRLN,
27
18
  Keystore,
28
19
  RLNInstance,
29
20
  IdentityCredential,
30
21
  Proof,
31
- ProofMetadata,
32
22
  RLNEncoder,
33
23
  RLNDecoder,
34
24
  MerkleRootTracker,
@@ -36,4 +26,5 @@ export {
36
26
  RLN_STORAGE_ABI,
37
27
  RLN_REGISTRY_ABI,
38
28
  SEPOLIA_CONTRACT,
29
+ extractMetaMaskSigner
39
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 ?? "");
@@ -14,7 +14,7 @@ export function toRLNSignal(contentTopic: string, msg: IMessage): Uint8Array {
14
14
  }
15
15
 
16
16
  export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
17
- public pubSubTopic = "";
17
+ public pubsubTopic = "";
18
18
 
19
19
  constructor(
20
20
  public rlnInstance: RLNInstance,
@@ -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
+ }