From 70ddd4827491d2c6473ec0203ba38dbe44b98ebf Mon Sep 17 00:00:00 2001 From: harsh-98 Date: Sun, 2 Apr 2023 16:23:37 +0530 Subject: [PATCH] refactor: setting nodeKey via GOWAKU-NODEKEY env --- cmd/waku/flags.go | 4 ++-- waku/node.go | 26 ++++++++------------------ 2 files changed, 10 insertions(+), 20 deletions(-) diff --git a/cmd/waku/flags.go b/cmd/waku/flags.go index ab880d48..d8c714df 100644 --- a/cmd/waku/flags.go +++ b/cmd/waku/flags.go @@ -88,11 +88,11 @@ var ( }) NodeKey = cliutils.NewGenericFlagSingleValue(&cli.GenericFlag{ Name: "nodekey", - Usage: "P2P node private key as hex. Can also be set with GOWAKU-NODEKEY env variable (default random)", + Usage: "P2P node private key as hex.", Value: &cliutils.PrivateKeyValue{ Value: &options.NodeKey, }, - EnvVars: []string{"WAKUNODE2_NODEKEY"}, + EnvVars: []string{"WAKUNODE2_NODEKEY", "GOWAKU-NODEKEY"}, }) KeyFile = altsrc.NewPathFlag(&cli.PathFlag{ Name: "key-file", diff --git a/waku/node.go b/waku/node.go index bdd9d481..d13cc932 100644 --- a/waku/node.go +++ b/waku/node.go @@ -17,7 +17,6 @@ import ( "github.com/waku-org/go-waku/waku/v2/rendezvous" "github.com/ethereum/go-ethereum/accounts/keystore" - "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/p2p/enode" dssql "github.com/ipfs/go-ds-sql" @@ -458,30 +457,21 @@ func writePrivateKeyToFile(path string, passwd []byte, overwrite bool) error { func getPrivKey(options Options) (*ecdsa.PrivateKey, error) { var prvKey *ecdsa.PrivateKey - var err error - + // get private key from nodeKey or keyFile if options.NodeKey != nil { prvKey = options.NodeKey } else { - // TODO: once https://github.com/urfave/cli/issues/1272 is fixed, remove env variable logic - keyString := os.Getenv("GOWAKU-NODEKEY") - if keyString != "" { - if prvKey, err = crypto.ToECDSA(common.FromHex(keyString)); err != nil { - return nil, fmt.Errorf("error converting key into valid ecdsa key: %w", err) + if _, err := os.Stat(options.KeyFile); err == nil { + if prvKey, err = loadPrivateKeyFromFile(options.KeyFile, options.KeyPasswd); err != nil { + return nil, fmt.Errorf("could not read keyfile: %w", err) } } else { - if _, err := os.Stat(options.KeyFile); err == nil { - if prvKey, err = loadPrivateKeyFromFile(options.KeyFile, options.KeyPasswd); err != nil { - return nil, fmt.Errorf("could not read keyfile: %w", err) + if os.IsNotExist(err) { + if prvKey, err = crypto.GenerateKey(); err != nil { + return nil, fmt.Errorf("error generating key: %w", err) } } else { - if os.IsNotExist(err) { - if prvKey, err = crypto.GenerateKey(); err != nil { - return nil, fmt.Errorf("error generating key: %w", err) - } - } else { - return nil, fmt.Errorf("could not read keyfile: %w", err) - } + return nil, fmt.Errorf("could not read keyfile: %w", err) } } }