github.com/mandrigin/go-ethereum@v1.7.4-0.20180116162341-02aeb3d76652/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  
    27  Private key information can be printed by using the --private flag;
    28  make sure to use this feature with great caution!`,
    29  	Flags: []cli.Flag{
    30  		passphraseFlag,
    31  		jsonFlag,
    32  		cli.BoolFlag{
    33  			Name:  "private",
    34  			Usage: "include the private key in the output",
    35  		},
    36  	},
    37  	Action: func(ctx *cli.Context) error {
    38  		keyfilepath := ctx.Args().First()
    39  
    40  		// Read key from file.
    41  		keyjson, err := ioutil.ReadFile(keyfilepath)
    42  		if err != nil {
    43  			utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
    44  		}
    45  
    46  		// Decrypt key with passphrase.
    47  		passphrase := getPassPhrase(ctx, false)
    48  		key, err := keystore.DecryptKey(keyjson, passphrase)
    49  		if err != nil {
    50  			utils.Fatalf("Error decrypting key: %v", err)
    51  		}
    52  
    53  		// Output all relevant information we can retrieve.
    54  		showPrivate := ctx.Bool("private")
    55  		out := outputInspect{
    56  			Address: key.Address.Hex(),
    57  			PublicKey: hex.EncodeToString(
    58  				crypto.FromECDSAPub(&key.PrivateKey.PublicKey)),
    59  		}
    60  		if showPrivate {
    61  			out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey))
    62  		}
    63  
    64  		if ctx.Bool(jsonFlag.Name) {
    65  			mustPrintJSON(out)
    66  		} else {
    67  			fmt.Println("Address:       ", out.Address)
    68  			fmt.Println("Public key:    ", out.PublicKey)
    69  			if showPrivate {
    70  				fmt.Println("Private key:   ", out.PrivateKey)
    71  			}
    72  		}
    73  		return nil
    74  	},
    75  }