node_announcement.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. package lnwire
  2. import (
  3. "bytes"
  4. "fmt"
  5. "image/color"
  6. "io"
  7. "net"
  8. "unicode/utf8"
  9. )
  10. // ErrUnknownAddrType is an error returned if we encounter an unknown address type
  11. // when parsing addresses.
  12. type ErrUnknownAddrType struct {
  13. addrType addressType
  14. }
  15. // Error returns a human readable string describing the error.
  16. //
  17. // NOTE: implements the error interface.
  18. func (e ErrUnknownAddrType) Error() string {
  19. return fmt.Sprintf("unknown address type: %v", e.addrType)
  20. }
  21. // ErrInvalidNodeAlias is an error returned if a node alias we parse on the
  22. // wire is invalid, as in it has non UTF-8 characters.
  23. type ErrInvalidNodeAlias struct{}
  24. // Error returns a human readable string describing the error.
  25. //
  26. // NOTE: implements the error interface.
  27. func (e ErrInvalidNodeAlias) Error() string {
  28. return "node alias has non-utf8 characters"
  29. }
  30. // NodeAlias is a hex encoded UTF-8 string that may be displayed as an
  31. // alternative to the node's ID. Notice that aliases are not unique and may be
  32. // freely chosen by the node operators.
  33. type NodeAlias [32]byte
  34. // NewNodeAlias creates a new instance of a NodeAlias. Verification is
  35. // performed on the passed string to ensure it meets the alias requirements.
  36. func NewNodeAlias(s string) (NodeAlias, error) {
  37. var n NodeAlias
  38. if len(s) > 32 {
  39. return n, fmt.Errorf("alias too large: max is %v, got %v", 32,
  40. len(s))
  41. }
  42. if !utf8.ValidString(s) {
  43. return n, &ErrInvalidNodeAlias{}
  44. }
  45. copy(n[:], []byte(s))
  46. return n, nil
  47. }
  48. // String returns a utf8 string representation of the alias bytes.
  49. func (n NodeAlias) String() string {
  50. // Trim trailing zero-bytes for presentation
  51. return string(bytes.Trim(n[:], "\x00"))
  52. }
  53. // NodeAnnouncement message is used to announce the presence of a Lightning
  54. // node and also to signal that the node is accepting incoming connections.
  55. // Each NodeAnnouncement authenticating the advertised information within the
  56. // announcement via a signature using the advertised node pubkey.
  57. type NodeAnnouncement struct {
  58. // Signature is used to prove the ownership of node id.
  59. Signature Sig
  60. // Features is the list of protocol features this node supports.
  61. Features *RawFeatureVector
  62. // Timestamp allows ordering in the case of multiple announcements.
  63. Timestamp uint32
  64. // NodeID is a public key which is used as node identification.
  65. NodeID [33]byte
  66. // RGBColor is used to customize their node's appearance in maps and
  67. // graphs
  68. RGBColor color.RGBA
  69. // Alias is used to customize their node's appearance in maps and
  70. // graphs
  71. Alias NodeAlias
  72. // Address includes two specification fields: 'ipv6' and 'port' on
  73. // which the node is accepting incoming connections.
  74. Addresses []net.Addr
  75. // ExtraOpaqueData is the set of data that was appended to this
  76. // message, some of which we may not actually know how to iterate or
  77. // parse. By holding onto this data, we ensure that we're able to
  78. // properly validate the set of signatures that cover these new fields,
  79. // and ensure we're able to make upgrades to the network in a forwards
  80. // compatible manner.
  81. ExtraOpaqueData ExtraOpaqueData
  82. }
  83. // A compile time check to ensure NodeAnnouncement implements the
  84. // lnwire.Message interface.
  85. var _ Message = (*NodeAnnouncement)(nil)
  86. // Decode deserializes a serialized NodeAnnouncement stored in the passed
  87. // io.Reader observing the specified protocol version.
  88. //
  89. // This is part of the lnwire.Message interface.
  90. func (a *NodeAnnouncement) Decode(r io.Reader, pver uint32) error {
  91. return ReadElements(r,
  92. &a.Signature,
  93. &a.Features,
  94. &a.Timestamp,
  95. &a.NodeID,
  96. &a.RGBColor,
  97. &a.Alias,
  98. &a.Addresses,
  99. &a.ExtraOpaqueData,
  100. )
  101. }
  102. // Encode serializes the target NodeAnnouncement into the passed io.Writer
  103. // observing the protocol version specified.
  104. //
  105. // This is part of the lnwire.Message interface.
  106. func (a *NodeAnnouncement) Encode(w *bytes.Buffer, pver uint32) error {
  107. if err := WriteSig(w, a.Signature); err != nil {
  108. return err
  109. }
  110. if err := WriteRawFeatureVector(w, a.Features); err != nil {
  111. return err
  112. }
  113. if err := WriteUint32(w, a.Timestamp); err != nil {
  114. return err
  115. }
  116. if err := WriteBytes(w, a.NodeID[:]); err != nil {
  117. return err
  118. }
  119. if err := WriteColorRGBA(w, a.RGBColor); err != nil {
  120. return err
  121. }
  122. if err := WriteNodeAlias(w, a.Alias); err != nil {
  123. return err
  124. }
  125. if err := WriteNetAddrs(w, a.Addresses); err != nil {
  126. return err
  127. }
  128. return WriteBytes(w, a.ExtraOpaqueData)
  129. }
  130. // MsgType returns the integer uniquely identifying this message type on the
  131. // wire.
  132. //
  133. // This is part of the lnwire.Message interface.
  134. func (a *NodeAnnouncement) MsgType() MessageType {
  135. return MsgNodeAnnouncement
  136. }
  137. // DataToSign returns the part of the message that should be signed.
  138. func (a *NodeAnnouncement) DataToSign() ([]byte, error) {
  139. // We should not include the signatures itself.
  140. buffer := make([]byte, 0, MaxMsgBody)
  141. buf := bytes.NewBuffer(buffer)
  142. if err := WriteRawFeatureVector(buf, a.Features); err != nil {
  143. return nil, err
  144. }
  145. if err := WriteUint32(buf, a.Timestamp); err != nil {
  146. return nil, err
  147. }
  148. if err := WriteBytes(buf, a.NodeID[:]); err != nil {
  149. return nil, err
  150. }
  151. if err := WriteColorRGBA(buf, a.RGBColor); err != nil {
  152. return nil, err
  153. }
  154. if err := WriteNodeAlias(buf, a.Alias); err != nil {
  155. return nil, err
  156. }
  157. if err := WriteNetAddrs(buf, a.Addresses); err != nil {
  158. return nil, err
  159. }
  160. if err := WriteBytes(buf, a.ExtraOpaqueData); err != nil {
  161. return nil, err
  162. }
  163. return buf.Bytes(), nil
  164. }