@waku/rln 0.1.1 → 0.1.2-3dc5908

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 (106) hide show
  1. package/README.md +26 -2
  2. package/bundle/assets/rln_wasm_bg-a503e304.wasm +0 -0
  3. package/bundle/index.js +56177 -8760
  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.d.ts → contract/constants.d.ts} +2 -2
  8. package/dist/contract/constants.js +67 -0
  9. package/dist/contract/constants.js.map +1 -0
  10. package/dist/contract/index.d.ts +2 -0
  11. package/dist/contract/index.js +3 -0
  12. package/dist/contract/index.js.map +1 -0
  13. package/dist/contract/rln_contract.d.ts +46 -0
  14. package/dist/contract/rln_contract.js +211 -0
  15. package/dist/contract/rln_contract.js.map +1 -0
  16. package/dist/create.d.ts +2 -0
  17. package/dist/create.js +8 -0
  18. package/dist/create.js.map +1 -0
  19. package/dist/identity.d.ts +9 -0
  20. package/dist/identity.js +24 -0
  21. package/dist/identity.js.map +1 -0
  22. package/dist/index.d.ts +9 -5
  23. package/dist/index.js +9 -12
  24. package/dist/index.js.map +1 -1
  25. package/dist/keystore/cipher.d.ts +4 -0
  26. package/dist/keystore/cipher.js +28 -0
  27. package/dist/keystore/cipher.js.map +1 -0
  28. package/dist/keystore/credential_validation_generated.d.ts +8 -0
  29. package/dist/keystore/credential_validation_generated.js +121 -0
  30. package/dist/keystore/credential_validation_generated.js.map +1 -0
  31. package/dist/keystore/index.d.ts +4 -0
  32. package/dist/keystore/index.js +3 -0
  33. package/dist/keystore/index.js.map +1 -0
  34. package/dist/keystore/keystore.d.ts +50 -0
  35. package/dist/keystore/keystore.js +205 -0
  36. package/dist/keystore/keystore.js.map +1 -0
  37. package/dist/keystore/keystore_validation_generated.d.ts +8 -0
  38. package/dist/keystore/keystore_validation_generated.js +75 -0
  39. package/dist/keystore/keystore_validation_generated.js.map +1 -0
  40. package/dist/keystore/schema_validator.d.ts +2 -0
  41. package/dist/keystore/schema_validator.js +18 -0
  42. package/dist/keystore/schema_validator.js.map +1 -0
  43. package/dist/keystore/types.d.ts +29 -0
  44. package/dist/keystore/types.js +2 -0
  45. package/dist/keystore/types.js.map +1 -0
  46. package/dist/message.d.ts +2 -1
  47. package/dist/message.js +10 -4
  48. package/dist/message.js.map +1 -1
  49. package/dist/proof.d.ts +21 -0
  50. package/dist/proof.js +49 -0
  51. package/dist/proof.js.map +1 -0
  52. package/dist/resources/verification_key.d.ts +9 -9
  53. package/dist/resources/witness_calculator.js.map +1 -0
  54. package/dist/rln.d.ts +55 -46
  55. package/dist/rln.js +136 -183
  56. package/dist/rln.js.map +1 -1
  57. package/dist/root_tracker.js +5 -2
  58. package/dist/root_tracker.js.map +1 -1
  59. package/dist/utils/bytes.d.ts +20 -0
  60. package/dist/utils/bytes.js +64 -0
  61. package/dist/utils/bytes.js.map +1 -0
  62. package/dist/utils/epoch.js.map +1 -0
  63. package/dist/utils/hash.d.ts +2 -0
  64. package/dist/utils/hash.js +13 -0
  65. package/dist/utils/hash.js.map +1 -0
  66. package/dist/utils/index.d.ts +4 -0
  67. package/dist/utils/index.js +5 -0
  68. package/dist/utils/index.js.map +1 -0
  69. package/dist/utils/metamask.d.ts +2 -0
  70. package/dist/utils/metamask.js +12 -0
  71. package/dist/utils/metamask.js.map +1 -0
  72. package/dist/zerokit.d.ts +19 -0
  73. package/dist/zerokit.js +105 -0
  74. package/dist/zerokit.js.map +1 -0
  75. package/package.json +38 -28
  76. package/src/codec.ts +18 -9
  77. package/src/create.ts +9 -0
  78. package/src/identity.ts +27 -0
  79. package/src/index.ts +16 -18
  80. package/src/message.ts +9 -5
  81. package/src/proof.ts +67 -0
  82. package/src/rln.ts +220 -270
  83. package/src/root_tracker.ts +4 -1
  84. package/src/zerokit.ts +181 -0
  85. package/bundle/assets/rln_wasm_bg-6f96f821.wasm +0 -0
  86. package/dist/.tsbuildinfo +0 -1
  87. package/dist/byte_utils.d.ts +0 -1
  88. package/dist/byte_utils.js +0 -24
  89. package/dist/byte_utils.js.map +0 -1
  90. package/dist/constants.js +0 -14
  91. package/dist/constants.js.map +0 -1
  92. package/dist/epoch.js.map +0 -1
  93. package/dist/rln_contract.d.ts +0 -34
  94. package/dist/rln_contract.js +0 -159
  95. package/dist/rln_contract.js.map +0 -1
  96. package/dist/witness_calculator.js.map +0 -1
  97. package/src/byte_utils.ts +0 -39
  98. package/src/constants.ts +0 -14
  99. package/src/epoch.ts +0 -30
  100. package/src/rln_contract.ts +0 -268
  101. package/src/witness_calculator.d.ts +0 -8
  102. package/src/witness_calculator.js +0 -335
  103. /package/dist/{witness_calculator.d.ts → resources/witness_calculator.d.ts} +0 -0
  104. /package/dist/{witness_calculator.js → resources/witness_calculator.js} +0 -0
  105. /package/dist/{epoch.d.ts → utils/epoch.d.ts} +0 -0
  106. /package/dist/{epoch.js → utils/epoch.js} +0 -0
