| | 1 | | using System.Net; |
| | 2 | |
|
| | 3 | | namespace NLightning.Domain.Node.Options; |
| | 4 | |
|
| | 5 | | using Enums; |
| | 6 | | using Protocol.Constants; |
| | 7 | | using Protocol.Models; |
| | 8 | | using Protocol.Tlv; |
| | 9 | | using ValueObjects; |
| | 10 | |
|
| | 11 | | public class FeatureOptions |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Enable data loss protection. |
| | 15 | | /// </summary> |
| 248 | 16 | | public FeatureSupport DataLossProtect { get; set; } = FeatureSupport.Compulsory; |
| | 17 | |
|
| | 18 | | /// <summary> |
| | 19 | | /// Enable initial routing sync. |
| | 20 | | /// </summary> |
| 64 | 21 | | public FeatureSupport InitialRoutingSync { get; set; } = FeatureSupport.No; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Enable upfront shutdown script. |
| | 25 | | /// </summary> |
| 248 | 26 | | public FeatureSupport UpfrontShutdownScript { get; set; } = FeatureSupport.Optional; |
| | 27 | |
|
| | 28 | | /// <summary> |
| | 29 | | /// Enable gossip queries. |
| | 30 | | /// </summary> |
| 248 | 31 | | public FeatureSupport GossipQueries { get; set; } = FeatureSupport.Optional; |
| | 32 | |
|
| | 33 | | /// <summary> |
| | 34 | | /// Enable expanded gossip queries. |
| | 35 | | /// </summary> |
| 248 | 36 | | public FeatureSupport ExpandedGossipQueries { get; set; } = FeatureSupport.Optional; |
| | 37 | |
|
| | 38 | | /// <summary> |
| | 39 | | /// Enable static remote key. |
| | 40 | | /// </summary> |
| 248 | 41 | | public FeatureSupport StaticRemoteKey { get; set; } = FeatureSupport.Compulsory; |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Enable payment secret. |
| | 45 | | /// </summary> |
| 248 | 46 | | public FeatureSupport PaymentSecret { get; set; } = FeatureSupport.Compulsory; |
| | 47 | |
|
| | 48 | | /// <summary> |
| | 49 | | /// Enable basic MPP. |
| | 50 | | /// </summary> |
| 248 | 51 | | public FeatureSupport BasicMpp { get; set; } = FeatureSupport.Optional; |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Enable large channels. |
| | 55 | | /// </summary> |
| 248 | 56 | | public FeatureSupport LargeChannels { get; set; } = FeatureSupport.Optional; |
| | 57 | |
|
| | 58 | | /// <summary> |
| | 59 | | /// Enable anchor outputs. |
| | 60 | | /// </summary> |
| 248 | 61 | | public FeatureSupport AnchorOutputs { get; set; } = FeatureSupport.Optional; |
| | 62 | |
|
| | 63 | | /// <summary> |
| | 64 | | /// Enable zero fee anchor tx. |
| | 65 | | /// </summary> |
| 64 | 66 | | public FeatureSupport ZeroFeeAnchorTx { get; set; } = FeatureSupport.No; |
| | 67 | |
|
| | 68 | | /// <summary> |
| | 69 | | /// Enable route blinding. |
| | 70 | | /// </summary> |
| 248 | 71 | | public FeatureSupport RouteBlinding { get; set; } = FeatureSupport.Optional; |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Enable beyond segwit shutdown. |
| | 75 | | /// </summary> |
| 64 | 76 | | public FeatureSupport BeyondSegwitShutdown { get; set; } = FeatureSupport.No; |
| | 77 | |
|
| | 78 | | /// <summary> |
| | 79 | | /// Enable dual fund. |
| | 80 | | /// </summary> |
| 248 | 81 | | public FeatureSupport DualFund { get; set; } = FeatureSupport.Optional; |
| | 82 | |
|
| | 83 | | /// <summary> |
| | 84 | | /// Enable onion messages. |
| | 85 | | /// </summary> |
| 64 | 86 | | public FeatureSupport OnionMessages { get; set; } = FeatureSupport.No; |
| | 87 | |
|
| | 88 | | /// <summary> |
| | 89 | | /// Enable channel type. |
| | 90 | | /// </summary> |
| 248 | 91 | | public FeatureSupport ChannelType { get; set; } = FeatureSupport.Optional; |
| | 92 | |
|
| | 93 | | /// <summary> |
| | 94 | | /// Enable scid alias. |
| | 95 | | /// </summary> |
| 248 | 96 | | public FeatureSupport ScidAlias { get; set; } = FeatureSupport.Optional; |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Enable payment metadata. |
| | 100 | | /// </summary> |
| 64 | 101 | | public FeatureSupport PaymentMetadata { get; set; } = FeatureSupport.No; |
| | 102 | |
|
| | 103 | | /// <summary> |
| | 104 | | /// Enable zero conf. |
| | 105 | | /// </summary> |
| 64 | 106 | | public FeatureSupport ZeroConf { get; set; } = FeatureSupport.No; |
| | 107 | |
|
| | 108 | | /// <summary> |
| | 109 | | /// The chain hashes of the node. |
| | 110 | | /// </summary> |
| | 111 | | /// <remarks> |
| | 112 | | /// Initialized as Mainnet if not set. |
| | 113 | | /// </remarks> |
| 240 | 114 | | public IEnumerable<ChainHash> ChainHashes { get; set; } = []; |
| | 115 | |
|
| | 116 | | /// <summary> |
| | 117 | | /// The remote address of the node. |
| | 118 | | /// </summary> |
| | 119 | | /// <remarks> |
| | 120 | | /// This is used to connect to our node. |
| | 121 | | /// </remarks> |
| 0 | 122 | | public IPAddress? RemoteAddress { get; set; } = null; |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Get Features set for the node. |
| | 126 | | /// </summary> |
| | 127 | | /// <returns>The features set for the node.</returns> |
| | 128 | | /// <remarks> |
| | 129 | | /// All features set as Optional. |
| | 130 | | /// </remarks> |
| | 131 | | public FeatureSet GetNodeFeatures() |
| | 132 | | { |
| 60 | 133 | | var features = new FeatureSet(); |
| | 134 | |
|
| | 135 | | // Set default features |
| 60 | 136 | | if (DataLossProtect != FeatureSupport.No) |
| | 137 | | { |
| 60 | 138 | | features.SetFeature(Feature.OptionDataLossProtect, DataLossProtect == FeatureSupport.Compulsory); |
| | 139 | | } |
| | 140 | |
|
| 60 | 141 | | if (InitialRoutingSync != FeatureSupport.No) |
| | 142 | | { |
| 0 | 143 | | features.SetFeature(Feature.InitialRoutingSync, InitialRoutingSync == FeatureSupport.Compulsory); |
| | 144 | | } |
| | 145 | |
|
| 60 | 146 | | if (UpfrontShutdownScript != FeatureSupport.No) |
| | 147 | | { |
| 60 | 148 | | features.SetFeature(Feature.OptionUpfrontShutdownScript, |
| 60 | 149 | | UpfrontShutdownScript == FeatureSupport.Compulsory); |
| | 150 | | } |
| | 151 | |
|
| 60 | 152 | | if (GossipQueries != FeatureSupport.No) |
| | 153 | | { |
| 60 | 154 | | features.SetFeature(Feature.GossipQueries, GossipQueries == FeatureSupport.Compulsory); |
| | 155 | | } |
| | 156 | |
|
| 60 | 157 | | if (ExpandedGossipQueries != FeatureSupport.No) |
| | 158 | | { |
| 60 | 159 | | features.SetFeature(Feature.GossipQueriesEx, ExpandedGossipQueries == FeatureSupport.Compulsory); |
| | 160 | | } |
| | 161 | |
|
| 60 | 162 | | if (StaticRemoteKey != FeatureSupport.No) |
| | 163 | | { |
| 60 | 164 | | features.SetFeature(Feature.OptionStaticRemoteKey, StaticRemoteKey == FeatureSupport.Compulsory); |
| | 165 | | } |
| | 166 | |
|
| 60 | 167 | | if (PaymentSecret != FeatureSupport.No) |
| | 168 | | { |
| 60 | 169 | | features.SetFeature(Feature.PaymentSecret, PaymentSecret == FeatureSupport.Compulsory); |
| | 170 | | } |
| | 171 | |
|
| 60 | 172 | | if (BasicMpp != FeatureSupport.No) |
| | 173 | | { |
| 60 | 174 | | features.SetFeature(Feature.BasicMpp, BasicMpp == FeatureSupport.Compulsory); |
| | 175 | | } |
| | 176 | |
|
| 60 | 177 | | if (LargeChannels != FeatureSupport.No) |
| | 178 | | { |
| 60 | 179 | | features.SetFeature(Feature.OptionSupportLargeChannel, LargeChannels == FeatureSupport.Compulsory); |
| | 180 | | } |
| | 181 | |
|
| 60 | 182 | | if (AnchorOutputs != FeatureSupport.No) |
| | 183 | | { |
| 60 | 184 | | features.SetFeature(Feature.OptionAnchorOutputs, AnchorOutputs == FeatureSupport.Compulsory); |
| | 185 | | } |
| | 186 | |
|
| 60 | 187 | | if (ZeroFeeAnchorTx != FeatureSupport.No) |
| | 188 | | { |
| 0 | 189 | | features.SetFeature(Feature.OptionAnchorsZeroFeeHtlcTx, ZeroFeeAnchorTx == FeatureSupport.Compulsory); |
| | 190 | | } |
| | 191 | |
|
| 60 | 192 | | if (RouteBlinding != FeatureSupport.No) |
| | 193 | | { |
| 60 | 194 | | features.SetFeature(Feature.OptionRouteBlinding, RouteBlinding == FeatureSupport.Compulsory); |
| | 195 | | } |
| | 196 | |
|
| 60 | 197 | | if (BeyondSegwitShutdown != FeatureSupport.No) |
| | 198 | | { |
| 0 | 199 | | features.SetFeature(Feature.OptionShutdownAnySegwit, BeyondSegwitShutdown == FeatureSupport.Compulsory); |
| | 200 | | } |
| | 201 | |
|
| 60 | 202 | | if (DualFund != FeatureSupport.No) |
| | 203 | | { |
| 60 | 204 | | features.SetFeature(Feature.OptionDualFund, DualFund == FeatureSupport.Compulsory); |
| | 205 | | } |
| | 206 | |
|
| 60 | 207 | | if (OnionMessages != FeatureSupport.No) |
| | 208 | | { |
| 0 | 209 | | features.SetFeature(Feature.OptionOnionMessages, OnionMessages == FeatureSupport.Compulsory); |
| | 210 | | } |
| | 211 | |
|
| 60 | 212 | | if (ChannelType != FeatureSupport.No) |
| | 213 | | { |
| 60 | 214 | | features.SetFeature(Feature.OptionChannelType, ChannelType == FeatureSupport.Compulsory); |
| | 215 | | } |
| | 216 | |
|
| 60 | 217 | | if (ScidAlias != FeatureSupport.No) |
| | 218 | | { |
| 60 | 219 | | features.SetFeature(Feature.OptionScidAlias, ScidAlias == FeatureSupport.Compulsory); |
| | 220 | | } |
| | 221 | |
|
| 60 | 222 | | if (PaymentMetadata != FeatureSupport.No) |
| | 223 | | { |
| 0 | 224 | | features.SetFeature(Feature.OptionPaymentMetadata, PaymentMetadata == FeatureSupport.Compulsory); |
| | 225 | | } |
| | 226 | |
|
| 60 | 227 | | if (ZeroConf != FeatureSupport.No) |
| | 228 | | { |
| 0 | 229 | | features.SetFeature(Feature.OptionZeroconf, ZeroConf == FeatureSupport.Compulsory); |
| | 230 | | } |
| | 231 | |
|
| 60 | 232 | | return features; |
| | 233 | | } |
| | 234 | |
|
| | 235 | | /// <summary> |
| | 236 | | /// Get the Init extension for the node. |
| | 237 | | /// </summary> |
| | 238 | | /// <returns>The Init extension for the node.</returns> |
| | 239 | | /// <remarks> |
| | 240 | | /// If there are no ChainHashes, Mainnet is used as default. |
| | 241 | | /// </remarks> |
| | 242 | | internal NetworksTlv GetInitTlvs() |
| | 243 | | { |
| | 244 | | // If there are no ChainHashes, use Mainnet as default |
| 48 | 245 | | if (!ChainHashes.Any()) |
| | 246 | | { |
| 4 | 247 | | ChainHashes = [ChainConstants.MAIN]; |
| | 248 | | } |
| | 249 | |
|
| 48 | 250 | | return new NetworksTlv(ChainHashes); |
| | 251 | |
|
| | 252 | | // TODO: Review this when implementing BOLT7 |
| | 253 | | // // If RemoteAddress is set, add it to the extension |
| | 254 | | // if (RemoteAddress != null) |
| | 255 | | // { |
| | 256 | | // extension.Add(new(new BigSize(3), RemoteAddress.GetAddressBytes())); |
| | 257 | | // } |
| | 258 | | } |
| | 259 | |
|
| | 260 | | /// <summary> |
| | 261 | | /// Get the node options from the features and extension. |
| | 262 | | /// </summary> |
| | 263 | | /// <param name="featureSet">The features of the node.</param> |
| | 264 | | /// <param name="extension">The extension of the node.</param> |
| | 265 | | /// <returns>The node options.</returns> |
| | 266 | | public static FeatureOptions GetNodeOptions(FeatureSet featureSet, TlvStream? extension) |
| | 267 | | { |
| 4 | 268 | | var options = new FeatureOptions |
| 4 | 269 | | { |
| 4 | 270 | | DataLossProtect = featureSet.IsFeatureSet(Feature.OptionDataLossProtect, true) |
| 4 | 271 | | ? FeatureSupport.Compulsory |
| 4 | 272 | | : featureSet.IsFeatureSet(Feature.OptionDataLossProtect, false) |
| 4 | 273 | | ? FeatureSupport.Optional |
| 4 | 274 | | : FeatureSupport.No, |
| 4 | 275 | | InitialRoutingSync = featureSet.IsFeatureSet(Feature.InitialRoutingSync, true) |
| 4 | 276 | | ? FeatureSupport.Compulsory |
| 4 | 277 | | : featureSet.IsFeatureSet(Feature.InitialRoutingSync, false) |
| 4 | 278 | | ? FeatureSupport.Optional |
| 4 | 279 | | : FeatureSupport.No, |
| 4 | 280 | | UpfrontShutdownScript = featureSet.IsFeatureSet(Feature.OptionUpfrontShutdownScript, true) |
| 4 | 281 | | ? FeatureSupport.Compulsory |
| 4 | 282 | | : featureSet.IsFeatureSet(Feature.OptionUpfrontShutdownScript, false) |
| 4 | 283 | | ? FeatureSupport.Optional |
| 4 | 284 | | : FeatureSupport.No, |
| 4 | 285 | | GossipQueries = featureSet.IsFeatureSet(Feature.GossipQueries, true) |
| 4 | 286 | | ? FeatureSupport.Compulsory |
| 4 | 287 | | : featureSet.IsFeatureSet(Feature.GossipQueries, false) |
| 4 | 288 | | ? FeatureSupport.Optional |
| 4 | 289 | | : FeatureSupport.No, |
| 4 | 290 | | ExpandedGossipQueries = featureSet.IsFeatureSet(Feature.GossipQueriesEx, true) |
| 4 | 291 | | ? FeatureSupport.Compulsory |
| 4 | 292 | | : featureSet.IsFeatureSet(Feature.GossipQueriesEx, false) |
| 4 | 293 | | ? FeatureSupport.Optional |
| 4 | 294 | | : FeatureSupport.No, |
| 4 | 295 | | StaticRemoteKey = featureSet.IsFeatureSet(Feature.OptionStaticRemoteKey, true) |
| 4 | 296 | | ? FeatureSupport.Compulsory |
| 4 | 297 | | : featureSet.IsFeatureSet(Feature.OptionStaticRemoteKey, false) |
| 4 | 298 | | ? FeatureSupport.Optional |
| 4 | 299 | | : FeatureSupport.No, |
| 4 | 300 | | PaymentSecret = featureSet.IsFeatureSet(Feature.PaymentSecret, true) |
| 4 | 301 | | ? FeatureSupport.Compulsory |
| 4 | 302 | | : featureSet.IsFeatureSet(Feature.PaymentSecret, false) |
| 4 | 303 | | ? FeatureSupport.Optional |
| 4 | 304 | | : FeatureSupport.No, |
| 4 | 305 | | BasicMpp = featureSet.IsFeatureSet(Feature.BasicMpp, true) |
| 4 | 306 | | ? FeatureSupport.Compulsory |
| 4 | 307 | | : featureSet.IsFeatureSet(Feature.BasicMpp, false) |
| 4 | 308 | | ? FeatureSupport.Optional |
| 4 | 309 | | : FeatureSupport.No, |
| 4 | 310 | | LargeChannels = featureSet.IsFeatureSet(Feature.OptionSupportLargeChannel, true) |
| 4 | 311 | | ? FeatureSupport.Compulsory |
| 4 | 312 | | : featureSet.IsFeatureSet(Feature.OptionSupportLargeChannel, false) |
| 4 | 313 | | ? FeatureSupport.Optional |
| 4 | 314 | | : FeatureSupport.No, |
| 4 | 315 | | AnchorOutputs = featureSet.IsFeatureSet(Feature.OptionAnchorOutputs, true) |
| 4 | 316 | | ? FeatureSupport.Compulsory |
| 4 | 317 | | : featureSet.IsFeatureSet(Feature.OptionAnchorOutputs, false) |
| 4 | 318 | | ? FeatureSupport.Optional |
| 4 | 319 | | : FeatureSupport.No, |
| 4 | 320 | | ZeroFeeAnchorTx = featureSet.IsFeatureSet(Feature.OptionAnchorsZeroFeeHtlcTx, true) |
| 4 | 321 | | ? FeatureSupport.Compulsory |
| 4 | 322 | | : featureSet.IsFeatureSet(Feature.OptionAnchorsZeroFeeHtlcTx, false) |
| 4 | 323 | | ? FeatureSupport.Optional |
| 4 | 324 | | : FeatureSupport.No, |
| 4 | 325 | | RouteBlinding = featureSet.IsFeatureSet(Feature.OptionRouteBlinding, true) |
| 4 | 326 | | ? FeatureSupport.Compulsory |
| 4 | 327 | | : featureSet.IsFeatureSet(Feature.OptionRouteBlinding, false) |
| 4 | 328 | | ? FeatureSupport.Optional |
| 4 | 329 | | : FeatureSupport.No, |
| 4 | 330 | | BeyondSegwitShutdown = featureSet.IsFeatureSet(Feature.OptionShutdownAnySegwit, true) |
| 4 | 331 | | ? FeatureSupport.Compulsory |
| 4 | 332 | | : featureSet.IsFeatureSet(Feature.OptionShutdownAnySegwit, false) |
| 4 | 333 | | ? FeatureSupport.Optional |
| 4 | 334 | | : FeatureSupport.No, |
| 4 | 335 | | DualFund = featureSet.IsFeatureSet(Feature.OptionDualFund, true) |
| 4 | 336 | | ? FeatureSupport.Compulsory |
| 4 | 337 | | : featureSet.IsFeatureSet(Feature.OptionDualFund, false) |
| 4 | 338 | | ? FeatureSupport.Optional |
| 4 | 339 | | : FeatureSupport.No, |
| 4 | 340 | | OnionMessages = featureSet.IsFeatureSet(Feature.OptionOnionMessages, true) |
| 4 | 341 | | ? FeatureSupport.Compulsory |
| 4 | 342 | | : featureSet.IsFeatureSet(Feature.OptionOnionMessages, false) |
| 4 | 343 | | ? FeatureSupport.Optional |
| 4 | 344 | | : FeatureSupport.No, |
| 4 | 345 | | ChannelType = featureSet.IsFeatureSet(Feature.OptionChannelType, true) |
| 4 | 346 | | ? FeatureSupport.Compulsory |
| 4 | 347 | | : featureSet.IsFeatureSet(Feature.OptionChannelType, false) |
| 4 | 348 | | ? FeatureSupport.Optional |
| 4 | 349 | | : FeatureSupport.No, |
| 4 | 350 | | ScidAlias = featureSet.IsFeatureSet(Feature.OptionScidAlias, true) |
| 4 | 351 | | ? FeatureSupport.Compulsory |
| 4 | 352 | | : featureSet.IsFeatureSet(Feature.OptionScidAlias, false) |
| 4 | 353 | | ? FeatureSupport.Optional |
| 4 | 354 | | : FeatureSupport.No, |
| 4 | 355 | | PaymentMetadata = featureSet.IsFeatureSet(Feature.OptionPaymentMetadata, true) |
| 4 | 356 | | ? FeatureSupport.Compulsory |
| 4 | 357 | | : featureSet.IsFeatureSet(Feature.OptionPaymentMetadata, false) |
| 4 | 358 | | ? FeatureSupport.Optional |
| 4 | 359 | | : FeatureSupport.No, |
| 4 | 360 | | ZeroConf = featureSet.IsFeatureSet(Feature.OptionZeroconf, true) |
| 4 | 361 | | ? FeatureSupport.Compulsory |
| 4 | 362 | | : featureSet.IsFeatureSet(Feature.OptionZeroconf, false) |
| 4 | 363 | | ? FeatureSupport.Optional |
| 4 | 364 | | : FeatureSupport.No |
| 4 | 365 | | }; |
| | 366 | |
|
| 4 | 367 | | if (extension?.TryGetTlv(new BigSize(1), out var chainHashes) ?? false) |
| | 368 | | { |
| 4 | 369 | | options.ChainHashes = Enumerable.Range(0, chainHashes!.Value.Length / ChainHash.LENGTH) |
| 4 | 370 | | .Select(i => new ChainHash(chainHashes.Value.Skip(i * 32).Take(32).ToArray() |
| | 371 | | } |
| | 372 | |
|
| | 373 | | // TODO: Add network when implementing BOLT7 |
| | 374 | |
|
| 4 | 375 | | return options; |
| | 376 | | } |
| | 377 | | } |