aboutsummaryrefslogtreecommitdiffstats
path: root/cmd/ethkey/inspect.go
diff options
context:
space:
mode:
Diffstat (limited to 'cmd/ethkey/inspect.go')
-rw-r--r--cmd/ethkey/inspect.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/cmd/ethkey/inspect.go b/cmd/ethkey/inspect.go
new file mode 100644
index 000000000..219a5460b
--- /dev/null
+++ b/cmd/ethkey/inspect.go
@@ -0,0 +1,75 @@
+package main
+
+import (
+ "encoding/hex"
+ "fmt"
+ "io/ioutil"
+
+ "github.com/ethereum/go-ethereum/accounts/keystore"
+ "github.com/ethereum/go-ethereum/cmd/utils"
+ "github.com/ethereum/go-ethereum/crypto"
+ "gopkg.in/urfave/cli.v1"
+)
+
+type outputInspect struct {
+ Address string
+ PublicKey string
+ PrivateKey string
+}
+
+var commandInspect = cli.Command{
+ Name: "inspect",
+ Usage: "inspect a keyfile",
+ ArgsUsage: "<keyfile>",
+ Description: `
+Print various information about the keyfile.
+
+Private key information can be printed by using the --private flag;
+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",
+ },
+ },
+ Action: func(ctx *cli.Context) error {
+ keyfilepath := ctx.Args().First()
+
+ // Read key from file.
+ keyjson, err := ioutil.ReadFile(keyfilepath)
+ if err != nil {
+ utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
+ }
+
+ // Decrypt key with passphrase.
+ passphrase := getPassPhrase(ctx, false)
+ key, err := keystore.DecryptKey(keyjson, passphrase)
+ if err != nil {
+ utils.Fatalf("Error decrypting key: %v", err)
+ }
+
+ // Output all relevant information we can retrieve.
+ showPrivate := ctx.Bool("private")
+ out := outputInspect{
+ Address: key.Address.Hex(),
+ PublicKey: hex.EncodeToString(
+ crypto.FromECDSAPub(&key.PrivateKey.PublicKey)),
+ }
+ if showPrivate {
+ out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey))
+ }
+
+ if ctx.Bool(jsonFlag.Name) {
+ mustPrintJSON(out)
+ } else {
+ fmt.Println("Address: ", out.Address)
+ fmt.Println("Public key: ", out.PublicKey)
+ if showPrivate {
+ fmt.Println("Private key: ", out.PrivateKey)
+ }
+ }
+ return nil
+ },
+}