From a4b038730cf3c499a7a00dd1fdf34f96b15f8dbf Mon Sep 17 00:00:00 2001 From: Richard Ramos Date: Thu, 27 Jul 2023 15:51:31 -0400 Subject: [PATCH] feat(rln-relay): pass in index to keystore credentials --- cmd/waku/flags_rln.go | 12 +++++++++--- cmd/waku/node_rln.go | 4 +++- cmd/waku/options.go | 3 ++- examples/chat2/exec.go | 2 ++ examples/chat2/flags.go | 10 ++++++++-- examples/chat2/options.go | 2 ++ waku/v2/node/wakunode2_rln.go | 2 ++ waku/v2/node/wakuoptions.go | 1 + waku/v2/node/wakuoptions_rln.go | 4 +++- .../protocol/rln/group_manager/dynamic/dynamic.go | 13 +++++++++---- waku/v2/protocol/rln/onchain_test.go | 8 ++++---- 11 files changed, 45 insertions(+), 16 deletions(-) diff --git a/cmd/waku/flags_rln.go b/cmd/waku/flags_rln.go index e1ef58f7..a0b10dfd 100644 --- a/cmd/waku/flags_rln.go +++ b/cmd/waku/flags_rln.go @@ -17,10 +17,10 @@ func rlnFlags() []cli.Flag { Destination: &options.RLNRelay.Enable, }, &cli.IntFlag{ - Name: "rln-relay-membership-index", + Name: "rln-relay-membership-group-index", Value: 0, - Usage: "(experimental) the index of node in the rln-relay group: a value between 0-99 inclusive", - Destination: &options.RLNRelay.MembershipIndex, + Usage: "the index of credentials to use, within a specific rln membership set", + Destination: &options.RLNRelay.MembershipGroupIndex, }, &cli.StringFlag{ Name: "rln-relay-pubsub-topic", @@ -51,6 +51,12 @@ func rlnFlags() []cli.Flag { Usage: "Password for encrypting RLN credentials", Destination: &options.RLNRelay.CredentialsPassword, }, + &cli.IntFlag{ + Name: "rln-relay-membership-index", + Value: 0, + Usage: "the index of credentials to use", + Destination: &options.RLNRelay.CredentialsIndex, + }, // TODO: this is a good candidate option for subcommands // TODO: consider accepting a private key file and passwd &cli.GenericFlag{ diff --git a/cmd/waku/node_rln.go b/cmd/waku/node_rln.go index 282d5189..4696ee79 100644 --- a/cmd/waku/node_rln.go +++ b/cmd/waku/node_rln.go @@ -18,7 +18,7 @@ func checkForRLN(logger *zap.Logger, options Options, nodeOpts *[]node.WakuNodeO failOnErr(errors.New("relay not available"), "Could not enable RLN Relay") } if !options.RLNRelay.Dynamic { - *nodeOpts = append(*nodeOpts, node.WithStaticRLNRelay(options.RLNRelay.PubsubTopic, options.RLNRelay.ContentTopic, rln.MembershipIndex(options.RLNRelay.MembershipIndex), nil)) + *nodeOpts = append(*nodeOpts, node.WithStaticRLNRelay(options.RLNRelay.PubsubTopic, options.RLNRelay.ContentTopic, rln.MembershipIndex(options.RLNRelay.MembershipGroupIndex), nil)) } else { var ethPrivKey *ecdsa.PrivateKey @@ -31,7 +31,9 @@ func checkForRLN(logger *zap.Logger, options Options, nodeOpts *[]node.WakuNodeO options.RLNRelay.ContentTopic, options.RLNRelay.CredentialsPath, options.RLNRelay.CredentialsPassword, + options.RLNRelay.CredentialsIndex, options.RLNRelay.MembershipContractAddress, + rln.MembershipIndex(options.RLNRelay.MembershipGroupIndex), nil, options.RLNRelay.ETHClientAddress, ethPrivKey, diff --git a/cmd/waku/options.go b/cmd/waku/options.go index 998297ba..62b9c100 100644 --- a/cmd/waku/options.go +++ b/cmd/waku/options.go @@ -37,7 +37,8 @@ type RLNRelayOptions struct { Enable bool CredentialsPath string CredentialsPassword string - MembershipIndex int + CredentialsIndex int + MembershipGroupIndex int PubsubTopic string ContentTopic string Dynamic bool diff --git a/examples/chat2/exec.go b/examples/chat2/exec.go index 7f65e450..1e94ff8c 100644 --- a/examples/chat2/exec.go +++ b/examples/chat2/exec.go @@ -76,7 +76,9 @@ func execute(options Options) { options.RLNRelay.ContentTopic, options.RLNRelay.CredentialsPath, options.RLNRelay.CredentialsPassword, + options.RLNRelay.CredentialsIndex, options.RLNRelay.MembershipContractAddress, + uint(options.RLNRelay.MembershipIndex), spamHandler, options.RLNRelay.ETHClientAddress, options.RLNRelay.ETHPrivateKey, diff --git a/examples/chat2/flags.go b/examples/chat2/flags.go index cd670487..a767c934 100644 --- a/examples/chat2/flags.go +++ b/examples/chat2/flags.go @@ -194,11 +194,17 @@ func getFlags() []cli.Flag { Usage: "Enable spam protection through rln-relay", Destination: &options.RLNRelay.Enable, }, + &cli.IntFlag{ + Name: "rln-relay-membership-group-index", + Value: 0, + Usage: "the index of credentials to use, within a specific rln membership set", + Destination: &options.RLNRelay.MembershipGroupIndex, + }, &cli.IntFlag{ Name: "rln-relay-membership-index", Value: 0, - Usage: "(experimental) the index of node in the rln-relay group: a value between 0-99 inclusive", - Destination: &options.RLNRelay.MembershipIndex, + Usage: "the index of credentials to use", + Destination: &options.RLNRelay.CredentialsIndex, }, &cli.StringFlag{ Name: "rln-relay-pubsub-topic", diff --git a/examples/chat2/options.go b/examples/chat2/options.go index b790c111..76e637f9 100644 --- a/examples/chat2/options.go +++ b/examples/chat2/options.go @@ -31,6 +31,8 @@ type RLNRelayOptions struct { Enable bool CredentialsPath string CredentialsPassword string + CredentialsIndex int + MembershipGroupIndex int MembershipIndex int PubsubTopic string ContentTopic string diff --git a/waku/v2/node/wakunode2_rln.go b/waku/v2/node/wakunode2_rln.go index c49cd28e..6920a77d 100644 --- a/waku/v2/node/wakunode2_rln.go +++ b/waku/v2/node/wakunode2_rln.go @@ -49,8 +49,10 @@ func (w *WakuNode) mountRlnRelay(ctx context.Context) error { w.opts.rlnETHClientAddress, w.opts.rlnETHPrivateKey, w.opts.rlnMembershipContractAddress, + w.opts.rlnRelayMemIndex, w.opts.keystorePath, w.opts.keystorePassword, + w.opts.keystoreIndex, true, w.opts.rlnRegistrationHandler, w.log, diff --git a/waku/v2/node/wakuoptions.go b/waku/v2/node/wakuoptions.go index 2be021d1..5f6cf24a 100644 --- a/waku/v2/node/wakuoptions.go +++ b/waku/v2/node/wakuoptions.go @@ -102,6 +102,7 @@ type WakuNodeParameters struct { rlnETHClientAddress string keystorePath string keystorePassword string + keystoreIndex int rlnMembershipContractAddress common.Address rlnRegistrationHandler func(tx *types.Transaction) diff --git a/waku/v2/node/wakuoptions_rln.go b/waku/v2/node/wakuoptions_rln.go index 263a4f82..ecf81d73 100644 --- a/waku/v2/node/wakuoptions_rln.go +++ b/waku/v2/node/wakuoptions_rln.go @@ -27,12 +27,13 @@ func WithStaticRLNRelay(pubsubTopic string, contentTopic string, memberIndex r.M // WithDynamicRLNRelay enables the Waku V2 RLN protocol in onchain mode. // Requires the `gowaku_rln` build constrain (or the env variable RLN=true if building go-waku) -func WithDynamicRLNRelay(pubsubTopic string, contentTopic string, keystorePath string, keystorePassword string, membershipContract common.Address, spamHandler rln.SpamHandler, ethClientAddress string, ethPrivateKey *ecdsa.PrivateKey, registrationHandler rln.RegistrationHandler) WakuNodeOption { +func WithDynamicRLNRelay(pubsubTopic string, contentTopic string, keystorePath string, keystorePassword string, keystoreIndex int, membershipContract common.Address, membershipGroupIndex uint, spamHandler rln.SpamHandler, ethClientAddress string, ethPrivateKey *ecdsa.PrivateKey, registrationHandler rln.RegistrationHandler) WakuNodeOption { return func(params *WakuNodeParameters) error { params.enableRLN = true params.rlnRelayDynamic = true params.keystorePassword = keystorePassword params.keystorePath = keystorePath + params.keystoreIndex = keystoreIndex params.rlnRelayPubsubTopic = pubsubTopic params.rlnRelayContentTopic = contentTopic params.rlnSpamHandler = spamHandler @@ -40,6 +41,7 @@ func WithDynamicRLNRelay(pubsubTopic string, contentTopic string, keystorePath s params.rlnETHPrivateKey = ethPrivateKey params.rlnMembershipContractAddress = membershipContract params.rlnRegistrationHandler = registrationHandler + params.rlnRelayMemIndex = membershipGroupIndex return nil } } diff --git a/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go b/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go index 55ab0b38..d646137c 100644 --- a/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go +++ b/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go @@ -37,6 +37,7 @@ type DynamicGroupManager struct { membershipIndex *rln.MembershipIndex membershipContractAddress common.Address + membershipGroupIndex uint ethClientAddress string ethClient *ethclient.Client @@ -55,6 +56,7 @@ type DynamicGroupManager struct { saveKeystore bool keystorePath string keystorePassword string + keystoreIndex int rootTracker *group_manager.MerkleRootTracker } @@ -101,8 +103,10 @@ func NewDynamicGroupManager( ethClientAddr string, ethAccountPrivateKey *ecdsa.PrivateKey, memContractAddr common.Address, + membershipGroupIndex uint, keystorePath string, keystorePassword string, + keystoreIndex int, saveKeystore bool, registrationHandler RegistrationHandler, log *zap.Logger, @@ -122,6 +126,7 @@ func NewDynamicGroupManager( } return &DynamicGroupManager{ + membershipGroupIndex: membershipGroupIndex, membershipContractAddress: memContractAddr, ethClientAddress: ethClientAddr, ethAccountPrivateKey: ethAccountPrivateKey, @@ -130,6 +135,7 @@ func NewDynamicGroupManager( saveKeystore: saveKeystore, keystorePath: path, keystorePassword: password, + keystoreIndex: keystoreIndex, log: log, }, nil } @@ -193,10 +199,9 @@ func (gm *DynamicGroupManager) Start(ctx context.Context, rlnInstance *rln.RLN, return err } - // TODO: accept an index from the config - if len(credentials) != 0 { - gm.identityCredential = &credentials[0].IdentityCredential - gm.membershipIndex = &credentials[0].MembershipGroups[0].TreeIndex + if len(credentials) >= gm.keystoreIndex+1 { + gm.identityCredential = &credentials[gm.keystoreIndex].IdentityCredential + gm.membershipIndex = &credentials[gm.keystoreIndex].MembershipGroups[gm.membershipGroupIndex].TreeIndex } } diff --git a/waku/v2/protocol/rln/onchain_test.go b/waku/v2/protocol/rln/onchain_test.go index 4e80e815..96d6f3da 100644 --- a/waku/v2/protocol/rln/onchain_test.go +++ b/waku/v2/protocol/rln/onchain_test.go @@ -138,7 +138,7 @@ func (s *WakuRLNRelayDynamicSuite) TestDynamicGroupManagement() { rt, err := group_manager.NewMerkleRootTracker(5, rlnInstance) s.Require().NoError(err) - gm, err := dynamic.NewDynamicGroupManager(s.clientAddr, s.u1PrivKey, s.rlnAddr, "./test_onchain.json", "", false, nil, utils.Logger()) + gm, err := dynamic.NewDynamicGroupManager(s.clientAddr, s.u1PrivKey, s.rlnAddr, 0, "./test_onchain.json", "", 0, false, nil, utils.Logger()) s.Require().NoError(err) // initialize the WakuRLNRelay @@ -239,7 +239,7 @@ func (s *WakuRLNRelayDynamicSuite) TestMerkleTreeConstruction() { defer sub.Unsubscribe() // mount the rln relay protocol in the on-chain/dynamic mode - gm, err := dynamic.NewDynamicGroupManager(s.clientAddr, s.u1PrivKey, s.rlnAddr, "./test_onchain.json", "", false, nil, utils.Logger()) + gm, err := dynamic.NewDynamicGroupManager(s.clientAddr, s.u1PrivKey, s.rlnAddr, 0, "./test_onchain.json", "", 0, false, nil, utils.Logger()) s.Require().NoError(err) rlnRelay, err := New(relay, gm, RLNRELAY_PUBSUB_TOPIC, RLNRELAY_CONTENT_TOPIC, nil, timesource.NewDefaultClock(), utils.Logger()) @@ -283,7 +283,7 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() { defer sub1.Unsubscribe() // mount the rln relay protocol in the on-chain/dynamic mode - gm1, err := dynamic.NewDynamicGroupManager(s.clientAddr, s.u1PrivKey, s.rlnAddr, "./test_onchain.json", "", false, nil, utils.Logger()) + gm1, err := dynamic.NewDynamicGroupManager(s.clientAddr, s.u1PrivKey, s.rlnAddr, 0, "./test_onchain.json", "", 0, false, nil, utils.Logger()) s.Require().NoError(err) rlnRelay1, err := New(relay1, gm1, RLNRELAY_PUBSUB_TOPIC, RLNRELAY_CONTENT_TOPIC, nil, timesource.NewDefaultClock(), utils.Logger()) @@ -309,7 +309,7 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() { defer sub2.Unsubscribe() // mount the rln relay protocol in the on-chain/dynamic mode - gm2, err := dynamic.NewDynamicGroupManager(s.clientAddr, s.u2PrivKey, s.rlnAddr, "./test_onchain.json", "", false, nil, utils.Logger()) + gm2, err := dynamic.NewDynamicGroupManager(s.clientAddr, s.u2PrivKey, s.rlnAddr, 0, "./test_onchain.json", "", 0, false, nil, utils.Logger()) s.Require().NoError(err) rlnRelay2, err := New(relay2, gm2, RLNRELAY_PUBSUB_TOPIC, RLNRELAY_CONTENT_TOPIC, nil, timesource.NewDefaultClock(), utils.Logger())