lnd.xprv/lnwire/node_announcement.go

155 lines
3.8 KiB
Go
Raw Normal View History

package lnwire
import (
"bytes"
"fmt"
"io"
"net"
"unicode/utf8"
"github.com/roasbeef/btcd/btcec"
)
var (
startPort uint16 = 1024
endPort uint16 = 49151
)
// RGB is used to represent the "color" of a particular node encoded as a 24
// bit value.
type RGB struct {
red uint8
green uint8
blue uint8
}
// NodeAlias a hex encoded UTF-8 string that may be displayed as an alternative
// to the node's ID. Notice that aliases are not unique and may be freely
// chosen by the node operators.
type NodeAlias [32]byte
// NewNodeAlias creates a new instance of a NodeAlias. Verification is
// performed on the passed string to ensure it meets the alias requirements.
func NewNodeAlias(s string) (NodeAlias, error) {
var n NodeAlias
if len(s) > 32 {
return n, fmt.Errorf("alias too large: max is %v, got %v", 32, len(s))
}
if !utf8.ValidString(s) {
return n, fmt.Errorf("invalid utf8 string")
}
copy(n[:], []byte(s))
return n, nil
}
// String returns a utf8 string representation of the alias bytes.
func (n NodeAlias) String() string {
return string(n[:])
}
// NodeAnnouncement message is used to announce the presence of a Lightning
// node and also to signal that the node is accepting incoming connections.
// Each NodeAnnouncement authenticating the advertised information within the
// announcement via a signature using the advertised node pubkey.
type NodeAnnouncement struct {
// Signature is used to prove the ownership of node id.
Signature *btcec.Signature
// Features is the list of protocol features this node supports.
Features *FeatureVector
// Timestamp allows ordering in the case of multiple announcements.
Timestamp uint32
// NodeID is a public key which is used as node identification.
NodeID *btcec.PublicKey
// RGBColor is used to customize their node's appearance in maps and
// graphs
RGBColor RGB
// Alias is used to customize their node's appearance in maps and
// graphs
Alias NodeAlias
// Address includes two specification fields: 'ipv6' and 'port' on
// which the node is accepting incoming connections.
Addresses []net.Addr
}
// A compile time check to ensure NodeAnnouncement implements the
// lnwire.Message interface.
var _ Message = (*NodeAnnouncement)(nil)
// Decode deserializes a serialized NodeAnnouncement stored in the passed
// io.Reader observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
2017-02-23 22:56:47 +03:00
func (a *NodeAnnouncement) Decode(r io.Reader, pver uint32) error {
2017-02-23 21:59:50 +03:00
return readElements(r,
2017-02-23 22:56:47 +03:00
&a.Signature,
&a.Features,
2017-02-23 22:56:47 +03:00
&a.Timestamp,
&a.NodeID,
&a.RGBColor,
a.Alias[:],
&a.Addresses,
)
}
// Encode serializes the target NodeAnnouncement into the passed io.Writer
// observing the protocol version specified.
//
2017-02-23 22:56:47 +03:00
func (a *NodeAnnouncement) Encode(w io.Writer, pver uint32) error {
2017-02-23 21:59:50 +03:00
return writeElements(w,
2017-02-23 22:56:47 +03:00
a.Signature,
a.Features,
2017-02-23 22:56:47 +03:00
a.Timestamp,
a.NodeID,
a.RGBColor,
a.Alias[:],
a.Addresses,
)
}
// MsgType returns the integer uniquely identifying this message type on the
// wire.
//
// This is part of the lnwire.Message interface.
func (a *NodeAnnouncement) MsgType() MessageType {
return MsgNodeAnnouncement
}
// MaxPayloadLength returns the maximum allowed payload size for this message
// observing the specified protocol version.
//
// This is part of the lnwire.Message interface.
2017-02-23 22:56:47 +03:00
func (a *NodeAnnouncement) MaxPayloadLength(pver uint32) uint32 {
return 65533
}
2017-02-23 22:56:47 +03:00
// DataToSign returns the part of the message that should be signed.
func (a *NodeAnnouncement) DataToSign() ([]byte, error) {
// We should not include the signatures itself.
var w bytes.Buffer
err := writeElements(&w,
a.Features,
2017-02-23 22:56:47 +03:00
a.Timestamp,
a.NodeID,
a.RGBColor,
a.Alias[:],
a.Addresses,
)
if err != nil {
return nil, err
}
// TODO(roasbeef): also capture the excess bytes in msg padded out?
return w.Bytes(), nil
}