| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Bolt11.Models.TaggedFields; |
| | 4 | |
|
| | 5 | | using Common.Utils; |
| | 6 | | using Constants; |
| | 7 | | using Enums; |
| | 8 | | using Interfaces; |
| | 9 | |
|
| | 10 | | /// <summary> |
| | 11 | | /// Tagged field for the description hash |
| | 12 | | /// </summary> |
| | 13 | | /// <remarks> |
| | 14 | | /// The description hash is a 32 byte hash that is used to identify a description |
| | 15 | | /// </remarks> |
| | 16 | | /// <seealso cref="ITaggedField"/> |
| | 17 | | internal sealed class DescriptionHashTaggedField : ITaggedField |
| | 18 | | { |
| 552 | 19 | | public TaggedFieldTypes Type => TaggedFieldTypes.DESCRIPTION_HASH; |
| 96 | 20 | | internal uint256 Value { get; } |
| 80 | 21 | | public short Length => TaggedFieldConstants.HASH_LENGTH; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes a new instance of the <see cref="DescriptionHashTaggedField"/> class. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="value">The Description Hash</param> |
| 72 | 27 | | internal DescriptionHashTaggedField(uint256 value) |
| | 28 | | { |
| 72 | 29 | | Value = value; |
| 72 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <inheritdoc/> |
| | 33 | | public void WriteToBitWriter(BitWriter bitWriter) |
| | 34 | | { |
| 28 | 35 | | var data = Value.ToBytes(); |
| 28 | 36 | | if (BitConverter.IsLittleEndian) |
| | 37 | | { |
| 28 | 38 | | Array.Reverse(data); |
| | 39 | | } |
| | 40 | |
|
| | 41 | | // Write data |
| 28 | 42 | | bitWriter.WriteBits(data, Length * 5); |
| 28 | 43 | | } |
| | 44 | |
|
| | 45 | | /// <inheritdoc/> |
| | 46 | | public bool IsValid() |
| | 47 | | { |
| 36 | 48 | | return Value != uint256.Zero; |
| | 49 | | } |
| | 50 | |
|
| | 51 | | /// <summary> |
| | 52 | | /// Reads a DescriptionHashTaggedField from a BitReader |
| | 53 | | /// </summary> |
| | 54 | | /// <param name="bitReader">The BitReader to read from</param> |
| | 55 | | /// <param name="length">The length of the field</param> |
| | 56 | | /// <returns>The DescriptionHashTaggedField</returns> |
| | 57 | | /// <exception cref="ArgumentException">Thrown when the length is invalid</exception> |
| | 58 | | internal static DescriptionHashTaggedField FromBitReader(BitReader bitReader, short length) |
| | 59 | | { |
| 36 | 60 | | if (length != TaggedFieldConstants.HASH_LENGTH) |
| | 61 | | { |
| 4 | 62 | | throw new ArgumentException($"Invalid length for DescriptionHashTaggedField. Expected {TaggedFieldConstants. |
| | 63 | | } |
| | 64 | |
|
| | 65 | | // Read the data from the BitReader |
| 32 | 66 | | var data = new byte[(TaggedFieldConstants.HASH_LENGTH * 5 + 7) / 8]; |
| 32 | 67 | | bitReader.ReadBits(data, length * 5); |
| 32 | 68 | | data = data[..^1]; |
| | 69 | |
|
| 32 | 70 | | if (BitConverter.IsLittleEndian) |
| | 71 | | { |
| 32 | 72 | | Array.Reverse(data); |
| | 73 | | } |
| | 74 | |
|
| 32 | 75 | | return new DescriptionHashTaggedField(new uint256(data)); |
| | 76 | | } |
| | 77 | | } |