-
Notifications
You must be signed in to change notification settings - Fork 22
/
handler_chain.go
156 lines (124 loc) · 3.29 KB
/
handler_chain.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
package main
import (
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
"github.com/golang/protobuf/jsonpb"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/emptypb"
"google.golang.org/protobuf/types/known/structpb"
pb "github.com/cosmology-tech/starship/exposer/exposer"
)
func fetchNodeStatus(url string) (*pb.Status, error) {
statusResp := &pb.Status{}
resp, err := http.Get(url)
if err != nil {
return nil, fmt.Errorf("unable to fetch status, err: %d", err)
}
defer resp.Body.Close()
if err := json.NewDecoder(resp.Body).Decode(statusResp); err != nil {
return nil, fmt.Errorf("unable to parse status response, err: %d", err)
}
return statusResp, nil
}
func (a *AppServer) readJSONFile(filePath string) ([]byte, error) {
jsonFile, err := os.Open(filePath)
if err != nil {
a.logger.Error("Error opening file",
zap.String("file", filePath),
zap.Error(err))
return nil, fmt.Errorf("error opening json file: %s", filePath)
}
return io.ReadAll(jsonFile)
}
func (a *AppServer) GetNodeID(ctx context.Context, _ *emptypb.Empty) (*pb.ResponseNodeID, error) {
jsonFile, err := os.Open(a.config.NodeIDFile)
if err != nil {
return nil, err
}
data := &pb.ResponseNodeID{}
err = jsonpb.Unmarshal(jsonFile, data)
if err != nil {
return nil, err
}
return data, nil
}
func (a *AppServer) GetPubKey(ctx context.Context, _ *emptypb.Empty) (*pb.ResponsePubKey, error) {
status, err := fetchNodeStatus(a.config.StatusURL)
if err != nil {
return nil, err
}
resPubKey := &pb.ResponsePubKey{
Type: "/cosmos.crypto.ed25519.PubKey",
Key: status.Result.ValidatorInfo.PubKey.Value,
}
return resPubKey, nil
}
func (a *AppServer) GetGenesisFile(ctx context.Context, _ *emptypb.Empty) (*structpb.Struct, error) {
jsonFile, err := os.Open(a.config.GenesisFile)
if err != nil {
return nil, err
}
byteValue, err := ioutil.ReadAll(jsonFile)
if err != nil {
return nil, err
}
state := map[string]interface{}{}
err = json.Unmarshal(byteValue, &state)
if err != nil {
return nil, err
}
return structpb.NewStruct(state)
}
func (a *AppServer) GetKeys(ctx context.Context, _ *emptypb.Empty) (*pb.Keys, error) {
jsonFile, err := os.Open(a.config.MnemonicFile)
if err != nil {
return nil, err
}
keys := &pb.Keys{}
err = jsonpb.Unmarshal(jsonFile, keys)
if err != nil {
return nil, err
}
return keys, nil
}
func (a *AppServer) GetPrivKey(ctx context.Context, _ *emptypb.Empty) (*pb.PrivValidatorKey, error) {
jsonFile, err := os.Open(a.config.PrivValFile)
if err != nil {
return nil, err
}
keys := &pb.PrivValidatorKey{}
err = jsonpb.Unmarshal(jsonFile, keys)
if err != nil {
return nil, err
}
return keys, nil
}
func (a *AppServer) GetPrivValidatorState(ctx context.Context, _ *emptypb.Empty) (*pb.PrivValidatorState, error) {
jsonFile, err := os.Open(a.config.PrivValStateFile)
if err != nil {
return nil, err
}
data := &pb.PrivValidatorState{}
err = jsonpb.Unmarshal(jsonFile, data)
if err != nil {
return nil, err
}
return data, nil
}
func (a *AppServer) GetNodeKey(ctx context.Context, _ *emptypb.Empty) (*pb.NodeKey, error) {
jsonFile, err := os.Open(a.config.NodeKeyFile)
if err != nil {
return nil, err
}
keys := &pb.NodeKey{}
err = jsonpb.Unmarshal(jsonFile, keys)
if err != nil {
return nil, err
}
return keys, nil
}