This repository has been archived by the owner on Dec 13, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
node.go
118 lines (98 loc) · 2.64 KB
/
node.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
package cmd
import (
"encoding/json"
"fmt"
"github.com/spf13/cobra"
"github.com/tendermint/tendermint/p2p"
"github.com/cosmos/cosmos-sdk/client"
cryptocodec "github.com/cosmos/cosmos-sdk/crypto/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server"
)
type SignInfo struct {
NodeID string `json:"node_id"`
PubKey string `json:"pub_key"`
Signature []byte `json:"signature"`
}
// NodeCmd creates a main CLI command
func NodeCmd() *cobra.Command {
cmd := &cobra.Command{
Use: "node",
Short: "Sign and Verify node information",
RunE: client.ValidateCmd,
}
cmd.AddCommand(SignCmd())
cmd.AddCommand(VerifyCmd())
return cmd
}
func SignCmd() *cobra.Command {
return &cobra.Command{
Use: "sign-info",
Short: "sign with nodeID and export signature",
RunE: func(cmd *cobra.Command, args []string) error {
serverCtx := server.GetServerContextFromCmd(cmd)
cfg := serverCtx.Config
nodeKey, err := p2p.LoadNodeKey(cfg.NodeKeyFile())
if err != nil {
return err
}
pubKey := nodeKey.PubKey()
nodeID := string(p2p.PubKeyToID(pubKey))
sdkPK, err := cryptocodec.FromTmPubKeyInterface(pubKey)
if err != nil {
return err
}
sigByte, err := nodeKey.PrivKey.Sign([]byte(nodeID))
if err != nil {
return err
}
clientCtx := client.GetClientContextFromCmd(cmd)
pkByte, err := clientCtx.Codec.MarshalInterfaceJSON(sdkPK)
if err != nil {
return err
}
nodeInfo := SignInfo{
NodeID: nodeID,
PubKey: string(pkByte),
Signature: sigByte,
}
bz, err := json.Marshal(nodeInfo)
if err != nil {
return err
}
fmt.Println(string(bz))
return nil
},
}
}
func VerifyCmd() *cobra.Command {
return &cobra.Command{
Use: "verify [sign-info]",
Short: "Verify a node sign-info",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
var signInfo SignInfo
if err := json.Unmarshal([]byte(args[0]), &signInfo); err != nil {
return err
}
clientCtx := client.GetClientContextFromCmd(cmd)
var sdkPK cryptotypes.PubKey
if err := clientCtx.Codec.UnmarshalInterfaceJSON([]byte(signInfo.PubKey), &sdkPK); err != nil {
return err
}
pubKey, err := cryptocodec.ToTmPubKeyInterface(sdkPK)
if err != nil {
return err
}
nodeID := string(p2p.PubKeyToID(pubKey))
if nodeID != signInfo.NodeID {
return fmt.Errorf("invalid sign-info,except node_id : %s, but got :%s", nodeID, signInfo.NodeID)
}
if !pubKey.VerifySignature([]byte(nodeID), signInfo.Signature) {
return fmt.Errorf("invalid signature")
}
fmt.Println("verify success")
return nil
},
}
}