13ce0c773c144acd4d573c8e9151c63e6696c07b
[NovacoinLibrary.git] / Novacoin / CTransaction.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     /// Represents the transaction. Any transaction must provide one input and one output at least.
27     /// </summary>
28     public class CTransaction
29     {
30         /// <summary>
31         /// Version of transaction schema.
32         /// </summary>
33         public uint nVersion = 1;
34
35         /// <summary>
36         /// Transaction timestamp.
37         /// </summary>
38         public uint nTime = 0;
39
40         /// <summary>
41         /// Array of transaction inputs
42         /// </summary>
43         public CTxIn[] vin;
44
45         /// <summary>
46         /// Array of transaction outputs
47         /// </summary>
48         public CTxOut[] vout;
49
50         /// <summary>
51         /// Block height or timestamp when transaction is final
52         /// </summary>
53         public uint nLockTime = 0;
54
55         /// <summary>
56         /// Initialize an empty instance
57         /// </summary>
58         public CTransaction()
59         {
60             // Initialize empty input and output arrays. Please note that such 
61             // configuration is not valid for real transaction, you have to supply 
62             // at least one input and one output.
63             vin = new CTxIn[0];
64             vout = new CTxOut[0];
65         }
66
67         /// <summary>
68         /// Initialize new instance as a copy of another transaction
69         /// </summary>
70         /// <param name="tx">Transaction to copy from</param>
71         public CTransaction(CTransaction tx)
72         {
73             nVersion = tx.nVersion;
74             nTime = tx.nTime;
75
76             vin = new CTxIn[tx.vin.Length];
77
78             for (int i = 0; i < vin.Length; i++)
79             {
80                 vin[i] = new CTxIn(tx.vin[i]);
81             }
82
83             vout = new CTxOut[tx.vout.Length];
84
85             for (int i = 0; i < vout.Length; i++)
86             {
87                 vout[i] = new CTxOut(tx.vout[i]);
88             }
89
90             nLockTime = tx.nLockTime;
91         }
92
93
94         /// <summary>
95         /// Parse byte sequence and initialize new instance of CTransaction
96         /// </summary>
97         /// <param name="txBytes">Byte sequence</param>
98                 public CTransaction(byte[] txBytes)
99         {
100             var wBytes = new ByteQueue(txBytes);
101
102             nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
103             nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
104
105             int nInputs = (int)(int)wBytes.GetVarInt();
106             vin = new CTxIn[nInputs];
107
108             for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
109             {
110                 // Fill inputs array
111                 vin[nCurrentInput] = new CTxIn();
112                 
113                 vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
114
115                 int nScriptSigLen = (int)wBytes.GetVarInt();
116                 vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
117
118                 vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
119             }
120
121             int nOutputs = (int)wBytes.GetVarInt();
122             vout = new CTxOut[nOutputs];
123
124             for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
125             {
126                 // Fill outputs array
127                 vout[nCurrentOutput] = new CTxOut();
128                 vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
129
130                 int nScriptPKLen = (int)wBytes.GetVarInt();
131                 vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
132             }
133
134             nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
135         }
136
137         /// <summary>
138         /// Read transactions array which is encoded in the block body.
139         /// </summary>
140         /// <param name="wTxBytes">Bytes sequence</param>
141         /// <returns>Transactions array</returns>
142         public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
143         {
144             // Read amount of transactions
145             int nTransactions = (int)wTxBytes.GetVarInt();
146             var tx = new CTransaction[nTransactions];
147
148             for (int nTx = 0; nTx < nTransactions; nTx++)
149             {
150                 // Fill the transactions array
151                 tx[nTx] = new CTransaction();
152
153                 tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
154                 tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
155
156                 // Inputs array
157                 tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
158
159                 // outputs array
160                 tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
161
162                 tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
163             }
164
165             return tx;
166         }
167
168         public bool IsCoinBase
169         {
170             get { return (vin.Length == 1 && vin[0].prevout.IsNull && vout.Length >= 1); }
171         }
172
173         public bool IsCoinStake
174         {
175             get
176             {
177                 return (vin.Length > 0 && (!vin[0].prevout.IsNull) && vout.Length >= 2 && vout[0].IsEmpty);
178             }
179         }
180
181         /// <summary>
182         /// Transaction hash
183         /// </summary>
184         public Hash256 Hash
185         {
186             get { return Hash256.Compute256(Bytes); }
187         }
188
189         /// <summary>
190         /// A sequence of bytes, which corresponds to the current state of CTransaction.
191         /// </summary>
192         public byte[] Bytes
193         {
194             get
195             {
196                 var resultBytes = new List<byte>();
197
198                 resultBytes.AddRange(BitConverter.GetBytes(nVersion));
199                 resultBytes.AddRange(BitConverter.GetBytes(nTime));
200                 resultBytes.AddRange(VarInt.EncodeVarInt(vin.LongLength));
201
202                 foreach (var input in vin)
203                 {
204                     resultBytes.AddRange(input.Bytes);
205                 }
206
207                 resultBytes.AddRange(VarInt.EncodeVarInt(vout.LongLength));
208
209                 foreach (var output in vout)
210                 {
211                     resultBytes.AddRange(output.Bytes);
212                 }
213
214                 resultBytes.AddRange(BitConverter.GetBytes(nLockTime));
215
216                 return resultBytes.ToArray();
217             }
218         }
219
220         public override string ToString()
221         {
222             var sb = new StringBuilder();
223
224             sb.AppendFormat("CTransaction(\n nVersion={0},\n nTime={1},\n", nVersion, nTime);
225
226             foreach (var txin in vin)
227             {
228                 sb.AppendFormat(" {0},\n", txin.ToString());
229             }
230
231             foreach (var txout in vout)
232             {
233                 sb.AppendFormat(" {0},\n", txout.ToString());
234             }
235
236             sb.AppendFormat("\nnLockTime={0}\n)", nLockTime);
237
238             return sb.ToString();
239         }
240         }
241 }