refactor: setting nodeKey via GOWAKU-NODEKEY env

This commit is contained in:
harsh-98 2023-04-02 16:23:37 +05:30 committed by RichΛrd
parent 3b3e3b6968
commit 70ddd48274
2 changed files with 10 additions and 20 deletions

View File

@ -88,11 +88,11 @@ var (
}) })
NodeKey = cliutils.NewGenericFlagSingleValue(&cli.GenericFlag{ NodeKey = cliutils.NewGenericFlagSingleValue(&cli.GenericFlag{
Name: "nodekey", 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: &cliutils.PrivateKeyValue{
Value: &options.NodeKey, Value: &options.NodeKey,
}, },
EnvVars: []string{"WAKUNODE2_NODEKEY"}, EnvVars: []string{"WAKUNODE2_NODEKEY", "GOWAKU-NODEKEY"},
}) })
KeyFile = altsrc.NewPathFlag(&cli.PathFlag{ KeyFile = altsrc.NewPathFlag(&cli.PathFlag{
Name: "key-file", Name: "key-file",

View File

@ -17,7 +17,6 @@ import (
"github.com/waku-org/go-waku/waku/v2/rendezvous" "github.com/waku-org/go-waku/waku/v2/rendezvous"
"github.com/ethereum/go-ethereum/accounts/keystore" "github.com/ethereum/go-ethereum/accounts/keystore"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/p2p/enode" "github.com/ethereum/go-ethereum/p2p/enode"
dssql "github.com/ipfs/go-ds-sql" 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) { func getPrivKey(options Options) (*ecdsa.PrivateKey, error) {
var prvKey *ecdsa.PrivateKey var prvKey *ecdsa.PrivateKey
var err error // get private key from nodeKey or keyFile
if options.NodeKey != nil { if options.NodeKey != nil {
prvKey = options.NodeKey prvKey = options.NodeKey
} else { } else {
// TODO: once https://github.com/urfave/cli/issues/1272 is fixed, remove env variable logic if _, err := os.Stat(options.KeyFile); err == nil {
keyString := os.Getenv("GOWAKU-NODEKEY") if prvKey, err = loadPrivateKeyFromFile(options.KeyFile, options.KeyPasswd); err != nil {
if keyString != "" { return nil, fmt.Errorf("could not read keyfile: %w", err)
if prvKey, err = crypto.ToECDSA(common.FromHex(keyString)); err != nil {
return nil, fmt.Errorf("error converting key into valid ecdsa key: %w", err)
} }
} else { } else {
if _, err := os.Stat(options.KeyFile); err == nil { if os.IsNotExist(err) {
if prvKey, err = loadPrivateKeyFromFile(options.KeyFile, options.KeyPasswd); err != nil { if prvKey, err = crypto.GenerateKey(); err != nil {
return nil, fmt.Errorf("could not read keyfile: %w", err) return nil, fmt.Errorf("error generating key: %w", err)
} }
} else { } else {
if os.IsNotExist(err) { return nil, fmt.Errorf("could not read keyfile: %w", 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)
}
} }
} }
} }