| | 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.Protocol.Payloads; |
| | 13 | | using Exceptions; |
| | 14 | |
|
| | 15 | | public class ChannelReestablishPayloadSerializer : IPayloadSerializer<ChannelReestablishPayload> |
| | 16 | | { |
| | 17 | | private readonly IValueObjectSerializerFactory _valueObjectSerializerFactory; |
| | 18 | |
|
| 4 | 19 | | public ChannelReestablishPayloadSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | 20 | | { |
| 4 | 21 | | _valueObjectSerializerFactory = valueObjectSerializerFactory; |
| 4 | 22 | | } |
| | 23 | |
|
| | 24 | | public async Task SerializeAsync(IMessagePayload payload, Stream stream) |
| | 25 | | { |
| 8 | 26 | | if (payload is not ChannelReestablishPayload channelReestablishPayload) |
| 0 | 27 | | throw new SerializationException($"Payload is not of type {nameof(ChannelReestablishPayload)}"); |
| | 28 | |
|
| | 29 | | // Get the value object serializer |
| 8 | 30 | | var channelIdSerializer = |
| 8 | 31 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 8 | 32 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 8 | 33 | | await channelIdSerializer.SerializeAsync(channelReestablishPayload.ChannelId, stream); |
| | 34 | |
|
| | 35 | | // Serialize other types |
| 8 | 36 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(channelReestablishPayload.NextCommitmentNumber)); |
| 8 | 37 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(channelReestablishPayload.NextRevocationNumber)); |
| 8 | 38 | | await stream.WriteAsync(channelReestablishPayload.YourLastPerCommitmentSecret); |
| 8 | 39 | | await stream.WriteAsync(channelReestablishPayload.MyCurrentPerCommitmentPoint); |
| 8 | 40 | | } |
| | 41 | |
|
| | 42 | | public async Task<ChannelReestablishPayload?> DeserializeAsync(Stream stream) |
| | 43 | | { |
| 12 | 44 | | var buffer = ArrayPool<byte>.Shared.Rent(CryptoConstants.CompactPubkeyLen); |
| | 45 | |
|
| | 46 | | try |
| | 47 | | { |
| | 48 | | // Get the value object serializer |
| 12 | 49 | | var channelIdSerializer = |
| 12 | 50 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 12 | 51 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 12 | 52 | | var channelId = await channelIdSerializer.DeserializeAsync(stream); |
| | 53 | |
|
| 12 | 54 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]); |
| 12 | 55 | | var nextCommitmentNumber = EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]); |
| | 56 | |
|
| 12 | 57 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]); |
| 12 | 58 | | var nextRevocationNumber = EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]); |
| | 59 | |
|
| 12 | 60 | | var yourLastPerCommitmentSecret = new byte[32]; |
| 12 | 61 | | await stream.ReadExactlyAsync(yourLastPerCommitmentSecret); |
| | 62 | |
|
| 12 | 63 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..CryptoConstants.CompactPubkeyLen]); |
| 12 | 64 | | var myCurrentPerCommitmentPoint = new CompactPubKey(buffer[..CryptoConstants.CompactPubkeyLen]); |
| | 65 | |
|
| 12 | 66 | | return new ChannelReestablishPayload(channelId, myCurrentPerCommitmentPoint, nextCommitmentNumber, |
| 12 | 67 | | nextRevocationNumber, yourLastPerCommitmentSecret); |
| | 68 | | } |
| 0 | 69 | | catch (Exception e) |
| | 70 | | { |
| 0 | 71 | | throw new PayloadSerializationException("Error deserializing ChannelReestablishPayload", e); |
| | 72 | | } |
| | 73 | | finally |
| | 74 | | { |
| 12 | 75 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 76 | | } |
| 12 | 77 | | } |
| | 78 | |
|
| | 79 | | async Task<IMessagePayload?> IPayloadSerializer.DeserializeAsync(Stream stream) |
| | 80 | | { |
| 0 | 81 | | return await DeserializeAsync(stream); |
| 0 | 82 | | } |
| | 83 | | } |