| | 1 | | using System.Buffers; |
| | 2 | | using System.Runtime.Serialization; |
| | 3 | | using NLightning.Domain.Bitcoin.ValueObjects; |
| | 4 | | using NLightning.Domain.Interfaces; |
| | 5 | | using NLightning.Domain.Serialization.Interfaces; |
| | 6 | |
|
| | 7 | | namespace NLightning.Infrastructure.Serialization.ValueObjects; |
| | 8 | |
|
| | 9 | | using Converters; |
| | 10 | |
|
| | 11 | | public class WitnessTypeSerializer : IValueObjectTypeSerializer<Witness> |
| | 12 | | { |
| | 13 | | /// <summary> |
| | 14 | | /// Serializes a Witness value into a stream. |
| | 15 | | /// </summary> |
| | 16 | | /// <param name="valueObject">The Witness value to serialize.</param> |
| | 17 | | /// <param name="stream">The stream where the serialized value will be written.</param> |
| | 18 | | /// <returns>A task that represents the asynchronous serialization operation.</returns> |
| | 19 | | /// <exception cref="ArgumentNullException">Thrown when the stream is null.</exception> |
| | 20 | | /// <exception cref="IOException">Thrown when an I/O error occurs during the write operation.</exception> |
| | 21 | | public async Task SerializeAsync(IValueObject valueObject, Stream stream) |
| | 22 | | { |
| 16 | 23 | | if (valueObject is not Witness witness) |
| 0 | 24 | | throw new ArgumentException("Value object must be of type Witness.", nameof(valueObject)); |
| | 25 | |
|
| 16 | 26 | | await stream.WriteAsync(EndianBitConverter.GetBytesBigEndian(witness.Length)); |
| 16 | 27 | | await stream.WriteAsync(witness); |
| 16 | 28 | | } |
| | 29 | |
|
| | 30 | | /// <summary> |
| | 31 | | /// Deserializes a Witness value from a stream. |
| | 32 | | /// </summary> |
| | 33 | | /// <param name="stream">The stream from which the Witness value will be deserialized.</param> |
| | 34 | | /// <returns>A task that represents the asynchronous deserialization operation, containing the deserialized Witness |
| | 35 | | /// <exception cref="ArgumentException">Thrown when the stream is empty or contains insufficient data for deserializ |
| | 36 | | /// <exception cref="IOException">Thrown when an I/O error occurs during the read operation.</exception> |
| | 37 | | public async Task<Witness> DeserializeAsync(Stream stream) |
| | 38 | | { |
| 20 | 39 | | var buffer = ArrayPool<byte>.Shared.Rent(sizeof(ushort)); |
| | 40 | |
|
| | 41 | | try |
| | 42 | | { |
| 20 | 43 | | await stream.ReadExactlyAsync(buffer.AsMemory()[..sizeof(ushort)]); |
| 16 | 44 | | var len = EndianBitConverter.ToUInt16BigEndian(buffer[..sizeof(ushort)]); |
| | 45 | |
|
| | 46 | | // if (length > CryptoConstants.MAX_SIGNATURE_SIZE) |
| | 47 | | // throw new SerializationException( |
| | 48 | | // $"Witness length {length} exceeds maximum size of {CryptoConstants.MAX_SIGNATURE_SIZE} bytes."); |
| | 49 | |
|
| 16 | 50 | | var witnessBytes = new byte[len]; |
| 16 | 51 | | await stream.ReadExactlyAsync(witnessBytes); |
| | 52 | |
|
| 16 | 53 | | return new Witness(witnessBytes); |
| | 54 | | } |
| 4 | 55 | | catch (Exception e) |
| | 56 | | { |
| 4 | 57 | | throw new SerializationException("Error deserializing Witness", e); |
| | 58 | | } |
| | 59 | | finally |
| | 60 | | { |
| 20 | 61 | | ArrayPool<byte>.Shared.Return(buffer); |
| | 62 | | } |
| 16 | 63 | | } |
| | 64 | |
|
| | 65 | | async Task<IValueObject> IValueObjectTypeSerializer.DeserializeAsync(Stream stream) |
| | 66 | | { |
| 0 | 67 | | return await DeserializeAsync(stream); |
| 0 | 68 | | } |
| | 69 | | } |