| | 1 | | using System.Buffers; |
| | 2 | | using System.Runtime.Serialization; |
| | 3 | | using NLightning.Domain.Protocol.Interfaces; |
| | 4 | | using NLightning.Domain.Serialization.Interfaces; |
| | 5 | |
|
| | 6 | | namespace NLightning.Infrastructure.Serialization.Payloads; |
| | 7 | |
|
| | 8 | | using Converters; |
| | 9 | | using Domain.Channels.ValueObjects; |
| | 10 | | using Domain.Crypto.Constants; |
| | 11 | | using Domain.Crypto.ValueObjects; |
| | 12 | | using Domain.Enums; |
| | 13 | | using Domain.Money; |
| | 14 | | using Domain.Protocol.Payloads; |
| | 15 | | using Exceptions; |
| | 16 | |
|
| | 17 | | public class ClosingSignedPayloadSerializer : IPayloadSerializer<ClosingSignedPayload> |
| | 18 | | { |
| | 19 | | private readonly IValueObjectSerializerFactory _valueObjectSerializerFactory; |
| | 20 | |
|
| 4 | 21 | | public ClosingSignedPayloadSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | 22 | | { |
| 4 | 23 | | _valueObjectSerializerFactory = valueObjectSerializerFactory; |
| 4 | 24 | | } |
| | 25 | |
|
| | 26 | | public async Task SerializeAsync(IMessagePayload payload, Stream stream) |
| | 27 | | { |
| 4 | 28 | | if (payload is not ClosingSignedPayload closingSignedPayload) |
| 0 | 29 | | throw new SerializationException($"Payload is not of type {nameof(ClosingSignedPayload)}"); |
| | 30 | |
|
| | 31 | | // Get the value object serializer |
| 4 | 32 | | var channelIdSerializer = |
| 4 | 33 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 4 | 34 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 4 | 35 | | await channelIdSerializer.SerializeAsync(closingSignedPayload.ChannelId, stream); |
| | 36 | |
|
| | 37 | | // Serialize other types |
| 4 | 38 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(closingSignedPayload.FeeAmount.Satoshi)); |
| 4 | 39 | | await stream.WriteAsync(closingSignedPayload.Signature); |
| 4 | 40 | | } |
| | 41 | |
|
| | 42 | | public async Task<ClosingSignedPayload?> DeserializeAsync(Stream stream) |
| | 43 | | { |
| 8 | 44 | | var buffer = ArrayPool<byte>.Shared.Rent(CryptoConstants.MaxSignatureSize); |
| | 45 | |
|
| | 46 | | try |
| | 47 | | { |
| | 48 | | // Get the value object serializer |
| 8 | 49 | | var channelIdSerializer = |
| 8 | 50 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 8 | 51 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 8 | 52 | | var channelId = await channelIdSerializer.DeserializeAsync(stream); |
| | 53 | |
|
| 8 | 54 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]); |
| 8 | 55 | | var feeSatoshis = LightningMoney.FromUnit(EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]), |
| 8 | 56 | | LightningMoneyUnit.Satoshi); |
| | 57 | |
|
| 8 | 58 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..CryptoConstants.MaxSignatureSize]); |
| 8 | 59 | | var signature = new CompactSignature(buffer[..CryptoConstants.MaxSignatureSize]); |
| | 60 | |
|
| 8 | 61 | | return new ClosingSignedPayload(channelId, feeSatoshis, signature); |
| | 62 | | } |
| 0 | 63 | | catch (Exception e) |
| | 64 | | { |
| 0 | 65 | | throw new PayloadSerializationException("Error deserializing ClosingSignedPayload", e); |
| | 66 | | } |
| | 67 | | finally |
| | 68 | | { |
| 8 | 69 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 70 | | } |
| 8 | 71 | | } |
| | 72 | |
|
| | 73 | | async Task<IMessagePayload?> IPayloadSerializer.DeserializeAsync(Stream stream) |
| | 74 | | { |
| 0 | 75 | | return await DeserializeAsync(stream); |
| 0 | 76 | | } |
| | 77 | | } |