| | 1 | | using System.Buffers; |
| | 2 | | using System.Runtime.Serialization; |
| | 3 | | using NBitcoin.Crypto; |
| | 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.Enums; |
| | 11 | | using Domain.Money; |
| | 12 | | using Domain.Protocol.Payloads; |
| | 13 | | using Domain.Protocol.Payloads.Interfaces; |
| | 14 | | using Domain.Serialization.Factories; |
| | 15 | | using Domain.ValueObjects; |
| | 16 | | using Exceptions; |
| | 17 | |
|
| | 18 | | public class ClosingSignedPayloadSerializer : IPayloadSerializer<ClosingSignedPayload> |
| | 19 | | { |
| | 20 | | private readonly IValueObjectSerializerFactory _valueObjectSerializerFactory; |
| | 21 | |
|
| 4 | 22 | | public ClosingSignedPayloadSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | 23 | | { |
| 4 | 24 | | _valueObjectSerializerFactory = valueObjectSerializerFactory; |
| 4 | 25 | | } |
| | 26 | |
|
| | 27 | | public async Task SerializeAsync(IMessagePayload payload, Stream stream) |
| | 28 | | { |
| 4 | 29 | | if (payload is not ClosingSignedPayload closingSignedPayload) |
| 0 | 30 | | throw new SerializationException($"Payload is not of type {nameof(ClosingSignedPayload)}"); |
| | 31 | |
|
| | 32 | | // Get the value object serializer |
| 4 | 33 | | var channelIdSerializer = |
| 4 | 34 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 4 | 35 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 4 | 36 | | await channelIdSerializer.SerializeAsync(closingSignedPayload.ChannelId, stream); |
| | 37 | |
|
| | 38 | | // Serialize other types |
| 4 | 39 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(closingSignedPayload.FeeAmount.Satoshi)); |
| 4 | 40 | | await stream.WriteAsync(closingSignedPayload.Signature.ToCompact()); |
| 4 | 41 | | } |
| | 42 | |
|
| | 43 | | public async Task<ClosingSignedPayload?> DeserializeAsync(Stream stream) |
| | 44 | | { |
| 8 | 45 | | var buffer = ArrayPool<byte>.Shared.Rent(CryptoConstants.MAX_SIGNATURE_SIZE); |
| | 46 | |
|
| | 47 | | try |
| | 48 | | { |
| | 49 | | // Get the value object serializer |
| 8 | 50 | | var channelIdSerializer = |
| 8 | 51 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 8 | 52 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 8 | 53 | | var channelId = await channelIdSerializer.DeserializeAsync(stream); |
| | 54 | |
|
| 8 | 55 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ulong)]); |
| 8 | 56 | | var feeSatoshis = LightningMoney.FromUnit(EndianBitConverter.ToUInt64BigEndian(buffer[..sizeof(ulong)]), |
| 8 | 57 | | LightningMoneyUnit.Satoshi); |
| | 58 | |
|
| 8 | 59 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..CryptoConstants.MAX_SIGNATURE_SIZE]); |
| 8 | 60 | | if (!ECDSASignature.TryParseFromCompact(buffer[..CryptoConstants.MAX_SIGNATURE_SIZE], out var signature)) |
| | 61 | | { |
| 0 | 62 | | throw new Exception("Unable to parse signature"); |
| | 63 | | } |
| | 64 | |
|
| 8 | 65 | | return new ClosingSignedPayload(channelId, feeSatoshis, signature); |
| | 66 | | } |
| 0 | 67 | | catch (Exception e) |
| | 68 | | { |
| 0 | 69 | | throw new PayloadSerializationException("Error deserializing ClosingSignedPayload", e); |
| | 70 | | } |
| | 71 | | finally |
| | 72 | | { |
| 8 | 73 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 74 | | } |
| 8 | 75 | | } |
| | 76 | |
|
| | 77 | | async Task<IMessagePayload?> IPayloadSerializer.DeserializeAsync(Stream stream) |
| | 78 | | { |
| 0 | 79 | | return await DeserializeAsync(stream); |
| 0 | 80 | | } |
| | 81 | | } |