-
Notifications
You must be signed in to change notification settings - Fork 1
/
sign.go
76 lines (63 loc) · 1.93 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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package sign
import (
"encoding/hex"
"encoding/json"
"fmt"
"strings"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/client/tx"
"github.com/spf13/cobra"
)
type SignatureData struct {
Address string `json:"address"`
PubKey string `json:"pub_key"`
Signature string `json:"signature"`
Value string `json:"value"`
}
// GetSignCmd returns the command allowing to sign an arbitrary for later verification
func GetSignCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "sign [value]",
Short: "Allows to sign the given value using the private key associated to the address or key specified using the --from flag",
RunE: func(cmd *cobra.Command, args []string) error {
clientCtx, err := client.GetClientTxContext(cmd)
if err != nil {
return err
}
f := cmd.Flags()
txFactory := tx.NewFactoryCLI(clientCtx, f)
from, _ := cmd.Flags().GetString(flags.FlagFrom)
_, fromName, _, err := client.GetFromFields(clientCtx, txFactory.Keybase(), from)
if err != nil {
return fmt.Errorf("error getting account from keybase: %w", err)
}
// Get the key from the keybase
key, err := txFactory.Keybase().Key(fromName)
if err != nil {
return err
}
// Sign the data with the private key
value := []byte(args[0])
bz, pubKey, err := txFactory.Keybase().Sign(key.GetName(), value)
if err != nil {
return err
}
// Build the signature data output
signatureData := SignatureData{
Address: strings.ToLower(pubKey.Address().String()),
Signature: strings.ToLower(hex.EncodeToString(bz)),
PubKey: strings.ToLower(hex.EncodeToString(pubKey.Bytes())),
Value: hex.EncodeToString(value),
}
// Serialize the output as JSON and print it
bz, err = json.Marshal(&signatureData)
if err != nil {
return err
}
return clientCtx.PrintBytes(bz)
},
}
flags.AddTxFlagsToCmd(cmd)
return cmd
}