X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTxIn.cs;h=376f27f1b2bb311b653d1ca7bcf84c231d6e90dd;hb=161220f258cf9eb640d354951a48b45598301b1a;hp=a33606a6b95efbacde8242db1bf59434070dd76e;hpb=0279648337efe9bbe32b5204e247529243805484;p=NovacoinLibrary.git
diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs
index a33606a..376f27f 100644
--- a/Novacoin/CTxIn.cs
+++ b/Novacoin/CTxIn.cs
@@ -19,13 +19,32 @@
using System;
using System.Text;
using System.Collections.Generic;
+using System.IO;
namespace Novacoin
{
- ///
- /// Transaction input.
- ///
- public class CTxIn
+ [Serializable]
+ public class TxInConstructorException : Exception
+ {
+ public TxInConstructorException()
+ {
+ }
+
+ public TxInConstructorException(string message)
+ : base(message)
+ {
+ }
+
+ public TxInConstructorException(string message, Exception inner)
+ : base(message, inner)
+ {
+ }
+ }
+
+ ///
+ /// Transaction input.
+ ///
+ public class CTxIn
{
///
/// Previous input data
@@ -65,25 +84,46 @@ namespace Novacoin
///
/// Read vin list from byte sequence.
///
- /// Reference to byte sequence
+ /// Reference to binary reader
/// Inputs array
- public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
+ internal static CTxIn[] ReadTxInList(ref BinaryReader reader)
{
- // Get amount
- int nInputs = (int)wBytes.GetVarInt();
- var vin = new CTxIn[nInputs];
-
- for (int nIndex = 0; nIndex < nInputs; nIndex++)
+ try
{
- // Fill inputs array
- vin[nIndex] = new CTxIn();
- vin[nIndex].prevout = new COutPoint(wBytes.Get(36));
- vin[nIndex].scriptSig = new CScript(wBytes.Get((int)wBytes.GetVarInt()));
- vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ // Get amount
+ int nInputs = (int)VarInt.ReadVarInt(ref reader);
+ var vin = new CTxIn[nInputs];
+
+ for (int nIndex = 0; nIndex < nInputs; nIndex++)
+ {
+ // Fill inputs array
+ vin[nIndex] = new CTxIn();
+ vin[nIndex].prevout = new COutPoint(reader.ReadBytes(36));
+ vin[nIndex].scriptSig = new CScript(reader.ReadBytes((int)VarInt.ReadVarInt(ref reader)));
+ vin[nIndex].nSequence = reader.ReadUInt32();
+ }
+
+ // Return inputs array
+ return vin;
}
+ catch (Exception e)
+ {
+ throw new TxInConstructorException("Desirealization failed.", e);
+ }
+ }
+
+ ///
+ /// Serialized size
+ ///
+ public int Size
+ {
+ get {
+ int nSize = 40; // COutPoint, nSequence
+ nSize += VarInt.GetEncodedSize(scriptSig.Size);
+ nSize += scriptSig.Size;
- // Return inputs array
- return vin;
+ return nSize;
+ }
}
///
@@ -92,19 +132,19 @@ namespace Novacoin
/// Byte sequence.
public static implicit operator byte[] (CTxIn input)
{
- var inputBytes = new List();
-
- inputBytes.AddRange((byte[])input.prevout); // prevout
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
- var s = (byte[])input.scriptSig;
- inputBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptSig length
- inputBytes.AddRange(s); // scriptSig
- inputBytes.AddRange(BitConverter.GetBytes(input.nSequence)); // Sequence
+ writer.Write(input.prevout); // prevout
+ writer.Write(VarInt.EncodeVarInt(input.scriptSig.Size)); // scriptSig length
+ writer.Write(input.scriptSig); // scriptSig
+ writer.Write(input.nSequence); // nSequence
- return inputBytes.ToArray();
+ var inputBytes = stream.ToArray();
+ writer.Close();
+ return inputBytes;
}
-
public bool IsFinal
{
get { return (nSequence == uint.MaxValue); }
@@ -138,4 +178,3 @@ namespace Novacoin
}
}
-