| | 1 | | using System.Diagnostics; |
| | 2 | | using System.Diagnostics.CodeAnalysis; |
| | 3 | |
|
| | 4 | | namespace NLightning.Bolt11.Models; |
| | 5 | |
|
| | 6 | | using Domain.Protocol.ValueObjects; |
| | 7 | | using Domain.Utils; |
| | 8 | | using Enums; |
| | 9 | | using Factories; |
| | 10 | | using Interfaces; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// A list of tagged fields |
| | 14 | | /// </summary> |
| | 15 | | internal class TaggedFieldList : List<ITaggedField> |
| | 16 | | { |
| 460 | 17 | | private bool _shouldInvokeChangedEvent = true; |
| | 18 | | public event EventHandler? Changed; |
| | 19 | |
|
| | 20 | | /// <summary> |
| | 21 | | /// Add a tagged field to the list |
| | 22 | | /// </summary> |
| | 23 | | /// <param name="taggedField">The tagged field to add</param> |
| | 24 | | /// <exception cref="ArgumentException">If the tagged field is not unique</exception> |
| | 25 | | internal new void Add(ITaggedField taggedField) |
| | 26 | | { |
| | 27 | | // Individual field validation |
| 1012 | 28 | | if (!taggedField.IsValid()) |
| 0 | 29 | | throw new ArgumentException($"Invalid {taggedField.Type} field: field validation failed"); |
| | 30 | |
|
| | 31 | | // Check for uniqueness |
| 2736 | 32 | | if (this.Any(x => x.Type.Equals(taggedField.Type)) && taggedField.Type != TaggedFieldTypes.FallbackAddress) |
| 4 | 33 | | throw new ArgumentException( |
| 4 | 34 | | $"TaggedFieldDictionary already contains a tagged field of type {taggedField.Type}"); |
| | 35 | |
|
| | 36 | | // Mutual exclusivity validation |
| 1008 | 37 | | if (taggedField.Type == TaggedFieldTypes.Description |
| 1148 | 38 | | && this.Any(x => x.Type.Equals(TaggedFieldTypes.DescriptionHash))) |
| 4 | 39 | | throw new ArgumentException( |
| 4 | 40 | | $"TaggedFieldDictionary already contains a tagged field of type {taggedField.Type}"); |
| | 41 | |
|
| 1004 | 42 | | if (taggedField.Type == TaggedFieldTypes.DescriptionHash |
| 1084 | 43 | | && this.Any(x => x.Type.Equals(TaggedFieldTypes.Description))) |
| 4 | 44 | | throw new ArgumentException( |
| 4 | 45 | | $"TaggedFieldDictionary already contains a tagged field of type {taggedField.Type}"); |
| | 46 | |
|
| | 47 | | // Add the tagged field |
| 1000 | 48 | | base.Add(taggedField); |
| 1000 | 49 | | if (_shouldInvokeChangedEvent) |
| 964 | 50 | | OnChanged(); |
| 1000 | 51 | | } |
| | 52 | |
|
| | 53 | | /// <summary> |
| | 54 | | /// Add a range of tagged fields to the list |
| | 55 | | /// </summary> |
| | 56 | | /// <param name="taggedFields">The tagged fields to add</param> |
| | 57 | | internal new void AddRange(IEnumerable<ITaggedField> taggedFields) |
| | 58 | | { |
| 28 | 59 | | _shouldInvokeChangedEvent = false; |
| | 60 | |
|
| 128 | 61 | | foreach (var taggedField in taggedFields) |
| 36 | 62 | | Add(taggedField); |
| | 63 | |
|
| 28 | 64 | | _shouldInvokeChangedEvent = true; |
| 28 | 65 | | OnChanged(); |
| 28 | 66 | | } |
| | 67 | |
|
| | 68 | | internal new bool Remove(ITaggedField item) |
| | 69 | | { |
| 8 | 70 | | if (!base.Remove(item)) |
| 4 | 71 | | return false; |
| | 72 | |
|
| 4 | 73 | | OnChanged(); |
| 4 | 74 | | return true; |
| | 75 | | } |
| | 76 | |
|
| | 77 | | internal new void RemoveAt(int index) |
| | 78 | | { |
| 4 | 79 | | base.RemoveAt(index); |
| 4 | 80 | | OnChanged(); |
| 4 | 81 | | } |
| | 82 | |
|
| | 83 | | internal new int RemoveAll(Predicate<ITaggedField> match) |
| | 84 | | { |
| 4 | 85 | | var removed = base.RemoveAll(match); |
| 4 | 86 | | if (removed > 0) |
| 4 | 87 | | OnChanged(); |
| | 88 | |
|
| 4 | 89 | | return removed; |
| | 90 | | } |
| | 91 | |
|
| | 92 | | internal new void RemoveRange(int index, int count) |
| | 93 | | { |
| 4 | 94 | | base.RemoveRange(index, count); |
| 4 | 95 | | OnChanged(); |
| 4 | 96 | | } |
| | 97 | |
|
| | 98 | | /// <summary> |
| | 99 | | /// Attempts to retrieve a tagged field of the specified type from the list. |
| | 100 | | /// </summary> |
| | 101 | | /// <typeparam name="T">The type of the tagged field to retrieve.</typeparam> |
| | 102 | | /// <param name="taggedFieldType">The type of the tagged field being searched for.</param> |
| | 103 | | /// <param name="taggedField">The output parameter to hold the retrieved tagged field if found; otherwise, the defau |
| | 104 | | /// <returns>True if the tagged field of the specified type is found; otherwise, false.</returns> |
| | 105 | | internal bool TryGet<T>(TaggedFieldTypes taggedFieldType, [MaybeNullWhen(false)] out T taggedField) |
| | 106 | | where T : ITaggedField |
| | 107 | | { |
| 1168 | 108 | | var value = Get<T>(taggedFieldType); |
| 1168 | 109 | | if (value != null) |
| | 110 | | { |
| 872 | 111 | | taggedField = value; |
| 872 | 112 | | return true; |
| | 113 | | } |
| | 114 | |
|
| 296 | 115 | | taggedField = default; |
| 296 | 116 | | return false; |
| | 117 | | } |
| | 118 | |
|
| | 119 | | /// <summary> |
| | 120 | | /// Try to get all tagged fields of a specific type |
| | 121 | | /// </summary> |
| | 122 | | /// <param name="taggedFieldType">The type of the tagged field</param> |
| | 123 | | /// <param name="taggedFieldList">A list containing the tagged fields</param> |
| | 124 | | /// <typeparam name="T">The type of the tagged field</typeparam> |
| | 125 | | /// <returns>True if the tagged fields were found, false otherwise</returns> |
| | 126 | | internal bool TryGetAll<T>(TaggedFieldTypes taggedFieldType, [MaybeNullWhen(false)] out List<T> taggedFieldList) |
| | 127 | | where T : ITaggedField |
| | 128 | | { |
| 44 | 129 | | var value = GetAll<T>(taggedFieldType); |
| 44 | 130 | | if (value != null) |
| | 131 | | { |
| 40 | 132 | | taggedFieldList = value; |
| 40 | 133 | | return true; |
| | 134 | | } |
| | 135 | |
|
| 4 | 136 | | taggedFieldList = null; |
| 4 | 137 | | return false; |
| | 138 | | } |
| | 139 | |
|
| | 140 | | /// <summary> |
| | 141 | | /// Get a new TaggedFieldList from a BitReader |
| | 142 | | /// </summary> |
| | 143 | | /// <param name="bitReader">The BitReader to read from</param> |
| | 144 | | /// <param name="bitcoinNetwork">The network type</param> |
| | 145 | | /// <returns>A new TaggedFieldList</returns> |
| | 146 | | internal static TaggedFieldList FromBitReader(BitReader bitReader, BitcoinNetwork bitcoinNetwork) |
| | 147 | | { |
| 72 | 148 | | var taggedFields = new TaggedFieldList(); |
| 948 | 149 | | while (bitReader.HasMoreBits(15)) |
| | 150 | | { |
| 876 | 151 | | var type = (TaggedFieldTypes)bitReader.ReadByteFromBits(5); |
| 876 | 152 | | var length = bitReader.ReadInt16FromBits(10); |
| 876 | 153 | | if (length == 0 || !bitReader.HasMoreBits(length * 5)) |
| | 154 | | continue; |
| | 155 | |
|
| 348 | 156 | | if (!Enum.IsDefined(typeof(TaggedFieldTypes), type)) |
| | 157 | | { |
| 12 | 158 | | bitReader.SkipBits(length * 5); |
| | 159 | | } |
| | 160 | | else |
| | 161 | | { |
| | 162 | | try |
| | 163 | | { |
| 336 | 164 | | var taggedField = |
| 336 | 165 | | TaggedFieldFactory.CreateTaggedFieldFromBitReader(type, bitReader, length, bitcoinNetwork); |
| | 166 | |
|
| | 167 | | try |
| | 168 | | { |
| 316 | 169 | | taggedFields.Add(taggedField); |
| 316 | 170 | | } |
| 0 | 171 | | catch (Exception e) |
| | 172 | | { |
| | 173 | | Debug.WriteLine(e.Message); |
| | 174 | | // Skip for now, log latter |
| 0 | 175 | | } |
| 316 | 176 | | } |
| 20 | 177 | | catch (Exception e) |
| | 178 | | { |
| | 179 | | Debug.WriteLine(e.Message); |
| | 180 | | // Skip for now, log latter |
| 20 | 181 | | } |
| | 182 | | } |
| | 183 | | } |
| | 184 | |
|
| 72 | 185 | | return taggedFields; |
| | 186 | | } |
| | 187 | |
|
| | 188 | | /// <summary> |
| | 189 | | /// Write the TaggedFieldList to a BitWriter |
| | 190 | | /// </summary> |
| | 191 | | /// <param name="bitWriter">The BitWriter to write to</param> |
| | 192 | | internal void WriteToBitWriter(BitWriter bitWriter) |
| | 193 | | { |
| 736 | 194 | | foreach (var taggedField in this) |
| | 195 | | { |
| | 196 | | // Write type |
| 292 | 197 | | bitWriter.WriteByteAsBits((byte)taggedField.Type, 5); |
| | 198 | |
|
| | 199 | | // Write length |
| 292 | 200 | | bitWriter.WriteInt16AsBits(taggedField.Length, 10); |
| | 201 | |
|
| 292 | 202 | | taggedField.WriteToBitWriter(bitWriter); |
| | 203 | | } |
| 76 | 204 | | } |
| | 205 | |
|
| | 206 | | /// <summary> |
| | 207 | | /// Calculate the size of the TaggedFieldList in bits |
| | 208 | | /// </summary> |
| | 209 | | /// <returns>The size of the TaggedFieldList in bits</returns> |
| | 210 | | internal int CalculateSizeInBits() |
| | 211 | | { |
| 368 | 212 | | return this.Sum(x => x.Length); |
| | 213 | | } |
| | 214 | |
|
| | 215 | | /// <summary> |
| | 216 | | /// Get a tagged field of a specific type |
| | 217 | | /// </summary> |
| | 218 | | /// <param name="taggedFieldType">The type of the tagged field</param> |
| | 219 | | /// <typeparam name="T">The type of the tagged field</typeparam> |
| | 220 | | /// <returns>The tagged field</returns> |
| | 221 | | private T? Get<T>(TaggedFieldTypes taggedFieldType) where T : ITaggedField |
| | 222 | | { |
| 4848 | 223 | | return (T?)this.FirstOrDefault(x => x.Type.Equals(taggedFieldType)); |
| | 224 | | } |
| | 225 | |
|
| | 226 | | /// <summary> |
| | 227 | | /// Get all tagged fields of a specific type |
| | 228 | | /// </summary> |
| | 229 | | /// <param name="taggedFieldType">The type of the tagged field</param> |
| | 230 | | /// <typeparam name="T">The type of the tagged field</typeparam> |
| | 231 | | /// <returns>A list containing the tagged fields</returns> |
| | 232 | | private List<T>? GetAll<T>(TaggedFieldTypes taggedFieldType) where T : ITaggedField |
| | 233 | | { |
| 208 | 234 | | var taggedFields = this.Where(x => x.Type.Equals(taggedFieldType)).ToList(); |
| 44 | 235 | | return taggedFields.Count == 0 |
| 44 | 236 | | ? null |
| 44 | 237 | | : taggedFields.Cast<T>().ToList(); |
| | 238 | | } |
| | 239 | |
|
| | 240 | | private void OnChanged() |
| | 241 | | { |
| 1008 | 242 | | Changed?.Invoke(this, EventArgs.Empty); |
| 500 | 243 | | } |
| | 244 | | } |