Skip to content

Commit

Permalink
Rln Credentials Merged.
Browse files Browse the repository at this point in the history
Reading credentials from file abstracted away.
  • Loading branch information
kgrgpg committed Jul 24, 2022
1 parent b375418 commit 39a30ec
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 30 deletions.
3 changes: 1 addition & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -37,5 +37,4 @@ node_modules/

# Ignore Jetbrains IDE files
.idea/
keyPair.txt
rlnIndex.txt
rlnCredentials.txt
9 changes: 7 additions & 2 deletions waku/v2/protocol/waku_rln_relay/waku_rln_relay_types.nim
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ type MembershipKeyPair* = object
# more details in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Membership
idCommitment*: IDCommitment

type MembershipIndex* = uint

type RlnMembershipCredentials* = object
membershipKeyPair*: MembershipKeyPair
rlnIndex*: MembershipIndex

type RateLimitProof* = object
## RateLimitProof holds the public inputs to rln circuit as
## defined in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Public-Inputs
Expand All @@ -56,8 +62,6 @@ type RateLimitProof* = object
## see details in https://hackmd.io/tMTLMYmTR5eynw2lwK9n1w?view#Nullifiers
nullifier*: Nullifier

type MembershipIndex* = uint

type ProofMetadata* = object
nullifier*: Nullifier
shareX*: MerkleNode
Expand Down Expand Up @@ -91,6 +95,7 @@ type MessageValidationResult* {.pure.} = enum
const
KEYPAIR_FILEPATH* = "keyPair.txt"
RLN_INDEX_FILEPATH* = "rlnIndex.txt"
RLN_CREDENTIALS_FILEPATH* = "rlnCredentials.txt"

# inputs of the membership contract constructor
# TODO may be able to make these constants private and put them inside the waku_rln_relay_utils
Expand Down
48 changes: 22 additions & 26 deletions waku/v2/protocol/waku_rln_relay/waku_rln_relay_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -791,11 +791,11 @@ proc mountRlnRelayDynamic*(node: WakuNode,
keyPair = memKeyPair.get()
rlnIndex = memIndex.get()

#Write KeyPair
writeFile(KEYPAIR_FILEPATH, pretty(%keyPair))
var
rlnMembershipCredentials = RlnMembershipCredentials(membershipKeyPair: keyPair, rlnIndex: rlnIndex)

#Write rlnIndex
writeFile(RLN_INDEX_FILEPATH, pretty(%rlnIndex))
#Write RLN credentials
writeFile(RLN_CREDENTIALS_FILEPATH, pretty(%rlnMembershipCredentials))

#Since the files are stored as a raw text file, it is highly susceptible to theft.
#The files needs some encryption to resolve this.
Expand Down Expand Up @@ -830,6 +830,20 @@ proc mountRlnRelayDynamic*(node: WakuNode,

node.wakuRlnRelay = rlnPeer

proc readPersistentRlnCredentials*() : RlnMembershipCredentials {.raises: [Defect, OSError, IOError, Exception].} =
info "keyPair and rlnIndex exist"
#With regards to printing the keys, it is purely for debugging purposes so that the user becomes explicitly aware of the current keys in use when nwaku is started.
#Note that this is only until the RLN contract being used is the one deployed on Goerli testnet.
#These prints need to omitted once RLN contract is deployed on Ethereum mainnet and using valuable funds for staking.

let entireRlnCredentialsFile = readFile(RLN_CREDENTIALS_FILEPATH)

let jsonObject = parseJson(entireRlnCredentialsFile)
let deserializedRlnCredentials = to(jsonObject, RlnMembershipCredentials)

info "Deserialized Rln credentials", rlnCredentials=deserializedRlnCredentials
result = deserializedRlnCredentials

proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf) {.raises: [Defect, ValueError, IOError, CatchableError, Exception].} =
if not conf.rlnRelayDynamic:
info " setting up waku-rln-relay in on-chain mode... "
Expand Down Expand Up @@ -873,29 +887,11 @@ proc mountRlnRelay*(node: WakuNode, conf: WakuNodeConf) {.raises: [Defect, Value
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)
elif fileExists("keyPair.txt") and fileExists("rlnIndex.txt"):
info "keyPair and rlnIndex files exist"
#With regards to printing the keys, it is purely for debugging purposes so that the user becomes explicitly aware of the current keys in use when nwaku is started.
#Note that this is only until the RLN contract being used is the one deployed on Goerli testnet.
#These prints need to omitted once RLN contract is deployed on Ethereum mainnet and using valuable funds for staking.

let entireKeyPairFile = readFile(KEYPAIR_FILEPATH)

let jsonObjectKeyPair = parseJson(entireKeyPairFile)
let deserializedKeyPair = to(jsonObjectKeyPair, MembershipKeyPair)

info "Deserialized key pair", keyPair=deserializedKeyPair


let entireRlnIndexFile = readFile(RLN_INDEX_FILEPATH)

let jsonObjectRlnIndex = parseJson(entireRlnIndexFile)
let deserializedRlnIndex = to(jsonObjectRlnIndex, MembershipIndex)

info "Deserialized rln index", rlnIndex=deserializedRlnIndex
elif fileExists(RLN_CREDENTIALS_FILEPATH):
var credentials = readPersistentRlnCredentials()
waitFor node.mountRlnRelayDynamic(memContractAddr = ethMemContractAddress, ethClientAddr = ethClientAddr,
memKeyPair = some(deserializedKeyPair), memIndex = some(deserializedRlnIndex), ethAccAddr = ethAccountAddr,
ethAccountPrivKey = ethAccountPrivKey, pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic)
memKeyPair = some(credentials.membershipKeyPair), memIndex = some(credentials.rlnIndex), ethAccAddr = ethAccountAddr,
ethAccountPrivKey = ethAccountPrivKey, pubsubTopic = conf.rlnRelayPubsubTopic, contentTopic = conf.rlnRelayContentTopic)
else:
# no rln credential is provided
# mount the rln relay protocol in the on-chain/dynamic mode
Expand Down

0 comments on commit 39a30ec

Please sign in to comment.