You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

143 lines
3.4 KiB

  1. package p2p
  2. import (
  3. "encoding/hex"
  4. "errors"
  5. "fmt"
  6. "io/ioutil"
  7. "strings"
  8. "github.com/tendermint/tendermint/crypto"
  9. "github.com/tendermint/tendermint/crypto/ed25519"
  10. tmjson "github.com/tendermint/tendermint/libs/json"
  11. tmos "github.com/tendermint/tendermint/libs/os"
  12. )
  13. // NodeIDByteLength is the length of a crypto.Address. Currently only 20.
  14. // FIXME: support other length addresses?
  15. const NodeIDByteLength = crypto.AddressSize
  16. // NodeID is a hex-encoded crypto.Address.
  17. type NodeID string
  18. // NewNodeID returns a lowercased (normalized) NodeID.
  19. func NewNodeID(nodeID string) (NodeID, error) {
  20. if _, err := NodeID(nodeID).Bytes(); err != nil {
  21. return NodeID(""), err
  22. }
  23. return NodeID(strings.ToLower(nodeID)), nil
  24. }
  25. // NodeIDFromPubKey returns the noe ID corresponding to the given PubKey. It's
  26. // the hex-encoding of the pubKey.Address().
  27. func NodeIDFromPubKey(pubKey crypto.PubKey) NodeID {
  28. return NodeID(hex.EncodeToString(pubKey.Address()))
  29. }
  30. // Bytes converts the node ID to it's binary byte representation.
  31. func (id NodeID) Bytes() ([]byte, error) {
  32. bz, err := hex.DecodeString(string(id))
  33. if err != nil {
  34. return nil, fmt.Errorf("invalid node ID encoding: %w", err)
  35. }
  36. return bz, nil
  37. }
  38. // Validate validates the NodeID.
  39. func (id NodeID) Validate() error {
  40. if len(id) == 0 {
  41. return errors.New("empty node ID")
  42. }
  43. bz, err := id.Bytes()
  44. if err != nil {
  45. return err
  46. }
  47. if len(bz) != NodeIDByteLength {
  48. return fmt.Errorf("invalid node ID length; got %d, expected %d", len(bz), NodeIDByteLength)
  49. }
  50. idStr := string(id)
  51. if strings.ToLower(idStr) != idStr {
  52. return fmt.Errorf("invalid node ID; must be lowercased")
  53. }
  54. return nil
  55. }
  56. //------------------------------------------------------------------------------
  57. // Persistent peer ID
  58. // TODO: encrypt on disk
  59. // NodeKey is the persistent peer key.
  60. // It contains the nodes private key for authentication.
  61. type NodeKey struct {
  62. // Canonical ID - hex-encoded pubkey's address (IDByteLength bytes)
  63. ID NodeID `json:"id"`
  64. // Private key
  65. PrivKey crypto.PrivKey `json:"priv_key"`
  66. }
  67. // PubKey returns the peer's PubKey
  68. func (nodeKey NodeKey) PubKey() crypto.PubKey {
  69. return nodeKey.PrivKey.PubKey()
  70. }
  71. // SaveAs persists the NodeKey to filePath.
  72. func (nodeKey NodeKey) SaveAs(filePath string) error {
  73. jsonBytes, err := tmjson.Marshal(nodeKey)
  74. if err != nil {
  75. return err
  76. }
  77. err = ioutil.WriteFile(filePath, jsonBytes, 0600)
  78. if err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. // LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If
  84. // the file does not exist, it generates and saves a new NodeKey.
  85. func LoadOrGenNodeKey(filePath string) (NodeKey, error) {
  86. if tmos.FileExists(filePath) {
  87. nodeKey, err := LoadNodeKey(filePath)
  88. if err != nil {
  89. return NodeKey{}, err
  90. }
  91. return nodeKey, nil
  92. }
  93. nodeKey := GenNodeKey()
  94. if err := nodeKey.SaveAs(filePath); err != nil {
  95. return NodeKey{}, err
  96. }
  97. return nodeKey, nil
  98. }
  99. // GenNodeKey generates a new node key.
  100. func GenNodeKey() NodeKey {
  101. privKey := ed25519.GenPrivKey()
  102. return NodeKey{
  103. ID: NodeIDFromPubKey(privKey.PubKey()),
  104. PrivKey: privKey,
  105. }
  106. }
  107. // LoadNodeKey loads NodeKey located in filePath.
  108. func LoadNodeKey(filePath string) (NodeKey, error) {
  109. jsonBytes, err := ioutil.ReadFile(filePath)
  110. if err != nil {
  111. return NodeKey{}, err
  112. }
  113. nodeKey := NodeKey{}
  114. err = tmjson.Unmarshal(jsonBytes, &nodeKey)
  115. if err != nil {
  116. return NodeKey{}, err
  117. }
  118. nodeKey.ID = NodeIDFromPubKey(nodeKey.PubKey())
  119. return nodeKey, nil
  120. }