| | 1 | | namespace NLightning.Bolt11.Models.TaggedFields; |
| | 2 | |
|
| | 3 | | using Common.Utils; |
| | 4 | | using Domain.Node; |
| | 5 | | using Enums; |
| | 6 | | using Interfaces; |
| | 7 | |
|
| | 8 | | /// <summary> |
| | 9 | | /// Tagged field for features |
| | 10 | | /// </summary> |
| | 11 | | /// <remarks> |
| | 12 | | /// The features are a collection of features that are supported by the node. |
| | 13 | | /// </remarks> |
| | 14 | | /// <seealso cref="ITaggedField"/> |
| | 15 | | internal sealed class FeaturesTaggedField : ITaggedField |
| | 16 | | { |
| 1008 | 17 | | public TaggedFieldTypes Type => TaggedFieldTypes.FEATURES; |
| 220 | 18 | | internal FeatureSet Value { get; } |
| 320 | 19 | | public short Length { get; } |
| | 20 | |
|
| | 21 | | /// <summary> |
| | 22 | | /// Initializes a new instance of the <see cref="DescriptionTaggedField"/> class. |
| | 23 | | /// </summary> |
| | 24 | | /// <param name="value">The Description</param> |
| 152 | 25 | | internal FeaturesTaggedField(FeatureSet value) |
| | 26 | | { |
| 152 | 27 | | Value = value; |
| 152 | 28 | | Length = (short)(value.SizeInBits / 5 + (value.SizeInBits % 5 == 0 ? 0 : 1)); |
| 152 | 29 | | } |
| | 30 | |
|
| | 31 | | /// <inheritdoc/> |
| | 32 | | public void WriteToBitWriter(BitWriter bitWriter) |
| | 33 | | { |
| 64 | 34 | | var shouldPad = Length * 5 / 8 == (Length * 5 - 7) / 8; |
| | 35 | |
|
| | 36 | | // Write data |
| 64 | 37 | | Value.WriteToBitWriter(bitWriter, Length * 5, shouldPad); |
| 64 | 38 | | } |
| | 39 | |
|
| | 40 | | /// <inheritdoc/> |
| | 41 | | public bool IsValid() |
| | 42 | | { |
| 64 | 43 | | return true; |
| | 44 | | } |
| | 45 | |
|
| | 46 | | /// <summary> |
| | 47 | | /// Reads a FeaturesTaggedField from a BitReader |
| | 48 | | /// </summary> |
| | 49 | | /// <param name="bitReader">The BitReader to read from</param> |
| | 50 | | /// <param name="length">The length of the field</param> |
| | 51 | | /// <returns>The FeaturesTaggedField</returns> |
| | 52 | | /// <exception cref="ArgumentException">Thrown when the length is invalid</exception> |
| | 53 | | internal static FeaturesTaggedField FromBitReader(BitReader bitReader, short length) |
| | 54 | | { |
| 80 | 55 | | if (length <= 0) |
| | 56 | | { |
| 4 | 57 | | throw new ArgumentException("Invalid length for FeaturesTaggedField. Length must be greater than 0", nameof( |
| | 58 | | } |
| | 59 | |
|
| 76 | 60 | | var shouldPad = length * 5 / 8 == (length * 5 - 7) / 8; |
| 76 | 61 | | var features = FeatureSet.DeserializeFromBitReader(bitReader, length * 5, shouldPad); |
| | 62 | |
|
| 76 | 63 | | return new FeaturesTaggedField(features); |
| | 64 | | } |
| | 65 | | } |