github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/ethkey/inspect.go (about) 1 2 //此源码被清华学神尹成大魔王专业翻译分析并修改 3 //尹成QQ77025077 4 //尹成微信18510341407 5 //尹成所在QQ群721929980 6 //尹成邮箱 yinc13@mails.tsinghua.edu.cn 7 //尹成毕业于清华大学,微软区块链领域全球最有价值专家 8 //https://mvp.microsoft.com/zh-cn/PublicProfile/4033620 9 //版权所有2017 Go Ethereum作者 10 //此文件是Go以太坊的一部分。 11 // 12 //Go以太坊是免费软件:您可以重新发布和/或修改它 13 //根据GNU通用公共许可证的条款 14 //自由软件基金会,或者许可证的第3版,或者 15 //(由您选择)任何更高版本。 16 // 17 //Go以太坊的分布希望它会有用, 18 //但没有任何保证;甚至没有 19 //适销性或特定用途的适用性。见 20 //GNU通用公共许可证了解更多详细信息。 21 // 22 //你应该已经收到一份GNU通用公共许可证的副本 23 //一起去以太坊吧。如果没有,请参见<http://www.gnu.org/licenses/>。 24 25 package main 26 27 import ( 28 "encoding/hex" 29 "fmt" 30 "io/ioutil" 31 32 "github.com/ethereum/go-ethereum/accounts/keystore" 33 "github.com/ethereum/go-ethereum/cmd/utils" 34 "github.com/ethereum/go-ethereum/crypto" 35 "gopkg.in/urfave/cli.v1" 36 ) 37 38 type outputInspect struct { 39 Address string 40 PublicKey string 41 PrivateKey string 42 } 43 44 var commandInspect = cli.Command{ 45 Name: "inspect", 46 Usage: "inspect a keyfile", 47 ArgsUsage: "<keyfile>", 48 Description: ` 49 Print various information about the keyfile. 50 51 Private key information can be printed by using the --private flag; 52 make sure to use this feature with great caution!`, 53 Flags: []cli.Flag{ 54 passphraseFlag, 55 jsonFlag, 56 cli.BoolFlag{ 57 Name: "private", 58 Usage: "include the private key in the output", 59 }, 60 }, 61 Action: func(ctx *cli.Context) error { 62 keyfilepath := ctx.Args().First() 63 64 //从文件中读取密钥。 65 keyjson, err := ioutil.ReadFile(keyfilepath) 66 if err != nil { 67 utils.Fatalf("Failed to read the keyfile at '%s': %v", keyfilepath, err) 68 } 69 70 //用密码短语解密密钥。 71 passphrase := getPassphrase(ctx) 72 key, err := keystore.DecryptKey(keyjson, passphrase) 73 if err != nil { 74 utils.Fatalf("Error decrypting key: %v", err) 75 } 76 77 //输出我们能检索到的所有相关信息。 78 showPrivate := ctx.Bool("private") 79 out := outputInspect{ 80 Address: key.Address.Hex(), 81 PublicKey: hex.EncodeToString( 82 crypto.FromECDSAPub(&key.PrivateKey.PublicKey)), 83 } 84 if showPrivate { 85 out.PrivateKey = hex.EncodeToString(crypto.FromECDSA(key.PrivateKey)) 86 } 87 88 if ctx.Bool(jsonFlag.Name) { 89 mustPrintJSON(out) 90 } else { 91 fmt.Println("Address: ", out.Address) 92 fmt.Println("Public key: ", out.PublicKey) 93 if showPrivate { 94 fmt.Println("Private key: ", out.PrivateKey) 95 } 96 } 97 return nil 98 }, 99 }