| | 1 | | using System.Buffers; |
| | 2 | | using System.Runtime.Serialization; |
| | 3 | | using NLightning.Domain.Serialization.Payloads; |
| | 4 | |
|
| | 5 | | namespace NLightning.Infrastructure.Serialization.Payloads; |
| | 6 | |
|
| | 7 | | using Converters; |
| | 8 | | using Domain.Protocol.Payloads; |
| | 9 | | using Domain.Protocol.Payloads.Interfaces; |
| | 10 | | using Domain.Serialization.Factories; |
| | 11 | | using Domain.ValueObjects; |
| | 12 | | using Exceptions; |
| | 13 | |
|
| | 14 | | public class TxAddInputPayloadSerializer : IPayloadSerializer<TxAddInputPayload> |
| | 15 | | { |
| | 16 | | private readonly IValueObjectSerializerFactory _valueObjectSerializerFactory; |
| | 17 | |
|
| 4 | 18 | | public TxAddInputPayloadSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | 19 | | { |
| 4 | 20 | | _valueObjectSerializerFactory = valueObjectSerializerFactory; |
| 4 | 21 | | } |
| | 22 | |
|
| | 23 | | public async Task SerializeAsync(IMessagePayload payload, Stream stream) |
| | 24 | | { |
| 4 | 25 | | if (payload is not TxAddInputPayload txAddInputPayload) |
| 0 | 26 | | throw new SerializationException($"Payload is not of type {nameof(TxAddInputPayload)}"); |
| | 27 | |
|
| | 28 | | // Get the value object serializer |
| 4 | 29 | | var channelIdSerializer = |
| 4 | 30 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 4 | 31 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 4 | 32 | | await channelIdSerializer.SerializeAsync(txAddInputPayload.ChannelId, stream); |
| | 33 | |
|
| 4 | 34 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(txAddInputPayload.SerialId)); |
| 4 | 35 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)txAddInputPayload.PrevTx.Length)); |
| 4 | 36 | | await stream.WriteAsync(txAddInputPayload.PrevTx); |
| 4 | 37 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(txAddInputPayload.PrevTxVout)); |
| 4 | 38 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(txAddInputPayload.Sequence)); |
| 4 | 39 | | } |
| | 40 | |
|
| | 41 | | public async Task<TxAddInputPayload?> DeserializeAsync(Stream stream) |
| | 42 | | { |
| 4 | 43 | | var buffer = ArrayPool<byte>.Shared.Rent(sizeof(ulong)); |
| | 44 | |
|
| | 45 | | try |
| | 46 | | { |
| | 47 | | // Get the value object serializer |
| 4 | 48 | | var channelIdSerializer = |
| 4 | 49 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 4 | 50 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 4 | 51 | | var channelId = await channelIdSerializer.DeserializeAsync(stream); |
| | 52 | |
|
| 4 | 53 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]); |
| 4 | 54 | | var serialId = EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]); |
| | 55 | |
|
| 4 | 56 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ushort)]); |
| 4 | 57 | | var prevTxLength = EndianBitConverter.ToUInt16BigEndian(buffer[..sizeof(ushort)]); |
| | 58 | |
|
| 4 | 59 | | var prevTx = new byte[prevTxLength]; |
| 4 | 60 | | await stream.ReadExactlyAsync(prevTx); |
| | 61 | |
|
| 4 | 62 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(uint)]); |
| 4 | 63 | | var prevTxVout = EndianBitConverter.ToUInt32BigEndian(buffer[..sizeof(uint)]); |
| | 64 | |
|
| 4 | 65 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(uint)]); |
| 4 | 66 | | var sequence = EndianBitConverter.ToUInt32BigEndian(buffer[..sizeof(uint)]); |
| | 67 | |
|
| 4 | 68 | | return new TxAddInputPayload(channelId, serialId, prevTx, prevTxVout, sequence); |
| | 69 | | } |
| 0 | 70 | | catch (Exception e) |
| | 71 | | { |
| 0 | 72 | | throw new PayloadSerializationException($"Error deserializing {nameof(TxAddInputPayload)}", e); |
| | 73 | | } |
| | 74 | | finally |
| | 75 | | { |
| 4 | 76 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 77 | | } |
| 4 | 78 | | } |
| | 79 | |
|
| | 80 | | async Task<IMessagePayload?> IPayloadSerializer.DeserializeAsync(Stream stream) |
| | 81 | | { |
| 0 | 82 | | return await DeserializeAsync(stream); |
| 0 | 83 | | } |
| | 84 | | } |