| | 1 | | using System.Text; |
| | 2 | |
|
| | 3 | | namespace NLightning.Bolt11.Models.TaggedFields; |
| | 4 | |
|
| | 5 | | using Domain.Utils; |
| | 6 | | using Enums; |
| | 7 | | using Interfaces; |
| | 8 | |
|
| | 9 | | /// <summary> |
| | 10 | | /// Tagged field for the description |
| | 11 | | /// </summary> |
| | 12 | | /// <remarks> |
| | 13 | | /// The description is a UTF-8 encoded string that describes, in short, the purpose of payment |
| | 14 | | /// </remarks> |
| | 15 | | /// <seealso cref="ITaggedField"/> |
| | 16 | | internal sealed class DescriptionTaggedField : ITaggedField |
| | 17 | | { |
| | 18 | | private const int MaxDescriptionBytes = 639; |
| | 19 | | private readonly byte[] _data; |
| | 20 | |
|
| 1308 | 21 | | public TaggedFieldTypes Type => TaggedFieldTypes.Description; |
| 728 | 22 | | internal string Value { get; } |
| 208 | 23 | | public short Length { get; } |
| | 24 | |
|
| | 25 | | /// <summary> |
| | 26 | | /// Initializes a new instance of the <see cref="DescriptionTaggedField"/> class. |
| | 27 | | /// </summary> |
| | 28 | | /// <param name="value">The Description</param> |
| 216 | 29 | | internal DescriptionTaggedField(string value) |
| | 30 | | { |
| 216 | 31 | | Value = value; |
| | 32 | |
|
| 216 | 33 | | if (string.IsNullOrEmpty(value)) |
| | 34 | | { |
| 0 | 35 | | _data = []; |
| 0 | 36 | | Length = 0; |
| | 37 | | } |
| | 38 | | else |
| | 39 | | { |
| | 40 | | // Add Padding if needed |
| 216 | 41 | | var data = Encoding.UTF8.GetBytes(Value); |
| 216 | 42 | | if (data.Length > MaxDescriptionBytes) |
| 0 | 43 | | throw new ArgumentException( |
| 0 | 44 | | $"Description exceeds maximum length of {MaxDescriptionBytes} UTF-8 bytes. Current: {data.Length} by |
| 0 | 45 | | nameof(value)); |
| | 46 | |
|
| 216 | 47 | | var bitLength = data.Length * 8; |
| 216 | 48 | | var totalBits = bitLength + (5 - bitLength % 5) % 5; |
| 216 | 49 | | if (totalBits != bitLength) |
| | 50 | | { |
| 196 | 51 | | _data = new byte[data.Length + 1]; |
| 196 | 52 | | Array.Copy(data, _data, data.Length); |
| | 53 | | } |
| | 54 | | else |
| | 55 | | { |
| 20 | 56 | | _data = data; |
| | 57 | | } |
| | 58 | |
|
| 216 | 59 | | Length = (short)(totalBits / 5); |
| | 60 | | } |
| 216 | 61 | | } |
| | 62 | |
|
| | 63 | | /// <inheritdoc/> |
| | 64 | | public void WriteToBitWriter(BitWriter bitWriter) |
| | 65 | | { |
| 52 | 66 | | if (Length == 0) |
| 0 | 67 | | return; |
| | 68 | |
|
| | 69 | | // Write data |
| 52 | 70 | | bitWriter.WriteBits(_data, Length * 5); |
| 52 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <inheritdoc/> |
| | 74 | | public bool IsValid() |
| | 75 | | { |
| | 76 | | // Field-level validation |
| 156 | 77 | | if (string.IsNullOrEmpty(Value)) |
| 0 | 78 | | return true; // Empty description is valid |
| | 79 | |
|
| 156 | 80 | | var utf8Bytes = Encoding.UTF8.GetBytes(Value); |
| 156 | 81 | | return utf8Bytes.Length <= MaxDescriptionBytes; |
| | 82 | | } |
| | 83 | |
|
| | 84 | | /// <summary> |
| | 85 | | /// Reads a DescriptionTaggedField from a BitReader |
| | 86 | | /// </summary> |
| | 87 | | /// <param name="bitReader">The BitReader to read from</param> |
| | 88 | | /// <param name="length">The length of the field</param> |
| | 89 | | /// <returns>The DescriptionTaggedField</returns> |
| | 90 | | /// <exception cref="ArgumentException">Thrown when the length is invalid</exception> |
| | 91 | | internal static DescriptionTaggedField FromBitReader(BitReader bitReader, short length) |
| | 92 | | { |
| | 93 | | switch (length) |
| | 94 | | { |
| | 95 | | case < 0: |
| 4 | 96 | | throw new ArgumentException( |
| 4 | 97 | | $"Invalid length for {nameof(DescriptionTaggedField)}. Length must be greater or equal to 0", |
| 4 | 98 | | nameof(length)); |
| | 99 | | case 0: |
| 0 | 100 | | return new DescriptionTaggedField(string.Empty); |
| | 101 | | } |
| | 102 | |
|
| | 103 | | // Read the data from the BitReader |
| 56 | 104 | | var data = new byte[(length * 5 + 7) / 8]; |
| 56 | 105 | | bitReader.ReadBits(data, length * 5); |
| | 106 | |
|
| 56 | 107 | | return new DescriptionTaggedField(Encoding.UTF8.GetString(length * 5 % 8 > 0 ? data[..^1] : data)); |
| | 108 | | } |
| | 109 | | } |