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.0 KiB

  1. package p2p
  2. import (
  3. "bytes"
  4. "encoding/hex"
  5. "encoding/json"
  6. "fmt"
  7. "io/ioutil"
  8. crypto "github.com/tendermint/go-crypto"
  9. cmn "github.com/tendermint/tmlibs/common"
  10. )
  11. // ID is a hex-encoded crypto.Address
  12. type ID string
  13. // IDByteLength is the length of a crypto.Address. Currently only 20.
  14. // TODO: support other length addresses ?
  15. const IDByteLength = 20
  16. //------------------------------------------------------------------------------
  17. // Persistent peer ID
  18. // TODO: encrypt on disk
  19. // NodeKey is the persistent peer key.
  20. // It contains the nodes private key for authentication.
  21. type NodeKey struct {
  22. PrivKey crypto.PrivKey `json:"priv_key"` // our priv key
  23. }
  24. // ID returns the peer's canonical ID - the hash of its public key.
  25. func (nodeKey *NodeKey) ID() ID {
  26. return PubKeyToID(nodeKey.PubKey())
  27. }
  28. // PubKey returns the peer's PubKey
  29. func (nodeKey *NodeKey) PubKey() crypto.PubKey {
  30. return nodeKey.PrivKey.PubKey()
  31. }
  32. // PubKeyToID returns the ID corresponding to the given PubKey.
  33. // It's the hex-encoding of the pubKey.Address().
  34. func PubKeyToID(pubKey crypto.PubKey) ID {
  35. return ID(hex.EncodeToString(pubKey.Address()))
  36. }
  37. // LoadOrGenNodeKey attempts to load the NodeKey from the given filePath.
  38. // If the file does not exist, it generates and saves a new NodeKey.
  39. func LoadOrGenNodeKey(filePath string) (*NodeKey, error) {
  40. if cmn.FileExists(filePath) {
  41. nodeKey, err := loadNodeKey(filePath)
  42. if err != nil {
  43. return nil, err
  44. }
  45. return nodeKey, nil
  46. } else {
  47. return genNodeKey(filePath)
  48. }
  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 = json.Unmarshal(jsonBytes, nodeKey)
  57. if err != nil {
  58. return nil, fmt.Errorf("Error reading NodeKey from %v: %v\n", filePath, err)
  59. }
  60. return nodeKey, nil
  61. }
  62. func genNodeKey(filePath string) (*NodeKey, error) {
  63. privKey := crypto.GenPrivKeyEd25519().Wrap()
  64. nodeKey := &NodeKey{
  65. PrivKey: privKey,
  66. }
  67. jsonBytes, err := json.Marshal(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. }