| | | 1 | | using System.Buffers; |
| | | 2 | | using System.Runtime.Serialization; |
| | | 3 | | using NLightning.Domain.Bitcoin.Transactions.Constants; |
| | | 4 | | using NLightning.Domain.Bitcoin.ValueObjects; |
| | | 5 | | using NLightning.Domain.Channels.ValueObjects; |
| | | 6 | | using NLightning.Domain.Protocol.Interfaces; |
| | | 7 | | using NLightning.Domain.Serialization.Interfaces; |
| | | 8 | | |
| | | 9 | | namespace NLightning.Infrastructure.Serialization.Payloads; |
| | | 10 | | |
| | | 11 | | using Converters; |
| | | 12 | | using Domain.Protocol.Payloads; |
| | | 13 | | using Exceptions; |
| | | 14 | | |
| | | 15 | | public class TxSignaturesPayloadSerializer : IPayloadSerializer<TxSignaturesPayload> |
| | | 16 | | { |
| | | 17 | | private readonly IValueObjectSerializerFactory _valueObjectSerializerFactory; |
| | | 18 | | |
| | 4 | 19 | | public TxSignaturesPayloadSerializer(IValueObjectSerializerFactory valueObjectSerializerFactory) |
| | | 20 | | { |
| | 4 | 21 | | _valueObjectSerializerFactory = valueObjectSerializerFactory; |
| | 4 | 22 | | } |
| | | 23 | | |
| | | 24 | | public async Task SerializeAsync(IMessagePayload payload, Stream stream) |
| | | 25 | | { |
| | 4 | 26 | | if (payload is not TxSignaturesPayload txSignaturesPayload) |
| | 0 | 27 | | throw new SerializationException($"Payload is not of type {nameof(TxSignaturesPayload)}"); |
| | | 28 | | |
| | | 29 | | // Get the ChannelId serializer |
| | 4 | 30 | | var channelIdSerializer = |
| | 4 | 31 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| | 4 | 32 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| | 4 | 33 | | await channelIdSerializer.SerializeAsync(txSignaturesPayload.ChannelId, stream); |
| | | 34 | | |
| | 4 | 35 | | await stream.WriteAsync(txSignaturesPayload.TxId); |
| | 4 | 36 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian((ushort)txSignaturesPayload.Witnesses.Count)); |
| | | 37 | | |
| | | 38 | | // Get the Witness serializer |
| | 4 | 39 | | var witnessSerializer = |
| | 4 | 40 | | _valueObjectSerializerFactory.GetSerializer<Witness>() |
| | 4 | 41 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(Witness)}"); |
| | | 42 | | |
| | 16 | 43 | | foreach (var witness in txSignaturesPayload.Witnesses) |
| | | 44 | | { |
| | 4 | 45 | | await witnessSerializer.SerializeAsync(witness, stream); |
| | | 46 | | } |
| | 4 | 47 | | } |
| | | 48 | | |
| | | 49 | | public async Task<TxSignaturesPayload?> DeserializeAsync(Stream stream) |
| | | 50 | | { |
| | 4 | 51 | | var buffer = ArrayPool<byte>.Shared.Rent(sizeof(ushort)); |
| | | 52 | | |
| | | 53 | | try |
| | | 54 | | { |
| | | 55 | | // Get the ChannelId serializer |
| | 4 | 56 | | var channelIdSerializer = |
| | 4 | 57 | | _valueObjectSerializerFactory.GetSerializer<ChannelId>() |
| | 4 | 58 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(ChannelId)}"); |
| | 4 | 59 | | var channelId = await channelIdSerializer.DeserializeAsync(stream); |
| | | 60 | | |
| | 4 | 61 | | var txId = new byte[TransactionConstants.TxIdLength]; |
| | 4 | 62 | | await stream.ReadExactlyAsync(txId); |
| | | 63 | | |
| | 4 | 64 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ushort)]); |
| | 4 | 65 | | var numWitnesses = EndianBitConverter.ToUInt16BigEndian(buffer[..sizeof(ushort)]); |
| | | 66 | | |
| | | 67 | | // Get the Witness serializer |
| | 4 | 68 | | var witnessSerializer = |
| | 4 | 69 | | _valueObjectSerializerFactory.GetSerializer<Witness>() |
| | 4 | 70 | | ?? throw new SerializationException($"No serializer found for value object type {nameof(Witness)}"); |
| | | 71 | | |
| | 4 | 72 | | var witnesses = new List<Witness>(numWitnesses); |
| | 16 | 73 | | for (var i = 0; i < numWitnesses; i++) |
| | | 74 | | { |
| | 4 | 75 | | witnesses.Add(await witnessSerializer.DeserializeAsync(stream)); |
| | | 76 | | } |
| | | 77 | | |
| | 4 | 78 | | return new TxSignaturesPayload(channelId, txId, witnesses); |
| | | 79 | | } |
| | 0 | 80 | | catch (Exception e) |
| | | 81 | | { |
| | 0 | 82 | | throw new PayloadSerializationException($"Error deserializing {nameof(TxSignaturesPayload)}", e); |
| | | 83 | | } |
| | | 84 | | finally |
| | | 85 | | { |
| | 4 | 86 | | ArrayPool<byte>.Shared.Return(buffer); |
| | | 87 | | } |
| | 4 | 88 | | } |
| | | 89 | | |
| | | 90 | | async Task<IMessagePayload?> IPayloadSerializer.DeserializeAsync(Stream stream) |
| | | 91 | | { |
| | 0 | 92 | | return await DeserializeAsync(stream); |
| | 0 | 93 | | } |
| | | 94 | | } |