Skip to content

Commit

Permalink
p2p: expose SaveAs on NodeKey (tendermint#4981)
Browse files Browse the repository at this point in the history
  • Loading branch information
melekes authored Jun 8, 2020
1 parent 3b256cc commit 4d422e4
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 13 deletions.
1 change: 1 addition & 0 deletions CHANGELOG_PENDING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down
33 changes: 20 additions & 13 deletions p2p/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
}

//------------------------------------------------------------------------------
Expand Down
30 changes: 30 additions & 0 deletions p2p/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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 {
Expand Down

0 comments on commit 4d422e4

Please sign in to comment.