| | 1 | | using System.Runtime.Serialization; |
| | 2 | | using NLightning.Domain.Protocol.Interfaces; |
| | 3 | | using NLightning.Domain.Serialization.Interfaces; |
| | 4 | |
|
| | 5 | | namespace NLightning.Infrastructure.Serialization.Messages.Types; |
| | 6 | |
|
| | 7 | | using Domain.Protocol.Constants; |
| | 8 | | using Domain.Protocol.Messages; |
| | 9 | | using Domain.Protocol.Payloads; |
| | 10 | | using Domain.Protocol.Tlv; |
| | 11 | | using Exceptions; |
| | 12 | | using Interfaces; |
| | 13 | |
|
| | 14 | | public class TxInitRbfMessageTypeSerializer : IMessageTypeSerializer<TxInitRbfMessage> |
| | 15 | | { |
| | 16 | | private readonly IPayloadSerializerFactory _payloadSerializerFactory; |
| | 17 | | private readonly ITlvConverterFactory _tlvConverterFactory; |
| | 18 | | private readonly ITlvStreamSerializer _tlvStreamSerializer; |
| | 19 | |
|
| 20 | 20 | | public TxInitRbfMessageTypeSerializer(IPayloadSerializerFactory payloadSerializerFactory, |
| 20 | 21 | | ITlvConverterFactory tlvConverterFactory, |
| 20 | 22 | | ITlvStreamSerializer tlvStreamSerializer) |
| | 23 | | { |
| 20 | 24 | | _payloadSerializerFactory = payloadSerializerFactory; |
| 20 | 25 | | _tlvConverterFactory = tlvConverterFactory; |
| 20 | 26 | | _tlvStreamSerializer = tlvStreamSerializer; |
| 20 | 27 | | } |
| | 28 | |
|
| | 29 | | public async Task SerializeAsync(IMessage message, Stream stream) |
| | 30 | | { |
| 8 | 31 | | if (message is not TxInitRbfMessage txAckRbfMessage) |
| 0 | 32 | | throw new SerializationException("Message is not of type TxInitRbfMessage"); |
| | 33 | |
|
| | 34 | | // Get the payload serializer |
| 8 | 35 | | var payloadTypeSerializer = _payloadSerializerFactory.GetSerializer(message.Type) |
| 8 | 36 | | ?? throw new SerializationException("No serializer found for payload type"); |
| 8 | 37 | | await payloadTypeSerializer.SerializeAsync(message.Payload, stream); |
| | 38 | |
|
| | 39 | | // Serialize the TLV stream |
| 8 | 40 | | await _tlvStreamSerializer.SerializeAsync(txAckRbfMessage.Extension, stream); |
| 8 | 41 | | } |
| | 42 | |
|
| | 43 | | /// <summary> |
| | 44 | | /// Deserialize a TxInitRbfMessage from a stream. |
| | 45 | | /// </summary> |
| | 46 | | /// <param name="stream">The stream to deserialize from.</param> |
| | 47 | | /// <returns>The deserialized TxInitRbfMessage.</returns> |
| | 48 | | /// <exception cref="MessageSerializationException">Error deserializing TxInitRbfMessage</exception> |
| | 49 | | public async Task<TxInitRbfMessage> DeserializeAsync(Stream stream) |
| | 50 | | { |
| | 51 | | try |
| | 52 | | { |
| | 53 | | // Deserialize payload |
| 12 | 54 | | var payloadSerializer = _payloadSerializerFactory.GetSerializer<TxInitRbfPayload>() |
| 12 | 55 | | ?? throw new SerializationException("No serializer found for payload type"); |
| 12 | 56 | | var payload = await payloadSerializer.DeserializeAsync(stream) |
| 12 | 57 | | ?? throw new SerializationException("Error serializing payload"); |
| | 58 | |
|
| | 59 | | // Deserialize extension |
| 12 | 60 | | if (stream.Position >= stream.Length) |
| 4 | 61 | | return new TxInitRbfMessage(payload); |
| | 62 | |
|
| 8 | 63 | | var extension = await _tlvStreamSerializer.DeserializeAsync(stream); |
| 4 | 64 | | if (extension is null) |
| 0 | 65 | | return new TxInitRbfMessage(payload); |
| | 66 | |
|
| 4 | 67 | | FundingOutputContributionTlv? fundingOutputContributionTlv = null; |
| 4 | 68 | | if (extension.TryGetTlv(TlvConstants.FundingOutputContribution, out var baseFundingOutputContributionTlv)) |
| | 69 | | { |
| 4 | 70 | | var tlvConverter = _tlvConverterFactory.GetConverter<FundingOutputContributionTlv>() |
| 4 | 71 | | ?? throw new SerializationException( |
| 4 | 72 | | $"No serializer found for tlv type {nameof(FundingOutputContributionTlv)}"); |
| 4 | 73 | | fundingOutputContributionTlv = tlvConverter.ConvertFromBase(baseFundingOutputContributionTlv!); |
| | 74 | | } |
| | 75 | |
|
| 4 | 76 | | RequireConfirmedInputsTlv? requireConfirmedInputsTlv = null; |
| 4 | 77 | | if (extension.TryGetTlv(TlvConstants.RequireConfirmedInputs, out var baserequireConfirmedInputsTlv)) |
| | 78 | | { |
| 4 | 79 | | var tlvConverter = |
| 4 | 80 | | _tlvConverterFactory.GetConverter<RequireConfirmedInputsTlv>() |
| 4 | 81 | | ?? throw new SerializationException( |
| 4 | 82 | | $"No serializer found for tlv type {nameof(RequireConfirmedInputsTlv)}"); |
| 4 | 83 | | requireConfirmedInputsTlv = tlvConverter.ConvertFromBase(baserequireConfirmedInputsTlv!); |
| | 84 | | } |
| | 85 | |
|
| 4 | 86 | | return new TxInitRbfMessage(payload, fundingOutputContributionTlv, requireConfirmedInputsTlv); |
| | 87 | | } |
| 4 | 88 | | catch (SerializationException e) |
| | 89 | | { |
| 4 | 90 | | throw new MessageSerializationException("Error deserializing TxInitRbfMessage", e); |
| | 91 | | } |
| 8 | 92 | | } |
| | 93 | |
|
| | 94 | | async Task<IMessage> IMessageTypeSerializer.DeserializeAsync(Stream stream) |
| | 95 | | { |
| 0 | 96 | | return await DeserializeAsync(stream); |
| 0 | 97 | | } |
| | 98 | | } |