Use CScript for vin and vout,
[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 CScript 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         /// <summary>
52         /// Read vin list from byte sequence.
53         /// </summary>
54         /// <param name="wBytes">Reference to byte sequence</param>
55         /// <returns>Inputs array</returns>
56         public static CTxIn[] ReadTxInList(ref WrappedList<byte> wBytes)
57         {
58             CTxIn[] vin;
59
60             // Get amount
61             int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
62             vin = new CTxIn[nInputs];
63
64             for (int nIndex = 0; nIndex < nInputs; nIndex++)
65             {
66                 // Fill inputs array
67                 vin[nIndex] = new CTxIn();
68
69                 vin[nIndex].txID = new Hash256(wBytes.GetItems(32));
70                 vin[nIndex].n = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
71                 vin[nIndex].scriptSig = new CScript(wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)));
72                 vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
73             }
74
75             // Return inputs array
76             return vin;
77         }
78
79         /// <summary>
80         /// Get raw bytes representation of our input.
81         /// </summary>
82         /// <returns>Byte sequence.</returns>
83         public IList<byte> Bytes
84         {
85             get
86             {
87                 List<byte> inputBytes = new List<byte>();
88
89                 inputBytes.AddRange(txID.hashBytes); // Input transaction id
90                 inputBytes.AddRange(BitConverter.GetBytes(n)); // Output number
91
92                 List<byte> s = new List<byte>(scriptSig.Bytes);
93
94                 inputBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptSig length
95                 inputBytes.AddRange(s); // scriptSig
96                 inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
97
98                 return inputBytes;
99             }
100         }
101
102         public bool IsCoinBase
103         {
104             get { return txID.IsZero; }
105         }
106
107                 public override string ToString ()
108                 {
109                         StringBuilder sb = new StringBuilder ();
110
111             if (IsCoinBase)
112             {
113                 sb.AppendFormat("CTxIn(txId={0}, coinbase={2}, nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig.Bytes), nSequence);
114             }
115             else
116             {
117                 sb.AppendFormat("CTxIn(txId={0}, n={1}, scriptSig={2}, nSequence={3})", txID.ToString(), n, scriptSig.ToString(), nSequence);
118             }
119
120                         return sb.ToString ();
121                 }
122
123         }
124 }
125