diff --git a/cmd/waku/flags_rln.go b/cmd/waku/flags_rln.go index 4765a4cfd..e87094d3a 100644 --- a/cmd/waku/flags_rln.go +++ b/cmd/waku/flags_rln.go @@ -64,15 +64,6 @@ func rlnFlags() []cli.Flag { 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{ - Name: "rln-relay-eth-account-private-key", - Usage: "Ethereum account private key used for registering in member contract", - Value: &wcli.PrivateKeyValue{ - Value: &options.RLNRelay.ETHPrivateKey, - }, - }, &cli.StringFlag{ Name: "rln-relay-eth-client-address", Usage: "Ethereum testnet client address", diff --git a/cmd/waku/node_rln.go b/cmd/waku/node_rln.go index 7c0e38dfe..094ca037e 100644 --- a/cmd/waku/node_rln.go +++ b/cmd/waku/node_rln.go @@ -4,7 +4,6 @@ package main import ( - "crypto/ecdsa" "errors" "github.com/waku-org/go-waku/waku/v2/node" @@ -20,12 +19,6 @@ func checkForRLN(logger *zap.Logger, options NodeOptions, nodeOpts *[]node.WakuN if !options.RLNRelay.Dynamic { *nodeOpts = append(*nodeOpts, node.WithStaticRLNRelay(options.RLNRelay.PubsubTopic, options.RLNRelay.ContentTopic, rln.MembershipIndex(options.RLNRelay.MembershipGroupIndex), nil)) } else { - - var ethPrivKey *ecdsa.PrivateKey - if options.RLNRelay.ETHPrivateKey != nil { - ethPrivKey = options.RLNRelay.ETHPrivateKey - } - // TODO: too many parameters in this function // consider passing a config struct instead *nodeOpts = append(*nodeOpts, node.WithDynamicRLNRelay( @@ -39,8 +32,6 @@ func checkForRLN(logger *zap.Logger, options NodeOptions, nodeOpts *[]node.WakuN rln.MembershipIndex(options.RLNRelay.MembershipGroupIndex), nil, options.RLNRelay.ETHClientAddress, - ethPrivKey, - nil, )) } } diff --git a/cmd/waku/options.go b/cmd/waku/options.go index 03865004a..c6286400e 100644 --- a/cmd/waku/options.go +++ b/cmd/waku/options.go @@ -43,7 +43,6 @@ type RLNRelayOptions struct { PubsubTopic string ContentTopic string Dynamic bool - ETHPrivateKey *ecdsa.PrivateKey ETHClientAddress string MembershipContractAddress common.Address } diff --git a/waku/v2/node/wakunode2_rln.go b/waku/v2/node/wakunode2_rln.go index 4d3c022fe..70f7aebd7 100644 --- a/waku/v2/node/wakunode2_rln.go +++ b/waku/v2/node/wakunode2_rln.go @@ -48,14 +48,12 @@ func (w *WakuNode) mountRlnRelay(ctx context.Context) error { groupManager, err = dynamic.NewDynamicGroupManager( 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, ) if err != nil { diff --git a/waku/v2/node/wakuoptions.go b/waku/v2/node/wakuoptions.go index f63190677..edee8b24f 100644 --- a/waku/v2/node/wakuoptions.go +++ b/waku/v2/node/wakuoptions.go @@ -9,7 +9,6 @@ import ( "time" "github.com/ethereum/go-ethereum/common" - "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/p2p/enode" logging "github.com/ipfs/go-log/v2" "github.com/libp2p/go-libp2p" @@ -100,14 +99,12 @@ type WakuNodeParameters struct { rlnRelayContentTopic string rlnRelayDynamic bool rlnSpamHandler func(message *pb.WakuMessage) error - rlnETHPrivateKey *ecdsa.PrivateKey rlnETHClientAddress string keystorePath string keystorePassword string keystoreIndex uint rlnTreePath string rlnMembershipContractAddress common.Address - rlnRegistrationHandler func(tx *types.Transaction) keepAliveInterval time.Duration diff --git a/waku/v2/node/wakuoptions_rln.go b/waku/v2/node/wakuoptions_rln.go index a8cf212e0..f0b5715f0 100644 --- a/waku/v2/node/wakuoptions_rln.go +++ b/waku/v2/node/wakuoptions_rln.go @@ -4,8 +4,6 @@ package node import ( - "crypto/ecdsa" - "github.com/ethereum/go-ethereum/common" "github.com/waku-org/go-waku/waku/v2/protocol/rln" r "github.com/waku-org/go-zerokit-rln/rln" @@ -27,7 +25,7 @@ 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, keystoreIndex uint, treePath string, membershipContract common.Address, membershipGroupIndex uint, spamHandler rln.SpamHandler, ethClientAddress string, ethPrivateKey *ecdsa.PrivateKey, registrationHandler rln.RegistrationHandler) WakuNodeOption { +func WithDynamicRLNRelay(pubsubTopic string, contentTopic string, keystorePath string, keystorePassword string, keystoreIndex uint, treePath string, membershipContract common.Address, membershipGroupIndex uint, spamHandler rln.SpamHandler, ethClientAddress string) WakuNodeOption { return func(params *WakuNodeParameters) error { params.enableRLN = true params.rlnRelayDynamic = true @@ -38,9 +36,7 @@ func WithDynamicRLNRelay(pubsubTopic string, contentTopic string, keystorePath s params.rlnRelayContentTopic = contentTopic params.rlnSpamHandler = spamHandler params.rlnETHClientAddress = ethClientAddress - params.rlnETHPrivateKey = ethPrivateKey params.rlnMembershipContractAddress = membershipContract - params.rlnRegistrationHandler = registrationHandler params.rlnRelayMemIndex = membershipGroupIndex params.rlnTreePath = treePath 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 525505b8d..09b79e419 100644 --- a/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go +++ b/waku/v2/protocol/rln/group_manager/dynamic/dynamic.go @@ -2,7 +2,6 @@ package dynamic import ( "context" - "crypto/ecdsa" "errors" "fmt" "math/big" @@ -43,17 +42,10 @@ type DynamicGroupManager struct { lastBlockProcessed uint64 - // ethAccountPrivateKey is required for signing transactions - // TODO may need to erase this ethAccountPrivateKey when is not used - // TODO may need to make ethAccountPrivateKey mandatory - ethAccountPrivateKey *ecdsa.PrivateKey - eventHandler RegistrationEventHandler - registrationHandler RegistrationHandler - chainId *big.Int - rlnContract *contracts.RLN - membershipFee *big.Int + chainId *big.Int + rlnContract *contracts.RLN saveKeystore bool keystorePath string @@ -120,14 +112,12 @@ type RegistrationHandler = func(tx *types.Transaction) func NewDynamicGroupManager( ethClientAddr string, - ethAccountPrivateKey *ecdsa.PrivateKey, memContractAddr common.Address, membershipGroupIndex uint, keystorePath string, keystorePassword string, keystoreIndex uint, saveKeystore bool, - registrationHandler RegistrationHandler, log *zap.Logger, ) (*DynamicGroupManager, error) { log = log.Named("rln-dynamic") @@ -148,8 +138,6 @@ func NewDynamicGroupManager( membershipGroupIndex: membershipGroupIndex, membershipContractAddress: memContractAddr, ethClientAddress: ethClientAddr, - ethAccountPrivateKey: ethAccountPrivateKey, - registrationHandler: registrationHandler, eventHandler: handler, saveKeystore: saveKeystore, keystorePath: path, @@ -193,7 +181,7 @@ func (gm *DynamicGroupManager) Start(ctx context.Context, rlnInstance *rln.RLN, } // check if the contract exists by calling a static function - gm.membershipFee, err = gm.getMembershipFee(ctx) + _, err = gm.getMembershipFee(ctx) if err != nil { return err } @@ -227,34 +215,6 @@ func (gm *DynamicGroupManager) Start(ctx context.Context, rlnInstance *rln.RLN, } } - if gm.identityCredential == nil && gm.ethAccountPrivateKey == nil { - return errors.New("either a credentials path or a private key must be specified") - } - - // prepare rln membership key pair - if gm.identityCredential == nil && gm.ethAccountPrivateKey != nil { - gm.log.Info("no rln-relay key is provided, generating one") - identityCredential, err := rlnInstance.MembershipKeyGen() - if err != nil { - return err - } - - gm.identityCredential = identityCredential - - // register the rln-relay peer to the membership contract - gm.membershipIndex, err = gm.Register(ctx) - if err != nil { - return err - } - - err = gm.persistCredentials() - if err != nil { - return err - } - - gm.log.Info("registered peer into the membership contract") - } - if gm.identityCredential == nil || gm.membershipIndex == nil { return errors.New("no credentials available") } @@ -266,31 +226,6 @@ func (gm *DynamicGroupManager) Start(ctx context.Context, rlnInstance *rln.RLN, return nil } -func (gm *DynamicGroupManager) persistCredentials() error { - if !gm.saveKeystore { - return nil - } - - if gm.identityCredential == nil || gm.membershipIndex == nil { - return errors.New("no credentials to persist") - } - - membershipGroup := keystore.MembershipGroup{ - TreeIndex: *gm.membershipIndex, - MembershipContract: keystore.MembershipContract{ - ChainId: fmt.Sprintf("0x%X", gm.chainId), - Address: gm.membershipContractAddress.String(), - }, - } - - _, _, err := keystore.AddMembershipCredentials(gm.keystorePath, gm.identityCredential, membershipGroup, gm.keystorePassword, RLNAppInfo, keystore.DefaultSeparator) - if err != nil { - return fmt.Errorf("failed to persist credentials: %w", err) - } - - return nil -} - func (gm *DynamicGroupManager) InsertMembers(toInsert *om.OrderedMap) error { for pair := toInsert.Oldest(); pair != nil; pair = pair.Next() { events := pair.Value.([]*contracts.RLNMemberRegistered) // TODO: should these be sortered by index? we assume all members arrive in order @@ -345,11 +280,6 @@ func (gm *DynamicGroupManager) IdentityCredentials() (rln.IdentityCredential, er return *gm.identityCredential, nil } -func (gm *DynamicGroupManager) SetCredentials(identityCredential *rln.IdentityCredential, index *rln.MembershipIndex) { - gm.identityCredential = identityCredential - gm.membershipIndex = index -} - func (gm *DynamicGroupManager) MembershipIndex() (rln.MembershipIndex, error) { if gm.membershipIndex == nil { return 0, errors.New("membership index has not been setup") diff --git a/waku/v2/protocol/rln/group_manager/dynamic/web3.go b/waku/v2/protocol/rln/group_manager/dynamic/web3.go index 92f8f2b37..e8e15f475 100644 --- a/waku/v2/protocol/rln/group_manager/dynamic/web3.go +++ b/waku/v2/protocol/rln/group_manager/dynamic/web3.go @@ -2,90 +2,17 @@ package dynamic import ( "context" - "crypto/ecdsa" "errors" - "math/big" "time" "github.com/ethereum/go-ethereum/accounts/abi/bind" "github.com/ethereum/go-ethereum/core/types" - "github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/rpc" "github.com/waku-org/go-waku/waku/v2/protocol/rln/contracts" - "github.com/waku-org/go-zerokit-rln/rln" "go.uber.org/zap" ) -func register(ctx context.Context, backend *ethclient.Client, membershipFee *big.Int, idComm rln.IDCommitment, ethAccountPrivateKey *ecdsa.PrivateKey, rlnContract *contracts.RLN, chainID *big.Int, registrationHandler RegistrationHandler, log *zap.Logger) (*rln.MembershipIndex, error) { - auth, err := bind.NewKeyedTransactorWithChainID(ethAccountPrivateKey, chainID) - if err != nil { - return nil, err - } - auth.Value = membershipFee - auth.Context = ctx - - log.Debug("registering an id commitment", zap.Binary("idComm", idComm[:])) - - // registers the idComm into the membership contract whose address is in rlnPeer.membershipContractAddress - tx, err := rlnContract.Register(auth, rln.Bytes32ToBigInt(idComm)) - if err != nil { - return nil, err - } - - log.Info("transaction broadcasted", zap.String("transactionHash", tx.Hash().Hex())) - - if registrationHandler != nil { - registrationHandler(tx) - } - - txReceipt, err := bind.WaitMined(ctx, backend, tx) - if err != nil { - return nil, err - } - - if txReceipt.Status != types.ReceiptStatusSuccessful { - return nil, errors.New("transaction reverted") - } - - // the receipt topic holds the hash of signature of the raised events - evt, err := rlnContract.ParseMemberRegistered(*txReceipt.Logs[0]) - if err != nil { - return nil, err - } - - var eventIDComm rln.IDCommitment = rln.BigIntToBytes32(evt.Pubkey) - - log.Debug("the identity commitment key extracted from tx log", zap.Binary("eventIDComm", eventIDComm[:])) - - if eventIDComm != idComm { - return nil, errors.New("invalid id commitment key") - } - - result := new(rln.MembershipIndex) - *result = rln.MembershipIndex(uint(evt.Index.Int64())) - - // debug "the index of registered identity commitment key", eventIndex=eventIndex - - log.Debug("the index of registered identity commitment key", zap.Uint("eventIndex", uint(*result))) - - return result, nil -} - -// Register registers the public key of the rlnPeer which is rlnPeer.membershipKeyPair.publicKey -// into the membership contract whose address is in rlnPeer.membershipContractAddress -func (gm *DynamicGroupManager) Register(ctx context.Context) (*rln.MembershipIndex, error) { - return register(ctx, - gm.ethClient, - gm.membershipFee, - gm.identityCredential.IDCommitment, - gm.ethAccountPrivateKey, - gm.rlnContract, - gm.chainId, - gm.registrationHandler, - gm.log) -} - // the types of inputs to this handler matches the MemberRegistered event/proc defined in the MembershipContract interface type RegistrationEventHandler = func(*DynamicGroupManager, []*contracts.RLNMemberRegistered) error