node.go 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright 2015 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. package discover
  17. import (
  18. "crypto/ecdsa"
  19. "crypto/elliptic"
  20. "errors"
  21. "math/big"
  22. "net"
  23. "time"
  24. "github.com/ethereum/go-ethereum/common/math"
  25. "github.com/ethereum/go-ethereum/crypto"
  26. "github.com/ethereum/go-ethereum/p2p/enode"
  27. )
  28. // node represents a host on the network.
  29. // The fields of Node may not be modified.
  30. type node struct {
  31. enode.Node
  32. addedAt time.Time // time when the node was added to the table
  33. livenessChecks uint // how often liveness was checked
  34. }
  35. type encPubkey [64]byte
  36. func encodePubkey(key *ecdsa.PublicKey) encPubkey {
  37. var e encPubkey
  38. math.ReadBits(key.X, e[:len(e)/2])
  39. math.ReadBits(key.Y, e[len(e)/2:])
  40. return e
  41. }
  42. func decodePubkey(curve elliptic.Curve, e []byte) (*ecdsa.PublicKey, error) {
  43. if len(e) != len(encPubkey{}) {
  44. return nil, errors.New("wrong size public key data")
  45. }
  46. p := &ecdsa.PublicKey{Curve: curve, X: new(big.Int), Y: new(big.Int)}
  47. half := len(e) / 2
  48. p.X.SetBytes(e[:half])
  49. p.Y.SetBytes(e[half:])
  50. if !p.Curve.IsOnCurve(p.X, p.Y) {
  51. return nil, errors.New("invalid curve point")
  52. }
  53. return p, nil
  54. }
  55. func (e encPubkey) id() enode.ID {
  56. return enode.ID(crypto.Keccak256Hash(e[:]))
  57. }
  58. func wrapNode(n *enode.Node) *node {
  59. return &node{Node: *n}
  60. }
  61. func wrapNodes(ns []*enode.Node) []*node {
  62. result := make([]*node, len(ns))
  63. for i, n := range ns {
  64. result[i] = wrapNode(n)
  65. }
  66. return result
  67. }
  68. func unwrapNode(n *node) *enode.Node {
  69. return &n.Node
  70. }
  71. func unwrapNodes(ns []*node) []*enode.Node {
  72. result := make([]*enode.Node, len(ns))
  73. for i, n := range ns {
  74. result[i] = unwrapNode(n)
  75. }
  76. return result
  77. }
  78. func (n *node) addr() *net.UDPAddr {
  79. return &net.UDPAddr{IP: n.IP(), Port: n.UDP()}
  80. }
  81. func (n *node) String() string {
  82. return n.Node.String()
  83. }