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.

112 lines
3.0 KiB

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