github.com/arieschain/arieschain@v0.0.0-20191023063405-37c074544356/cmd/qctkey/inspect.go (about)

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