| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Infrastructure.Protocol.Tlv.Converters; |
| | | 4 | | |
| | | 5 | | using Domain.Enums; |
| | | 6 | | using Domain.Money; |
| | | 7 | | using Domain.Protocol.Constants; |
| | | 8 | | using Domain.Protocol.Interfaces; |
| | | 9 | | using Domain.Protocol.Tlv; |
| | | 10 | | using Infrastructure.Converters; |
| | | 11 | | |
| | | 12 | | public class FeeRangeTlvConverter : ITlvConverter<FeeRangeTlv> |
| | | 13 | | { |
| | | 14 | | public BaseTlv ConvertToBase(FeeRangeTlv tlv) |
| | | 15 | | { |
| | 8 | 16 | | var tlvValue = new byte[sizeof(ulong) * 2]; |
| | 8 | 17 | | EndianBitConverter.GetBytesBigEndian(tlv.MinFeeAmount.Satoshi).CopyTo(tlvValue, 0); |
| | 8 | 18 | | EndianBitConverter.GetBytesBigEndian(tlv.MaxFeeAmount.Satoshi).CopyTo(tlvValue, sizeof(ulong)); |
| | | 19 | | |
| | 8 | 20 | | return new BaseTlv(tlv.Type, tlvValue); |
| | | 21 | | } |
| | | 22 | | |
| | | 23 | | public FeeRangeTlv ConvertFromBase(BaseTlv baseTlv) |
| | | 24 | | { |
| | 8 | 25 | | if (baseTlv.Type != TlvConstants.FeeRange) |
| | 0 | 26 | | throw new InvalidCastException("Invalid TLV type"); |
| | | 27 | | |
| | 8 | 28 | | if (baseTlv.Length != sizeof(ulong) * 2) // 2 long (128 bits) is 16 bytes |
| | 0 | 29 | | throw new InvalidCastException("Invalid length"); |
| | | 30 | | |
| | 8 | 31 | | var minFeeAmount = LightningMoney |
| | 8 | 32 | | .FromUnit(EndianBitConverter.ToUInt64BigEndian(baseTlv.Value[..sizeof(ulong)]), LightningMoneyUnit.Satoshi); |
| | 8 | 33 | | var maxFeeAmount = LightningMoney |
| | 8 | 34 | | .FromUnit(EndianBitConverter.ToUInt64BigEndian(baseTlv.Value[sizeof(ulong)..]), LightningMoneyUnit.Satoshi); |
| | | 35 | | |
| | 8 | 36 | | return new FeeRangeTlv(minFeeAmount, maxFeeAmount); |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | [ExcludeFromCodeCoverage] |
| | | 40 | | BaseTlv ITlvConverter.ConvertFromBase(BaseTlv tlv) |
| | | 41 | | { |
| | | 42 | | return ConvertFromBase(tlv); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | [ExcludeFromCodeCoverage] |
| | | 46 | | BaseTlv ITlvConverter.ConvertToBase(BaseTlv tlv) |
| | | 47 | | { |
| | | 48 | | return ConvertToBase(tlv as FeeRangeTlv |
| | | 49 | | ?? throw new InvalidCastException($"Error converting BaseTlv to {nameof(FeeRangeTlv)}")); |
| | | 50 | | } |
| | | 51 | | } |