Implement ReadTxInList and ReadTxoutList as static methods
[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                 /// Hash of parent transaction.
14                 /// </summary>
15                 public Hash256 txID = new Hash256();
16
17                 /// <summary>
18                 /// Parent input number.
19                 /// </summary>
20         public uint n = 0;
21
22                 /// <summary>
23                 /// First half of script, signatures for the scriptPubKey
24                 /// </summary>
25         public byte[] scriptSig;
26
27                 /// <summary>
28                 /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
29                 /// </summary>
30         public uint nSequence = 0xffffffff;
31
32         /// <summary>
33         /// Initialize new CTxIn instance as copy of another one.
34         /// </summary>
35         /// <param name="i">CTxIn instance.</param>
36         public CTxIn(CTxIn i)
37         {
38             txID = i.txID;
39             n = i.n;
40             scriptSig = i.scriptSig;
41             nSequence = i.nSequence;
42         }
43
44         /// <summary>
45         /// Initialize an empty instance of CTxIn class
46         /// </summary>
47         public CTxIn()
48         {
49         }
50
51         public static CTxIn[] ReadTxInList(ref WrappedList<byte> wBytes)
52         {
53             CTxIn[] vin;
54
55             // Get amount
56             int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
57             vin = new CTxIn[nInputs];
58
59             for (int nIndex = 0; nIndex < nInputs; nIndex++)
60             {
61                 // Fill inputs array
62                 vin[nIndex] = new CTxIn();
63
64                 vin[nIndex].txID = new Hash256(wBytes.GetItems(32));
65                 vin[nIndex].n = Interop.LEBytesToUInt32(wBytes.GetItems(4));
66                 vin[nIndex].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
67                 vin[nIndex].nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4));
68             }
69
70             // Return inputs array
71             return vin;
72         }
73
74         /// <summary>
75         /// Get raw bytes representation of our input.
76         /// </summary>
77         /// <returns>Byte sequence.</returns>
78         public IList<byte> ToBytes()
79         {
80             List<byte> inputBytes = new List<byte>();
81
82             inputBytes.AddRange(txID.hashBytes); // Input transaction id
83             inputBytes.AddRange(Interop.LEBytes(n)); // Output number
84             inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // scriptSig length
85             inputBytes.AddRange(scriptSig); // scriptSig
86             inputBytes.AddRange(Interop.LEBytes(nSequence)); // Sequence
87
88             return inputBytes;
89         }
90
91         public bool IsCoinBase()
92         {
93             return txID.IsZero();
94         }
95
96                 public override string ToString ()
97                 {
98                         StringBuilder sb = new StringBuilder ();
99
100             if (IsCoinBase())
101             {
102                 sb.AppendFormat("CTxIn(txId={0},coinbase={2},nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig), nSequence);
103             }
104             else
105             {
106                 sb.AppendFormat("CTxIn(txId={0},n={1},scriptSig={2},nSequence={3})", txID.ToString(), n, (new CScript(scriptSig)).ToString(), nSequence);
107             }
108
109                         return sb.ToString ();
110                 }
111
112         }
113 }
114