forked from bnb-chain/node
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.go
112 lines (98 loc) · 2.55 KB
/
client.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
package admin
import (
"fmt"
"math/big"
"crypto/rand"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/context"
"github.com/pkg/errors"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/bnb-chain/node/wire"
)
const (
flagPVPath = "pvpath"
errRandF = "failed to generate random int with error: %s"
randMax = 2147483647
)
func AddCommands(cmd *cobra.Command, cdc *wire.Codec) {
adminCmd := &cobra.Command{
Use: "admin",
Short: "admin commands",
}
adminCmd.AddCommand(
client.GetCommands(
setModeCmd(cdc),
getModeCmd(cdc))...,
)
adminCmd.AddCommand(client.LineBreak)
cmd.AddCommand(adminCmd)
}
func setModeCmd(cdc *wire.Codec) *cobra.Command {
cmd := cobra.Command{
Use: "set-mode [0|1|2]",
Short: "set the current running mode, 0: Normal, 1: TransferOnly, 2: RecoverOnly",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
pvFile := viper.GetString(flagPVPath)
privKey, _, err := readPrivValidator(pvFile)
if err != nil {
return err
}
mode := args[0]
if mode == "0" || mode == "1" || mode == "2" {
cliCtx := context.NewCLIContext().WithCodec(cdc)
nonceB, err := rand.Int(rand.Reader, big.NewInt(randMax))
if err != nil {
return fmt.Errorf(errRandF, err.Error())
}
nonce := nonceB.Bytes()
sig, err := privKey.Sign([]byte(nonce))
if err != nil {
return err
}
res, err := cliCtx.QueryWithData(fmt.Sprintf("/admin/mode/%s/%s", mode, nonce), sig)
if err != nil {
return err
}
fmt.Println(res)
} else {
return errors.New("invalid mode")
}
return nil
},
}
cmd.Flags().StringP(flagPVPath, "p", "", "path of priv_val file")
return &cmd
}
func getModeCmd(cdc *wire.Codec) *cobra.Command {
cmd := cobra.Command{
Use: "get-mode",
Short: "get the current running mode",
RunE: func(cmd *cobra.Command, args []string) error {
pvFile := viper.GetString(flagPVPath)
privKey, _, err := readPrivValidator(pvFile)
if err != nil {
return err
}
cliCtx := context.NewCLIContext().WithCodec(cdc)
nonceB, err := rand.Int(rand.Reader, big.NewInt(randMax))
if err != nil {
return fmt.Errorf(errRandF, err.Error())
}
nonce := nonceB.Bytes()
sig, err := privKey.Sign([]byte(nonce))
if err != nil {
return err
}
res, err := cliCtx.QueryWithData(fmt.Sprintf("/admin/mode/%s", nonce), sig)
if err != nil {
return err
}
fmt.Println(res)
return nil
},
}
cmd.Flags().StringP(flagPVPath, "p", "", "path of priv_val file")
return &cmd
}