/
context.go
210 lines (167 loc) · 4.49 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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package node
import (
"encoding/base64"
"errors"
"io/ioutil"
"os"
"strings"
"github.com/btcsuite/btcd/chaincfg"
"github.com/btcsuite/btcutil"
"github.com/tendermint/tendermint/p2p"
"github.com/tendermint/tendermint/privval"
"github.com/Oneledger/protocol/config"
"github.com/Oneledger/protocol/consensus"
"github.com/Oneledger/protocol/data/keys"
)
// 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
// validator ECDSA key
ecdsaPrivVal keys.PrivateKey //Used BY ETHEREUM TX
}
// 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
}
//func (n Context) EthPrivKey() *ecdsa.PrivateKey {
// return &n.ethPrivKey
//}
// 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) ValidatorECDSAPubKey() keys.PublicKey {
priv, err := n.ecdsaPrivVal.GetHandler()
if err != nil {
return keys.PublicKey{}
}
return priv.PubKey()
}
func (n Context) ValidatorECDSAPrivateKey() *keys.PrivateKey {
return &n.ecdsaPrivVal
}
func (n Context) ValidatorBTCScriptAddress(params *chaincfg.Params) (keys.Address, error) {
h, err := n.ecdsaPrivVal.GetHandler()
if err != nil {
return nil, err
}
apk, err := btcutil.NewAddressPubKey(h.PubKey().Data, params)
if err != nil {
return nil, err
}
return apk.ScriptAddress(), nil
}
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
}
ecdsaFile := strings.Replace(pvKeyF, ".json", "_ecdsa.json", 1)
ecdsaPrivateKeyB64, err := ioutil.ReadFile(ecdsaFile)
if err != nil {
return nil, err
}
ecdsaPrivateKey, err := base64.StdEncoding.DecodeString(string(ecdsaPrivateKeyB64))
if err != nil {
return nil, err
}
//, err := keys.GetPrivateKeyFromBytes(ecdsaPrivateKey, keys.BTCECSECP)
ecdsaPrivKey, err := keys.GetPrivateKeyFromBytes([]byte(ecdsaPrivateKey[:]), keys.SECP256K1)
if err != nil {
return nil, err
}
return &Context{
privateKey: priv,
privval: pvkey,
ecdsaPrivVal: ecdsaPrivKey,
}, nil
}