| | | 1 | | using System.Diagnostics.CodeAnalysis; |
| | | 2 | | using NLightning.Domain.Money; |
| | | 3 | | |
| | | 4 | | namespace NLightning.Infrastructure.Protocol.Tlv.Converters; |
| | | 5 | | |
| | | 6 | | using Domain.Protocol.Constants; |
| | | 7 | | using Domain.Protocol.Interfaces; |
| | | 8 | | using Domain.Protocol.Tlv; |
| | | 9 | | using Infrastructure.Converters; |
| | | 10 | | |
| | | 11 | | public class FundingOutputContributionTlvConverter : ITlvConverter<FundingOutputContributionTlv> |
| | | 12 | | { |
| | | 13 | | public BaseTlv ConvertToBase(FundingOutputContributionTlv tlv) |
| | | 14 | | { |
| | 16 | 15 | | return new BaseTlv(tlv.Type, EndianBitConverter.GetBytesBigEndian(tlv.Amount.Satoshi)); |
| | | 16 | | } |
| | | 17 | | |
| | | 18 | | public FundingOutputContributionTlv ConvertFromBase(BaseTlv baseTlv) |
| | | 19 | | { |
| | 12 | 20 | | if (baseTlv.Type != TlvConstants.FundingOutputContribution) |
| | | 21 | | { |
| | 0 | 22 | | throw new InvalidCastException("Invalid TLV type"); |
| | | 23 | | } |
| | | 24 | | |
| | 12 | 25 | | if (baseTlv.Length != 8) // long (64 bits) is 8 bytes |
| | | 26 | | { |
| | 0 | 27 | | throw new InvalidCastException("Invalid length"); |
| | | 28 | | } |
| | | 29 | | |
| | 12 | 30 | | var amount = LightningMoney.Satoshis(EndianBitConverter.ToInt64BigEndian(baseTlv.Value)); |
| | | 31 | | |
| | 12 | 32 | | return new FundingOutputContributionTlv(amount); |
| | | 33 | | } |
| | | 34 | | |
| | | 35 | | [ExcludeFromCodeCoverage] |
| | | 36 | | BaseTlv ITlvConverter.ConvertFromBase(BaseTlv tlv) |
| | | 37 | | { |
| | | 38 | | return ConvertFromBase(tlv); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | [ExcludeFromCodeCoverage] |
| | | 42 | | BaseTlv ITlvConverter.ConvertToBase(BaseTlv tlv) |
| | | 43 | | { |
| | | 44 | | return ConvertToBase(tlv as FundingOutputContributionTlv |
| | | 45 | | ?? throw new InvalidCastException( |
| | | 46 | | $"Error casting BaseTlv to {nameof(FundingOutputContributionTlv)}")); |
| | | 47 | | } |
| | | 48 | | } |