entries.go 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. // Copyright 2017 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 enr
  17. import (
  18. "crypto/ecdsa"
  19. "fmt"
  20. "io"
  21. "net"
  22. "github.com/ethereum/go-ethereum/crypto"
  23. "github.com/ethereum/go-ethereum/rlp"
  24. )
  25. // Entry is implemented by known node record entry types.
  26. //
  27. // To define a new entry that is to be included in a node record,
  28. // create a Go type that satisfies this interface. The type should
  29. // also implement rlp.Decoder if additional checks are needed on the value.
  30. type Entry interface {
  31. ENRKey() string
  32. }
  33. type generic struct {
  34. key string
  35. value interface{}
  36. }
  37. func (g generic) ENRKey() string { return g.key }
  38. func (g generic) EncodeRLP(w io.Writer) error {
  39. return rlp.Encode(w, g.value)
  40. }
  41. func (g *generic) DecodeRLP(s *rlp.Stream) error {
  42. return s.Decode(g.value)
  43. }
  44. // WithEntry wraps any value with a key name. It can be used to set and load arbitrary values
  45. // in a record. The value v must be supported by rlp. To use WithEntry with Load, the value
  46. // must be a pointer.
  47. func WithEntry(k string, v interface{}) Entry {
  48. return &generic{key: k, value: v}
  49. }
  50. // DiscPort is the "discv5" key, which holds the UDP port for discovery v5.
  51. type DiscPort uint16
  52. func (v DiscPort) ENRKey() string { return "discv5" }
  53. // ID is the "id" key, which holds the name of the identity scheme.
  54. type ID string
  55. func (v ID) ENRKey() string { return "id" }
  56. // IP4 is the "ip4" key, which holds a 4-byte IPv4 address.
  57. type IP4 net.IP
  58. func (v IP4) ENRKey() string { return "ip4" }
  59. // EncodeRLP implements rlp.Encoder.
  60. func (v IP4) EncodeRLP(w io.Writer) error {
  61. ip4 := net.IP(v).To4()
  62. if ip4 == nil {
  63. return fmt.Errorf("invalid IPv4 address: %v", v)
  64. }
  65. return rlp.Encode(w, ip4)
  66. }
  67. // DecodeRLP implements rlp.Decoder.
  68. func (v *IP4) DecodeRLP(s *rlp.Stream) error {
  69. if err := s.Decode((*net.IP)(v)); err != nil {
  70. return err
  71. }
  72. if len(*v) != 4 {
  73. return fmt.Errorf("invalid IPv4 address, want 4 bytes: %v", *v)
  74. }
  75. return nil
  76. }
  77. // IP6 is the "ip6" key, which holds a 16-byte IPv6 address.
  78. type IP6 net.IP
  79. func (v IP6) ENRKey() string { return "ip6" }
  80. // EncodeRLP implements rlp.Encoder.
  81. func (v IP6) EncodeRLP(w io.Writer) error {
  82. ip6 := net.IP(v)
  83. return rlp.Encode(w, ip6)
  84. }
  85. // DecodeRLP implements rlp.Decoder.
  86. func (v *IP6) DecodeRLP(s *rlp.Stream) error {
  87. if err := s.Decode((*net.IP)(v)); err != nil {
  88. return err
  89. }
  90. if len(*v) != 16 {
  91. return fmt.Errorf("invalid IPv6 address, want 16 bytes: %v", *v)
  92. }
  93. return nil
  94. }
  95. // Secp256k1 is the "secp256k1" key, which holds a public key.
  96. type Secp256k1 ecdsa.PublicKey
  97. func (v Secp256k1) ENRKey() string { return "secp256k1" }
  98. // EncodeRLP implements rlp.Encoder.
  99. func (v Secp256k1) EncodeRLP(w io.Writer) error {
  100. return rlp.Encode(w, crypto.CompressPubkey((*ecdsa.PublicKey)(&v)))
  101. }
  102. // DecodeRLP implements rlp.Decoder.
  103. func (v *Secp256k1) DecodeRLP(s *rlp.Stream) error {
  104. buf, err := s.Bytes()
  105. if err != nil {
  106. return err
  107. }
  108. pk, err := crypto.DecompressPubkey(buf)
  109. if err != nil {
  110. return err
  111. }
  112. *v = (Secp256k1)(*pk)
  113. return nil
  114. }
  115. // KeyError is an error related to a key.
  116. type KeyError struct {
  117. Key string
  118. Err error
  119. }
  120. // Error implements error.
  121. func (err *KeyError) Error() string {
  122. if err.Err == errNotFound {
  123. return fmt.Sprintf("missing ENR key %q", err.Key)
  124. }
  125. return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err)
  126. }
  127. // IsNotFound reports whether the given error means that a key/value pair is
  128. // missing from a record.
  129. func IsNotFound(err error) bool {
  130. kerr, ok := err.(*KeyError)
  131. return ok && kerr.Err == errNotFound
  132. }