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