-
Notifications
You must be signed in to change notification settings - Fork 13
/
tx.go
213 lines (182 loc) · 6.29 KB
/
tx.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
package cli
import (
"fmt"
"io/ioutil"
"strconv"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/gitopia/gitopia/v2/x/offchain/types"
)
// GetTxCmd clubs together all the CLI tx commands
func GetTxCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "offchain",
Short: "Offchain transactions subcommands",
DisableFlagParsing: true,
SuggestionsMinimumDistance: 2,
RunE: client.ValidateCmd,
}
cmd.AddCommand(
CmdVerifyMsg(),
CmdSignMsg(),
)
return cmd
}
func CmdVerifyMsg() *cobra.Command {
cmd := &cobra.Command{
Use: "verify file",
Aliases: []string{"v", "verify-msg", "verify-message", "vm"},
Short: "verify an arbitrary message signature",
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
verifier := types.NewVerifier(clientCtx.TxConfig.SignModeHandler())
outputFormat, err := cmd.Flags().GetString("format")
if err != nil {
return err
}
outputFile, err := cmd.Flags().GetString("file")
if err != nil {
return err
}
if len(args) == 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "you must provide the file containing the signed transaction")
}
file := args[0]
data, err := ioutil.ReadFile(file)
if err != nil {
return err
}
txData, err := clientCtx.TxConfig.TxJSONDecoder()(data)
err = verifier.Verify(txData)
if err != nil {
return err
}
msgs := make([]*types.MsgSignData, len(txData.GetMsgs()))
for i, msg := range txData.GetMsgs() {
msgSign, ok := msg.(*types.MsgSignData)
if !ok {
return fmt.Errorf("invalid message type %T at index %v", msg, i)
}
msgs[i] = msgSign
}
switch outputFormat {
case "json":
data = clientCtx.Codec.MustMarshalJSON(&types.ListOfMsgSignData{
Msgs: msgs,
})
fmt.Println(string(data))
case "simple":
fmt.Printf("%v messages in transaction\n", len(msgs))
for index, msg := range msgs {
fmt.Printf("%v. Signer: %s\n", index+1, msg.Signer)
}
case "text":
fmt.Printf("%v messages in transaction\n", len(msgs))
for index, msg := range msgs {
fmt.Printf("%v.\n\tSigner: %s\n\tData: %s\n", index+1, msg.Signer, msg.Data)
}
case "file":
for index, msg := range msgs {
filename := outputFile
if len(filename) == 0 {
filename = file + ".orig"
}
if len(msgs) > 1 {
filename += "." + strconv.Itoa(index)
}
err := ioutil.WriteFile(filename, msg.Data, 0660)
if err != nil {
return err
}
fmt.Printf("%v.\n\tSigner: %s\n\tFile: %s\n", index+1, msg.Signer, filename)
}
default:
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "invalid output format type %v", outputFormat)
}
return nil
},
}
cmd.Flags().StringP("format", "f", "simple", "This determines the output format, the default format is the simple format (simple|text|json|file). "+
"\nThe simple format just displays the signers for each message."+
"The text format displays the signer and the textual representation of data for each message. "+
"The JSON output is a ListOfMsgSignData serialized object, with base64 encoded data. "+
"If the file format is selected, then each message will be written in a file, with the signer written in the standard output")
cmd.Flags().StringP("file", "o", "", "This determines the name of the output file, if the chosen format is file."+
" If multiple messages are present, a number will be appended to the filename")
return cmd
}
func CmdSignMsg() *cobra.Command {
cmd := &cobra.Command{
Use: "sign destination-file",
Aliases: []string{"s", "sign-message", "sign-msg"},
Short: "sign an arbitrary message",
RunE: func(cmd *cobra.Command, args []string) (err error) {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
signerAddr := clientCtx.GetFromAddress()
if signerAddr == nil {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "you must provide the --from flag")
}
if len(args) != 1 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "you must provide a destination file for the signed message")
}
destFile := args[0]
file, err := cmd.Flags().GetString("file")
if err != nil {
return err
}
stringData, err := cmd.Flags().GetString("text")
if err != nil {
return err
}
var data []byte
if len(file) != 0 && len(stringData) != 0 {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "you must provide only one of --file or --text flags")
} else if len(file) != 0 {
data, err = ioutil.ReadFile(file)
if err != nil {
return err
}
} else if len(stringData) != 0 {
data = []byte(stringData)
} else {
return sdkerrors.Wrapf(sdkerrors.ErrInvalidRequest, "you must provide at least one of --file or --text flags")
}
msg := types.NewMsgSignData(signerAddr, data)
signer, err := types.NewSignerFromClientContext(clientCtx)
if err != nil {
return sdkerrors.Wrapf(err, "error while creating the signer object")
}
txObj, err := signer.Sign([]sdk.Msg{msg})
if err != nil {
return err
}
txData, err := clientCtx.TxConfig.TxJSONEncoder()(txObj)
if err != nil {
return err
}
return ioutil.WriteFile(destFile, txData, 0660)
},
}
// add flags
addKeyFlags(cmd.Flags())
cmd.Flags().StringP("file", "f", "", "File to sign (only one of file or text must be provided)")
cmd.Flags().StringP("text", "t", "", "Text data to sign (only one of file or text must be provided)")
return cmd
}
// addKeyFlags adds all flags relating to authentication and private key management
func addKeyFlags(fs *pflag.FlagSet) {
fs.String(flags.FlagFrom, "", "Name or address of private key with which to sign")
fs.String(flags.FlagKeyringBackend, flags.DefaultKeyringBackend, "Select keyring's backend (os|file|kwallet|pass|test|memory)")
fs.Bool(flags.FlagUseLedger, false, "Use a connected Ledger device")
fs.String(flags.FlagKeyringDir, "", "The client Keyring directory; if omitted, the default 'home' directory will be used")
}