diff options
author | Wei-Ning Huang <w@dexon.org> | 2019-03-09 00:09:59 +0800 |
---|---|---|
committer | Wei-Ning Huang <w@dexon.org> | 2019-04-09 21:32:57 +0800 |
commit | 4ba54663806828d31bc37da4127f29fbe7dbc9a3 (patch) | |
tree | ea0d3d8af64ba7ffc2a895a82a28bbfb1d460436 /cmd | |
parent | cdef678e25c9c5a2ce8677169235a84aff731ef8 (diff) | |
download | dexon-4ba54663806828d31bc37da4127f29fbe7dbc9a3.tar.gz dexon-4ba54663806828d31bc37da4127f29fbe7dbc9a3.tar.zst dexon-4ba54663806828d31bc37da4127f29fbe7dbc9a3.zip |
cmd: nodekey: add command to generate and inspect nodekey (#233)
Diffstat (limited to 'cmd')
-rw-r--r-- | cmd/nodekey/main.go | 93 |
1 files changed, 93 insertions, 0 deletions
diff --git a/cmd/nodekey/main.go b/cmd/nodekey/main.go new file mode 100644 index 000000000..1ba417704 --- /dev/null +++ b/cmd/nodekey/main.go @@ -0,0 +1,93 @@ +package main + +import ( + "encoding/hex" + "fmt" + "os" + + "github.com/dexon-foundation/dexon/cmd/utils" + "github.com/dexon-foundation/dexon/crypto" + + "gopkg.in/urfave/cli.v1" +) + +const ( + defaultKeyfileName = "node.key" +) + +// Git SHA1 commit hash of the release (set via linker flags) +var gitCommit = "" + +var app *cli.App + +func init() { + app = utils.NewApp(gitCommit, "DEXON node key manager") + app.Commands = []cli.Command{ + commandGenerate, + commandInspect, + } +} + +var commandGenerate = cli.Command{ + Name: "generate", + Usage: "generate new keyfile", + ArgsUsage: "[ <keyfile> ]", + Description: `Generate a new node key.`, + Action: func(ctx *cli.Context) error { + // Check if keyfile path given and make sure it doesn't already exist. + keyfilepath := ctx.Args().First() + if keyfilepath == "" { + keyfilepath = defaultKeyfileName + } + if _, err := os.Stat(keyfilepath); err == nil { + utils.Fatalf("Keyfile already exists at %s.", keyfilepath) + } else if !os.IsNotExist(err) { + utils.Fatalf("Error checking if keyfile exists: %v", err) + } + + privKey, err := crypto.GenerateKey() + if err != nil { + utils.Fatalf("Failed to generate random private key: %v", err) + } + + address := crypto.PubkeyToAddress(privKey.PublicKey) + err = crypto.SaveECDSA(keyfilepath, privKey) + if err != nil { + utils.Fatalf("Failed to save keyfile: %v", err) + } + + fmt.Printf("Node Address: %s\n", address.String()) + fmt.Printf("Public Key: 0x%s\n", + hex.EncodeToString(crypto.FromECDSAPub(&privKey.PublicKey))) + return nil + }, +} + +var commandInspect = cli.Command{ + Name: "inspect", + Usage: "inspect a keyfile", + ArgsUsage: "[ <keyfile> ]", + Description: `Generate a new node key.`, + Action: func(ctx *cli.Context) error { + keyfilepath := ctx.Args().First() + + privKey, err := crypto.LoadECDSA(keyfilepath) + if err != nil { + utils.Fatalf("Failed to read key file: %v", err) + } + + address := crypto.PubkeyToAddress(privKey.PublicKey) + + fmt.Printf("Node Address: %s\n", address.String()) + fmt.Printf("Public Key: 0x%s\n", + hex.EncodeToString(crypto.FromECDSAPub(&privKey.PublicKey))) + return nil + }, +} + +func main() { + if err := app.Run(os.Args); err != nil { + fmt.Fprintln(os.Stderr, err) + os.Exit(1) + } +} |