@@ -27,6 +27,7 @@ import (
2727 "strings"
2828
2929 "github.com/CovenantSQL/CovenantSQL/conf/testnet"
30+ "github.com/CovenantSQL/CovenantSQL/crypto"
3031 "github.com/CovenantSQL/CovenantSQL/crypto/asymmetric"
3132 "github.com/CovenantSQL/CovenantSQL/crypto/kms"
3233 "github.com/CovenantSQL/CovenantSQL/proto"
@@ -36,12 +37,17 @@ import (
3637
3738// CmdGenerate is cql generate command entity.
3839var CmdGenerate = & Command {
39- UsageLine : "cql generate [common params] config | public " ,
40- Short : "generate config related file or keys " ,
40+ UsageLine : "cql generate [common params]" ,
41+ Short : "generate a folder contains config file and private key " ,
4142 Long : `
4243Generate generates private.key and config.yaml for CovenantSQL.
44+ You can input a passphrase for local encrypt your private key file by set -no-password=false
4345e.g.
44- cql generate config
46+ cql generate
47+
48+ or input a passphrase by
49+
50+ cql generate -no-password=false
4551` ,
4652}
4753
@@ -80,27 +86,8 @@ func askDeletePath(path string) {
8086}
8187
8288func runGenerate (cmd * Command , args []string ) {
83- if len (args ) != 1 {
84- ConsoleLog .Error ("Generate command need specific type as params" )
85- SetExitStatus (1 )
86- return
87- }
88- genType := args [0 ]
89-
90- switch genType {
91- case "config" :
92- configGen ()
93- case "public" :
94- publicKey := getPublicFromConfig ()
95- fmt .Printf ("Public key's hex: %s\n " , hex .EncodeToString (publicKey .Serialize ()))
96- default :
97- cmd .Usage ()
98- SetExitStatus (1 )
99- return
100- }
101- }
89+ commonFlagsInit (cmd )
10290
103- func configGen () {
10491 workingRoot := utils .HomeDirExpand (configFile )
10592 if workingRoot == "" {
10693 ConsoleLog .Error ("config directory is required for generate config" )
@@ -124,8 +111,7 @@ func configGen() {
124111 return
125112 }
126113
127- fmt .Println ("Generating key pair..." )
128-
114+ fmt .Println ("Generating private key..." )
129115 if password == "" {
130116 password = readMasterKey (noPassword )
131117 }
@@ -142,12 +128,17 @@ func configGen() {
142128 SetExitStatus (1 )
143129 return
144130 }
131+ fmt .Println ("Generated private key." )
145132
146- fmt .Printf ("Private key file: %s\n " , privateKeyFile )
147- fmt .Printf ("Public key's hex: %s\n " , hex .EncodeToString (privateKey .PubKey ().Serialize ()))
148133 publicKey := privateKey .PubKey ()
134+ keyHash , err := crypto .PubKeyHash (publicKey )
135+ if err != nil {
136+ ConsoleLog .WithError (err ).Error ("unexpected error" )
137+ SetExitStatus (1 )
138+ return
139+ }
149140
150- fmt . Println ( "Generated key pair." )
141+ walletAddr := keyHash . String ( )
151142
152143 fmt .Println ("Generating nonce..." )
153144 nonce := nonceGen (publicKey )
@@ -159,6 +150,7 @@ func configGen() {
159150 testnetConfig := testnet .GetTestNetConfig ()
160151 // Add client config
161152 testnetConfig .PrivateKeyFile = privateKeyFileName
153+ testnetConfig .WalletAddress = walletAddr
162154 testnetConfig .ThisNodeID = cliNodeID
163155 if testnetConfig .KnownNodes == nil {
164156 testnetConfig .KnownNodes = make ([]proto.Node , 0 , 1 )
@@ -178,12 +170,22 @@ func configGen() {
178170 SetExitStatus (1 )
179171 return
180172 }
181- err = ioutil .WriteFile (path .Join (workingRoot , "config.yaml" ), out , 0644 )
173+ configFilePath := path .Join (workingRoot , "config.yaml" )
174+ err = ioutil .WriteFile (configFilePath , out , 0644 )
182175 if err != nil {
183176 ConsoleLog .WithError (err ).Error ("unexpected error" )
184177 SetExitStatus (1 )
185178 return
186179 }
187180 fmt .Println ("Generated config." )
188181
182+ fmt .Printf ("\n Config file: %s\n " , configFilePath )
183+ fmt .Printf ("Private key file: %s\n " , privateKeyFile )
184+ fmt .Printf ("Public key's hex: %s\n " , hex .EncodeToString (publicKey .Serialize ()))
185+
186+ fmt .Printf ("\n Any further command could costs PTC.\n You can get some free PTC from: https://testnet.covenansql.io/wallet/%s\n " , walletAddr )
187+
188+ if password != "" {
189+ fmt .Println ("Your private key had been encrypted by a passphrase, add -no-password=false in any further command" )
190+ }
189191}
0 commit comments