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