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