Skip to content

Commit

Permalink
refactor: remove credential registering from waku
Browse files Browse the repository at this point in the history
  • Loading branch information
richard-ramos committed Aug 20, 2023
1 parent 6ff7116 commit 8213081
Show file tree
Hide file tree
Showing 11 changed files with 118 additions and 250 deletions.
9 changes: 0 additions & 9 deletions cmd/waku/flags_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 0 additions & 9 deletions cmd/waku/node_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
package main

import (
"crypto/ecdsa"
"errors"

"github.com/waku-org/go-waku/waku/v2/node"
Expand All @@ -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(
Expand All @@ -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,
))
}
}
Expand Down
1 change: 0 additions & 1 deletion cmd/waku/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ type RLNRelayOptions struct {
PubsubTopic string
ContentTopic string
Dynamic bool
ETHPrivateKey *ecdsa.PrivateKey
ETHClientAddress string
MembershipContractAddress common.Address
}
Expand Down
5 changes: 3 additions & 2 deletions cmd/waku/rlngenerate/command_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,12 +130,13 @@ func persistCredentials(identityCredential *rln.IdentityCredential, membershipIn
},
}

keystoreIndex, membershipGroupIndex, err := keystore.AddMembershipCredentials(options.CredentialsPath, identityCredential, membershipGroup, options.CredentialsPassword, dynamic.RLNAppInfo, keystore.DefaultSeparator)
membershipGroupIndex, err := keystore.AddMembershipCredentials(options.CredentialsPath, identityCredential, membershipGroup, options.CredentialsPassword, dynamic.RLNAppInfo, keystore.DefaultSeparator)
if err != nil {
return fmt.Errorf("failed to persist credentials: %w", err)
}

logger.Info("persisted credentials succesfully", zap.Int("keystoreIndex", keystoreIndex), zap.Int("membershipGroupIndex", membershipGroupIndex))
// TODO: obtain keystore index?
logger.Info("persisted credentials succesfully", zap.Uint("membershipGroupIndex", membershipGroupIndex))

return nil
}
2 changes: 0 additions & 2 deletions waku/v2/node/wakunode2_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
3 changes: 0 additions & 3 deletions waku/v2/node/wakuoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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

Expand Down
6 changes: 1 addition & 5 deletions waku/v2/node/wakuoptions_rln.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand All @@ -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
Expand Down
76 changes: 3 additions & 73 deletions waku/v2/protocol/rln/group_manager/dynamic/dynamic.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package dynamic

import (
"context"
"crypto/ecdsa"
"errors"
"fmt"
"math/big"
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand All @@ -148,8 +138,6 @@ func NewDynamicGroupManager(
membershipGroupIndex: membershipGroupIndex,
membershipContractAddress: memContractAddr,
ethClientAddress: ethClientAddr,
ethAccountPrivateKey: ethAccountPrivateKey,
registrationHandler: registrationHandler,
eventHandler: handler,
saveKeystore: saveKeystore,
keystorePath: path,
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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")
}
Expand All @@ -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
Expand Down Expand Up @@ -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")
Expand Down
73 changes: 0 additions & 73 deletions waku/v2/protocol/rln/group_manager/dynamic/web3.go
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Loading

0 comments on commit 8213081

Please sign in to comment.