X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTxIn.cs;h=376f27f1b2bb311b653d1ca7bcf84c231d6e90dd;hb=161220f258cf9eb640d354951a48b45598301b1a;hp=fad0d7e75b93de57d7944eb8cfcca593ed0da2ef;hpb=9f4f9d3f60f89dcf29fb7b24d60ffd40d7360a7d;p=NovacoinLibrary.git
diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs
index fad0d7e..376f27f 100644
--- a/Novacoin/CTxIn.cs
+++ b/Novacoin/CTxIn.cs
@@ -1,13 +1,50 @@
-using System;
+/**
+ * Novacoin classes library
+ * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Affero General Public License for more details.
+
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program. If not, see .
+ */
+
+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
@@ -47,49 +84,65 @@ namespace Novacoin
///
/// Read vin list from byte sequence.
///
- /// Reference to byte sequence
+ /// Reference to binary reader
/// Inputs array
- public static CTxIn[] ReadTxInList(ref WrappedList wBytes)
+ internal static CTxIn[] ReadTxInList(ref BinaryReader reader)
{
- CTxIn[] vin;
-
- // Get amount
- int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
- vin = new CTxIn[nInputs];
-
- for (int nIndex = 0; nIndex < nInputs; nIndex++)
+ try
+ {
+ // 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)
{
- // Fill inputs array
- vin[nIndex] = new CTxIn();
- vin[nIndex].prevout = new COutPoint(wBytes.GetItems(36));
- vin[nIndex].scriptSig = new CScript(wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)));
- vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
+ 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;
+ }
}
///
/// Get raw bytes representation of our input.
///
/// Byte sequence.
- public IList Bytes
+ public static implicit operator byte[] (CTxIn input)
{
- get
- {
- List inputBytes = new List();
-
- inputBytes.AddRange(prevout.Bytes); // prevout
+ var stream = new MemoryStream();
+ var writer = new BinaryWriter(stream);
- List s = new List(scriptSig.Bytes);
+ writer.Write(input.prevout); // prevout
+ writer.Write(VarInt.EncodeVarInt(input.scriptSig.Size)); // scriptSig length
+ writer.Write(input.scriptSig); // scriptSig
+ writer.Write(input.nSequence); // nSequence
- inputBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptSig length
- inputBytes.AddRange(s); // scriptSig
- inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
-
- return inputBytes;
- }
+ var inputBytes = stream.ToArray();
+ writer.Close();
+ return inputBytes;
}
public bool IsFinal
@@ -105,7 +158,7 @@ namespace Novacoin
if(prevout.IsNull)
{
- sb.AppendFormat(", coinbase={0}", Interop.ToHex(scriptSig.Bytes));
+ sb.AppendFormat(", coinbase={0}", Interop.ToHex((byte[])scriptSig));
}
else
{
@@ -125,4 +178,3 @@ namespace Novacoin
}
}
-