| | 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.Protocol.Payloads; |
| | 11 | | using Domain.Protocol.Payloads.Interfaces; |
| | 12 | | using Domain.Serialization.Factories; |
| | 13 | | using Domain.ValueObjects; |
| | 14 | | using Exceptions; |
| | 15 | |
|
| | 16 | | public class CommitmentSignedPayloadSerializer : IPayloadSerializer<CommitmentSignedPayload> |
| | 17 | | { |
| | 18 | | private readonly IValueObjectSerializerFactory _valueObjectSerializerFactory; |
| | 19 | |
|
| 4 | 20 | | public CommitmentSignedPayloadSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | 21 | | { |
| 4 | 22 | | _valueObjectSerializerFactory = valueObjectSerializerFactory; |
| 4 | 23 | | } |
| | 24 | |
|
| | 25 | | public async Task SerializeAsync(IMessagePayload payload, Stream stream) |
| | 26 | | { |
| 4 | 27 | | if (payload is not CommitmentSignedPayload commitmentSignedPayload) |
| 0 | 28 | | throw new SerializationException($"Payload is not of type {nameof(CommitmentSignedPayload)}"); |
| | 29 | |
|
| | 30 | | // Get the value object serializer |
| 4 | 31 | | var channelIdSerializer = |
| 4 | 32 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 4 | 33 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 4 | 34 | | await channelIdSerializer.SerializeAsync(commitmentSignedPayload.ChannelId, stream); |
| | 35 | |
|
| | 36 | | // Serialize other types |
| 4 | 37 | | await stream.WriteAsync(commitmentSignedPayload.Signature.ToCompact()); |
| 4 | 38 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(commitmentSignedPayload.NumHtlcs)); |
| 24 | 39 | | foreach (var htlcsSignature in commitmentSignedPayload.HtlcSignatures) |
| | 40 | | { |
| 8 | 41 | | await stream.WriteAsync(htlcsSignature.ToCompact()); |
| | 42 | | } |
| 4 | 43 | | } |
| | 44 | |
|
| | 45 | | public async Task<CommitmentSignedPayload?> DeserializeAsync(Stream stream) |
| | 46 | | { |
| 4 | 47 | | var buffer = ArrayPool<byte>.Shared.Rent(CryptoConstants.MAX_SIGNATURE_SIZE); |
| | 48 | |
|
| | 49 | | try |
| | 50 | | { |
| | 51 | | // Get the value object serializer |
| 4 | 52 | | var channelIdSerializer = |
| 4 | 53 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| 4 | 54 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| 4 | 55 | | var channelId = await channelIdSerializer.DeserializeAsync(stream); |
| | 56 | |
|
| 4 | 57 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..CryptoConstants.MAX_SIGNATURE_SIZE]); |
| 4 | 58 | | if (!ECDSASignature.TryParseFromCompact(buffer[..CryptoConstants.MAX_SIGNATURE_SIZE], out var signature)) |
| | 59 | | { |
| 0 | 60 | | throw new Exception("Unable to parse signature"); |
| | 61 | | } |
| | 62 | |
|
| 4 | 63 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ushort)]); |
| 4 | 64 | | var numHtlcs = EndianBitConverter.ToUInt16BigEndian(buffer[..sizeof(ushort)]); |
| | 65 | |
|
| 4 | 66 | | var htlcSignatures = new List<ECDSASignature>(numHtlcs); |
| 24 | 67 | | for (var i = 0; i < numHtlcs; i++) |
| | 68 | | { |
| 8 | 69 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..CryptoConstants.MAX_SIGNATURE_SIZE]); |
| 8 | 70 | | if (!ECDSASignature.TryParseFromCompact(buffer[..CryptoConstants.MAX_SIGNATURE_SIZE], |
| 8 | 71 | | out var htlcSignature)) |
| | 72 | | { |
| 0 | 73 | | throw new Exception("Unable to parse htcl signature"); |
| | 74 | | } |
| | 75 | |
|
| 8 | 76 | | htlcSignatures.Add(htlcSignature); |
| | 77 | | } |
| | 78 | |
|
| 4 | 79 | | return new CommitmentSignedPayload(channelId, htlcSignatures, signature); |
| | 80 | | } |
| 0 | 81 | | catch (Exception e) |
| | 82 | | { |
| 0 | 83 | | throw new PayloadSerializationException("Error deserializing CommitmentSignedPayload", e); |
| | 84 | | } |
| | 85 | | finally |
| | 86 | | { |
| 4 | 87 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 88 | | } |
| 4 | 89 | | } |
| | 90 | |
|
| | 91 | | async Task<IMessagePayload?> IPayloadSerializer.DeserializeAsync(Stream stream) |
| | 92 | | { |
| 0 | 93 | | return await DeserializeAsync(stream); |
| 0 | 94 | | } |
| | 95 | | } |