github.com/yinchengtsinghua/golang-Eos-dpos-Ethereum@v0.0.0-20190121132951-92cc4225ed8e/cmd/ethkey/generate.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 "crypto/ecdsa" 29 "fmt" 30 "io/ioutil" 31 "os" 32 "path/filepath" 33 34 "github.com/ethereum/go-ethereum/accounts/keystore" 35 "github.com/ethereum/go-ethereum/cmd/utils" 36 "github.com/ethereum/go-ethereum/crypto" 37 "github.com/pborman/uuid" 38 "gopkg.in/urfave/cli.v1" 39 ) 40 41 type outputGenerate struct { 42 Address string 43 AddressEIP55 string 44 } 45 46 var commandGenerate = cli.Command{ 47 Name: "generate", 48 Usage: "generate new keyfile", 49 ArgsUsage: "[ <keyfile> ]", 50 Description: ` 51 Generate a new keyfile. 52 53 If you want to encrypt an existing private key, it can be specified by setting 54 --privatekey with the location of the file containing the private key. 55 `, 56 Flags: []cli.Flag{ 57 passphraseFlag, 58 jsonFlag, 59 cli.StringFlag{ 60 Name: "privatekey", 61 Usage: "file containing a raw private key to encrypt", 62 }, 63 }, 64 Action: func(ctx *cli.Context) error { 65 //检查是否指定了关键文件路径,并确保它不存在。 66 keyfilepath := ctx.Args().First() 67 if keyfilepath == "" { 68 keyfilepath = defaultKeyfileName 69 } 70 if _, err := os.Stat(keyfilepath); err == nil { 71 utils.Fatalf("Keyfile already exists at %s.", keyfilepath) 72 } else if !os.IsNotExist(err) { 73 utils.Fatalf("Error checking if keyfile exists: %v", err) 74 } 75 76 var privateKey *ecdsa.PrivateKey 77 var err error 78 if file := ctx.String("privatekey"); file != "" { 79 //从文件加载私钥。 80 privateKey, err = crypto.LoadECDSA(file) 81 if err != nil { 82 utils.Fatalf("Can't load private key: %v", err) 83 } 84 } else { 85 //如果未加载,则生成随机。 86 privateKey, err = crypto.GenerateKey() 87 if err != nil { 88 utils.Fatalf("Failed to generate random private key: %v", err) 89 } 90 } 91 92 //使用随机UUID创建keyfile对象。 93 id := uuid.NewRandom() 94 key := &keystore.Key{ 95 Id: id, 96 Address: crypto.PubkeyToAddress(privateKey.PublicKey), 97 PrivateKey: privateKey, 98 } 99 100 //用密码短语加密密钥。 101 passphrase := promptPassphrase(true) 102 keyjson, err := keystore.EncryptKey(key, passphrase, keystore.StandardScryptN, keystore.StandardScryptP) 103 if err != nil { 104 utils.Fatalf("Error encrypting key: %v", err) 105 } 106 107 //将文件存储到磁盘。 108 if err := os.MkdirAll(filepath.Dir(keyfilepath), 0700); err != nil { 109 utils.Fatalf("Could not create directory %s", filepath.Dir(keyfilepath)) 110 } 111 if err := ioutil.WriteFile(keyfilepath, keyjson, 0600); err != nil { 112 utils.Fatalf("Failed to write keyfile to %s: %v", keyfilepath, err) 113 } 114 115 //输出一些信息。 116 out := outputGenerate{ 117 Address: key.Address.Hex(), 118 } 119 if ctx.Bool(jsonFlag.Name) { 120 mustPrintJSON(out) 121 } else { 122 fmt.Println("Address:", out.Address) 123 } 124 return nil 125 }, 126 }