-
Notifications
You must be signed in to change notification settings - Fork 2
/
query.go
151 lines (131 loc) · 3.75 KB
/
query.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cli
import (
"encoding/base64"
"encoding/hex"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"github.com/HermitMatrixNetwork/HermitMatrixNetwork/x/registration/internal/keeper"
flag "github.com/spf13/pflag"
"github.com/spf13/cobra"
"github.com/HermitMatrixNetwork/HermitMatrixNetwork/x/registration/internal/types"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
)
func GetQueryCmd() *cobra.Command {
queryCmd := &cobra.Command{
Use: types.ModuleName,
Short: "Querying commands for the compute module",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
queryCmd.AddCommand(
GetCmdEncryptedSeed(),
GetCmdMasterParams(),
)
return queryCmd
}
// GetCmdListCode lists all wasm code uploaded
func GetCmdEncryptedSeed() *cobra.Command {
cmd := &cobra.Command{
Use: "seed [node-id]",
Short: "Get encrypted seed for a node",
Long: "Get encrypted seed for a node",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
nodeId := args[0]
if len(nodeId) != types.PublicKeyLength {
return fmt.Errorf("invalid Node ID format (req: hex string of length %d)", types.PublicKeyLength)
}
route := fmt.Sprintf("custom/%s/%s/%s", types.QuerierRoute, keeper.QueryEncryptedSeed, nodeId)
res, _, err := clientCtx.Query(route)
if err != nil {
return err
}
fmt.Println(fmt.Sprintf("0x%s", hex.EncodeToString(res)))
return nil
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
// GetCmdListCode lists all wasm code uploaded
func GetCmdMasterParams() *cobra.Command {
cmd := &cobra.Command{
Use: "secret-network-params",
Short: "Get parameters for the secret network",
Long: "Get parameters for the secret network - writes the parameters to [master-cert.der] by default",
Args: cobra.ExactArgs(0),
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientQueryContext(cmd)
if err != nil {
return err
}
route := fmt.Sprintf("custom/%s/%s", types.QuerierRoute, keeper.QueryMasterCertificate)
res, _, err := clientCtx.Query(route)
if err != nil {
return err
}
var certs types.GenesisState
err = json.Unmarshal(res, &certs)
if err != nil {
return err
}
err = ioutil.WriteFile(types.IoExchMasterCertPath, certs.IoMasterCertificate.Bytes, 0644)
if err != nil {
return err
}
err = ioutil.WriteFile(types.NodeExchMasterCertPath, certs.NodeExchMasterCertificate.Bytes, 0644)
if err != nil {
return err
}
return nil
},
}
flags.AddQueryFlagsToCmd(cmd)
return cmd
}
type argumentDecoder struct {
// dec is the default decoder
dec func(string) ([]byte, error)
asciiF, hexF, b64F bool
}
func newArgDecoder(def func(string) ([]byte, error)) *argumentDecoder {
return &argumentDecoder{dec: def}
}
func (a *argumentDecoder) RegisterFlags(f *flag.FlagSet, argName string) {
f.BoolVar(&a.asciiF, "ascii", false, "ascii encoded "+argName)
f.BoolVar(&a.hexF, "hex", false, "hex encoded "+argName)
f.BoolVar(&a.b64F, "b64", false, "base64 encoded "+argName)
}
func (a *argumentDecoder) DecodeString(s string) ([]byte, error) {
found := -1
for i, v := range []*bool{&a.asciiF, &a.hexF, &a.b64F} {
if !*v {
continue
}
if found != -1 {
return nil, errors.New("multiple decoding flags used")
}
found = i
}
switch found {
case 0:
return asciiDecodeString(s)
case 1:
return hex.DecodeString(s)
case 2:
return base64.StdEncoding.DecodeString(s)
default:
return a.dec(s)
}
}
func asciiDecodeString(s string) ([]byte, error) {
return []byte(s), nil
}