Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rln-relay): use the only key from keystore if only 1 exists #708

Merged
merged 1 commit into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 5 additions & 5 deletions .codeclimate.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
plugins:
golint:
enabled: true
# golint:
# enabled: true
gofmt:
enabled: true
govet:
enabled: true
# golangci-lint:
#enabled: true
# golangci-lint:
# enabled: true
exclude_patterns:
- "."
- "**/*.pb.go"
- "**/rln/contracts/*.go"
- "**/bindata.go"
- "./examples/waku-csharp"
- "./examples/swift-waku"
- "./examples/swift-waku"
11 changes: 6 additions & 5 deletions cmd/waku/flags_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ func rlnFlags() []cli.Flag {
Usage: "Enable spam protection through rln-relay",
Destination: &options.RLNRelay.Enable,
},
&cli.UintFlag{
Name: "rln-relay-membership-index",
Value: 0,
Usage: "the index of credentials to use",
Destination: &options.RLNRelay.MembershipIndex,
&cli.GenericFlag{
Name: "rln-relay-cred-index",
Usage: "the index of the onchain commitment to use",
Value: &wcli.OptionalUint{
Value: &options.RLNRelay.MembershipIndex,
},
},
&cli.BoolFlag{
Name: "rln-relay-dynamic",
Expand Down
4 changes: 2 additions & 2 deletions cmd/waku/node_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func checkForRLN(logger *zap.Logger, options NodeOptions, nodeOpts *[]node.WakuN
failOnErr(errors.New("relay not available"), "Could not enable RLN Relay")
}
if !options.RLNRelay.Dynamic {
*nodeOpts = append(*nodeOpts, node.WithStaticRLNRelay(rln.MembershipIndex(options.RLNRelay.MembershipIndex), nil))
*nodeOpts = append(*nodeOpts, node.WithStaticRLNRelay((*rln.MembershipIndex)(options.RLNRelay.MembershipIndex), nil))
} else {
// TODO: too many parameters in this function
// consider passing a config struct instead
Expand All @@ -26,7 +26,7 @@ func checkForRLN(logger *zap.Logger, options NodeOptions, nodeOpts *[]node.WakuN
options.RLNRelay.CredentialsPassword,
options.RLNRelay.TreePath,
options.RLNRelay.MembershipContractAddress,
rln.MembershipIndex(options.RLNRelay.MembershipIndex),
options.RLNRelay.MembershipIndex,
nil,
options.RLNRelay.ETHClientAddress,
))
Expand Down
2 changes: 1 addition & 1 deletion cmd/waku/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ type RLNRelayOptions struct {
CredentialsPath string
CredentialsPassword string
TreePath string
MembershipIndex uint
MembershipIndex *uint
Dynamic bool
ETHClientAddress string
MembershipContractAddress common.Address
Expand Down
4 changes: 2 additions & 2 deletions examples/chat2/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ func execute(options Options) {
options.RLNRelay.CredentialsPassword,
"", // Will use default tree path
options.RLNRelay.MembershipContractAddress,
uint(options.RLNRelay.MembershipIndex),
options.RLNRelay.MembershipIndex,
spamHandler,
options.RLNRelay.ETHClientAddress,
))
} else {
opts = append(opts, node.WithStaticRLNRelay(
uint(options.RLNRelay.MembershipIndex),
options.RLNRelay.MembershipIndex,
spamHandler))
}
}
Expand Down
14 changes: 7 additions & 7 deletions examples/chat2/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,7 @@ func getFlags() []cli.Flag {

testCT, err := protocol.NewContentTopic("toy-chat", 3, "mingde", "proto")
if err != nil {
fmt.Println("Invalid contentTopic")
return nil
panic("invalid contentTopic")
}
testnetContentTopic := testCT.String()

Expand Down Expand Up @@ -190,11 +189,12 @@ func getFlags() []cli.Flag {
Usage: "Enable spam protection through rln-relay",
Destination: &options.RLNRelay.Enable,
},
&cli.UintFlag{
Name: "rln-relay-membership-index",
Value: 0,
Usage: "the index of credentials to use",
Destination: &options.RLNRelay.MembershipIndex,
&cli.GenericFlag{
Name: "rln-relay-cred-index",
Usage: "the index of the onchain commitment to use",
Value: &wcli.OptionalUint{
Value: &options.RLNRelay.MembershipIndex,
},
},
&cli.BoolFlag{
Name: "rln-relay-dynamic",
Expand Down
2 changes: 1 addition & 1 deletion examples/chat2/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type RLNRelayOptions struct {
Enable bool
CredentialsPath string
CredentialsPassword string
MembershipIndex uint
MembershipIndex *uint
Dynamic bool
ETHClientAddress string
MembershipContractAddress common.Address
Expand Down
31 changes: 31 additions & 0 deletions waku/cliutils/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"crypto/ecdsa"
"errors"
"fmt"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -73,3 +74,33 @@ func (v *ChoiceValue) String() string {
}
return *v.Value
}

// OptionalUint represents a urfave/cli flag to store uint values that can be
// optionally set and not have any default value assigned to it
type OptionalUint struct {
Value **uint
}

// Set assigns a value to the flag only if it represents a valid uint value
func (v *OptionalUint) Set(value string) error {
if value != "" {
uintVal, err := strconv.ParseUint(value, 10, 0)
if err != nil {
return err
}
uVal := uint(uintVal)
*v.Value = &uVal
} else {
v.Value = nil
}

return nil
}

// String returns the string representation of the OptionalUint flag, if set
func (v *OptionalUint) String() string {
if v.Value == nil {
return ""
}
return fmt.Sprintf("%d", *v.Value)
}
10 changes: 7 additions & 3 deletions waku/v2/node/wakunode2_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,14 +38,18 @@ func (w *WakuNode) setupRLNRelay() error {
if !w.opts.rlnRelayDynamic {
w.log.Info("setting up waku-rln-relay in off-chain mode")

index := uint(0)
if w.opts.rlnRelayMemIndex != nil {
index = *w.opts.rlnRelayMemIndex
}

// set up rln relay inputs
groupKeys, idCredential, err := static.Setup(w.opts.rlnRelayMemIndex)
groupKeys, idCredential, err := static.Setup(index)
if err != nil {
return err
}

groupManager, err = static.NewStaticGroupManager(groupKeys, idCredential, w.opts.rlnRelayMemIndex, rlnInstance,
rootTracker, w.log)
groupManager, err = static.NewStaticGroupManager(groupKeys, idCredential, index, rlnInstance, rootTracker, w.log)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion waku/v2/node/wakuoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ type WakuNodeParameters struct {
enablePeerExchange bool

enableRLN bool
rlnRelayMemIndex uint
rlnRelayMemIndex *uint
rlnRelayDynamic bool
rlnSpamHandler func(message *pb.WakuMessage) error
rlnETHClientAddress string
Expand Down
4 changes: 2 additions & 2 deletions waku/v2/node/wakuoptions_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (

// WithStaticRLNRelay enables the Waku V2 RLN protocol in offchain mode
// Requires the `gowaku_rln` build constrain (or the env variable RLN=true if building go-waku)
func WithStaticRLNRelay(memberIndex r.MembershipIndex, spamHandler rln.SpamHandler) WakuNodeOption {
func WithStaticRLNRelay(memberIndex *r.MembershipIndex, spamHandler rln.SpamHandler) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableRLN = true
params.rlnRelayDynamic = false
Expand All @@ -23,7 +23,7 @@ func WithStaticRLNRelay(memberIndex r.MembershipIndex, spamHandler rln.SpamHandl

// 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(keystorePath string, keystorePassword string, treePath string, membershipContract common.Address, membershipIndex uint, spamHandler rln.SpamHandler, ethClientAddress string) WakuNodeOption {
func WithDynamicRLNRelay(keystorePath string, keystorePassword string, treePath string, membershipContract common.Address, membershipIndex *uint, spamHandler rln.SpamHandler, ethClientAddress string) WakuNodeOption {
return func(params *WakuNodeParameters) error {
params.enableRLN = true
params.rlnRelayDynamic = true
Expand Down
20 changes: 11 additions & 9 deletions waku/v2/protocol/rln/group_manager/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,9 @@ type DynamicGroupManager struct {

lastBlockProcessed uint64

appKeystore *keystore.AppKeystore
keystorePassword string
appKeystore *keystore.AppKeystore
keystorePassword string
membershipIndexToLoad *uint
}

func (gm *DynamicGroupManager) handler(events []*contracts.RLNMemberRegistered) error {
Expand Down Expand Up @@ -104,7 +105,7 @@ type RegistrationHandler = func(tx *types.Transaction)
func NewDynamicGroupManager(
ethClientAddr string,
memContractAddr common.Address,
membershipIndex uint,
membershipIndexToLoad *uint,
appKeystore *keystore.AppKeystore,
keystorePassword string,
reg prometheus.Registerer,
Expand All @@ -116,11 +117,11 @@ func NewDynamicGroupManager(

web3Config := web3.NewConfig(ethClientAddr, memContractAddr)
return &DynamicGroupManager{
membershipIndex: membershipIndex,
appKeystore: appKeystore,
keystorePassword: keystorePassword,
MembershipFetcher: NewMembershipFetcher(web3Config, rlnInstance, rootTracker, log),
metrics: newMetrics(reg),
membershipIndexToLoad: membershipIndexToLoad,
appKeystore: appKeystore,
keystorePassword: keystorePassword,
MembershipFetcher: NewMembershipFetcher(web3Config, rlnInstance, rootTracker, log),
metrics: newMetrics(reg),
}, nil
}

Expand Down Expand Up @@ -170,7 +171,7 @@ func (gm *DynamicGroupManager) loadCredential(ctx context.Context) error {

credentials, err := gm.appKeystore.GetMembershipCredentials(
gm.keystorePassword,
gm.membershipIndex,
gm.membershipIndexToLoad,
keystore.NewMembershipContractInfo(gm.web3Config.ChainID, gm.web3Config.RegistryContract.Address))
if err != nil {
return err
Expand All @@ -191,6 +192,7 @@ func (gm *DynamicGroupManager) loadCredential(ctx context.Context) error {
}

gm.identityCredential = credentials.IdentityCredential
gm.membershipIndex = credentials.TreeIndex

return nil
}
Expand Down
33 changes: 27 additions & 6 deletions waku/v2/protocol/rln/keystore/keystore.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,31 @@ func getKey(treeIndex rln.MembershipIndex, filterMembershipContract MembershipCo
}

// GetMembershipCredentials decrypts and retrieves membership credentials from the keystore applying filters
func (k *AppKeystore) GetMembershipCredentials(keystorePassword string, treeIndex rln.MembershipIndex, filterMembershipContract MembershipContractInfo) (*MembershipCredentials, error) {
key, err := getKey(treeIndex, filterMembershipContract)
if err != nil {
return nil, err
func (k *AppKeystore) GetMembershipCredentials(keystorePassword string, index *rln.MembershipIndex, filterMembershipContract MembershipContractInfo) (*MembershipCredentials, error) {
// If there is only one, and index to laod nil, assume 0,
// if there is more than one, complain if the index to load is nil

var key Key
var err error
if len(k.Credentials) == 1 {
// Only one credential, the tree index does not matter.
k.logger.Warn("automatically loading the only credential found on the keystore")
for k := range k.Credentials {
key = k // Obtain the first c
break
}
} else {
treeIndex := uint(0)
if index != nil {
treeIndex = *index
} else {
return nil, errors.New("the index of the onchain commitment to use was not specified")
}

key, err = getKey(treeIndex, filterMembershipContract)
if err != nil {
return nil, err
}
}

credential, ok := k.Credentials[key]
Expand All @@ -108,7 +129,7 @@ func (k *AppKeystore) GetMembershipCredentials(keystorePassword string, treeInde

// AddMembershipCredentials inserts a membership credential to the keystore matching the application, appIdentifier and version filters.
func (k *AppKeystore) AddMembershipCredentials(newCredential MembershipCredentials, password string) error {
credentials, err := k.GetMembershipCredentials(password, newCredential.TreeIndex, newCredential.MembershipContractInfo)
credentials, err := k.GetMembershipCredentials(password, &newCredential.TreeIndex, newCredential.MembershipContractInfo)
if err != nil {
return err
}
Expand All @@ -118,7 +139,7 @@ func (k *AppKeystore) AddMembershipCredentials(newCredential MembershipCredentia
return err
}

if credentials != nil {
if credentials != nil && credentials.TreeIndex == newCredential.TreeIndex && credentials.MembershipContractInfo.Equals(newCredential.MembershipContractInfo) {
return errors.New("credential already present")
}

Expand Down
8 changes: 4 additions & 4 deletions waku/v2/protocol/rln/onchain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ func (s *WakuRLNRelayDynamicSuite) TestDynamicGroupManagement() {

membershipIndex := s.register(appKeystore, u1Credentials, s.u1PrivKey)

gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rlnInstance, rt, utils.Logger())
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, &membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rlnInstance, rt, utils.Logger())
s.Require().NoError(err)

// initialize the WakuRLNRelay
Expand Down Expand Up @@ -236,7 +236,7 @@ func (s *WakuRLNRelayDynamicSuite) TestMerkleTreeConstruction() {
rlnInstance, rootTracker, err := GetRLNInstanceAndRootTracker(s.tmpRLNDBPath())
s.Require().NoError(err)
// mount the rln relay protocol in the on-chain/dynamic mode
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rlnInstance, rootTracker, utils.Logger())
gm, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, &membershipIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rlnInstance, rootTracker, utils.Logger())
s.Require().NoError(err)

rlnRelay := New(group_manager.Details{
Expand Down Expand Up @@ -275,7 +275,7 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() {
// mount the rln relay protocol in the on-chain/dynamic mode
rootInstance, rootTracker, err := GetRLNInstanceAndRootTracker(s.tmpRLNDBPath())
s.Require().NoError(err)
gm1, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rootInstance, rootTracker, utils.Logger())
gm1, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, &membershipGroupIndex, appKeystore, keystorePassword, prometheus.DefaultRegisterer, rootInstance, rootTracker, utils.Logger())
s.Require().NoError(err)

rlnRelay1 := New(group_manager.Details{
Expand All @@ -299,7 +299,7 @@ func (s *WakuRLNRelayDynamicSuite) TestCorrectRegistrationOfPeers() {
// mount the rln relay protocol in the on-chain/dynamic mode
rootInstance, rootTracker, err = GetRLNInstanceAndRootTracker(s.tmpRLNDBPath())
s.Require().NoError(err)
gm2, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, membershipGroupIndex, appKeystore2, keystorePassword, prometheus.DefaultRegisterer, rootInstance, rootTracker, utils.Logger())
gm2, err := dynamic.NewDynamicGroupManager(s.web3Config.ETHClientAddress, s.web3Config.RegistryContract.Address, &membershipGroupIndex, appKeystore2, keystorePassword, prometheus.DefaultRegisterer, rootInstance, rootTracker, utils.Logger())
s.Require().NoError(err)

rlnRelay2 := New(group_manager.Details{
Expand Down