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