| | 1 | | using System.Diagnostics.CodeAnalysis; |
| | 2 | |
|
| | 3 | | namespace NLightning.Infrastructure.Protocol.Tlv.Converters; |
| | 4 | |
|
| | 5 | | using Domain.Protocol.Constants; |
| | 6 | | using Domain.Protocol.Tlv; |
| | 7 | | using Domain.Protocol.Tlv.Converters; |
| | 8 | | using Domain.ValueObjects; |
| | 9 | |
|
| | 10 | | public class NetworksTlvConverter : ITlvConverter<NetworksTlv> |
| | 11 | | { |
| | 12 | | public BaseTlv ConvertToBase(NetworksTlv tlv) |
| | 13 | | { |
| 8 | 14 | | return tlv; |
| | 15 | | } |
| | 16 | |
|
| | 17 | | public NetworksTlv ConvertFromBase(BaseTlv baseTlv) |
| | 18 | | { |
| 8 | 19 | | if (baseTlv.Type != TlvConstants.NETWORKS) |
| | 20 | | { |
| 0 | 21 | | throw new InvalidCastException("Invalid TLV type"); |
| | 22 | | } |
| | 23 | |
|
| 8 | 24 | | if (baseTlv.Length % ChainHash.LENGTH != 0) |
| | 25 | | { |
| 0 | 26 | | throw new InvalidCastException("Invalid length"); |
| | 27 | | } |
| | 28 | |
|
| 8 | 29 | | var chainHashes = new List<ChainHash>(); |
| | 30 | | // split the Value into 32 bytes chunks and add it to the list |
| 32 | 31 | | for (var i = 0; i < baseTlv.Length; i += ChainHash.LENGTH) |
| | 32 | | { |
| 8 | 33 | | chainHashes.Add(baseTlv.Value[i..(i + ChainHash.LENGTH)]); |
| | 34 | | } |
| | 35 | |
|
| 8 | 36 | | return new NetworksTlv(chainHashes); |
| | 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 NetworksTlv |
| | 49 | | ?? throw new InvalidCastException($"Error converting BaseTlv to {nameof(NetworksTlv)}")); |
| | 50 | | } |
| | 51 | | } |