2016-12-07 18:46:22 +03:00
|
|
|
package lnwire
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2016-12-22 23:24:48 +03:00
|
|
|
"io"
|
|
|
|
"net"
|
|
|
|
|
2016-12-07 18:46:22 +03:00
|
|
|
"github.com/go-errors/errors"
|
|
|
|
"github.com/roasbeef/btcd/btcec"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
startPort uint16 = 1024
|
|
|
|
endPort uint16 = 49151
|
|
|
|
aliasSpecLen = 21
|
|
|
|
)
|
|
|
|
|
|
|
|
// RGB is used to represent the color.
|
|
|
|
type RGB struct {
|
|
|
|
red uint8
|
|
|
|
green uint8
|
|
|
|
blue uint8
|
|
|
|
}
|
|
|
|
|
2016-12-25 01:55:31 +03:00
|
|
|
// Alias 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 Alias struct {
|
|
|
|
data [32]byte
|
|
|
|
aliasLen int
|
|
|
|
}
|
2016-12-07 18:46:22 +03:00
|
|
|
|
|
|
|
// NewAlias create the alias from string and also checks spec requirements.
|
2017-04-20 00:59:16 +03:00
|
|
|
func NewAlias(s string) Alias {
|
2016-12-07 18:46:22 +03:00
|
|
|
data := []byte(s)
|
2016-12-25 01:55:31 +03:00
|
|
|
return newAlias(data)
|
|
|
|
}
|
|
|
|
|
2017-04-20 00:59:16 +03:00
|
|
|
func newAlias(data []byte) Alias {
|
2016-12-25 01:55:31 +03:00
|
|
|
var a [32]byte
|
|
|
|
|
2017-03-09 01:21:50 +03:00
|
|
|
rawAlias := data
|
|
|
|
if len(data) > aliasSpecLen {
|
|
|
|
rawAlias = data[:aliasSpecLen]
|
|
|
|
}
|
|
|
|
|
|
|
|
aliasEnd := len(rawAlias)
|
|
|
|
for rawAlias[aliasEnd-1] == 0 && aliasEnd > 0 {
|
2016-12-25 01:55:31 +03:00
|
|
|
aliasEnd--
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
2017-03-09 01:21:50 +03:00
|
|
|
copy(a[:aliasEnd], rawAlias[:aliasEnd])
|
2016-12-25 01:55:31 +03:00
|
|
|
|
|
|
|
return Alias{
|
|
|
|
data: a,
|
|
|
|
aliasLen: aliasEnd,
|
2017-04-20 00:59:16 +03:00
|
|
|
}
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Alias) String() string {
|
2016-12-25 01:55:31 +03:00
|
|
|
return string(a.data[:a.aliasLen])
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
2016-12-25 01:55:31 +03:00
|
|
|
// Validate check that alias data length is lower than spec size.
|
2016-12-07 18:46:22 +03:00
|
|
|
func (a *Alias) Validate() error {
|
2016-12-25 01:55:31 +03:00
|
|
|
nonzero := len(a.data)
|
|
|
|
for a.data[nonzero-1] == 0 && nonzero > 0 {
|
2016-12-07 18:46:22 +03:00
|
|
|
nonzero--
|
|
|
|
}
|
|
|
|
|
|
|
|
if nonzero > aliasSpecLen {
|
|
|
|
return errors.New("alias should be less then 21 bytes")
|
|
|
|
}
|
2016-12-25 01:55:31 +03:00
|
|
|
|
2016-12-07 18:46:22 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-06 07:43:34 +03:00
|
|
|
// 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.
|
2016-12-07 18:46:22 +03:00
|
|
|
type NodeAnnouncement struct {
|
|
|
|
// Signature is used to prove the ownership of node id.
|
|
|
|
Signature *btcec.Signature
|
|
|
|
|
2017-03-06 07:43:34 +03:00
|
|
|
// Timestamp allows ordering in the case of multiple
|
|
|
|
// announcements.
|
2016-12-07 18:46:22 +03:00
|
|
|
Timestamp uint32
|
|
|
|
|
2017-02-17 12:29:23 +03:00
|
|
|
// NodeID is a public key which is used as node identification.
|
2016-12-07 18:46:22 +03:00
|
|
|
NodeID *btcec.PublicKey
|
|
|
|
|
2017-03-06 07:43:34 +03:00
|
|
|
// RGBColor is used to customize their node's appearance in
|
|
|
|
// maps and graphs
|
2016-12-07 18:46:22 +03:00
|
|
|
RGBColor RGB
|
|
|
|
|
2017-02-17 12:29:23 +03:00
|
|
|
// Alias is used to customize their node's appearance in maps and graphs
|
2016-12-07 18:46:22 +03:00
|
|
|
Alias Alias
|
2017-02-17 12:29:23 +03:00
|
|
|
|
2017-03-20 12:24:55 +03:00
|
|
|
// Features is the list of protocol features this node supports.
|
|
|
|
Features *FeatureVector
|
|
|
|
|
2017-02-17 12:29:23 +03:00
|
|
|
// Address includes two specification fields: 'ipv6' and 'port' on which
|
|
|
|
// the node is accepting incoming connections.
|
|
|
|
Addresses []net.Addr
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// A compile time check to ensure NodeAnnouncement implements the
|
|
|
|
// lnwire.Message interface.
|
|
|
|
var _ Message = (*NodeAnnouncement)(nil)
|
|
|
|
|
2017-03-06 07:43:34 +03:00
|
|
|
// Decode deserializes a serialized NodeAnnouncement stored in the passed
|
|
|
|
// io.Reader observing the specified protocol version.
|
2016-12-07 18:46:22 +03:00
|
|
|
//
|
|
|
|
// 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.Timestamp,
|
|
|
|
&a.NodeID,
|
|
|
|
&a.RGBColor,
|
|
|
|
&a.Alias,
|
2017-03-20 12:24:55 +03:00
|
|
|
&a.Features,
|
2017-05-13 00:30:10 +03:00
|
|
|
&a.Addresses,
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-03-06 07:43:34 +03:00
|
|
|
// Encode serializes the target NodeAnnouncement into the passed io.Writer
|
|
|
|
// observing the protocol version specified.
|
2016-12-07 18:46:22 +03:00
|
|
|
//
|
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.Timestamp,
|
|
|
|
a.NodeID,
|
|
|
|
a.RGBColor,
|
|
|
|
a.Alias,
|
2017-03-20 12:24:55 +03:00
|
|
|
a.Features,
|
2017-05-13 00:30:10 +03:00
|
|
|
a.Addresses,
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2017-04-20 01:57:43 +03:00
|
|
|
// MsgType returns the integer uniquely identifying this message type on the
|
2016-12-07 18:46:22 +03:00
|
|
|
// wire.
|
|
|
|
//
|
|
|
|
// This is part of the lnwire.Message interface.
|
2017-04-20 01:57:43 +03:00
|
|
|
func (a *NodeAnnouncement) MsgType() MessageType {
|
|
|
|
return MsgNodeAnnouncement
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2017-04-20 02:04:38 +03:00
|
|
|
return 65533
|
2016-12-07 18:46:22 +03:00
|
|
|
}
|
|
|
|
|
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) {
|
2016-12-07 18:46:22 +03:00
|
|
|
|
|
|
|
// We should not include the signatures itself.
|
|
|
|
var w bytes.Buffer
|
|
|
|
err := writeElements(&w,
|
2017-02-23 22:56:47 +03:00
|
|
|
a.Timestamp,
|
|
|
|
a.NodeID,
|
|
|
|
a.RGBColor,
|
|
|
|
a.Alias,
|
2017-03-20 12:24:55 +03:00
|
|
|
a.Features,
|
2017-05-13 00:30:10 +03:00
|
|
|
a.Addresses,
|
2016-12-07 18:46:22 +03:00
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w.Bytes(), nil
|
|
|
|
}
|