/
node.go
51 lines (43 loc) · 1.13 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
package node
import (
"fmt"
"io/ioutil"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
cryptoAmino "github.com/tendermint/tendermint/crypto/encoding/amino"
"github.com/tendermint/tendermint/crypto/secp256k1"
"github.com/tendermint/tendermint/p2p"
)
var cdc = amino.NewCodec()
func init() {
cryptoAmino.RegisterAmino(cdc)
}
func GenNodeKeyByPrivKey(filePath string, privKey crypto.PrivKey) (*p2p.NodeKey, error) {
nodeKey := &p2p.NodeKey{
PrivKey: privKey,
}
jsonBytes, err := cdc.MarshalJSON(nodeKey)
if err != nil {
return nil, err
}
err = ioutil.WriteFile(filePath, jsonBytes, 0600)
if err != nil {
return nil, err
}
return nodeKey, nil
}
func GenNodeKey(filePath string) (*p2p.NodeKey, error) {
return GenNodeKeyByPrivKey(filePath, secp256k1.GenPrivKey())
}
func LoadNodeKey(filePath string) (*p2p.NodeKey, error) {
jsonBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
nodeKey := new(p2p.NodeKey)
err = cdc.UnmarshalJSON(jsonBytes, nodeKey)
if err != nil {
return nil, fmt.Errorf("Error reading NodeKey from %v: %v", filePath, err)
}
return nodeKey, nil
}