github.com/vantum/vantum@v0.0.0-20180815184342-fe37d5f7a990/cmd/ethkey/inspect.go (about)

     1  // Copyright 2017 The go-ethereum Authors
     2  // This file is part of go-ethereum.
     3  //
     4  // go-ethereum is free software: you can redistribute it and/or modify
     5  // it under the terms of the GNU General Public License as published by
     6  // the Free Software Foundation, either version 3 of the License, or
     7  // (at your option) any later version.
     8  //
     9  // go-ethereum is distributed in the hope that it will be useful,
    10  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    11  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    12  // GNU General Public License for more details.
    13  //
    14  // You should have received a copy of the GNU General Public License
    15  // along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package main
    18  
    19  import (
    20  	"encoding/hex"
    21  	"fmt"
    22  	"io/ioutil"
    23  
    24  	"github.com/vantum/vantum/accounts/keystore"
    25  	"github.com/vantum/vantum/cmd/utils"
    26  	"github.com/vantum/vantum/crypto"
    27  	"gopkg.in/urfave/cli.v1"
    28  )
    29  
    30  type outputInspect struct {
    31  	Address    string
    32  	PublicKey  string
    33  	PrivateKey string
    34  }
    35  
    36  var commandInspect = cli.Command{
    37  	Name:      "inspect",
    38  	Usage:     "inspect a keyfile",
    39  	ArgsUsage: "<keyfile>",
    40  	Description: `
    41  Print various information about the keyfile.
    42  
    43  Private key information can be printed by using the --private flag;
    44  make sure to use this feature with great caution!`,
    45  	Flags: []cli.Flag{
    46  		passphraseFlag,
    47  		jsonFlag,
    48  		cli.BoolFlag{
    49  			Name:  "private",
    50  			Usage: "include the private key in the output",
    51  		},
    52  	},
    53  	Action: func(ctx *cli.Context) error {
    54  		keyfilepath := ctx.Args().First()
    55  
    56  		// Read key from file.
    57  		keyjson, err := ioutil.ReadFile(keyfilepath)
    58  		if err != nil {
    59  			utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
    60  		}
    61  
    62  		// Decrypt key with passphrase.
    63  		passphrase := getPassPhrase(ctx, false)
    64  		key, err := keystore.DecryptKey(keyjson, passphrase)
    65  		if err != nil {
    66  			utils.Fatalf("Error decrypting key: %v", err)
    67  		}
    68  
    69  		// Output all relevant information we can retrieve.
    70  		showPrivate := ctx.Bool("private")
    71  		out := outputInspect{
    72  			Address: key.Address.Hex(),
    73  			PublicKey: hex.EncodeToString(
    74  				crypto.FromECDSAPub(&key.PrivateKey.PublicKey)),
    75  		}
    76  		if showPrivate {
    77  			out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey))
    78  		}
    79  
    80  		if ctx.Bool(jsonFlag.Name) {
    81  			mustPrintJSON(out)
    82  		} else {
    83  			fmt.Println("Address:       ", out.Address)
    84  			fmt.Println("Public key:    ", out.PublicKey)
    85  			if showPrivate {
    86  				fmt.Println("Private key:   ", out.PrivateKey)
    87  			}
    88  		}
    89  		return nil
    90  	},
    91  }