From f1f1c8a2577b67349bed56e68a0e6c51b3748fda Mon Sep 17 00:00:00 2001 From: Benjamin Congdon Date: Fri, 15 Dec 2017 16:57:34 -0600 Subject: [PATCH 1/4] rpc: Remove SetAlias and add Alias to GetInfo Response This commit removes the SetAlias RPC function because it was deemed unnecessary. This commit also populates the Alias field in the GetInfo response --- rpcserver.go | 1 + 1 file changed, 1 insertion(+) diff --git a/rpcserver.go b/rpcserver.go index bf8e9908..b36e03b9 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -1141,6 +1141,7 @@ func (r *rpcServer) GetInfo(ctx context.Context, Testnet: activeNetParams.Params == &chaincfg.TestNet3Params, Chains: activeChains, Uris: uris, + Alias: nodeAnn.Alias.String(), }, nil } From ecff8f2a07f9f2299d68b236e7bc234a46407482 Mon Sep 17 00:00:00 2001 From: Benjamin Congdon Date: Fri, 15 Dec 2017 17:02:04 -0600 Subject: [PATCH 2/4] lnwire: Trim zero-bytes from NodeAlias String representation This commit alters the NodeAlias String method to trim null-bytes from the end of the alias. This is helpful for presentation in contexts such as the GetInfo response. --- lnwire/node_announcement.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lnwire/node_announcement.go b/lnwire/node_announcement.go index 9e9ce0cf..1eaeade3 100644 --- a/lnwire/node_announcement.go +++ b/lnwire/node_announcement.go @@ -40,7 +40,8 @@ func NewNodeAlias(s string) (NodeAlias, error) { // String returns a utf8 string representation of the alias bytes. func (n NodeAlias) String() string { - return string(n[:]) + // Trim trailing zero-bytes for presentation + return string(bytes.Trim(n[:], "\x00")) } // NodeAnnouncement message is used to announce the presence of a Lightning From a40ee8fe8aca5fb49192a310feaa0ff447eff7a7 Mon Sep 17 00:00:00 2001 From: Benjamin Congdon Date: Fri, 15 Dec 2017 17:03:38 -0600 Subject: [PATCH 3/4] config: Add settings for Alias and Color This commit adds configuration variables for setting the Color and Alias of the Node. --- config.go | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/config.go b/config.go index 3258512c..3cdf38f5 100644 --- a/config.go +++ b/config.go @@ -55,6 +55,9 @@ const ( defaultLitecoinBaseFeeMSat = 1000 defaultLitecoinFeeRate = 1 defaultLitecoinTimeLockDelta = 576 + + defaultAlias = "" + defaultColor = "#3399FF" ) var ( @@ -172,6 +175,9 @@ type config struct { NoEncryptWallet bool `long:"noencryptwallet" description:"If set, wallet will be encrypted using the default passphrase."` TrickleDelay int `long:"trickledelay" description:"Time in milliseconds between each release of announcements to the network"` + + Alias string `long:"alias" description:"The node alias. Used as a moniker by peers and intelligence services"` + Color string `long:"color" description:"The color of the node in hex format (i.e. '#3399FF'). Used to customize node appearance in intelligence services"` } // loadConfig initializes and parses the config using a config file and command @@ -227,6 +233,8 @@ func loadConfig() (*config, error) { Allocation: 0.6, }, TrickleDelay: defaultTrickleDelay, + Alias: defaultAlias, + Color: defaultColor, } // Pre-parse the command line options to pick up an alternative config From 92ac81b3b0564dbac3666f0b675ef693f9275cfe Mon Sep 17 00:00:00 2001 From: Benjamin Congdon Date: Fri, 15 Dec 2017 17:06:20 -0600 Subject: [PATCH 4/4] lnd+server: Allow configurable Node Alias and Color This commit sets the Node's Alias and Color to the values set in config. --- server.go | 43 +++++++++++++++++++++++++++++++++---------- server_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 67 insertions(+), 10 deletions(-) create mode 100644 server_test.go diff --git a/server.go b/server.go index 163c6c97..35388ce9 100644 --- a/server.go +++ b/server.go @@ -220,14 +220,19 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, chanGraph := chanDB.ChannelGraph() - defaultColor := color.RGBA{ // #3399FF - R: 51, - G: 153, - B: 255, + // Parse node color from configuration. + color, err := parseHexColor(cfg.Color) + if err != nil { + srvrLog.Errorf("unable to parse color: %v\n", err) + return nil, err } - // TODO(roasbeef): make alias configurable - alias, err := lnwire.NewNodeAlias(hex.EncodeToString(serializedPubKey[:10])) + // If no alias is provided, default to first 10 characters of public key + alias := cfg.Alias + if alias == "" { + alias = hex.EncodeToString(serializedPubKey[:10]) + } + nodeAlias, err := lnwire.NewNodeAlias(alias) if err != nil { return nil, err } @@ -236,9 +241,9 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, LastUpdate: time.Now(), Addresses: selfAddrs, PubKey: privKey.PubKey(), - Alias: alias.String(), + Alias: nodeAlias.String(), Features: s.globalFeatures, - Color: defaultColor, + Color: color, } // If our information has changed since our last boot, then we'll @@ -250,9 +255,9 @@ func newServer(listenAddrs []string, chanDB *channeldb.DB, cc *chainControl, Timestamp: uint32(selfNode.LastUpdate.Unix()), Addresses: selfNode.Addresses, NodeID: selfNode.PubKey, - Alias: alias, + Alias: nodeAlias, Features: selfNode.Features.RawFeatureVector, - RGBColor: defaultColor, + RGBColor: color, } selfNode.AuthSig, err = discovery.SignAnnouncement(s.nodeSigner, s.identityPriv.PubKey(), nodeAnn, @@ -1703,3 +1708,21 @@ func (s *server) Peers() []*peer { return peers } + +// parseHexColor takes a hex string representation of a color in the +// form "#RRGGBB", parses the hex color values, and returns a color.RGBA +// struct of the same color. +func parseHexColor(colorStr string) (color.RGBA, error) { + if len(colorStr) != 7 || colorStr[0] != '#' { + return color.RGBA{}, errors.New("Color must be in format #RRGGBB") + } + + // Decode the hex color string to bytes. + // The resulting byte array is in the form [R, G, B]. + colorBytes, err := hex.DecodeString(colorStr[1:]) + if err != nil { + return color.RGBA{}, err + } + + return color.RGBA{R: colorBytes[0], G: colorBytes[1], B: colorBytes[2]}, nil +} diff --git a/server_test.go b/server_test.go new file mode 100644 index 00000000..bd0d1444 --- /dev/null +++ b/server_test.go @@ -0,0 +1,34 @@ +package main + +import "testing" + +func TestParseHexColor(t *testing.T) { + empty := "" + color, err := parseHexColor(empty) + if err == nil { + t.Fatalf("Empty color string should return error, but did not") + } + + tooLong := "#1234567" + color, err = parseHexColor(tooLong) + if err == nil { + t.Fatalf("Invalid color string %s should return error, but did not", + tooLong) + } + + invalidFormat := "$123456" + color, err = parseHexColor(invalidFormat) + if err == nil { + t.Fatalf("Invalid color string %s should return error, but did not", + invalidFormat) + } + + valid := "#C0FfeE" + color, err = parseHexColor(valid) + if err != nil { + t.Fatalf("Color %s valid to parse: %s", valid, err) + } + if color.R != 0xc0 || color.G != 0xff || color.B != 0xee { + t.Fatalf("Color %s incorrectly parsed as %v", valid, color) + } +}