-
Notifications
You must be signed in to change notification settings - Fork 16
/
context.go
149 lines (121 loc) · 3.16 KB
/
context.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
package node
import (
"errors"
"os"
"github.com/Oneledger/protocol/config"
"github.com/Oneledger/protocol/consensus"
"github.com/Oneledger/protocol/data/keys"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/privval"
)
// Context holds key information about the running node. This is generally used to
// to access its address and perform signing functions
type Context struct {
NodeName string
// Node keys
privateKey keys.PrivateKey
// Validator key
privval keys.PrivateKey
}
// PrivVal returns the private validator file
func (n Context) PrivVal() keys.PrivateKey {
return n.privval
}
// private key of the nodes
func (n Context) PrivKey() keys.PrivateKey {
return n.privateKey
}
// PubKey returns the public key of the node's NodeKey
func (n Context) PubKey() keys.PublicKey {
h, err := n.privateKey.GetHandler()
if err != nil {
return keys.PublicKey{}
}
return h.PubKey()
}
// Address returns the address of the node's public key (the key's hash)
func (n Context) Address() keys.Address {
priv, err := n.privateKey.GetHandler()
if err != nil {
return nil
}
pub, err := priv.PubKey().GetHandler()
if err != nil {
return nil
}
return pub.Address()
}
func (n Context) ValidatorPubKey() keys.PublicKey {
priv, err := n.privval.GetHandler()
if err != nil {
return keys.PublicKey{}
}
return priv.PubKey()
}
func (n Context) ValidatorAddress() keys.Address {
priv, err := n.privval.GetHandler()
if err != nil {
return nil
}
pub, err := priv.PubKey().GetHandler()
if err != nil {
return nil
}
return pub.Address()
}
func (n Context) isValid() bool {
if n.ValidatorAddress() == nil || n.Address() == nil {
return false
} //else if n.NodeName == "" { return false }
return true
}
// NewNodeContext returns a Context by reading from the specified configuration files.
// This function WILL exit if the private validator key files (priv_validator_state, and priv_validator_key) don't
// exist in the configured location
func NewNodeContext(cfg *config.Server) (*Context, error) {
if cfg == nil {
return nil, errors.New("got nil argument")
}
// (1) Read the node_keys
// Read the public key
tmcfg := consensus.Config(cfg.TMConfig())
ctx, err := readKeyFiles(&tmcfg)
if err != nil {
return nil, err
}
ctx.NodeName = cfg.Node.NodeName
if !ctx.isValid() {
return nil, errors.New("failed to read keys properly")
}
return ctx, nil
}
func readKeyFiles(cfg *consensus.Config) (*Context, error) {
nodeKeyF := cfg.NodeKeyFile()
nodeKey, err := p2p.LoadNodeKey(nodeKeyF)
if err != nil {
return nil, err
}
priv, err := keys.NodeKeyFromTendermint(nodeKey)
if err != nil {
return nil, err
}
pvKeyF := cfg.PrivValidatorKeyFile()
pvStateF := cfg.PrivValidatorStateFile()
paths := []string{pvKeyF, pvStateF}
for _, path := range paths {
_, err := os.Stat(path)
if err != nil {
return nil, errors.New("failed to find " + path)
}
}
// This function quits the process if either of these files don't exist
filePV := privval.LoadFilePV(pvKeyF, pvStateF)
pvkey, err := keys.PVKeyFromTendermint(&filePV.Key)
if err != nil {
return nil, err
}
return &Context{
privateKey: priv,
privval: pvkey,
}, nil
}