| | 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.Crypto.Constants; |
| | 11 | | using Domain.Protocol.Payloads; |
| | 12 | | using Exceptions; |
| | 13 | |
|
| | 14 | | public class UpdateAddHtlcPayloadSerializer : IPayloadSerializer<UpdateAddHtlcPayload> |
| | 15 | | { |
| | 16 | | private readonly IValueObjectSerializerFactory _valueObjectSerializerFactory; |
| | 17 | |
|
| 4 | 18 | | public UpdateAddHtlcPayloadSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | 19 | | { |
| 4 | 20 | | _valueObjectSerializerFactory = valueObjectSerializerFactory; |
| 4 | 21 | | } |
| | 22 | |
|
| | 23 | | public async Task SerializeAsync(IMessagePayload payload, Stream stream) |
| | 24 | | { |
| 12 | 25 | | if (payload is not UpdateAddHtlcPayload updateAddHtlcPayload) |
| 0 | 26 | | throw new SerializationException($"Payload is not of type {nameof(UpdateAddHtlcPayload)}"); |
| | 27 | |
|
| | 28 | | // Get the ChannelId serializer |
| 12 | 29 | | var channelIdSerializer = |
| 12 | 30 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 12 | 31 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 12 | 32 | | await channelIdSerializer.SerializeAsync(updateAddHtlcPayload.ChannelId, stream); |
| | 33 | |
|
| 12 | 34 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(updateAddHtlcPayload.Id)); |
| 12 | 35 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(updateAddHtlcPayload.Amount.MilliSatoshi)); |
| 12 | 36 | | await stream.WriteAsync(updateAddHtlcPayload.PaymentHash); |
| 12 | 37 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(updateAddHtlcPayload.CltvExpiry)); |
| 12 | 38 | | if (updateAddHtlcPayload.OnionRoutingPacket is not null) |
| 4 | 39 | | await stream.WriteAsync(updateAddHtlcPayload.OnionRoutingPacket.Value); |
| 12 | 40 | | } |
| | 41 | |
|
| | 42 | | public async Task<UpdateAddHtlcPayload?> DeserializeAsync(Stream stream) |
| | 43 | | { |
| 16 | 44 | | var buffer = ArrayPool<byte>.Shared.Rent(sizeof(ulong)); |
| | 45 | |
|
| | 46 | | try |
| | 47 | | { |
| | 48 | | // Get the ChannelId serializer |
| 16 | 49 | | var channelIdSerializer = |
| 16 | 50 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 16 | 51 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 16 | 52 | | var channelId = await channelIdSerializer.DeserializeAsync(stream); |
| | 53 | |
|
| 16 | 54 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]); |
| 16 | 55 | | var id = EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]); |
| | 56 | |
|
| 16 | 57 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]); |
| 16 | 58 | | var amountMsat = EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]); |
| | 59 | |
|
| 16 | 60 | | var paymentHash = new byte[CryptoConstants.Sha256HashLen]; |
| 16 | 61 | | await stream.ReadExactlyAsync(paymentHash); |
| | 62 | |
|
| 16 | 63 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(uint)]); |
| 16 | 64 | | var cltvExpiry = EndianBitConverter.ToUInt32BigEndian(buffer[..sizeof(uint)]); |
| | 65 | |
|
| 16 | 66 | | byte[]? onionRoutingPacket = null; |
| 16 | 67 | | if (stream.Position + 1366 <= stream.Length) |
| | 68 | | { |
| 4 | 69 | | onionRoutingPacket = new byte[1366]; |
| 4 | 70 | | await stream.ReadExactlyAsync(onionRoutingPacket); |
| | 71 | | } |
| | 72 | |
|
| 16 | 73 | | return new UpdateAddHtlcPayload(amountMsat, channelId, cltvExpiry, id, paymentHash, onionRoutingPacket); |
| | 74 | | } |
| 0 | 75 | | catch (Exception e) |
| | 76 | | { |
| 0 | 77 | | throw new PayloadSerializationException($"Error deserializing {nameof(UpdateAddHtlcPayload)}", e); |
| | 78 | | } |
| | 79 | | finally |
| | 80 | | { |
| 16 | 81 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 82 | | } |
| 16 | 83 | | } |
| | 84 | |
|
| | 85 | | async Task<IMessagePayload?> IPayloadSerializer.DeserializeAsync(Stream stream) |
| | 86 | | { |
| 0 | 87 | | return await DeserializeAsync(stream); |
| 0 | 88 | | } |
| | 89 | | } |