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