cmd/*: refactor get flag value (#24761)

This commit is contained in:
s7v7nislands
2022-04-26 09:32:31 +02:00
committed by GitHub
parent 0914234d10
commit 195c2d3d69
5 changed files with 87 additions and 59 deletions
+15 -10
View File
@@ -35,6 +35,17 @@ type outputGenerate struct {
AddressEIP55 string
}
var (
privateKeyFlag = cli.StringFlag{
Name: "privatekey",
Usage: "file containing a raw private key to encrypt",
}
lightKDFFlag = cli.BoolFlag{
Name: "lightkdf",
Usage: "use less secure scrypt parameters",
}
)
var commandGenerate = cli.Command{
Name: "generate",
Usage: "generate new keyfile",
@@ -48,14 +59,8 @@ If you want to encrypt an existing private key, it can be specified by setting
Flags: []cli.Flag{
passphraseFlag,
jsonFlag,
cli.StringFlag{
Name: "privatekey",
Usage: "file containing a raw private key to encrypt",
},
cli.BoolFlag{
Name: "lightkdf",
Usage: "use less secure scrypt parameters",
},
privateKeyFlag,
lightKDFFlag,
},
Action: func(ctx *cli.Context) error {
// Check if keyfile path given and make sure it doesn't already exist.
@@ -71,7 +76,7 @@ If you want to encrypt an existing private key, it can be specified by setting
var privateKey *ecdsa.PrivateKey
var err error
if file := ctx.String("privatekey"); file != "" {
if file := ctx.String(privateKeyFlag.Name); file != "" {
// Load private key from file.
privateKey, err = crypto.LoadECDSA(file)
if err != nil {
@@ -99,7 +104,7 @@ If you want to encrypt an existing private key, it can be specified by setting
// Encrypt key with passphrase.
passphrase := getPassphrase(ctx, true)
scryptN, scryptP := keystore.StandardScryptN, keystore.StandardScryptP
if ctx.Bool("lightkdf") {
if ctx.Bool(lightKDFFlag.Name) {
scryptN, scryptP = keystore.LightScryptN, keystore.LightScryptP
}
keyjson, err := keystore.EncryptKey(key, passphrase, scryptN, scryptP)
+9 -5
View File
@@ -33,6 +33,13 @@ type outputInspect struct {
PrivateKey string
}
var (
privateFlag = cli.BoolFlag{
Name: "private",
Usage: "include the private key in the output",
}
)
var commandInspect = cli.Command{
Name: "inspect",
Usage: "inspect a keyfile",
@@ -45,10 +52,7 @@ make sure to use this feature with great caution!`,
Flags: []cli.Flag{
passphraseFlag,
jsonFlag,
cli.BoolFlag{
Name: "private",
Usage: "include the private key in the output",
},
privateFlag,
},
Action: func(ctx *cli.Context) error {
keyfilepath := ctx.Args().First()
@@ -67,7 +71,7 @@ make sure to use this feature with great caution!`,
}
// Output all relevant information we can retrieve.
showPrivate := ctx.Bool("private")
showPrivate := ctx.Bool(privateFlag.Name)
out := outputInspect{
Address: key.Address.Hex(),
PublicKey: hex.EncodeToString(
+1 -1
View File
@@ -142,7 +142,7 @@ It is possible to refer to a file containing the message.`,
}
func getMessage(ctx *cli.Context, msgarg int) []byte {
if file := ctx.String("msgfile"); file != "" {
if file := ctx.String(msgfileFlag.Name); file != "" {
if len(ctx.Args()) > msgarg {
utils.Fatalf("Can't use --msgfile and message argument at the same time.")
}