entries.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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. // TCP is the "tcp" key, which holds the TCP port of the node.
  51. type TCP uint16
  52. func (v TCP) ENRKey() string { return "tcp" }
  53. // UDP is the "udp" key, which holds the UDP port of the node.
  54. type UDP uint16
  55. func (v UDP) ENRKey() string { return "udp" }
  56. // ID is the "id" key, which holds the name of the identity scheme.
  57. type ID string
  58. const IDv4 = ID("v4") // the default identity scheme
  59. func (v ID) ENRKey() string { return "id" }
  60. // IP is the "ip" key, which holds the IP address of the node.
  61. type IP net.IP
  62. func (v IP) ENRKey() string { return "ip" }
  63. // EncodeRLP implements rlp.Encoder.
  64. func (v IP) EncodeRLP(w io.Writer) error {
  65. if ip4 := net.IP(v).To4(); ip4 != nil {
  66. return rlp.Encode(w, ip4)
  67. }
  68. return rlp.Encode(w, net.IP(v))
  69. }
  70. // DecodeRLP implements rlp.Decoder.
  71. func (v *IP) DecodeRLP(s *rlp.Stream) error {
  72. if err := s.Decode((*net.IP)(v)); err != nil {
  73. return err
  74. }
  75. if len(*v) != 4 && len(*v) != 16 {
  76. return fmt.Errorf("invalid IP address, want 4 or 16 bytes: %v", *v)
  77. }
  78. return nil
  79. }
  80. // Secp256k1 is the "secp256k1" key, which holds a public key.
  81. type Secp256k1 ecdsa.PublicKey
  82. func (v Secp256k1) ENRKey() string { return "secp256k1" }
  83. // EncodeRLP implements rlp.Encoder.
  84. func (v Secp256k1) EncodeRLP(w io.Writer) error {
  85. return rlp.Encode(w, crypto.CompressPubkey((*ecdsa.PublicKey)(&v)))
  86. }
  87. // DecodeRLP implements rlp.Decoder.
  88. func (v *Secp256k1) DecodeRLP(s *rlp.Stream) error {
  89. buf, err := s.Bytes()
  90. if err != nil {
  91. return err
  92. }
  93. pk, err := crypto.DecompressPubkey(buf)
  94. if err != nil {
  95. return err
  96. }
  97. *v = (Secp256k1)(*pk)
  98. return nil
  99. }
  100. // KeyError is an error related to a key.
  101. type KeyError struct {
  102. Key string
  103. Err error
  104. }
  105. // Error implements error.
  106. func (err *KeyError) Error() string {
  107. if err.Err == errNotFound {
  108. return fmt.Sprintf("missing ENR key %q", err.Key)
  109. }
  110. return fmt.Sprintf("ENR key %q: %v", err.Key, err.Err)
  111. }
  112. // IsNotFound reports whether the given error means that a key/value pair is
  113. // missing from a record.
  114. func IsNotFound(err error) bool {
  115. kerr, ok := err.(*KeyError)
  116. return ok && kerr.Err == errNotFound
  117. }