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