| | 1 | | using System.Diagnostics.CodeAnalysis; |
| | 2 | | using NBitcoin; |
| | 3 | |
|
| | 4 | | namespace NLightning.Infrastructure.Protocol.Tlv.Converters; |
| | 5 | |
|
| | 6 | | using Domain.Protocol.Constants; |
| | 7 | | using Domain.Protocol.Tlv; |
| | 8 | | using Domain.Protocol.Tlv.Converters; |
| | 9 | |
|
| | 10 | | public class BlindedPathTlvConverter : ITlvConverter<BlindedPathTlv> |
| | 11 | | { |
| | 12 | | public BaseTlv ConvertToBase(BlindedPathTlv tlv) |
| | 13 | | { |
| 8 | 14 | | tlv.Value = tlv.PathKey.ToBytes(); |
| | 15 | |
|
| 8 | 16 | | return tlv; |
| | 17 | | } |
| | 18 | |
|
| | 19 | | public BlindedPathTlv ConvertFromBase(BaseTlv baseTlv) |
| | 20 | | { |
| 8 | 21 | | if (baseTlv.Type != TlvConstants.BLINDED_PATH) |
| | 22 | | { |
| 0 | 23 | | throw new InvalidCastException("Invalid TLV type"); |
| | 24 | | } |
| | 25 | |
|
| 8 | 26 | | if (baseTlv.Length == 0) |
| | 27 | | { |
| 0 | 28 | | throw new InvalidCastException("Invalid length"); |
| | 29 | | } |
| | 30 | |
|
| 8 | 31 | | return new BlindedPathTlv(new PubKey(baseTlv.Value)); |
| | 32 | | } |
| | 33 | |
|
| | 34 | | [ExcludeFromCodeCoverage] |
| | 35 | | BaseTlv ITlvConverter.ConvertFromBase(BaseTlv tlv) |
| | 36 | | { |
| | 37 | | return ConvertFromBase(tlv); |
| | 38 | | } |
| | 39 | |
|
| | 40 | | [ExcludeFromCodeCoverage] |
| | 41 | | BaseTlv ITlvConverter.ConvertToBase(BaseTlv tlv) |
| | 42 | | { |
| | 43 | | return ConvertToBase(tlv as BlindedPathTlv |
| | 44 | | ?? throw new InvalidCastException($"Error converting BaseTlv to {nameof(BlindedPathTlv)}")) |
| | 45 | | } |
| | 46 | | } |