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