mirror of
https://github.com/tendermint/tendermint.git
synced 2026-08-16 04:06:12 +00:00
@@ -67,6 +67,7 @@ Friendly reminder, we have a [bug bounty program](https://hackerone.com/tendermi
|
||||
- [evidence] [\#4532](https://github.com/tendermint/tendermint/pull/4532) Handle evidence from light clients (@melekes)
|
||||
- [light] [\#4532](https://github.com/tendermint/tendermint/pull/4532) Submit conflicting headers, if any, to a full node & all witnesses (@melekes)
|
||||
- [rpc] [\#4532](https://github.com/tendermint/tendermint/pull/4923) Support `BlockByHash` query (@fedekunze)
|
||||
- [p2p] \#4981 Expose `SaveAs` func on NodeKey (@melekes)
|
||||
|
||||
### IMPROVEMENTS:
|
||||
|
||||
|
||||
+20
-13
@@ -44,8 +44,8 @@ func PubKeyToID(pubKey crypto.PubKey) ID {
|
||||
return ID(hex.EncodeToString(pubKey.Address()))
|
||||
}
|
||||
|
||||
// LoadOrGenNodeKey attempts to load the NodeKey from the given filePath.
|
||||
// If the file does not exist, it generates and saves a new NodeKey.
|
||||
// LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If
|
||||
// the file does not exist, it generates and saves a new NodeKey.
|
||||
func LoadOrGenNodeKey(filePath string) (*NodeKey, error) {
|
||||
if tmos.FileExists(filePath) {
|
||||
nodeKey, err := LoadNodeKey(filePath)
|
||||
@@ -54,9 +54,20 @@ func LoadOrGenNodeKey(filePath string) (*NodeKey, error) {
|
||||
}
|
||||
return nodeKey, nil
|
||||
}
|
||||
return genNodeKey(filePath)
|
||||
|
||||
privKey := ed25519.GenPrivKey()
|
||||
nodeKey := &NodeKey{
|
||||
PrivKey: privKey,
|
||||
}
|
||||
|
||||
if err := nodeKey.SaveAs(filePath); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return nodeKey, nil
|
||||
}
|
||||
|
||||
// LoadNodeKey loads NodeKey located in filePath.
|
||||
func LoadNodeKey(filePath string) (*NodeKey, error) {
|
||||
jsonBytes, err := ioutil.ReadFile(filePath)
|
||||
if err != nil {
|
||||
@@ -65,26 +76,22 @@ func LoadNodeKey(filePath string) (*NodeKey, error) {
|
||||
nodeKey := new(NodeKey)
|
||||
err = cdc.UnmarshalJSON(jsonBytes, nodeKey)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("error reading NodeKey from %v: %v", filePath, err)
|
||||
return nil, err
|
||||
}
|
||||
return nodeKey, nil
|
||||
}
|
||||
|
||||
func genNodeKey(filePath string) (*NodeKey, error) {
|
||||
privKey := ed25519.GenPrivKey()
|
||||
nodeKey := &NodeKey{
|
||||
PrivKey: privKey,
|
||||
}
|
||||
|
||||
// SaveAs persists the NodeKey to filePath.
|
||||
func (nodeKey *NodeKey) SaveAs(filePath string) error {
|
||||
jsonBytes, err := cdc.MarshalJSON(nodeKey)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
err = ioutil.WriteFile(filePath, jsonBytes, 0600)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
return err
|
||||
}
|
||||
return nodeKey, nil
|
||||
return nil
|
||||
}
|
||||
|
||||
//------------------------------------------------------------------------------
|
||||
|
||||
@@ -7,7 +7,9 @@ import (
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/tendermint/tendermint/crypto/ed25519"
|
||||
tmrand "github.com/tendermint/tendermint/libs/rand"
|
||||
)
|
||||
|
||||
@@ -23,6 +25,34 @@ func TestLoadOrGenNodeKey(t *testing.T) {
|
||||
assert.Equal(t, nodeKey, nodeKey2)
|
||||
}
|
||||
|
||||
func TestLoadNodeKey(t *testing.T) {
|
||||
filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
|
||||
|
||||
_, err := LoadNodeKey(filePath)
|
||||
assert.True(t, os.IsNotExist(err))
|
||||
|
||||
_, err = LoadOrGenNodeKey(filePath)
|
||||
require.NoError(t, err)
|
||||
|
||||
nodeKey, err := LoadNodeKey(filePath)
|
||||
assert.NoError(t, err)
|
||||
assert.NotNil(t, nodeKey)
|
||||
}
|
||||
|
||||
func TestNodeKeySaveAs(t *testing.T) {
|
||||
filePath := filepath.Join(os.TempDir(), tmrand.Str(12)+"_peer_id.json")
|
||||
|
||||
assert.NoFileExists(t, filePath)
|
||||
|
||||
privKey := ed25519.GenPrivKey()
|
||||
nodeKey := &NodeKey{
|
||||
PrivKey: privKey,
|
||||
}
|
||||
err := nodeKey.SaveAs(filePath)
|
||||
assert.NoError(t, err)
|
||||
assert.FileExists(t, filePath)
|
||||
}
|
||||
|
||||
//----------------------------------------------------------
|
||||
|
||||
func padBytes(bz []byte, targetBytes int) []byte {
|
||||
|
||||
Reference in New Issue
Block a user