| | 1 | | using NBitcoin; |
| | 2 | |
|
| | 3 | | namespace NLightning.Bolt11.Models.TaggedFields; |
| | 4 | |
|
| | 5 | | using Common.Utils; |
| | 6 | | using Constants; |
| | 7 | | using Domain.Models; |
| | 8 | | using Domain.ValueObjects; |
| | 9 | | using Enums; |
| | 10 | | using Interfaces; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Tagged field for routing information |
| | 14 | | /// </summary> |
| | 15 | | /// <remarks> |
| | 16 | | /// The routing information is a collection of routing information entries. |
| | 17 | | /// Each entry contains the public key of the node, the short channel id, the base fee in msat, the fee proportional mil |
| | 18 | | /// </remarks> |
| | 19 | | /// <seealso cref="ITaggedField"/> |
| | 20 | | internal sealed class RoutingInfoTaggedField : ITaggedField |
| | 21 | | { |
| 284 | 22 | | public TaggedFieldTypes Type => TaggedFieldTypes.ROUTING_INFO; |
| 236 | 23 | | internal RoutingInfoCollection Value { get; } |
| 68 | 24 | | public short Length { get; private set; } |
| | 25 | |
|
| | 26 | | /// <summary> |
| | 27 | | /// Initializes a new instance of the <see cref="DescriptionHashTaggedField"/> class. |
| | 28 | | /// </summary> |
| | 29 | | /// <param name="value">The Description Hash</param> |
| 24 | 30 | | internal RoutingInfoTaggedField(RoutingInfoCollection value) |
| | 31 | | { |
| 24 | 32 | | Value = value; |
| 24 | 33 | | Length = (short)((value.Count * TaggedFieldConstants.ROUTING_INFO_LENGTH + value.Count * 2) / 5); |
| | 34 | |
|
| 24 | 35 | | Value.Changed += OnRoutingInfoCollectionChanged; |
| 24 | 36 | | } |
| | 37 | |
|
| | 38 | | /// <inheritdoc/> |
| | 39 | | public void WriteToBitWriter(BitWriter bitWriter) |
| | 40 | | { |
| | 41 | | // Write data |
| 96 | 42 | | foreach (var routingInfo in Value) |
| | 43 | | { |
| 28 | 44 | | bitWriter.WriteBits(routingInfo.PubKey.ToBytes(), 264); |
| 28 | 45 | | bitWriter.WriteBits(routingInfo.ShortChannelId, 64); |
| 28 | 46 | | bitWriter.WriteInt32AsBits(routingInfo.FeeBaseMsat, 32); |
| 28 | 47 | | bitWriter.WriteInt32AsBits(routingInfo.FeeProportionalMillionths, 32); |
| 28 | 48 | | bitWriter.WriteInt16AsBits(routingInfo.CltvExpiryDelta, 16); |
| | 49 | | } |
| | 50 | |
|
| 152 | 51 | | for (var i = 0; i < Value.Count * 2; i++) |
| | 52 | | { |
| 56 | 53 | | bitWriter.WriteBit(false); |
| | 54 | | } |
| 20 | 55 | | } |
| | 56 | |
|
| | 57 | | /// <inheritdoc/> |
| | 58 | | public bool IsValid() |
| | 59 | | { |
| 40 | 60 | | foreach (var routingInfo in Value) |
| | 61 | | { |
| 12 | 62 | | if (routingInfo.FeeBaseMsat < 0) |
| | 63 | | { |
| 0 | 64 | | return false; |
| | 65 | | } |
| | 66 | |
|
| 12 | 67 | | if (routingInfo.FeeProportionalMillionths < 0) |
| | 68 | | { |
| 0 | 69 | | return false; |
| | 70 | | } |
| | 71 | |
|
| 12 | 72 | | if (routingInfo.CltvExpiryDelta < 0) |
| | 73 | | { |
| 0 | 74 | | return false; |
| | 75 | | } |
| | 76 | | } |
| | 77 | |
|
| 8 | 78 | | return true; |
| 0 | 79 | | } |
| | 80 | |
|
| | 81 | | /// <summary> |
| | 82 | | /// Create a new instance of the <see cref="RoutingInfoTaggedField"/> from a <see cref="BitReader"/> |
| | 83 | | /// </summary> |
| | 84 | | /// <param name="bitReader">The bit reader to read from</param> |
| | 85 | | /// <param name="length">The length of the tagged field</param> |
| | 86 | | /// <returns>A new instance of the <see cref="RoutingInfoTaggedField"/></returns> |
| | 87 | | internal static RoutingInfoTaggedField FromBitReader(BitReader bitReader, short length) |
| | 88 | | { |
| 8 | 89 | | var l = length * 5; |
| 8 | 90 | | var bitsReadAcc = 0; |
| 8 | 91 | | var routingInfos = new RoutingInfoCollection(); |
| | 92 | |
|
| 40 | 93 | | for (var i = 0; i < l && l - bitsReadAcc >= TaggedFieldConstants.ROUTING_INFO_LENGTH; i += TaggedFieldConstants. |
| | 94 | | { |
| 12 | 95 | | var pubkeyBytes = new byte[34]; |
| 12 | 96 | | bitsReadAcc += bitReader.ReadBits(pubkeyBytes, 264); |
| | 97 | |
|
| 12 | 98 | | var shortChannelBytes = new byte[9]; |
| 12 | 99 | | bitsReadAcc += bitReader.ReadBits(shortChannelBytes, 64); |
| | 100 | |
|
| 12 | 101 | | var feeBaseMsat = bitReader.ReadInt32FromBits(32); |
| 12 | 102 | | bitsReadAcc += 32; |
| | 103 | |
|
| 12 | 104 | | var feeProportionalMillionths = bitReader.ReadInt32FromBits(32); |
| 12 | 105 | | bitsReadAcc += 32; |
| | 106 | |
|
| 12 | 107 | | var minFinalCltvExpiry = bitReader.ReadInt16FromBits(16); |
| 12 | 108 | | bitsReadAcc += 16; |
| | 109 | |
|
| 12 | 110 | | routingInfos.Add(new RoutingInfo(new PubKey(pubkeyBytes[..^1]), |
| 12 | 111 | | new ShortChannelId(shortChannelBytes[..^1]), |
| 12 | 112 | | feeBaseMsat, |
| 12 | 113 | | feeProportionalMillionths, |
| 12 | 114 | | minFinalCltvExpiry)); |
| | 115 | | } |
| | 116 | |
|
| | 117 | | // Skip any extra bits since padding is expected |
| 8 | 118 | | var extraBitsToSkip = l - bitsReadAcc; |
| 8 | 119 | | if (extraBitsToSkip > 0) |
| | 120 | | { |
| 8 | 121 | | bitReader.SkipBits(extraBitsToSkip); |
| | 122 | | } |
| | 123 | |
|
| 8 | 124 | | return new RoutingInfoTaggedField(routingInfos); |
| | 125 | | } |
| | 126 | |
|
| | 127 | | private void OnRoutingInfoCollectionChanged(object? sender, EventArgs e) |
| | 128 | | { |
| 4 | 129 | | Length = (short)((Value.Count * TaggedFieldConstants.ROUTING_INFO_LENGTH + Value.Count * 2) / 5); |
| 4 | 130 | | } |
| | 131 | | } |