| | | 1 | | using System.Runtime.Serialization; |
| | | 2 | | using MessagePack; |
| | | 3 | | using MessagePack.Formatters; |
| | | 4 | | |
| | | 5 | | namespace NLightning.Transport.Ipc.MessagePack.Formatters; |
| | | 6 | | |
| | | 7 | | using Domain.Bitcoin.ValueObjects; |
| | | 8 | | |
| | | 9 | | public class SignedTransactionFormatter : IMessagePackFormatter<SignedTransaction?> |
| | | 10 | | { |
| | | 11 | | public void Serialize(ref MessagePackWriter writer, SignedTransaction? value, MessagePackSerializerOptions options) |
| | 0 | 12 | | { |
| | 0 | 13 | | writer.WriteArrayHeader(2); |
| | 0 | 14 | | writer.Write(value.TxId); |
| | 0 | 15 | | writer.Write(value.RawTxBytes); |
| | 0 | 16 | | } |
| | | 17 | | |
| | | 18 | | public SignedTransaction Deserialize(ref MessagePackReader reader, MessagePackSerializerOptions options) |
| | 0 | 19 | | { |
| | 0 | 20 | | if (reader.ReadArrayHeader() != 2) |
| | 0 | 21 | | throw new SerializationException($"Error deserializing {nameof(SignedTransaction)}"); |
| | | 22 | | |
| | 0 | 23 | | var txIdFormatter = options.Resolver.GetFormatterWithVerify<TxId>(); |
| | 0 | 24 | | var txId = txIdFormatter.Deserialize(ref reader, options); |
| | | 25 | | |
| | | 26 | | // Read RawTxBytes |
| | 0 | 27 | | var rawTxBytes = reader.ReadBytes()?.FirstSpan.ToArray() ?? |
| | 0 | 28 | | throw new SerializationException( |
| | 0 | 29 | | $"Error deserializing {nameof(SignedTransaction)}.{nameof(SignedTransaction.RawTxBytes)}"); |
| | | 30 | | |
| | 0 | 31 | | return new SignedTransaction(txId, rawTxBytes); |
| | 0 | 32 | | } |
| | | 33 | | } |