-
Notifications
You must be signed in to change notification settings - Fork 20
/
sign.go
58 lines (46 loc) · 1.37 KB
/
sign.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
package cmd
import (
"encoding/hex"
"fmt"
"github.com/spf13/cobra"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
)
// GetSignBytesCommand returns the string sign command.
func GetSignBytesCommand() *cobra.Command {
cmd := &cobra.Command{
Use: "sign_bytes [sign_data_string]",
Short: "Sign a string with an account",
Long: `Sign a string with an account.`,
RunE: makeSignCmd(),
Args: cobra.ExactArgs(1),
}
cmd.Flags().String(flags.FlagOutputDocument, "", "The document will be written to the given file instead of STDOUT")
cmd.Flags().String(flags.FlagChainID, "", "The network chain ID")
flags.AddTxFlagsToCmd(cmd)
cmd.MarkFlagRequired(flags.FlagFrom)
return cmd
}
func makeSignCmd() func(cmd *cobra.Command, args []string) error {
return func(cmd *cobra.Command, args []string) (err error) {
var clientCtx client.Context
clientCtx, err = client.GetClientTxContext(cmd)
if err != nil {
return err
}
from, _ := cmd.Flags().GetString(flags.FlagFrom)
// Sign those bytes
txFactory := tx.NewFactoryCLI(clientCtx, cmd.Flags())
bytesToSign, err := hex.DecodeString(args[0])
if err != nil {
return err
}
sigBytes, _, err := txFactory.Keybase().Sign(from, bytesToSign)
if err != nil {
return err
}
fmt.Println(hex.EncodeToString(sigBytes))
return nil
}
}