tendermint/p2p/key.go

112 lines
3.0 KiB
Go
Raw Normal View History

2018-01-01 17:21:42 -08:00
package p2p
import (
"bytes"
2018-01-01 18:27:38 -08:00
"encoding/hex"
2018-01-01 17:21:42 -08:00
"fmt"
"io/ioutil"
crypto "github.com/tendermint/go-crypto"
cmn "github.com/tendermint/tmlibs/common"
)
2018-01-13 13:06:31 -08:00
// ID is a hex-encoded crypto.Address
type ID string
2018-01-13 13:06:31 -08:00
// IDByteLength is the length of a crypto.Address. Currently only 20.
// TODO: support other length addresses ?
const IDByteLength = 20
2018-01-01 17:21:42 -08:00
//------------------------------------------------------------------------------
// Persistent peer ID
// TODO: encrypt on disk
// NodeKey is the persistent peer key.
// It contains the nodes private key for authentication.
type NodeKey struct {
PrivKey crypto.PrivKey `json:"priv_key"` // our priv key
}
// ID returns the peer's canonical ID - the hash of its public key.
2018-01-01 18:27:38 -08:00
func (nodeKey *NodeKey) ID() ID {
2018-01-13 13:14:28 -08:00
return PubKeyToID(nodeKey.PubKey())
2018-01-01 17:21:42 -08:00
}
// PubKey returns the peer's PubKey
func (nodeKey *NodeKey) PubKey() crypto.PubKey {
return nodeKey.PrivKey.PubKey()
}
2018-01-13 13:14:28 -08:00
// PubKeyToID returns the ID corresponding to the given PubKey.
// It's the hex-encoding of the pubKey.Address().
func PubKeyToID(pubKey crypto.PubKey) ID {
return ID(hex.EncodeToString(pubKey.Address()))
2018-01-01 18:27:38 -08:00
}
2018-01-13 12:38:40 -08:00
// 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) {
2018-01-01 17:21:42 -08:00
if cmn.FileExists(filePath) {
nodeKey, err := LoadNodeKey(filePath)
2018-01-01 17:21:42 -08:00
if err != nil {
return nil, err
}
return nodeKey, nil
}
return genNodeKey(filePath)
2018-01-01 17:21:42 -08:00
}
func LoadNodeKey(filePath string) (*NodeKey, error) {
2018-01-01 17:21:42 -08:00
jsonBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, err
}
nodeKey := new(NodeKey)
2018-03-25 21:40:02 -07:00
err = cdc.UnmarshalJSON(jsonBytes, nodeKey)
2018-01-01 17:21:42 -08:00
if err != nil {
return nil, fmt.Errorf("Error reading NodeKey from %v: %v", filePath, err)
2018-01-01 17:21:42 -08:00
}
return nodeKey, nil
}
2018-01-13 12:38:40 -08:00
func genNodeKey(filePath string) (*NodeKey, error) {
2018-03-25 21:40:02 -07:00
privKey := crypto.GenPrivKeyEd25519()
2018-01-01 17:21:42 -08:00
nodeKey := &NodeKey{
PrivKey: privKey,
}
2018-03-25 21:40:02 -07:00
jsonBytes, err := cdc.MarshalJSON(nodeKey)
2018-01-01 17:21:42 -08:00
if err != nil {
return nil, err
}
err = ioutil.WriteFile(filePath, jsonBytes, 0600)
if err != nil {
return nil, err
}
return nodeKey, nil
}
2018-01-13 12:38:40 -08:00
//------------------------------------------------------------------------------
2018-01-01 17:21:42 -08:00
2018-01-13 12:38:40 -08:00
// MakePoWTarget returns the big-endian encoding of 2^(targetBits - difficulty) - 1.
// It can be used as a Proof of Work target.
// NOTE: targetBits must be a multiple of 8 and difficulty must be less than targetBits.
func MakePoWTarget(difficulty, targetBits uint) []byte {
if targetBits%8 != 0 {
panic(fmt.Sprintf("targetBits (%d) not a multiple of 8", targetBits))
}
if difficulty >= targetBits {
panic(fmt.Sprintf("difficulty (%d) >= targetBits (%d)", difficulty, targetBits))
}
targetBytes := targetBits / 8
zeroPrefixLen := (int(difficulty) / 8)
prefix := bytes.Repeat([]byte{0}, zeroPrefixLen)
mod := (difficulty % 8)
if mod > 0 {
nonZeroPrefix := byte(1<<(8-mod) - 1)
prefix = append(prefix, nonZeroPrefix)
2018-01-01 17:21:42 -08:00
}
2018-01-13 12:38:40 -08:00
tailLen := int(targetBytes) - len(prefix)
return append(prefix, bytes.Repeat([]byte{0xFF}, tailLen)...)
2018-01-01 17:21:42 -08:00
}