/
seed.go
130 lines (114 loc) · 2.95 KB
/
seed.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright Fuzamei Corp. 2018 All Rights Reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package commands
import (
"fmt"
"github.com/33cn/chain33/rpc/jsonclient"
rpctypes "github.com/33cn/chain33/rpc/types"
"github.com/33cn/chain33/types"
"github.com/spf13/cobra"
)
// SeedCmd seed command
func SeedCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "seed",
Short: "Seed management",
Args: cobra.MinimumNArgs(1),
}
cmd.AddCommand(
GenSeedCmd(),
GetSeedCmd(),
SaveSeedCmd(),
)
return cmd
}
// GenSeedCmd generate seed
func GenSeedCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "generate",
Short: "Generate seed",
Run: genSeed,
}
addGenSeedFlags(cmd)
return cmd
}
func addGenSeedFlags(cmd *cobra.Command) {
cmd.Flags().Int32P("lang", "l", 0, "seed language(0:English, 1:简体中文)")
cmd.MarkFlagRequired("lang")
}
func genSeed(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
lang, _ := cmd.Flags().GetInt32("lang")
params := types.GenSeedLang{
Lang: lang,
}
var res types.ReplySeed
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.GenSeed", ¶ms, &res)
_, err := ctx.RunResult()
if err != nil {
fmt.Println(err)
return
}
fmt.Println(res.Seed)
}
// GetSeedCmd get seed
func GetSeedCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "get",
Short: "Get seed by password",
Run: getSeed,
}
addGetSeedFlags(cmd)
return cmd
}
func addGetSeedFlags(cmd *cobra.Command) {
cmd.Flags().StringP("pwd", "p", "", "password used to fetch seed")
cmd.MarkFlagRequired("pwd")
}
func getSeed(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
pwd, _ := cmd.Flags().GetString("pwd")
params := types.GetSeedByPw{
Passwd: pwd,
}
var res types.ReplySeed
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.GetSeed", ¶ms, &res)
ctx.Run()
}
// SaveSeedCmd save seed
func SaveSeedCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "save",
Short: "Save seed and encrypt with passwd",
Run: saveSeed,
}
addSaveSeedFlags(cmd)
return cmd
}
func addSaveSeedFlags(cmd *cobra.Command) {
cmd.Flags().StringP("seed", "s", "", "15 seed characters separated by space")
cmd.MarkFlagRequired("seed")
cmd.Flags().StringP("pwd", "p", "", "password used to encrypt seed, [8-30]letter and digit")
cmd.MarkFlagRequired("pwd")
}
func saveSeed(cmd *cobra.Command, args []string) {
rpcLaddr, _ := cmd.Flags().GetString("rpc_laddr")
seed, _ := cmd.Flags().GetString("seed")
pwd, _ := cmd.Flags().GetString("pwd")
params := types.SaveSeedByPw{
Seed: seed,
Passwd: pwd,
}
var res rpctypes.Reply
ctx := jsonclient.NewRPCCtx(rpcLaddr, "Chain33.SaveSeed", ¶ms, &res)
ctx.Run()
//钱包解锁60s
params1 := types.WalletUnLock{
Passwd: pwd,
Timeout: 60,
WalletOrTicket: false,
}
var res1 rpctypes.Reply
jsonclient.NewRPCCtx(rpcLaddr, "Chain33.UnLock", ¶ms1, &res1)
}