Skip to content

Commit 955ff13

Browse files
laodouyalaodouya
authored andcommitted
Refactor cql generate command.
1 parent 904bb3b commit 955ff13

4 files changed

Lines changed: 42 additions & 35 deletions

File tree

cmd/cql/internal/cfg.go

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,14 @@ func addCommonFlags(cmd *Command) {
6060
"Console log level: trace debug info warning error fatal panic")
6161
cmd.Flag.StringVar(&password, "password", "",
6262
"Master key password for covenantsql (NOT SAFE, for debug or script only)")
63-
cmd.Flag.BoolVar(&noPassword, "no-password", false,
63+
cmd.Flag.BoolVar(&noPassword, "no-password", true,
6464
"Use empty password for master key")
6565
cmd.Flag.BoolVar(&asymmetric.BypassSignature, "bypass-signature", false,
6666
"Disable signature sign and verify, for testing")
6767
cmd.Flag.BoolVar(&help, "help", false, "Show help message")
6868
}
6969

70-
func configInit(cmd *Command) {
70+
func commonFlagsInit(cmd *Command) {
7171
if help {
7272
_, _ = fmt.Fprintf(os.Stdout, "usage: %s\n", cmd.UsageLine)
7373
_, _ = fmt.Fprintf(os.Stdout, cmd.Long)
@@ -82,6 +82,10 @@ func configInit(cmd *Command) {
8282
} else {
8383
ConsoleLog.SetLevel(lvl)
8484
}
85+
}
86+
87+
func configInit(cmd *Command) {
88+
commonFlagsInit(cmd)
8589

8690
configFile = utils.HomeDirExpand(configFile)
8791

cmd/cql/internal/generate.go

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -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.
3839
var 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: `
4243
Generate 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
4345
e.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

8288
func 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("\nConfig 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("\nAny further command could costs PTC.\nYou 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
}

cmd/cql/internal/wallet.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func init() {
5050
CmdWallet.Flag.StringVar(&tokenName, "balance", "", "Get specific token's balance of current account, e.g. Particle, Wave, All")
5151
}
5252

53-
func walletGen() {
53+
func walletGen() string {
5454
//TODO if config has wallet, print and return
5555

5656
publicKey := getPublicFromConfig()
@@ -59,10 +59,10 @@ func walletGen() {
5959
if err != nil {
6060
ConsoleLog.WithError(err).Error("unexpected error")
6161
SetExitStatus(1)
62-
return
62+
return ""
6363
}
6464

65-
fmt.Printf("wallet address: %s\n", keyHash.String())
65+
return keyHash.String()
6666

6767
//TODO store in config.yaml
6868
}
@@ -72,7 +72,7 @@ func runWallet(cmd *Command, args []string) {
7272

7373
var err error
7474
if tokenName == "" {
75-
walletGen()
75+
fmt.Printf("wallet address: %s\n", walletGen())
7676
return
7777
}
7878

conf/config.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ type Config struct {
115115
WorkingRoot string `yaml:"WorkingRoot"`
116116
PubKeyStoreFile string `yaml:"PubKeyStoreFile"`
117117
PrivateKeyFile string `yaml:"PrivateKeyFile"`
118+
WalletAddress string `yaml:"WalletAddress"`
118119
DHTFileName string `yaml:"DHTFileName"`
119120
ListenAddr string `yaml:"ListenAddr"`
120121
ListenDirectAddr string `yaml:"ListenDirectAddr",omitempty`

0 commit comments

Comments
 (0)