| | 1 | | namespace NLightning.Bolt11.Models.TaggedFields; |
| | 2 | |
|
| | 3 | | using Constants; |
| | 4 | | using Domain.Channels.ValueObjects; |
| | 5 | | using Domain.Crypto.ValueObjects; |
| | 6 | | using Domain.Models; |
| | 7 | | using Domain.Utils; |
| | 8 | | using Enums; |
| | 9 | | using Interfaces; |
| | 10 | |
|
| | 11 | | /// <summary> |
| | 12 | | /// Tagged field for routing information |
| | 13 | | /// </summary> |
| | 14 | | /// <remarks> |
| | 15 | | /// The routing information is a collection of routing information entries. |
| | 16 | | /// Each entry contains the public key of the node, the short channel id, the base fee in msat, the fee proportional |
| | 17 | | /// millionths and the cltv expiry delta. |
| | 18 | | /// </remarks> |
| | 19 | | /// <seealso cref="ITaggedField"/> |
| | 20 | | internal sealed class RoutingInfoTaggedField : ITaggedField |
| | 21 | | { |
| 316 | 22 | | public TaggedFieldTypes Type => TaggedFieldTypes.RoutingInfo; |
| 252 | 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.RoutingInfoLength + 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.CompactPubKey, 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++) |
| 56 | 52 | | bitWriter.WriteBit(false); |
| 20 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <inheritdoc/> |
| | 56 | | public bool IsValid() |
| | 57 | | { |
| 112 | 58 | | foreach (var routingInfo in Value) |
| | 59 | | { |
| 32 | 60 | | if (routingInfo.FeeBaseMsat < 0) |
| 0 | 61 | | return false; |
| | 62 | |
|
| 32 | 63 | | if (routingInfo.FeeProportionalMillionths < 0) |
| 0 | 64 | | return false; |
| | 65 | |
|
| 32 | 66 | | if (routingInfo.CltvExpiryDelta < 0) |
| 0 | 67 | | return false; |
| | 68 | | } |
| | 69 | |
|
| 24 | 70 | | return true; |
| 0 | 71 | | } |
| | 72 | |
|
| | 73 | | /// <summary> |
| | 74 | | /// Create a new instance of the <see cref="RoutingInfoTaggedField"/> from a <see cref="BitReader"/> |
| | 75 | | /// </summary> |
| | 76 | | /// <param name="bitReader">The bit reader to read from</param> |
| | 77 | | /// <param name="length">The length of the tagged field</param> |
| | 78 | | /// <returns>A new instance of the <see cref="RoutingInfoTaggedField"/></returns> |
| | 79 | | internal static RoutingInfoTaggedField FromBitReader(BitReader bitReader, short length) |
| | 80 | | { |
| 8 | 81 | | var l = length * 5; |
| 8 | 82 | | var bitsReadAcc = 0; |
| 8 | 83 | | var routingInfos = new RoutingInfoCollection(); |
| | 84 | |
|
| 8 | 85 | | for (var i = 0; |
| 20 | 86 | | i < l && l - bitsReadAcc >= TaggedFieldConstants.RoutingInfoLength; |
| 12 | 87 | | i += TaggedFieldConstants.RoutingInfoLength) |
| | 88 | | { |
| 12 | 89 | | var pubkeyBytes = new byte[34]; |
| 12 | 90 | | bitsReadAcc += bitReader.ReadBits(pubkeyBytes, 264); |
| | 91 | |
|
| 12 | 92 | | var shortChannelBytes = new byte[9]; |
| 12 | 93 | | bitsReadAcc += bitReader.ReadBits(shortChannelBytes, 64); |
| | 94 | |
|
| 12 | 95 | | var feeBaseMsat = bitReader.ReadInt32FromBits(32); |
| 12 | 96 | | bitsReadAcc += 32; |
| | 97 | |
|
| 12 | 98 | | var feeProportionalMillionths = bitReader.ReadInt32FromBits(32); |
| 12 | 99 | | bitsReadAcc += 32; |
| | 100 | |
|
| 12 | 101 | | var minFinalCltvExpiry = bitReader.ReadInt16FromBits(16); |
| 12 | 102 | | bitsReadAcc += 16; |
| | 103 | |
|
| 12 | 104 | | routingInfos.Add(new RoutingInfo(new CompactPubKey(pubkeyBytes[..^1]), |
| 12 | 105 | | new ShortChannelId(shortChannelBytes[..^1]), |
| 12 | 106 | | feeBaseMsat, |
| 12 | 107 | | feeProportionalMillionths, |
| 12 | 108 | | minFinalCltvExpiry)); |
| | 109 | | } |
| | 110 | |
|
| | 111 | | // Skip any extra bits since padding is expected |
| 8 | 112 | | var extraBitsToSkip = l - bitsReadAcc; |
| 8 | 113 | | if (extraBitsToSkip > 0) |
| 8 | 114 | | bitReader.SkipBits(extraBitsToSkip); |
| | 115 | |
|
| 8 | 116 | | return new RoutingInfoTaggedField(routingInfos); |
| | 117 | | } |
| | 118 | |
|
| | 119 | | private void OnRoutingInfoCollectionChanged(object? sender, EventArgs e) |
| | 120 | | { |
| 4 | 121 | | Length = (short)((Value.Count * TaggedFieldConstants.RoutingInfoLength + Value.Count * 2) / 5); |
| 4 | 122 | | } |
| | 123 | | } |