From daaca4a4a38b748084569d2c2c1544d85b133b56 Mon Sep 17 00:00:00 2001 From: Sanaz Taheri Boshrooyeh <35961250+staheri14@users.noreply.github.com> Date: Thu, 14 Jul 2022 03:23:52 -0700 Subject: [PATCH] Feat(Rln relay): Enables and integrates onchain rln into chat2 (#1029) * adds wakunode2_types * removes unused imports * adds dynamic rln relay config options * deletes unused imports * replaces static rln-relay with both modes * introduces spamHandler parameter to mountRlnRelay * some debugging info * introduces the config option * adds the eth priv key to the register proc * refactors the code by adding eth private key to the relevant procs * revises config descriptions * minor wording fix * make a minor edit * further edits * adds Eth private key to the config options * calls spam handler when to rln credential is given * defaults to none for the spam handler * updates the membership fee to match the contract * fixes an issue for event subscription * adds eth private key to the transactions made in the tests * adds eth private key for the test of register process * adds comments * deletes lingering echo * displays registration info * removes an echo * removes Goerli testnet from the config descriptions * removes an excess space --- examples/v2/chat2.nim | 28 +++----------- examples/v2/config_chat2.nim | 37 ++++++++++++++++++- .../waku_rln_relay/waku_rln_relay_utils.nim | 13 ++++--- 3 files changed, 48 insertions(+), 30 deletions(-) diff --git a/examples/v2/chat2.nim b/examples/v2/chat2.nim index d58829bc2..b9d0fd9dd 100644 --- a/examples/v2/chat2.nim +++ b/examples/v2/chat2.nim @@ -508,29 +508,11 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} = echo "A spam message is found and discarded" chat.prompt = false showChatPrompt(chat) - - # set up rln relay inputs - let (groupOpt, memKeyPairOpt, memIndexOpt) = rlnRelayStaticSetUp(conf.rlnRelayMemIndex) - if memIndexOpt.isNone: - error "failed to mount WakuRLNRelay" - else: - # mount rlnrelay in offline mode (for now) - node.mountRlnRelayStatic(group = groupOpt.get(), memKeyPair = memKeyPairOpt.get(), memIndex = memIndexOpt.get(), pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic, spamHandler = some(spamHandler)) - - debug "membership id key", idkey=memKeyPairOpt.get().idKey.toHex - debug "membership id commitment key", idCommitmentkey=memKeyPairOpt.get().idCommitment.toHex - - # check the correct construction of the tree by comparing the calculated root against the expected root - # no error should happen as it is already captured in the unit tests - # TODO have added this check to account for unseen corner cases, will remove it later - let - root = node.wakuRlnRelay.rlnInstance.getMerkleRoot.value.toHex() - expectedRoot = STATIC_GROUP_MERKLE_ROOT - if root != expectedRoot: - error "root mismatch: something went wrong not in Merkle tree construction" - debug "the calculated root", root - debug "WakuRLNRelay is mounted successfully", pubsubtopic=conf.rlnRelayPubsubTopic, contentTopic=conf.rlnRelayContentTopic - + echo "rln-relay preparation is in progress ..." + node.mountRlnRelay(conf, some(spamHandler)) + echo "your membership index is: ", node.wakuRlnRelay.membershipIndex + echo "your rln identity key is: ", node.wakuRlnRelay.membershipKeyPair.idKey.toHex() + echo "your rln identity commitment key is: ", node.wakuRlnRelay.membershipKeyPair.idCommitment.toHex() await chat.readWriteLoop() diff --git a/examples/v2/config_chat2.nim b/examples/v2/config_chat2.nim index 3af169e89..7df585d1e 100644 --- a/examples/v2/config_chat2.nim +++ b/examples/v2/config_chat2.nim @@ -248,7 +248,42 @@ type desc: "the pubsub topic for which rln-relay gets enabled", defaultValue: "/waku/2/default-waku/proto" name: "rln-relay-pubsub-topic" }: string - + + rlnRelayDynamic* {. + desc: "Enable waku-rln-relay with on-chain dynamic group management: true|false", + defaultValue: false + name: "rln-relay-dynamic" }: bool + + rlnRelayIdKey* {. + desc: "Rln relay identity secret key as a Hex string", + defaultValue: "" + name: "rln-relay-id" }: string + + rlnRelayIdCommitmentKey* {. + desc: "Rln relay identity commitment key as a Hex string", + defaultValue: "" + name: "rln-relay-id-commitment" }: string + + rlnRelayEthAccount* {. + desc: "Ethereum account address for an Ethereum testnet", + defaultValue: "" + name: "eth-account-address" }: string + + rlnRelayEthAccountPrivKey* {. + desc: "Account private key for an Ethereum testnet", + defaultValue: "" + name: "eth-account-privatekey" }: string + + rlnRelayEthClientAddress* {. + desc: "Ethereum testnet client address e.g., ws://localhost:8540/", + defaultValue: "ws://localhost:8540/" + name: "eth-client-address" }: string + + rlnRelayEthMemContractAddress* {. + desc: "Address of membership contract on an Ethereum testnet", + defaultValue: "" + name: "eth-mem-contract-address" }: string + # NOTE: Keys are different in nim-libp2p proc parseCmdArg*(T: type crypto.PrivateKey, p: TaintedString): T = try: diff --git a/waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim b/waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim index b1f08b05d..1d48410d7 100644 --- a/waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim +++ b/waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim @@ -14,6 +14,7 @@ import rln, waku_rln_relay_types, ../../node/[wakunode2_types,config], + ../../../../../examples/v2/config_chat2, ../waku_message @@ -681,8 +682,8 @@ proc addRLNRelayValidator*(node: WakuNode, pubsubTopic: string, contentTopic: Co debug "A spam message is found! yay! discarding:", contentTopic=wakumessage.contentTopic, epoch=epoch, timestamp=wakumessage.timestamp, payload=payload trace "A spam message is found! yay! discarding:", proof=proof, root=root, shareX=shareX, shareY=shareY, nullifier=nullifier if spamHandler.isSome: - let handler = spamHandler.get - handler(wakumessage) + let handler = spamHandler.get + handler(wakumessage) return pubsub.ValidationResult.Reject # set a validator for the supplied pubsubTopic let pb = PubSub(node.wakuRelay) @@ -818,7 +819,7 @@ proc mountRlnRelayDynamic*(node: WakuNode, node.wakuRlnRelay = rlnPeer -proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf) {.raises: [Defect, ValueError, IOError, CatchableError].} = +proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf|Chat2Conf, spamHandler: Option[SpamHandler] = none(SpamHandler)) {.raises: [Defect, ValueError, IOError, CatchableError].} = if not conf.rlnRelayDynamic: info " setting up waku-rln-relay in on-chain mode... " # set up rln relay inputs @@ -827,7 +828,7 @@ proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf) {.raises: [Defect, Value error "failed to mount WakuRLNRelay" else: # mount rlnrelay in off-chain mode with a static group of users - node.mountRlnRelayStatic(group = groupOpt.get(), memKeyPair = memKeyPairOpt.get(), memIndex= memIndexOpt.get(), pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic) + node.mountRlnRelayStatic(group = groupOpt.get(), memKeyPair = memKeyPairOpt.get(), memIndex= memIndexOpt.get(), pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic, spamHandler = spamHandler) info "membership id key", idkey=memKeyPairOpt.get().idKey.toHex info "membership id commitment key", idCommitmentkey=memKeyPairOpt.get().idCommitment.toHex @@ -860,9 +861,9 @@ proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf) {.raises: [Defect, Value let keyPair = @[(rlnRelayId, rlnRelayIdCommitmentKey)] let memKeyPair = keyPair.toMembershipKeyPairs()[0] # mount the rln relay protocol in the on-chain/dynamic mode - waitFor node.mountRlnRelayDynamic(memContractAddr = ethMemContractAddress, ethClientAddr = ethClientAddr, memKeyPair = some(memKeyPair), memIndex = some(rlnRelayIndex), ethAccAddr = ethAccountAddr, ethAccountPrivKey = ethAccountPrivKey, pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic) + waitFor node.mountRlnRelayDynamic(memContractAddr = ethMemContractAddress, ethClientAddr = ethClientAddr, memKeyPair = some(memKeyPair), memIndex = some(rlnRelayIndex), ethAccAddr = ethAccountAddr, ethAccountPrivKey = ethAccountPrivKey, pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic, spamHandler = spamHandler) else: # no rln credential is provided # mount the rln relay protocol in the on-chain/dynamic mode - waitFor node.mountRlnRelayDynamic(memContractAddr = ethMemContractAddress, ethClientAddr = ethClientAddr, ethAccAddr = ethAccountAddr, ethAccountPrivKey = ethAccountPrivKey, pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic) + waitFor node.mountRlnRelayDynamic(memContractAddr = ethMemContractAddress, ethClientAddr = ethClientAddr, ethAccAddr = ethAccountAddr, ethAccountPrivKey = ethAccountPrivKey, pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic, spamHandler = spamHandler)