github.com/linapex/ethereum-dpos-chinese@v0.0.0-20190316121959-b78b3a4a1ece/cmd/ethkey/inspect.go (about) 1 2 //<developer> 3 // <name>linapex 曹一峰</name> 4 // <email>linapex@163.com</email> 5 // <wx>superexc</wx> 6 // <qqgroup>128148617</qqgroup> 7 // <url>https://jsq.ink</url> 8 // <role>pku engineer</role> 9 // <date>2019-03-16 12:09:27</date> 10 //</624342589314699264> 11 12 13 package main 14 15 import ( 16 "encoding/hex" 17 "fmt" 18 "io/ioutil" 19 20 "github.com/ethereum/go-ethereum/accounts/keystore" 21 "github.com/ethereum/go-ethereum/cmd/utils" 22 "github.com/ethereum/go-ethereum/crypto" 23 "gopkg.in/urfave/cli.v1" 24 ) 25 26 type outputInspect struct { 27 Address string 28 PublicKey string 29 PrivateKey string 30 } 31 32 var commandInspect = cli.Command{ 33 Name: "inspect", 34 Usage: "inspect a keyfile", 35 ArgsUsage: "<keyfile>", 36 Description: ` 37 Print various information about the keyfile. 38 39 Private key information can be printed by using the --private flag; 40 make sure to use this feature with great caution!`, 41 Flags: []cli.Flag{ 42 passphraseFlag, 43 jsonFlag, 44 cli.BoolFlag{ 45 Name: "private", 46 Usage: "include the private key in the output", 47 }, 48 }, 49 Action: func(ctx *cli.Context) error { 50 keyfilepath := ctx.Args().First() 51 52 //从文件中读取密钥。 53 keyjson, err := ioutil.ReadFile(keyfilepath) 54 if err != nil { 55 utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err) 56 } 57 58 //用密码短语解密密钥。 59 passphrase := getPassphrase(ctx) 60 key, err := keystore.DecryptKey(keyjson, passphrase) 61 if err != nil { 62 utils.Fatalf("Error decrypting key: %v", err) 63 } 64 65 //输出我们能检索到的所有相关信息。 66 showPrivate := ctx.Bool("private") 67 out := outputInspect{ 68 Address: key.Address.Hex(), 69 PublicKey: hex.EncodeToString( 70 crypto.FromECDSAPub(&key.PrivateKey.PublicKey)), 71 } 72 if showPrivate { 73 out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey)) 74 } 75 76 if ctx.Bool(jsonFlag.Name) { 77 mustPrintJSON(out) 78 } else { 79 fmt.Println("Address: ", out.Address) 80 fmt.Println("Public key: ", out.PublicKey) 81 if showPrivate { 82 fmt.Println("Private key: ", out.PrivateKey) 83 } 84 } 85 return nil 86 }, 87 } 88