github.com/ethxdao/go-ethereum@v0.0.0-20221218102228-5ae34a9cc189/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/ethxdao/go-ethereum/accounts/keystore" 25 "github.com/ethxdao/go-ethereum/cmd/utils" 26 "github.com/ethxdao/go-ethereum/crypto" 27 ) 28 29 type outputInspect struct { 30 Address string 31 PublicKey string 32 PrivateKey string 33 } 34 35 var ( 36 privateFlag = &cli.BoolFlag{ 37 Name: "private", 38 Usage: "include the private key 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 Private key 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 out := outputInspect{ 75 Address: key.Address.Hex(), 76 PublicKey: hex.EncodeToString( 77 crypto.FromECDSAPub(&key.PrivateKey.PublicKey)), 78 } 79 if showPrivate { 80 out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey)) 81 } 82 83 if ctx.Bool(jsonFlag.Name) { 84 mustPrintJSON(out) 85 } else { 86 fmt.Println("Address: ", out.Address) 87 fmt.Println("Public key: ", out.PublicKey) 88 if showPrivate { 89 fmt.Println("Private key: ", out.PrivateKey) 90 } 91 } 92 return nil 93 }, 94 }