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