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.

113 lines
3.1 KiB

  1. package p2p
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "fmt"
  6. "io/ioutil"
  7. crypto "github.com/tendermint/tendermint/crypto"
  8. "github.com/tendermint/tendermint/crypto/ed25519"
  9. "github.com/tendermint/tendermint/crypto/tmhash"
  10. cmn "github.com/tendermint/tendermint/libs/common"
  11. )
  12. // ID is a hex-encoded crypto.Address
  13. type ID string
  14. // IDByteLength is the length of a crypto.Address. Currently only 20.
  15. // TODO: support other length addresses ?
  16. const IDByteLength = tmhash.Size
  17. //------------------------------------------------------------------------------
  18. // Persistent peer ID
  19. // TODO: encrypt on disk
  20. // NodeKey is the persistent peer key.
  21. // It contains the nodes private key for authentication.
  22. type NodeKey struct {
  23. PrivKey crypto.PrivKey `json:"priv_key"` // our priv key
  24. }
  25. // ID returns the peer's canonical ID - the hash of its public key.
  26. func (nodeKey *NodeKey) ID() ID {
  27. return PubKeyToID(nodeKey.PubKey())
  28. }
  29. // PubKey returns the peer's PubKey
  30. func (nodeKey *NodeKey) PubKey() crypto.PubKey {
  31. return nodeKey.PrivKey.PubKey()
  32. }
  33. // PubKeyToID returns the ID corresponding to the given PubKey.
  34. // It's the hex-encoding of the pubKey.Address().
  35. func PubKeyToID(pubKey crypto.PubKey) ID {
  36. return ID(hex.EncodeToString(pubKey.Address()))
  37. }
  38. // LoadOrGenNodeKey attempts to load the NodeKey from the given filePath.
  39. // If the file does not exist, it generates and saves a new NodeKey.
  40. func LoadOrGenNodeKey(filePath string) (*NodeKey, error) {
  41. if cmn.FileExists(filePath) {
  42. nodeKey, err := LoadNodeKey(filePath)
  43. if err != nil {
  44. return nil, err
  45. }
  46. return nodeKey, nil
  47. }
  48. return genNodeKey(filePath)
  49. }
  50. func LoadNodeKey(filePath string) (*NodeKey, error) {
  51. jsonBytes, err := ioutil.ReadFile(filePath)
  52. if err != nil {
  53. return nil, err
  54. }
  55. nodeKey := new(NodeKey)
  56. err = cdc.UnmarshalJSON(jsonBytes, nodeKey)
  57. if err != nil {
  58. return nil, fmt.Errorf("Error reading NodeKey from %v: %v", filePath, err)
  59. }
  60. return nodeKey, nil
  61. }
  62. func genNodeKey(filePath string) (*NodeKey, error) {
  63. privKey := ed25519.GenPrivKey()
  64. nodeKey := &NodeKey{
  65. PrivKey: privKey,
  66. }
  67. jsonBytes, err := cdc.MarshalJSON(nodeKey)
  68. if err != nil {
  69. return nil, err
  70. }
  71. err = ioutil.WriteFile(filePath, jsonBytes, 0600)
  72. if err != nil {
  73. return nil, err
  74. }
  75. return nodeKey, nil
  76. }
  77. //------------------------------------------------------------------------------
  78. // MakePoWTarget returns the big-endian encoding of 2^(targetBits - difficulty) - 1.
  79. // It can be used as a Proof of Work target.
  80. // NOTE: targetBits must be a multiple of 8 and difficulty must be less than targetBits.
  81. func MakePoWTarget(difficulty, targetBits uint) []byte {
  82. if targetBits%8 != 0 {
  83. panic(fmt.Sprintf("targetBits (%d) not a multiple of 8", targetBits))
  84. }
  85. if difficulty >= targetBits {
  86. panic(fmt.Sprintf("difficulty (%d) >= targetBits (%d)", difficulty, targetBits))
  87. }
  88. targetBytes := targetBits / 8
  89. zeroPrefixLen := (int(difficulty) / 8)
  90. prefix := bytes.Repeat([]byte{0}, zeroPrefixLen)
  91. mod := (difficulty % 8)
  92. if mod > 0 {
  93. nonZeroPrefix := byte(1<<(8-mod) - 1)
  94. prefix = append(prefix, nonZeroPrefix)
  95. }
  96. tailLen := int(targetBytes) - len(prefix)
  97. return append(prefix, bytes.Repeat([]byte{0xFF}, tailLen)...)
  98. }