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