2022-11-04 09:52:27 +00:00
|
|
|
when (NimMajor, NimMinor) < (1, 4):
|
|
|
|
{.push raises: [Defect].}
|
|
|
|
else:
|
|
|
|
{.push raises: [].}
|
2021-07-22 08:43:41 +00:00
|
|
|
|
2024-03-15 23:08:47 +00:00
|
|
|
import std/[options, tables, deques], stew/arrayops, chronos, web3, eth/keys
|
|
|
|
import ../waku_core, ../waku_keystore, ../common/protobuf
|
|
|
|
|
|
|
|
export waku_keystore, waku_core
|
2023-02-28 13:38:30 +00:00
|
|
|
|
2022-11-04 03:00:42 +00:00
|
|
|
type RlnRelayResult*[T] = Result[T, string]
|
|
|
|
|
2022-11-09 18:45:04 +00:00
|
|
|
## RLN is a Nim wrapper for the data types used in zerokit RLN
|
|
|
|
type RLN* {.incompleteStruct.} = object
|
|
|
|
type RLNResult* = RlnRelayResult[ptr RLN]
|
2021-06-08 18:56:32 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
type
|
2024-03-15 23:08:47 +00:00
|
|
|
MerkleNode* = array[32, byte]
|
|
|
|
# Each node of the Merkle tee is a Poseidon hash which is a 32 byte value
|
2022-05-10 21:09:18 +00:00
|
|
|
Nullifier* = array[32, byte]
|
|
|
|
Epoch* = array[32, byte]
|
2022-10-04 20:20:44 +00:00
|
|
|
RlnIdentifier* = array[32, byte]
|
2022-11-09 18:45:04 +00:00
|
|
|
ZKSNARK* = array[128, byte]
|
2022-08-05 20:58:19 +00:00
|
|
|
|
2024-02-01 18:56:47 +00:00
|
|
|
when defined(rln_v2):
|
|
|
|
type
|
|
|
|
MessageId* = uint64
|
|
|
|
ExternalNullifier* = array[32, byte]
|
|
|
|
|
2024-02-09 11:01:45 +00:00
|
|
|
type RateCommitment* = object
|
|
|
|
idCommitment*: IDCommitment
|
|
|
|
userMessageLimit*: UserMessageLimit
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2021-06-08 18:56:32 +00:00
|
|
|
# Custom data types defined for waku rln relay -------------------------
|
2022-10-04 20:20:44 +00:00
|
|
|
type RateLimitProof* = object
|
|
|
|
## RateLimitProof holds the public inputs to rln circuit as
|
|
|
|
## defined in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Public-Inputs
|
|
|
|
## the `proof` field carries the actual zkSNARK proof
|
|
|
|
proof*: ZKSNARK
|
|
|
|
## the root of Merkle tree used for the generation of the `proof`
|
|
|
|
merkleRoot*: MerkleNode
|
|
|
|
## shareX and shareY are shares of user's identity key
|
|
|
|
## these shares are created using Shamir secret sharing scheme
|
|
|
|
## see details in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Linear-Equation-amp-SSS
|
|
|
|
shareX*: MerkleNode
|
|
|
|
shareY*: MerkleNode
|
|
|
|
## nullifier enables linking two messages published during the same epoch
|
|
|
|
## see details in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Nullifiers
|
|
|
|
nullifier*: Nullifier
|
2024-02-01 18:56:47 +00:00
|
|
|
## the epoch used for the generation of the `proof`
|
|
|
|
epoch*: Epoch
|
2022-10-04 20:20:44 +00:00
|
|
|
## Application specific RLN Identifier
|
|
|
|
rlnIdentifier*: RlnIdentifier
|
2024-02-01 18:56:47 +00:00
|
|
|
when defined(rln_v2):
|
|
|
|
## the external nullifier used for the generation of the `proof` (derived from poseidon([epoch, rln_identifier]))
|
|
|
|
externalNullifier*: ExternalNullifier
|
2022-08-05 20:58:19 +00:00
|
|
|
|
2021-11-23 22:48:40 +00:00
|
|
|
type ProofMetadata* = object
|
|
|
|
nullifier*: Nullifier
|
|
|
|
shareX*: MerkleNode
|
|
|
|
shareY*: MerkleNode
|
2023-03-13 14:39:33 +00:00
|
|
|
externalNullifier*: Nullifier
|
2021-11-23 22:48:40 +00:00
|
|
|
|
2023-02-28 13:38:30 +00:00
|
|
|
type
|
2022-11-09 18:45:04 +00:00
|
|
|
MessageValidationResult* {.pure.} = enum
|
2024-03-15 23:08:47 +00:00
|
|
|
Valid
|
|
|
|
Invalid
|
2022-11-09 18:45:04 +00:00
|
|
|
Spam
|
2024-03-15 23:08:47 +00:00
|
|
|
|
2022-11-09 18:45:04 +00:00
|
|
|
MerkleNodeResult* = RlnRelayResult[MerkleNode]
|
|
|
|
RateLimitProofResult* = RlnRelayResult[RateLimitProof]
|
2023-02-28 13:38:30 +00:00
|
|
|
|
2021-11-23 22:48:40 +00:00
|
|
|
# Protobufs enc and init
|
2021-10-20 00:37:29 +00:00
|
|
|
proc init*(T: type RateLimitProof, buffer: seq[byte]): ProtoResult[T] =
|
|
|
|
var nsp: RateLimitProof
|
2024-02-01 18:56:47 +00:00
|
|
|
|
2021-10-20 00:37:29 +00:00
|
|
|
let pb = initProtoBuffer(buffer)
|
|
|
|
|
|
|
|
var proof: seq[byte]
|
2024-03-15 23:08:47 +00:00
|
|
|
discard ?pb.getField(1, proof)
|
2021-10-20 00:37:29 +00:00
|
|
|
discard nsp.proof.copyFrom(proof)
|
|
|
|
|
|
|
|
var merkleRoot: seq[byte]
|
2024-03-15 23:08:47 +00:00
|
|
|
discard ?pb.getField(2, merkleRoot)
|
2021-10-20 00:37:29 +00:00
|
|
|
discard nsp.merkleRoot.copyFrom(merkleRoot)
|
|
|
|
|
|
|
|
var epoch: seq[byte]
|
2024-03-15 23:08:47 +00:00
|
|
|
discard ?pb.getField(3, epoch)
|
2021-10-20 00:37:29 +00:00
|
|
|
discard nsp.epoch.copyFrom(epoch)
|
|
|
|
|
|
|
|
var shareX: seq[byte]
|
2024-03-15 23:08:47 +00:00
|
|
|
discard ?pb.getField(4, shareX)
|
2021-10-20 00:37:29 +00:00
|
|
|
discard nsp.shareX.copyFrom(shareX)
|
|
|
|
|
|
|
|
var shareY: seq[byte]
|
2024-03-15 23:08:47 +00:00
|
|
|
discard ?pb.getField(5, shareY)
|
2021-10-20 00:37:29 +00:00
|
|
|
discard nsp.shareY.copyFrom(shareY)
|
|
|
|
|
|
|
|
var nullifier: seq[byte]
|
2024-03-15 23:08:47 +00:00
|
|
|
discard ?pb.getField(6, nullifier)
|
2021-10-20 00:37:29 +00:00
|
|
|
discard nsp.nullifier.copyFrom(nullifier)
|
2023-02-28 13:38:30 +00:00
|
|
|
|
2022-10-04 20:20:44 +00:00
|
|
|
var rlnIdentifier: seq[byte]
|
2024-03-15 23:08:47 +00:00
|
|
|
discard ?pb.getField(7, rlnIdentifier)
|
2022-10-04 20:20:44 +00:00
|
|
|
discard nsp.rlnIdentifier.copyFrom(rlnIdentifier)
|
2022-08-05 20:58:19 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
return ok(nsp)
|
2021-10-20 00:37:29 +00:00
|
|
|
|
2022-05-10 21:09:18 +00:00
|
|
|
proc encode*(nsp: RateLimitProof): ProtoBuffer =
|
2021-10-20 00:37:29 +00:00
|
|
|
var output = initProtoBuffer()
|
|
|
|
|
2022-06-16 14:04:47 +00:00
|
|
|
output.write3(1, nsp.proof)
|
|
|
|
output.write3(2, nsp.merkleRoot)
|
|
|
|
output.write3(3, nsp.epoch)
|
|
|
|
output.write3(4, nsp.shareX)
|
|
|
|
output.write3(5, nsp.shareY)
|
|
|
|
output.write3(6, nsp.nullifier)
|
2022-10-04 20:20:44 +00:00
|
|
|
output.write3(7, nsp.rlnIdentifier)
|
2022-08-05 20:58:19 +00:00
|
|
|
|
2022-06-16 14:04:47 +00:00
|
|
|
output.finish3()
|
2023-02-28 13:38:30 +00:00
|
|
|
return output
|
|
|
|
|
|
|
|
type
|
2024-03-15 23:08:47 +00:00
|
|
|
SpamHandler* =
|
|
|
|
proc(wakuMessage: WakuMessage): void {.gcsafe, closure, raises: [Defect].}
|
|
|
|
RegistrationHandler* =
|
|
|
|
proc(txHash: string): void {.gcsafe, closure, raises: [Defect].}
|