2 * Novacoin classes library
3 * Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
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.
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.
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/>.
21 using System.Collections.Generic;
26 /// Represents the transaction. Any transaction must provide one input and one output at least.
28 public class CTransaction
31 /// Version of transaction schema.
36 /// Transaction timestamp.
41 /// Array of transaction inputs
46 /// Array of transaction outputs
51 /// Block height or timestamp when transaction is final
53 public uint nLockTime;
56 /// Initialize an empty instance
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.
71 /// Initialize new instance as a copy of another transaction
73 /// <param name="tx">Transaction to copy from</param>
74 public CTransaction(CTransaction tx)
76 nVersion = tx.nVersion;
79 vin = new CTxIn[tx.vin.Length];
81 for (int i = 0; i < vin.Length; i++)
83 vin[i] = new CTxIn(tx.vin[i]);
86 vout = new CTxOut[tx.vout.Length];
88 for (int i = 0; i < vout.Length; i++)
90 vout[i] = new CTxOut(tx.vout[i]);
93 nLockTime = tx.nLockTime;
98 /// Parse byte sequence and initialize new instance of CTransaction
100 /// <param name="txBytes">Byte sequence</param>
101 public CTransaction(byte[] txBytes)
103 var wBytes = new ByteQueue(txBytes);
105 nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
106 nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
108 int nInputs = (int)wBytes.GetVarInt();
109 vin = new CTxIn[nInputs];
111 for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
114 vin[nCurrentInput] = new CTxIn();
116 vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
118 int nScriptSigLen = (int)wBytes.GetVarInt();
119 vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
121 vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
124 int nOutputs = (int)wBytes.GetVarInt();
125 vout = new CTxOut[nOutputs];
127 for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
129 // Fill outputs array
130 vout[nCurrentOutput] = new CTxOut();
131 vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
133 int nScriptPKLen = (int)wBytes.GetVarInt();
134 vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
137 nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
147 int nSize = 12; // nVersion, nTime, nLockLime
149 nSize += VarInt.GetEncodedSize(vin.Length);
150 nSize += VarInt.GetEncodedSize(vout.Length);
152 foreach (var input in vin)
157 foreach (var output in vout)
159 nSize += output.Size;
167 /// Read transactions array which is encoded in the block body.
169 /// <param name="wTxBytes">Bytes sequence</param>
170 /// <returns>Transactions array</returns>
171 public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
173 // Read amount of transactions
174 int nTransactions = (int)wTxBytes.GetVarInt();
175 var tx = new CTransaction[nTransactions];
177 for (int nTx = 0; nTx < nTransactions; nTx++)
179 // Fill the transactions array
180 tx[nTx] = new CTransaction();
182 tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
183 tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
186 tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
189 tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
191 tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
197 public bool IsCoinBase
199 get { return (vin.Length == 1 && vin[0].prevout.IsNull && vout.Length >= 1); }
202 public bool IsCoinStake
206 return (vin.Length > 0 && (!vin[0].prevout.IsNull) && vout.Length >= 2 && vout[0].IsEmpty);
215 get { return Hash256.Compute256(this); }
219 /// A sequence of bytes, which corresponds to the current state of CTransaction.
221 public static implicit operator byte[] (CTransaction tx)
223 var resultBytes = new List<byte>();
225 resultBytes.AddRange(BitConverter.GetBytes(tx.nVersion));
226 resultBytes.AddRange(BitConverter.GetBytes(tx.nTime));
227 resultBytes.AddRange(VarInt.EncodeVarInt(tx.vin.LongLength));
229 foreach (var input in tx.vin)
231 resultBytes.AddRange((byte[])input);
234 resultBytes.AddRange(VarInt.EncodeVarInt(tx.vout.LongLength));
236 foreach (var output in tx.vout)
238 resultBytes.AddRange((byte[])output);
241 resultBytes.AddRange(BitConverter.GetBytes(tx.nLockTime));
243 return resultBytes.ToArray();
246 public override string ToString()
248 var sb = new StringBuilder();
250 sb.AppendFormat("CTransaction(\n nVersion={0},\n nTime={1},\n", nVersion, nTime);
252 foreach (var txin in vin)
254 sb.AppendFormat(" {0},\n", txin.ToString());
257 foreach (var txout in vout)
259 sb.AppendFormat(" {0},\n", txout.ToString());
262 sb.AppendFormat("\nnLockTime={0}\n)", nLockTime);
264 return sb.ToString();