Use byte[] instead of IEnumerable<byte> if possible
[NovacoinLibrary.git] / Novacoin / CTxIn.cs
1 \feff/**
2  *  Novacoin classes library
3  *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
4
5  *  This program is free software: you can redistribute it and/or modify
6  *  it under the terms of the GNU Affero General Public License as
7  *  published by the Free Software Foundation, either version 3 of the
8  *  License, or (at your option) any later version.
9
10  *  This program is distributed in the hope that it will be useful,
11  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  *  GNU Affero General Public License for more details.
14
15  *  You should have received a copy of the GNU Affero General Public License
16  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using System;
20 using System.Text;
21 using System.Collections.Generic;
22
23 namespace Novacoin
24 {
25         /// <summary>
26         /// Transaction input.
27         /// </summary>
28         public class CTxIn
29         {
30         /// <summary>
31         /// Previous input data
32         /// </summary>
33         public COutPoint prevout;
34
35                 /// <summary>
36                 /// First half of script, signatures for the scriptPubKey
37                 /// </summary>
38         public CScript scriptSig;
39
40                 /// <summary>
41                 /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
42                 /// </summary>
43         public uint nSequence = uint.MaxValue;
44
45         /// <summary>
46         /// Initialize new CTxIn instance as copy of another one.
47         /// </summary>
48         /// <param name="i">CTxIn instance.</param>
49         public CTxIn(CTxIn i)
50         {
51             prevout = new COutPoint(i.prevout);
52             scriptSig = i.scriptSig;
53             nSequence = i.nSequence;
54         }
55
56         /// <summary>
57         /// Initialize an empty instance of CTxIn class
58         /// </summary>
59         public CTxIn()
60         {
61             prevout = new COutPoint();
62             scriptSig = new CScript();
63         }
64
65         /// <summary>
66         /// Read vin list from byte sequence.
67         /// </summary>
68         /// <param name="wBytes">Reference to byte sequence</param>
69         /// <returns>Inputs array</returns>
70         public static CTxIn[] ReadTxInList(ref ByteQueue wBytes)
71         {
72             // Get amount
73             int nInputs = (int)wBytes.GetVarInt();
74             var vin = new CTxIn[nInputs];
75
76             for (int nIndex = 0; nIndex < nInputs; nIndex++)
77             {
78                 // Fill inputs array
79                 vin[nIndex] = new CTxIn();
80                 vin[nIndex].prevout = new COutPoint(wBytes.Get(36));
81                 vin[nIndex].scriptSig = new CScript(wBytes.Get((int)wBytes.GetVarInt()));
82                 vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
83             }
84
85             // Return inputs array
86             return vin;
87         }
88
89         /// <summary>
90         /// Get raw bytes representation of our input.
91         /// </summary>
92         /// <returns>Byte sequence.</returns>
93         public IList<byte> Bytes
94         {
95             get
96             {
97                 var inputBytes = new List<byte>();
98
99                 inputBytes.AddRange(prevout.Bytes); // prevout
100
101                 var s = scriptSig.Bytes;
102                 inputBytes.AddRange(VarInt.EncodeVarInt(s.Length)); // scriptSig length
103                 inputBytes.AddRange(s); // scriptSig
104                 inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
105
106                 return inputBytes;
107             }
108         }
109
110         public bool IsFinal
111         {
112             get { return (nSequence == uint.MaxValue); }
113         }
114         public override string ToString ()
115                 {
116                         StringBuilder sb = new StringBuilder ();
117
118             sb.AppendFormat("CTxIn(");
119             sb.Append(prevout.ToString());
120
121             if(prevout.IsNull)
122             {
123                 sb.AppendFormat(", coinbase={0}", Interop.ToHex(scriptSig.Bytes));
124             }
125             else
126             {
127                 sb.AppendFormat(", scriptsig={0}", scriptSig.ToString());
128             }
129
130             if (nSequence != uint.MaxValue)
131             {
132                 sb.AppendFormat(", nSequence={0}", nSequence);
133             }
134
135             sb.Append(")");
136
137
138             return sb.ToString ();
139                 }
140
141         }
142 }
143