mirror of
https://github.com/lightningnetwork/lnd.git
synced 2025-08-31 17:51:33 +02:00
lnwire: update node alias type to respect utf8, use full 32 bytes
This commit is contained in:
@@ -301,10 +301,6 @@ func writeElement(w io.Writer, element interface{}) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case Alias:
|
|
||||||
if err := writeElements(w, e.data[:]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
case DeliveryAddress:
|
case DeliveryAddress:
|
||||||
var length [2]byte
|
var length [2]byte
|
||||||
binary.BigEndian.PutUint16(length[:], uint16(len(e)))
|
binary.BigEndian.PutUint16(length[:], uint16(len(e)))
|
||||||
@@ -584,13 +580,6 @@ func readElement(r io.Reader, element interface{}) error {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
case *Alias:
|
|
||||||
var a [32]byte
|
|
||||||
if err := readElements(r, a[:]); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
*e = newAlias(a[:])
|
|
||||||
case *DeliveryAddress:
|
case *DeliveryAddress:
|
||||||
var addrLen [2]byte
|
var addrLen [2]byte
|
||||||
if _, err = io.ReadFull(r, addrLen[:]); err != nil {
|
if _, err = io.ReadFull(r, addrLen[:]); err != nil {
|
||||||
|
@@ -2,77 +2,52 @@ package lnwire
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net"
|
"net"
|
||||||
|
"unicode/utf8"
|
||||||
|
|
||||||
"github.com/go-errors/errors"
|
|
||||||
"github.com/roasbeef/btcd/btcec"
|
"github.com/roasbeef/btcd/btcec"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
startPort uint16 = 1024
|
startPort uint16 = 1024
|
||||||
endPort uint16 = 49151
|
endPort uint16 = 49151
|
||||||
aliasSpecLen = 21
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// RGB is used to represent the color.
|
// RGB is used to represent the "color" of a particular node encoded as a 24
|
||||||
|
// bit value.
|
||||||
type RGB struct {
|
type RGB struct {
|
||||||
red uint8
|
red uint8
|
||||||
green uint8
|
green uint8
|
||||||
blue uint8
|
blue uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
// Alias a hex encoded UTF-8 string that may be displayed as an alternative to
|
// NodeAlias a hex encoded UTF-8 string that may be displayed as an alternative
|
||||||
// the node's ID. Notice that aliases are not unique and may be freely chosen
|
// to the node's ID. Notice that aliases are not unique and may be freely
|
||||||
// by the node operators.
|
// chosen by the node operators.
|
||||||
type Alias struct {
|
type NodeAlias [32]byte
|
||||||
data [32]byte
|
|
||||||
aliasLen int
|
// 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewAlias create the alias from string and also checks spec requirements.
|
// String returns a utf8 string representation of the alias bytes.
|
||||||
func NewAlias(s string) Alias {
|
func (n NodeAlias) String() string {
|
||||||
data := []byte(s)
|
return string(n[:])
|
||||||
return newAlias(data)
|
|
||||||
}
|
|
||||||
|
|
||||||
func newAlias(data []byte) Alias {
|
|
||||||
var a [32]byte
|
|
||||||
|
|
||||||
rawAlias := data
|
|
||||||
if len(data) > aliasSpecLen {
|
|
||||||
rawAlias = data[:aliasSpecLen]
|
|
||||||
}
|
|
||||||
|
|
||||||
aliasEnd := len(rawAlias)
|
|
||||||
for rawAlias[aliasEnd-1] == 0 && aliasEnd > 0 {
|
|
||||||
aliasEnd--
|
|
||||||
}
|
|
||||||
|
|
||||||
copy(a[:aliasEnd], rawAlias[:aliasEnd])
|
|
||||||
|
|
||||||
return Alias{
|
|
||||||
data: a,
|
|
||||||
aliasLen: aliasEnd,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (a *Alias) String() string {
|
|
||||||
return string(a.data[:a.aliasLen])
|
|
||||||
}
|
|
||||||
|
|
||||||
// Validate check that alias data length is lower than spec size.
|
|
||||||
func (a *Alias) Validate() error {
|
|
||||||
nonzero := len(a.data)
|
|
||||||
for a.data[nonzero-1] == 0 && nonzero > 0 {
|
|
||||||
nonzero--
|
|
||||||
}
|
|
||||||
|
|
||||||
if nonzero > aliasSpecLen {
|
|
||||||
return errors.New("alias should be less then 21 bytes")
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// NodeAnnouncement message is used to announce the presence of a Lightning
|
// NodeAnnouncement message is used to announce the presence of a Lightning
|
||||||
@@ -94,11 +69,12 @@ type NodeAnnouncement struct {
|
|||||||
// maps and graphs
|
// maps and graphs
|
||||||
RGBColor RGB
|
RGBColor RGB
|
||||||
|
|
||||||
// Alias is used to customize their node's appearance in maps and graphs
|
|
||||||
Alias Alias
|
|
||||||
|
|
||||||
// Features is the list of protocol features this node supports.
|
// Features is the list of protocol features this node supports.
|
||||||
Features *FeatureVector
|
Features *FeatureVector
|
||||||
|
// 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
|
// Address includes two specification fields: 'ipv6' and 'port' on which
|
||||||
// the node is accepting incoming connections.
|
// the node is accepting incoming connections.
|
||||||
|
@@ -1,16 +0,0 @@
|
|||||||
package lnwire
|
|
||||||
|
|
||||||
import "testing"
|
|
||||||
|
|
||||||
func TestValidateAlias(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
aliasStr := "012345678901234567890"
|
|
||||||
alias := NewAlias(aliasStr)
|
|
||||||
if err := alias.Validate(); err != nil {
|
|
||||||
t.Fatalf("alias was invalid: %v", err)
|
|
||||||
}
|
|
||||||
if aliasStr != alias.String() {
|
|
||||||
t.Fatalf("aliases don't match")
|
|
||||||
}
|
|
||||||
}
|
|
Reference in New Issue
Block a user