@@ -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
@@ -1,32 +1,30 @@
1
1
  import { RLNDecoder, RLNEncoder } from "./codec.js";
2
- import { RLN_ABI, SEPOLIA_CONTRACT } from "./constants.js";
3
2
  import {
4
- IdentityCredential,
5
- Proof,
6
- ProofMetadata,
7
- RLNInstance,
8
- } from "./rln.js";
9
- import { RLNContract } from "./rln_contract.js";
3
+ RLN_REGISTRY_ABI,
4
+ RLN_STORAGE_ABI,
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";
10
+ import { Keystore } from "./keystore/index.js";
11
+ import { Proof } from "./proof.js";
12
+ import { RLNInstance } from "./rln.js";
10
13
  import { MerkleRootTracker } from "./root_tracker.js";
11
-
12
- // reexport the create function, dynamically imported from rln.ts
13
- export async function create(): Promise<RLNInstance> {
14
- // A dependency graph that contains any wasm must all be imported
15
- // asynchronously. This file does the single async import, so
16
- // that no one else needs to worry about it again.
17
- const rlnModule = await import("./rln.js");
18
- return await rlnModule.create();
19
- }
14
+ import { extractMetaMaskSigner } from "./utils/index.js";
20
15
 
21
16
  export {
17
+ createRLN,
18
+ Keystore,
22
19
  RLNInstance,
23
20
  IdentityCredential,
24
21
  Proof,
25
- ProofMetadata,
26
22
  RLNEncoder,
27
23
  RLNDecoder,
28
24
  MerkleRootTracker,
29
25
  RLNContract,
30
- RLN_ABI,
26
+ RLN_STORAGE_ABI,
27
+ RLN_REGISTRY_ABI,
31
28
  SEPOLIA_CONTRACT,
29
+ extractMetaMaskSigner
32
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
@@ -57,6 +57,10 @@ export class RlnMessage<T extends IDecodedMessage> implements IDecodedMessage {
57
57
  return this.msg.ephemeral;
58
58
  }
59
59
 
60
+ get meta(): Uint8Array | undefined {
61
+ return this.msg.meta;
62
+ }
63
+
60
64
  get epoch(): number | undefined {
61
65
  const bytes = this.msg.rateLimitProof?.epoch;
62
66
  if (!bytes) return;
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
+ }