Skip to content

Commit c0577e1

Browse files
committed
Add configurable node color
1 parent a297a58 commit c0577e1

5 files changed

Lines changed: 74 additions & 15 deletions

File tree

bindings/ldk_node.udl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace ldk_node {
55

66
typedef dictionary Config;
77

8+
typedef dictionary NodeColor;
9+
810
typedef dictionary EsploraSyncConfig;
911

1012
typedef dictionary ElectrumSyncConfig;
@@ -58,6 +60,7 @@ interface Builder {
5860
void set_tor_config(TorConfig tor_config);
5961
[Throws=BuildError]
6062
void set_node_alias(string node_alias);
63+
void set_node_color(u8 red, u8 green, u8 blue);
6164
[Throws=BuildError]
6265
void set_async_payments_role(AsyncPaymentsRole? role);
6366
void set_wallet_recovery_mode();
@@ -92,6 +95,7 @@ interface Node {
9295
sequence<SocketAddress>? listening_addresses();
9396
sequence<SocketAddress>? announcement_addresses();
9497
NodeAlias? node_alias();
98+
NodeColor node_color();
9599
Bolt11Payment bolt11_payment();
96100
Bolt12Payment bolt12_payment();
97101
SpontaneousPayment spontaneous_payment();

src/builder.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,14 @@ impl NodeBuilder {
600600
Ok(self)
601601
}
602602

603+
/// Sets the RGB color that will be used when broadcasting announcements to the gossip network.
604+
pub fn set_node_color(&mut self, red: u8, green: u8, blue: u8) -> &mut Self {
605+
self.config.node_color.red = red;
606+
self.config.node_color.green = green;
607+
self.config.node_color.blue = blue;
608+
self
609+
}
610+
603611
/// Sets the role of the node in an asynchronous payments context.
604612
///
605613
/// See <https://github.com/lightning/bolts/pull/1149> for more information about the async payments protocol.
@@ -1093,6 +1101,11 @@ impl ArcedNodeBuilder {
10931101
self.inner.write().expect("lock").set_node_alias(node_alias).map(|_| ())
10941102
}
10951103

1104+
/// Sets the RGB color that will be used when broadcasting announcements to the gossip network.
1105+
pub fn set_node_color(&self, red: u8, green: u8, blue: u8) {
1106+
self.inner.write().expect("lock").set_node_color(red, green, blue);
1107+
}
1108+
10961109
/// Sets the role of the node in an asynchronous payments context.
10971110
pub fn set_async_payments_role(
10981111
&self, role: Option<AsyncPaymentsRole>,
@@ -2155,7 +2168,15 @@ pub(crate) fn sanitize_alias(alias_str: &str) -> Result<NodeAlias, BuildError> {
21552168

21562169
#[cfg(test)]
21572170
mod tests {
2158-
use super::{sanitize_alias, BuildError, NodeAlias};
2171+
use super::{sanitize_alias, BuildError, NodeAlias, NodeBuilder};
2172+
2173+
#[test]
2174+
fn set_node_color_updates_config() {
2175+
let mut builder = NodeBuilder::new();
2176+
builder.set_node_color(1, 2, 3);
2177+
2178+
assert_eq!(builder.config.node_color.as_rgb(), [1, 2, 3]);
2179+
}
21592180

21602181
#[test]
21612182
fn sanitize_empty_node_alias() {

src/config.rs

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -117,19 +117,20 @@ pub(crate) const LNURL_AUTH_TIMEOUT_SECS: u64 = 15;
117117
///
118118
/// ### Defaults
119119
///
120-
/// | Parameter | Value |
121-
/// |----------------------------------------|--------------------------------------|
122-
/// | `storage_dir_path` | /tmp/ldk_node/ |
123-
/// | `network` | Bitcoin |
124-
/// | `listening_addresses` | None |
125-
/// | `announcement_addresses` | None |
126-
/// | `node_alias` | None |
127-
/// | `trusted_peers_0conf` | [] |
128-
/// | `probing_liquidity_limit_multiplier` | 3 |
129-
/// | `anchor_channels_config` | Some(..) |
130-
/// | `route_parameters` | None |
131-
/// | `tor_config` | None |
132-
/// | `hrn_config` | HumanReadableNamesConfig::default() |
120+
/// | Parameter | Value |
121+
/// |----------------------------------------|--------------------------------------------|
122+
/// | `storage_dir_path` | /tmp/ldk_node/ |
123+
/// | `network` | Bitcoin |
124+
/// | `listening_addresses` | None |
125+
/// | `announcement_addresses` | None |
126+
/// | `node_alias` | None |
127+
/// | `node_color` | NodeColor { red: 0, green: 0, blue: 0 } |
128+
/// | `trusted_peers_0conf` | [] |
129+
/// | `probing_liquidity_limit_multiplier` | 3 |
130+
/// | `anchor_channels_config` | Some(..) |
131+
/// | `route_parameters` | None |
132+
/// | `tor_config` | None |
133+
/// | `hrn_config` | HumanReadableNamesConfig::default() |
133134
///
134135
/// See [`AnchorChannelsConfig`] and [`RouteParametersConfig`] for more information regarding their
135136
/// respective default values.
@@ -158,6 +159,8 @@ pub struct Config {
158159
/// **Note**: We will only allow opening and accepting public channels if the `node_alias` and the
159160
/// `listening_addresses` are set.
160161
pub node_alias: Option<NodeAlias>,
162+
/// The RGB color that will be used when broadcasting announcements to the gossip network.
163+
pub node_color: NodeColor,
161164
/// A list of peers that we allow to establish zero confirmation channels to us.
162165
///
163166
/// **Note:** Allowing payments via zero-confirmation channels is potentially insecure if the
@@ -220,11 +223,30 @@ impl Default for Config {
220223
tor_config: None,
221224
route_parameters: None,
222225
node_alias: None,
226+
node_color: NodeColor::default(),
223227
hrn_config: HumanReadableNamesConfig::default(),
224228
}
225229
}
226230
}
227231

232+
/// The RGB color that will be used when broadcasting node announcements.
233+
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
234+
#[cfg_attr(feature = "uniffi", derive(uniffi::Record))]
235+
pub struct NodeColor {
236+
/// The red color component.
237+
pub red: u8,
238+
/// The green color component.
239+
pub green: u8,
240+
/// The blue color component.
241+
pub blue: u8,
242+
}
243+
244+
impl NodeColor {
245+
pub(crate) fn as_rgb(&self) -> [u8; 3] {
246+
[self.red, self.green, self.blue]
247+
}
248+
}
249+
228250
/// Configuration options for how our node resolves Human-Readable Names (BIP 353).
229251
///
230252
/// [BIP 353]: https://github.com/bitcoin/bips/blob/master/bip-0353.mediawiki

src/graph.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use lightning::routing::gossip::RoutingFees;
1717
#[cfg(not(feature = "uniffi"))]
1818
use lightning::routing::gossip::{ChannelInfo, NodeInfo};
1919

20+
#[cfg(feature = "uniffi")]
21+
use crate::config::NodeColor;
2022
use crate::types::Graph;
2123

2224
/// Represents the network as nodes and channels between them.
@@ -159,6 +161,8 @@ pub struct NodeAnnouncementInfo {
159161
/// May be invalid or malicious (eg control chars),
160162
/// should not be exposed to the user.
161163
pub alias: String,
164+
/// RGB color assigned to the node.
165+
pub color: NodeColor,
162166
/// List of addresses on which this node is reachable
163167
pub addresses: Vec<SocketAddress>,
164168
}
@@ -169,6 +173,7 @@ impl From<lightning::routing::gossip::NodeAnnouncementInfo> for NodeAnnouncement
169173
Self {
170174
last_update: value.last_update(),
171175
alias: value.alias().to_string(),
176+
color: NodeColor { red: value.rgb()[0], green: value.rgb()[1], blue: value.rgb()[2] },
172177
addresses: value.addresses().iter().cloned().collect(),
173178
}
174179
}

src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,8 @@ pub use builder::BuildError;
128128
#[cfg(not(feature = "uniffi"))]
129129
pub use builder::NodeBuilder as Builder;
130130
use chain::ChainSource;
131+
#[cfg(feature = "uniffi")]
132+
use config::NodeColor;
131133
use config::{
132134
default_user_config, may_announce_channel, AsyncPaymentsRole, ChannelConfig, Config,
133135
LNURL_AUTH_TIMEOUT_SECS, NODE_ANN_BCAST_INTERVAL, PEER_RECONNECTION_INTERVAL,
@@ -538,7 +540,7 @@ impl Node {
538540
};
539541

540542
if let Some(node_alias) = node_alias.as_ref() {
541-
bcast_pm.broadcast_node_announcement([0; 3], node_alias.0, addresses);
543+
bcast_pm.broadcast_node_announcement(bcast_config.node_color.as_rgb(), node_alias.0, addresses);
542544

543545
let unix_time_secs_opt =
544546
SystemTime::now().duration_since(UNIX_EPOCH).ok().map(|d| d.as_secs());
@@ -873,6 +875,11 @@ impl Node {
873875
self.config.node_alias
874876
}
875877

878+
/// Returns our node color.
879+
pub fn node_color(&self) -> crate::config::NodeColor {
880+
self.config.node_color
881+
}
882+
876883
/// Returns a payment handler allowing to create and pay [BOLT 11] invoices.
877884
///
878885
/// [BOLT 11]: https://github.com/lightning/bolts/blob/master/11-payment-encoding.md

0 commit comments

Comments
 (0)