github.com/theQRL/go-zond@v0.1.1/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  	"os"
    23  
    24  	"github.com/theQRL/go-zond/accounts/keystore"
    25  	"github.com/theQRL/go-zond/cmd/utils"
    26  	"github.com/theQRL/go-zond/crypto"
    27  	"github.com/urfave/cli/v2"
    28  )
    29  
    30  type outputInspect struct {
    31  	Address    string
    32  	PublicKey  string
    33  	PrivateKey string
    34  }
    35  
    36  var (
    37  	privateFlag = &cli.BoolFlag{
    38  		Name:  "private",
    39  		Usage: "include the private key in the output",
    40  	}
    41  )
    42  
    43  var commandInspect = &cli.Command{
    44  	Name:      "inspect",
    45  	Usage:     "inspect a keyfile",
    46  	ArgsUsage: "<keyfile>",
    47  	Description: `
    48  Print various information about the keyfile.
    49  
    50  Private key information can be printed by using the --private flag;
    51  make sure to use this feature with great caution!`,
    52  	Flags: []cli.Flag{
    53  		passphraseFlag,
    54  		jsonFlag,
    55  		privateFlag,
    56  	},
    57  	Action: func(ctx *cli.Context) error {
    58  		keyfilepath := ctx.Args().First()
    59  
    60  		// Read key from file.
    61  		keyjson, err := os.ReadFile(keyfilepath)
    62  		if err != nil {
    63  			utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err)
    64  		}
    65  
    66  		// Decrypt key with passphrase.
    67  		passphrase := getPassphrase(ctx, false)
    68  		key, err := keystore.DecryptKey(keyjson, passphrase)
    69  		if err != nil {
    70  			utils.Fatalf("Error decrypting key: %v", err)
    71  		}
    72  
    73  		// Output all relevant information we can retrieve.
    74  		showPrivate := ctx.Bool(privateFlag.Name)
    75  		out := outputInspect{
    76  			Address: key.Address.Hex(),
    77  			PublicKey: hex.EncodeToString(
    78  				crypto.FromECDSAPub(&key.PrivateKey.PublicKey)),
    79  		}
    80  		if showPrivate {
    81  			out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey))
    82  		}
    83  
    84  		if ctx.Bool(jsonFlag.Name) {
    85  			mustPrintJSON(out)
    86  		} else {
    87  			fmt.Println("Address:       ", out.Address)
    88  			fmt.Println("Public key:    ", out.PublicKey)
    89  			if showPrivate {
    90  				fmt.Println("Private key:   ", out.PrivateKey)
    91  			}
    92  		}
    93  		return nil
    94  	},
    95  }