-
Notifications
You must be signed in to change notification settings - Fork 43
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
refactor: move --generate-key to a generate-key subcommand #637
Merged
+137
−76
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package keygen | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
|
||
"github.com/ethereum/go-ethereum/accounts/keystore" | ||
"github.com/ethereum/go-ethereum/crypto" | ||
"github.com/urfave/cli/v2" | ||
"github.com/waku-org/go-waku/waku/v2/utils" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// Command generates a key file used to generate the node's peerID, encrypted with an optional password | ||
var Command = cli.Command{ | ||
Name: "generate-key", | ||
Usage: "Generate private key file at path specified in --key-file with the password defined by --key-password", | ||
Action: func(cCtx *cli.Context) error { | ||
if err := generateKeyFile(Options.KeyFile, []byte(Options.KeyPasswd), Options.Overwrite); err != nil { | ||
utils.Logger().Fatal("could not write keyfile", zap.Error(err)) | ||
} | ||
return nil | ||
}, | ||
Flags: []cli.Flag{ | ||
KeyFile, | ||
KeyPassword, | ||
Overwrite, | ||
}, | ||
} | ||
|
||
func checkForFileExistence(path string, overwrite bool) error { | ||
_, err := os.Stat(path) | ||
|
||
if err == nil && !overwrite { | ||
return fmt.Errorf("%s already exists. Use --overwrite to overwrite the file", path) | ||
} | ||
|
||
if err := os.Remove(path); err != nil && !os.IsNotExist(err) { | ||
return err | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func generatePrivateKey() ([]byte, error) { | ||
key, err := crypto.GenerateKey() | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return key.D.Bytes(), nil | ||
} | ||
|
||
func writeKeyFile(path string, key []byte, passwd []byte) error { | ||
encryptedK, err := keystore.EncryptDataV3(key, passwd, keystore.StandardScryptN, keystore.StandardScryptP) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
output, err := json.Marshal(encryptedK) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return os.WriteFile(path, output, 0600) | ||
} | ||
|
||
func generateKeyFile(path string, passwd []byte, overwrite bool) error { | ||
if err := checkForFileExistence(path, overwrite); err != nil { | ||
return err | ||
} | ||
|
||
key, err := generatePrivateKey() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return writeKeyFile(path, key, passwd) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package keygen | ||
|
||
import ( | ||
cli "github.com/urfave/cli/v2" | ||
"github.com/urfave/cli/v2/altsrc" | ||
) | ||
|
||
// Options contain the settings used for generating a key file | ||
var Options GenerateKeyOptions | ||
|
||
var ( | ||
// KeyFile is a flag that contains the path where the node key will be written | ||
KeyFile = altsrc.NewPathFlag(&cli.PathFlag{ | ||
Name: "key-file", | ||
Value: "./nodekey", | ||
Usage: "Path to a file containing the private key for the P2P node", | ||
Destination: &Options.KeyFile, | ||
EnvVars: []string{"WAKUNODE2_KEY_FILE"}, | ||
}) | ||
// KeyPassword is a flag to set the password used to encrypt the file | ||
KeyPassword = altsrc.NewStringFlag(&cli.StringFlag{ | ||
Name: "key-password", | ||
Value: "secret", | ||
Usage: "Password used for the private key file", | ||
Destination: &Options.KeyPasswd, | ||
EnvVars: []string{"WAKUNODE2_KEY_PASSWORD"}, | ||
}) | ||
// Overwrite is a flag used to overwrite an existing key file | ||
Overwrite = altsrc.NewBoolFlag(&cli.BoolFlag{ | ||
Name: "overwrite", | ||
Value: false, | ||
Usage: "Overwrite the nodekey file if it already exists", | ||
Destination: &Options.Overwrite, | ||
}) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package keygen | ||
|
||
// GenerateKeyOptions contains all the settings that can be used when generating | ||
// a keyfile with the generate-key command | ||
type GenerateKeyOptions struct { | ||
KeyFile string | ||
KeyPasswd string | ||
Overwrite bool | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe set default to false.