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