New constructor for block header class
[NovacoinLibrary.git] / Novacoin / CTxIn.cs
1 \feffusing System;
2 using System.Text;
3 using System.Collections.Generic;
4
5 namespace Novacoin
6 {
7         /// <summary>
8         /// Transaction input.
9         /// </summary>
10         public class CTxIn
11         {
12         /// <summary>
13         /// Previous input data
14         /// </summary>
15         public COutPoint prevout;
16
17                 /// <summary>
18                 /// First half of script, signatures for the scriptPubKey
19                 /// </summary>
20         public CScript scriptSig;
21
22                 /// <summary>
23                 /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
24                 /// </summary>
25         public uint nSequence = uint.MaxValue;
26
27         /// <summary>
28         /// Initialize new CTxIn instance as copy of another one.
29         /// </summary>
30         /// <param name="i">CTxIn instance.</param>
31         public CTxIn(CTxIn i)
32         {
33             prevout = new COutPoint(i.prevout);
34             scriptSig = i.scriptSig;
35             nSequence = i.nSequence;
36         }
37
38         /// <summary>
39         /// Initialize an empty instance of CTxIn class
40         /// </summary>
41         public CTxIn()
42         {
43             prevout = new COutPoint();
44             scriptSig = new CScript();
45         }
46
47         /// <summary>
48         /// Read vin list from byte sequence.
49         /// </summary>
50         /// <param name="wBytes">Reference to byte sequence</param>
51         /// <returns>Inputs array</returns>
52         public static CTxIn[] ReadTxInList(ref WrappedList<byte> wBytes)
53         {
54             CTxIn[] vin;
55
56             // Get amount
57             int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
58             vin = new CTxIn[nInputs];
59
60             for (int nIndex = 0; nIndex < nInputs; nIndex++)
61             {
62                 // Fill inputs array
63                 vin[nIndex] = new CTxIn();
64                 vin[nIndex].prevout = new COutPoint(wBytes.GetItems(36));
65                 vin[nIndex].scriptSig = new CScript(wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)));
66                 vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
67             }
68
69             // Return inputs array
70             return vin;
71         }
72
73         /// <summary>
74         /// Get raw bytes representation of our input.
75         /// </summary>
76         /// <returns>Byte sequence.</returns>
77         public IList<byte> Bytes
78         {
79             get
80             {
81                 List<byte> inputBytes = new List<byte>();
82
83                 inputBytes.AddRange(prevout.Bytes); // prevout
84
85                 List<byte> s = new List<byte>(scriptSig.Bytes);
86
87                 inputBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptSig length
88                 inputBytes.AddRange(s); // scriptSig
89                 inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
90
91                 return inputBytes;
92             }
93         }
94
95         public bool IsFinal
96         {
97             get { return (nSequence == uint.MaxValue); }
98         }
99         public override string ToString ()
100                 {
101                         StringBuilder sb = new StringBuilder ();
102
103             sb.AppendFormat("CTxIn(");
104             sb.Append(prevout.ToString());
105
106             if(prevout.IsNull)
107             {
108                 sb.AppendFormat(", coinbase={0}", Interop.ToHex(scriptSig.Bytes));
109             }
110             else
111             {
112                 sb.AppendFormat(", scriptsig={0}", scriptSig.ToString());
113             }
114
115             if (nSequence != uint.MaxValue)
116             {
117                 sb.AppendFormat(", nSequence={0}", nSequence);
118             }
119
120             sb.Append(")");
121
122
123             return sb.ToString ();
124                 }
125
126         }
127 }
128