<commit_before>package lnwire

import (
	"bytes"
	"io"
	"net"

	"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
}

// 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
}

// NewAlias create the alias from string and also checks spec requirements.
func NewAlias(s string) (Alias, error) {
	data := []byte(s)
	return newAlias(data)
}

func newAlias(data []byte) (Alias, error) {
	var a [32]byte

	aliasEnd := len(data)
	for data[aliasEnd-1] == 0 && aliasEnd > 0 {
		aliasEnd--
	}

	copy(a[:aliasEnd], data[:aliasEnd])

	return Alias{
		data:     a,
		aliasLen: aliasEnd,
	}, nil
}

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
// 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

	// Timestamp allows ordering in the case of multiple
	// announcements.
	Timestamp uint32

	// Address includes two specification fields: 'ipv6' and
	// 'port' on which the node is accepting incoming connections.
	Address *net.TCPAddr

	// 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

	// pad is used to reserve to additional bytes for future
	// usage.
	pad uint16

	// Alias is used to customize their node's appearance in maps
	// and graphs
	Alias Alias
}

// A compile time check to ensure NodeAnnouncement implements the
// lnwire.Message interface.
var _ Message = (*NodeAnnouncement)(nil)

// Validate performs any necessary sanity checks to ensure all fields present
// on the NodeAnnouncement are valid.
//
// This is part of the lnwire.Message interface.
func (a *NodeAnnouncement) Validate() error {
	// TODO(roasbeef): move validation to discovery service
	return nil
}
<commit_msg>lnwire: modify newAlias to truncate alias strings if too long<commit_after>package lnwire

import (
	"bytes"
	"io"
	"net"

	"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
}

// 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
}

// NewAlias create the alias from string and also checks spec requirements.
func NewAlias(s string) (Alias, error) {
	data := []byte(s)
	return newAlias(data)
}

func newAlias(data []byte) (Alias, error) {
	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,
	}, nil
}

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
// 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

	// Timestamp allows ordering in the case of multiple
	// announcements.
	Timestamp uint32

	// Address includes two specification fields: 'ipv6' and
	// 'port' on which the node is accepting incoming connections.
	Address *net.TCPAddr

	// 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

	// TODO(roasbeef): add the global features here

	// pad is used to reserve to additional bytes for future
	// usage.
	pad uint16

	// Alias is used to customize their node's appearance in maps
	// and graphs
	Alias Alias
}

// A compile time check to ensure NodeAnnouncement implements the
// lnwire.Message interface.
var _ Message = (*NodeAnnouncement)(nil)

// Validate performs any necessary sanity checks to ensure all fields present
// on the NodeAnnouncement are valid.
//
// This is part of the lnwire.Message interface.
func (a *NodeAnnouncement) Validate() error {
	// TODO(roasbeef): move validation to discovery service
	return nil
}

{'fraction_non_alphanumeric': 0.08928571428571429, 'fraction_numerical': 0.012482662968099861, 'mean_word_length': 3.4342813220599537, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 10, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '939961', 'n_tokens_mistral': 1867, 'n_tokens_neox': 1751, 'n_words': 720}