X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTxIn.cs;h=44c21ad59b796741432a49713e7fd336645092b0;hb=f1d2e217ae6e24a84edfa98ca3471a8a836ecaa7;hp=52c355b8dc30359a3d64ccc58f01493ca2e0448e;hpb=cc948a567459f7df008429c99671077e0f953e22;p=NovacoinLibrary.git
diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs
index 52c355b..44c21ad 100644
--- a/Novacoin/CTxIn.cs
+++ b/Novacoin/CTxIn.cs
@@ -1,33 +1,64 @@
-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;
namespace Novacoin
{
- ///
- /// Transaction input.
- ///
- public class CTxIn
- {
- ///
- /// Hash of parent transaction.
- ///
- public Hash256 txID = new Hash256();
+ [Serializable]
+ public class TxInConstructorException : Exception
+ {
+ public TxInConstructorException()
+ {
+ }
- ///
- /// Parent input number.
- ///
- public uint n = 0;
+ public TxInConstructorException(string message)
+ : base(message)
+ {
+ }
+
+ public TxInConstructorException(string message, Exception inner)
+ : base(message, inner)
+ {
+ }
+ }
+
+ ///
+ /// Transaction input.
+ ///
+ public class CTxIn
+ {
+ ///
+ /// Previous input data
+ ///
+ public COutPoint prevout;
///
/// First half of script, signatures for the scriptPubKey
///
- public byte[] scriptSig;
+ public CScript scriptSig;
///
/// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
///
- public uint nSequence = 0xffffffff;
+ public uint nSequence = uint.MaxValue;
///
/// Initialize new CTxIn instance as copy of another one.
@@ -35,8 +66,7 @@ namespace Novacoin
/// CTxIn instance.
public CTxIn(CTxIn i)
{
- txID = i.txID;
- n = i.n;
+ prevout = new COutPoint(i.prevout);
scriptSig = i.scriptSig;
nSequence = i.nSequence;
}
@@ -46,69 +76,104 @@ namespace Novacoin
///
public CTxIn()
{
+ prevout = new COutPoint();
+ scriptSig = new CScript();
}
- public static CTxIn[] ReadTxInList(ref WrappedList wBytes)
+ ///
+ /// Read vin list from byte sequence.
+ ///
+ /// Reference to byte sequence
+ /// Inputs array
+ public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
{
- CTxIn[] vin;
-
- // Get amount
- int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
- vin = new CTxIn[nInputs];
-
- for (int nIndex = 0; nIndex < nInputs; nIndex++)
+ try
{
- // Fill inputs array
- vin[nIndex] = new CTxIn();
-
- vin[nIndex].txID = new Hash256(wBytes.GetItems(32));
- vin[nIndex].n = Interop.LEBytesToUInt32(wBytes.GetItems(4));
- vin[nIndex].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
- vin[nIndex].nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+ // Get amount
+ int nInputs = (int)wBytes.GetVarInt();
+ var vin = new CTxIn[nInputs];
+
+ for (int nIndex = 0; nIndex < nInputs; nIndex++)
+ {
+ // 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);
+ }
+
+ // Return inputs array
+ return vin;
}
+ catch (Exception e)
+ {
+ throw new TxInConstructorException("Desirealization failed.", e);
+ }
+ }
- // Return inputs array
- return vin;
+ ///
+ /// Serialized size
+ ///
+ public int Size
+ {
+ get {
+ int nSize = 40; // COutPoint, nSequence
+ nSize += VarInt.GetEncodedSize(scriptSig.Size);
+ nSize += scriptSig.Size;
+
+ return nSize;
+ }
}
///
/// Get raw bytes representation of our input.
///
/// Byte sequence.
- public IList ToBytes()
+ public static implicit operator byte[] (CTxIn input)
{
- List inputBytes = new List();
+ var inputBytes = new List();
- inputBytes.AddRange(txID.hashBytes); // Input transaction id
- inputBytes.AddRange(Interop.LEBytes(n)); // Output number
- inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // scriptSig length
- inputBytes.AddRange(scriptSig); // scriptSig
- inputBytes.AddRange(Interop.LEBytes(nSequence)); // Sequence
+ inputBytes.AddRange((byte[])input.prevout); // prevout
- return inputBytes;
+ var s = (byte[])input.scriptSig;
+ inputBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptSig length
+ inputBytes.AddRange(s); // scriptSig
+ inputBytes.AddRange(BitConverter.GetBytes(input.nSequence)); // Sequence
+
+ return inputBytes.ToArray();
}
- public bool IsCoinBase()
+
+ public bool IsFinal
{
- return txID.IsZero();
+ get { return (nSequence == uint.MaxValue); }
}
-
- public override string ToString ()
+ public override string ToString ()
{
StringBuilder sb = new StringBuilder ();
- if (IsCoinBase())
+ sb.AppendFormat("CTxIn(");
+ sb.Append(prevout.ToString());
+
+ if(prevout.IsNull)
{
- sb.AppendFormat("CTxIn(txId={0},coinbase={2},nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig), nSequence);
+ sb.AppendFormat(", coinbase={0}", Interop.ToHex((byte[])scriptSig));
}
else
{
- sb.AppendFormat("CTxIn(txId={0},n={1},scriptSig={2},nSequence={3})", txID.ToString(), n, (new CScript(scriptSig)).ToString(), nSequence);
+ sb.AppendFormat(", scriptsig={0}", scriptSig.ToString());
+ }
+
+ if (nSequence != uint.MaxValue)
+ {
+ sb.AppendFormat(", nSequence={0}", nSequence);
}
- return sb.ToString ();
+ sb.Append(")");
+
+
+ return sb.ToString ();
}
}
}
-