-
Notifications
You must be signed in to change notification settings - Fork 0
/
node.go
44 lines (39 loc) · 967 Bytes
/
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
package node
import (
"fmt"
"github.com/tendermint/go-amino"
"github.com/tendermint/tendermint/crypto"
"github.com/tendermint/tendermint/crypto/encoding/amino"
"github.com/tendermint/tendermint/p2p"
"io/ioutil"
)
var cdc = amino.NewCodec()
func init() {
cryptoAmino.RegisterAmino(cdc)
}
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
}
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
}