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.

99 lines
2.4 KiB

  1. package p2p
  2. import (
  3. "encoding/hex"
  4. "io/ioutil"
  5. "github.com/tendermint/tendermint/crypto"
  6. "github.com/tendermint/tendermint/crypto/ed25519"
  7. tmjson "github.com/tendermint/tendermint/libs/json"
  8. tmos "github.com/tendermint/tendermint/libs/os"
  9. )
  10. // ID is a hex-encoded crypto.Address
  11. type ID string
  12. // IDByteLength is the length of a crypto.Address. Currently only 20.
  13. // TODO: support other length addresses ?
  14. const IDByteLength = crypto.AddressSize
  15. //------------------------------------------------------------------------------
  16. // Persistent peer ID
  17. // TODO: encrypt on disk
  18. // NodeKey is the persistent peer key.
  19. // It contains the nodes private key for authentication.
  20. type NodeKey struct {
  21. // Canonical ID - hex-encoded pubkey's address (IDByteLength bytes)
  22. ID ID `json:"id"`
  23. // Private key
  24. PrivKey crypto.PrivKey `json:"priv_key"`
  25. }
  26. // PubKey returns the peer's PubKey
  27. func (nodeKey NodeKey) PubKey() crypto.PubKey {
  28. return nodeKey.PrivKey.PubKey()
  29. }
  30. // SaveAs persists the NodeKey to filePath.
  31. func (nodeKey NodeKey) SaveAs(filePath string) error {
  32. jsonBytes, err := tmjson.Marshal(nodeKey)
  33. if err != nil {
  34. return err
  35. }
  36. err = ioutil.WriteFile(filePath, jsonBytes, 0600)
  37. if err != nil {
  38. return err
  39. }
  40. return nil
  41. }
  42. // PubKeyToID returns the ID corresponding to the given PubKey.
  43. // It's the hex-encoding of the pubKey.Address().
  44. func PubKeyToID(pubKey crypto.PubKey) ID {
  45. return ID(hex.EncodeToString(pubKey.Address()))
  46. }
  47. // LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If
  48. // the file does not exist, it generates and saves a new NodeKey.
  49. func LoadOrGenNodeKey(filePath string) (NodeKey, error) {
  50. if tmos.FileExists(filePath) {
  51. nodeKey, err := LoadNodeKey(filePath)
  52. if err != nil {
  53. return NodeKey{}, err
  54. }
  55. return nodeKey, nil
  56. }
  57. nodeKey := GenNodeKey()
  58. if err := nodeKey.SaveAs(filePath); err != nil {
  59. return NodeKey{}, err
  60. }
  61. return nodeKey, nil
  62. }
  63. // GenNodeKey generates a new node key.
  64. func GenNodeKey() NodeKey {
  65. privKey := ed25519.GenPrivKey()
  66. return NodeKey{
  67. ID: PubKeyToID(privKey.PubKey()),
  68. PrivKey: privKey,
  69. }
  70. }
  71. // LoadNodeKey loads NodeKey located in filePath.
  72. func LoadNodeKey(filePath string) (NodeKey, error) {
  73. jsonBytes, err := ioutil.ReadFile(filePath)
  74. if err != nil {
  75. return NodeKey{}, err
  76. }
  77. nodeKey := NodeKey{}
  78. err = tmjson.Unmarshal(jsonBytes, &nodeKey)
  79. if err != nil {
  80. return NodeKey{}, err
  81. }
  82. nodeKey.ID = PubKeyToID(nodeKey.PubKey())
  83. return nodeKey, nil
  84. }