Feat(Rln relay): enables relay-mode only (#1051)

* makes eth private key optional

* makes eth account private key field optional

* clarifies a comment
This commit is contained in:
Sanaz Taheri Boshrooyeh 2022-07-28 11:45:29 -07:00 committed by GitHub
parent 6c28e3b837
commit 79459d29ce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 23 deletions

View File

@ -293,7 +293,7 @@ procSuite "Waku-rln-relay":
var rlnPeer = WakuRLNRelay(membershipKeyPair: membershipKeyPair.get(),
membershipIndex: MembershipIndex(0),
ethClientAddress: ETH_CLIENT,
ethAccountPrivateKey: ethPrivKey,
ethAccountPrivateKey: some(ethPrivKey),
ethAccountAddress: ethacc,
membershipContractAddress: contractAddress)
@ -430,7 +430,7 @@ procSuite "Waku-rln-relay":
node.mountRelay(@[RLNRELAY_PUBSUB_TOPIC])
await node.mountRlnRelayDynamic(ethClientAddr = EthClient,
ethAccAddr = ethacc,
ethAccountPrivKey = ethPrivKey,
ethAccountPrivKeyOpt = some(ethPrivKey),
memContractAddr = contractAddress,
memKeyPair = keyPair1,
memIndex = some(MembershipIndex(0)),
@ -483,7 +483,7 @@ procSuite "Waku-rln-relay":
node.mountRelay(@[RLNRELAY_PUBSUB_TOPIC])
await node.mountRlnRelayDynamic(ethClientAddr = EthClient,
ethAccAddr = ethacc,
ethAccountPrivKey = ethPrivKey,
ethAccountPrivKeyOpt = some(ethPrivKey),
memContractAddr = contractAddress,
memKeyPair = none(MembershipKeyPair),
memIndex = none(MembershipIndex),
@ -496,7 +496,7 @@ procSuite "Waku-rln-relay":
node2.mountRelay(@[RLNRELAY_PUBSUB_TOPIC])
await node2.mountRlnRelayDynamic(ethClientAddr = EthClient,
ethAccAddr = ethacc,
ethAccountPrivKey = ethPrivKey,
ethAccountPrivKeyOpt = some(ethPrivKey),
memContractAddr = contractAddress,
memKeyPair = none(MembershipKeyPair),
memIndex = none(MembershipIndex),
@ -511,4 +511,4 @@ procSuite "Waku-rln-relay":
node2.wakuRlnRelay.membershipIndex == MembershipIndex(1)
await node.stop()
await node2.stop()
await node2.stop()

View File

@ -75,7 +75,7 @@ type WakuRLNRelay* = ref object
# this field is required for signing transactions
# TODO may need to erase this ethAccountPrivateKey when is not used
# TODO may need to make ethAccountPrivateKey mandatory
ethAccountPrivateKey*: PrivateKey
ethAccountPrivateKey*: Option[PrivateKey]
rlnInstance*: RLN[Bn256]
pubsubTopic*: string # the pubsub topic for which rln relay is mounted
# contentTopic should be of type waku_message.ContentTopic, however, due to recursive module dependency, the underlying type of ContentTopic is used instead

View File

@ -170,7 +170,7 @@ proc register*(rlnPeer: WakuRLNRelay): Future[bool] {.async.} =
## registers the public key of the rlnPeer which is rlnPeer.membershipKeyPair.publicKey
## into the membership contract whose address is in rlnPeer.membershipContractAddress
let pk = rlnPeer.membershipKeyPair.idCommitment
discard await register(idComm = pk, ethAccountAddress = rlnPeer.ethAccountAddress, ethAccountPrivKey = rlnPeer.ethAccountPrivateKey, ethClientAddress = rlnPeer.ethClientAddress, membershipContractAddress = rlnPeer.membershipContractAddress )
discard await register(idComm = pk, ethAccountAddress = rlnPeer.ethAccountAddress, ethAccountPrivKey = rlnPeer.ethAccountPrivateKey.get(), ethClientAddress = rlnPeer.ethClientAddress, membershipContractAddress = rlnPeer.membershipContractAddress )
return true
@ -745,7 +745,7 @@ proc mountRlnRelayStatic*(node: WakuNode,
proc mountRlnRelayDynamic*(node: WakuNode,
ethClientAddr: string = "",
ethAccAddr: web3.Address,
ethAccountPrivKey: keys.PrivateKey,
ethAccountPrivKeyOpt: Option[keys.PrivateKey],
memContractAddr: web3.Address,
memKeyPair: Option[MembershipKeyPair] = none(MembershipKeyPair),
memIndex: Option[MembershipIndex] = none(MembershipIndex),
@ -773,17 +773,20 @@ proc mountRlnRelayDynamic*(node: WakuNode,
var
keyPair: MembershipKeyPair
rlnIndex: MembershipIndex
if memKeyPair.isNone: # if non provided, create one and register to the contract
trace "no rln-relay key is provided, generating one"
let keyPairOpt = rln.membershipKeyGen()
doAssert(keyPairOpt.isSome)
keyPair = keyPairOpt.get()
# register the rln-relay peer to the membership contract
let regIndexRes = await register(idComm = keyPair.idCommitment, ethAccountAddress = ethAccAddr, ethAccountPrivKey = ethAccountPrivKey, ethClientAddress = ethClientAddr, membershipContractAddress = memContractAddr)
# check whether registration is done
doAssert(regIndexRes.isOk())
rlnIndex = regIndexRes.value
debug "peer is successfully registered into the membership contract"
if memKeyPair.isNone:
if ethAccountPrivKeyOpt.isSome: # if no rln credentials provided, and an ethereum private key is supplied, then create rln credentials and register to the membership contract
trace "no rln-relay key is provided, generating one"
let keyPairOpt = rln.membershipKeyGen()
doAssert(keyPairOpt.isSome)
keyPair = keyPairOpt.get()
# register the rln-relay peer to the membership contract
let regIndexRes = await register(idComm = keyPair.idCommitment, ethAccountAddress = ethAccAddr, ethAccountPrivKey = ethAccountPrivKeyOpt.get(), ethClientAddress = ethClientAddr, membershipContractAddress = memContractAddr)
# check whether registration is done
doAssert(regIndexRes.isOk())
rlnIndex = regIndexRes.value
debug "peer is successfully registered into the membership contract"
else: # if no eth private key is available, skip registration
debug "running waku-rln-relay in relay-only mode"
else:
keyPair = memKeyPair.get()
rlnIndex = memIndex.get()
@ -794,7 +797,7 @@ proc mountRlnRelayDynamic*(node: WakuNode,
membershipContractAddress: memContractAddr,
ethClientAddress: ethClientAddr,
ethAccountAddress: ethAccAddr,
ethAccountPrivateKey: ethAccountPrivKey,
ethAccountPrivateKey: ethAccountPrivKeyOpt,
rlnInstance: rln,
pubsubTopic: pubsubTopic,
contentTopic: contentTopic)
@ -849,21 +852,23 @@ proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf|Chat2Conf, spamHandler: O
# read related inputs to run rln-relay in on-chain mode and do type conversion when needed
let
ethAccountAddr = web3.fromHex(web3.Address, conf.rlnRelayEthAccount)
ethAccountPrivKey = keys.PrivateKey(SkSecretKey.fromHex(conf.rlnRelayEthAccountPrivKey).value)
ethClientAddr = conf.rlnRelayEthClientAddress
ethMemContractAddress = web3.fromHex(web3.Address, conf.rlnRelayEthMemContractAddress)
rlnRelayId = conf.rlnRelayIdKey
rlnRelayIdCommitmentKey = conf.rlnRelayIdCommitmentKey
rlnRelayIndex = conf.rlnRelayMemIndex
var ethAccountPrivKeyOpt = none(keys.PrivateKey)
if conf.rlnRelayEthAccountPrivKey != "":
ethAccountPrivKeyOpt = some(keys.PrivateKey(SkSecretKey.fromHex(conf.rlnRelayEthAccountPrivKey).value))
# check if the peer has provided its rln credentials
if rlnRelayIdCommitmentKey != "" and rlnRelayId != "":
# type conversation from hex strings to MembershipKeyPair
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, spamHandler = spamHandler)
waitFor node.mountRlnRelayDynamic(memContractAddr = ethMemContractAddress, ethClientAddr = ethClientAddr, memKeyPair = some(memKeyPair), memIndex = some(rlnRelayIndex), ethAccAddr = ethAccountAddr, ethAccountPrivKeyOpt = ethAccountPrivKeyOpt, 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, spamHandler = spamHandler)
waitFor node.mountRlnRelayDynamic(memContractAddr = ethMemContractAddress, ethClientAddr = ethClientAddr, ethAccAddr = ethAccountAddr, ethAccountPrivKeyOpt = ethAccountPrivKeyOpt, pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic, spamHandler = spamHandler)