github.com/kivutar/go-ethereum@v1.7.4-0.20180117074026-6fdb126e9630/cmd/ethkey/inspect.go (about)

     1  package main
     2  
     3  import (
     4  	"encoding/hex"
     5  	"fmt"
     6  	"io/ioutil"
     7  
     8  	"github.com/ethereum/go-ethereum/accounts/keystore"
     9  	"github.com/ethereum/go-ethereum/cmd/utils"
    10  	"github.com/ethereum/go-ethereum/crypto"
    11  	"gopkg.in/urfave/cli.v1"
    12  )
    13  
    14  type outputInspect struct {
    15  	Address    string
    16  	PublicKey  string
    17  	PrivateKey string
    18  }
    19  
    20  var commandInspect = cli.Command{
    21  	Name:      "inspect",
    22  	Usage:     "inspect a keyfile",
    23  	ArgsUsage: "<keyfile>",
    24  	Description: `
    25  Print various information about the keyfile.
    26  Private key information can be printed by using the --private flag;
    27  make sure to use this feature with great caution!`,
    28  	Flags: []cli.Flag{
    29  		passphraseFlag,
    30  		jsonFlag,
    31  		cli.BoolFlag{
    32  			Name:  "private",
    33  			Usage: "include the private key in the output",
    34  		},
    35  	},
    36  	Action: func(ctx *cli.Context) error {
    37  		keyfilepath := ctx.Args().First()
    38  
    39  		// Read key from file.
    40  		keyjson, err := ioutil.ReadFile(keyfilepath)
    41  		if err != nil {
    42  			utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
    43  		}
    44  
    45  		// Decrypt key with passphrase.
    46  		passphrase := getPassPhrase(ctx, false)
    47  		key, err := keystore.DecryptKey(keyjson, passphrase)
    48  		if err != nil {
    49  			utils.Fatalf("Error decrypting key: %v", err)
    50  		}
    51  
    52  		// Output all relevant information we can retrieve.
    53  		showPrivate := ctx.Bool("private")
    54  		out := outputInspect{
    55  			Address: key.Address.Hex(),
    56  			PublicKey: hex.EncodeToString(
    57  				crypto.FromECDSAPub(&key.PrivateKey.PublicKey)),
    58  		}
    59  		if showPrivate {
    60  			out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey))
    61  		}
    62  
    63  		if ctx.Bool(jsonFlag.Name) {
    64  			mustPrintJSON(out)
    65  		} else {
    66  			fmt.Println("Address:       ", out.Address)
    67  			fmt.Println("Public key:    ", out.PublicKey)
    68  			if showPrivate {
    69  				fmt.Println("Private key:   ", out.PrivateKey)
    70  			}
    71  		}
    72  		return nil
    73  	},
    74  }