| | 1 | | using System.Text; |
| | 2 | |
|
| | 3 | | namespace NLightning.Bolt11.Models.TaggedFields; |
| | 4 | |
|
| | 5 | | using Common.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 readonly byte[] _data; |
| | 19 | |
|
| 808 | 20 | | public TaggedFieldTypes Type => TaggedFieldTypes.DESCRIPTION; |
| 248 | 21 | | internal string Value { get; } |
| 208 | 22 | | public short Length { get; } |
| | 23 | |
|
| | 24 | | /// <summary> |
| | 25 | | /// Initializes a new instance of the <see cref="DescriptionTaggedField"/> class. |
| | 26 | | /// </summary> |
| | 27 | | /// <param name="value">The Description</param> |
| 176 | 28 | | internal DescriptionTaggedField(string value) |
| | 29 | | { |
| 176 | 30 | | Value = value; |
| | 31 | |
|
| 176 | 32 | | if (string.IsNullOrEmpty(value)) |
| | 33 | | { |
| 0 | 34 | | _data = []; |
| 0 | 35 | | Length = 0; |
| | 36 | | } |
| | 37 | | else |
| | 38 | | { |
| | 39 | | // Add Padding if needed |
| 176 | 40 | | var data = Encoding.UTF8.GetBytes(Value); |
| 176 | 41 | | var bitLength = data.Length * 8; |
| 176 | 42 | | var totalBits = bitLength + (5 - bitLength % 5) % 5; |
| 176 | 43 | | if (totalBits != bitLength) |
| | 44 | | { |
| 156 | 45 | | _data = new byte[data.Length + 1]; |
| 156 | 46 | | Array.Copy(data, _data, data.Length); |
| | 47 | | } |
| | 48 | | else |
| | 49 | | { |
| 20 | 50 | | _data = data; |
| | 51 | | } |
| | 52 | |
|
| 176 | 53 | | Length = (short)(totalBits / 5); |
| | 54 | | } |
| 176 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <inheritdoc/> |
| | 58 | | public void WriteToBitWriter(BitWriter bitWriter) |
| | 59 | | { |
| 52 | 60 | | if (Length == 0) |
| | 61 | | { |
| 0 | 62 | | return; |
| | 63 | | } |
| | 64 | |
|
| | 65 | | // Write data |
| 52 | 66 | | bitWriter.WriteBits(_data, Length * 5); |
| 52 | 67 | | } |
| | 68 | |
|
| | 69 | | /// <inheritdoc/> |
| | 70 | | public bool IsValid() |
| | 71 | | { |
| 40 | 72 | | return true; |
| | 73 | | } |
| | 74 | |
|
| | 75 | | /// <summary> |
| | 76 | | /// Reads a DescriptionTaggedField from a BitReader |
| | 77 | | /// </summary> |
| | 78 | | /// <param name="bitReader">The BitReader to read from</param> |
| | 79 | | /// <param name="length">The length of the field</param> |
| | 80 | | /// <returns>The DescriptionTaggedField</returns> |
| | 81 | | /// <exception cref="ArgumentException">Thrown when the length is invalid</exception> |
| | 82 | | internal static DescriptionTaggedField FromBitReader(BitReader bitReader, short length) |
| | 83 | | { |
| | 84 | | switch (length) |
| | 85 | | { |
| | 86 | | case < 0: |
| 4 | 87 | | throw new ArgumentException("Invalid length for DescriptionTaggedField. Length must be greater or equal |
| | 88 | | case 0: |
| 0 | 89 | | return new DescriptionTaggedField(string.Empty); |
| | 90 | | } |
| | 91 | |
|
| | 92 | | // Read the data from the BitReader |
| 56 | 93 | | var data = new byte[(length * 5 + 7) / 8]; |
| 56 | 94 | | bitReader.ReadBits(data, length * 5); |
| | 95 | |
|
| 56 | 96 | | return new DescriptionTaggedField(Encoding.UTF8.GetString(length * 5 % 8 > 0 ? data[..^1] : data)); |
| | 97 | | } |
| | 98 | | } |