| | 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 payee public key |
| | 12 | | /// </summary> |
| | 13 | | /// <remarks> |
| | 14 | | /// The payee public key is a 33 byte public key that is used to identify the payee |
| | 15 | | /// </remarks> |
| | 16 | | /// <seealso cref="ITaggedField"/> |
| | 17 | | internal sealed class PayeePubKeyTaggedField : ITaggedField |
| | 18 | | { |
| 448 | 19 | | public TaggedFieldTypes Type => TaggedFieldTypes.PAYEE_PUB_KEY; |
| 60 | 20 | | internal PubKey Value { get; } |
| 12 | 21 | | public short Length => TaggedFieldConstants.PAYEE_PUBKEY_LENGTH; |
| | 22 | |
|
| | 23 | | /// <summary> |
| | 24 | | /// Initializes a new instance of the <see cref="ExpiryTimeTaggedField"/> class. |
| | 25 | | /// </summary> |
| | 26 | | /// <param name="value">The Expiry Time in seconds</param> |
| 64 | 27 | | internal PayeePubKeyTaggedField(PubKey value) |
| | 28 | | { |
| 64 | 29 | | Value = value; |
| 64 | 30 | | } |
| | 31 | |
|
| | 32 | | /// <inheritdoc/> |
| | 33 | | public void WriteToBitWriter(BitWriter bitWriter) |
| | 34 | | { |
| | 35 | | // Write data |
| 4 | 36 | | bitWriter.WriteBits(Value.ToBytes(), Length * 5); |
| 4 | 37 | | } |
| | 38 | |
|
| | 39 | | /// <inheritdoc/> |
| | 40 | | public bool IsValid() |
| | 41 | | { |
| 0 | 42 | | return true; |
| | 43 | | } |
| | 44 | |
|
| | 45 | | /// <summary> |
| | 46 | | /// Reads a PayeePubKeyTaggedField from a BitReader |
| | 47 | | /// </summary> |
| | 48 | | /// <param name="bitReader">The BitReader to read from</param> |
| | 49 | | /// <param name="length">The length of the field</param> |
| | 50 | | /// <returns>The PayeePubKeyTaggedField</returns> |
| | 51 | | /// <exception cref="ArgumentException">Thrown when the length is invalid</exception> |
| | 52 | | internal static PayeePubKeyTaggedField FromBitReader(BitReader bitReader, short length) |
| | 53 | | { |
| 4 | 54 | | if (length != TaggedFieldConstants.PAYEE_PUBKEY_LENGTH) |
| | 55 | | { |
| 4 | 56 | | throw new ArgumentException($"Invalid length for DescriptionHashTaggedField. Expected {TaggedFieldConstants. |
| | 57 | | } |
| | 58 | |
|
| | 59 | | // Read the data from the BitReader |
| 0 | 60 | | var data = new byte[33]; |
| 0 | 61 | | bitReader.ReadBits(data, length * 5); |
| | 62 | |
|
| 0 | 63 | | return new PayeePubKeyTaggedField(new PubKey(data)); |
| | 64 | | } |
| | 65 | | } |