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