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.
33 public uint nVersion = 1;
36 /// Transaction timestamp.
38 public uint nTime = 0;
41 /// Array of transaction inputs
46 /// Array of transaction outputs
51 /// Block height or timestamp when transaction is final
53 public uint nLockTime = 0;
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.
68 /// Initialize new instance as a copy of another transaction
70 /// <param name="tx">Transaction to copy from</param>
71 public CTransaction(CTransaction tx)
73 nVersion = tx.nVersion;
76 vin = new CTxIn[tx.vin.Length];
78 for (int i = 0; i < vin.Length; i++)
80 vin[i] = new CTxIn(tx.vin[i]);
83 vout = new CTxOut[tx.vout.Length];
85 for (int i = 0; i < vout.Length; i++)
87 vout[i] = new CTxOut(tx.vout[i]);
90 nLockTime = tx.nLockTime;
95 /// Parse byte sequence and initialize new instance of CTransaction
97 /// <param name="txBytes">Byte sequence</param>
98 public CTransaction(byte[] txBytes)
100 var wBytes = new ByteQueue(txBytes);
102 nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
103 nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
105 int nInputs = (int)(int)wBytes.GetVarInt();
106 vin = new CTxIn[nInputs];
108 for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
111 vin[nCurrentInput] = new CTxIn();
113 vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
115 int nScriptSigLen = (int)wBytes.GetVarInt();
116 vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
118 vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
121 int nOutputs = (int)wBytes.GetVarInt();
122 vout = new CTxOut[nOutputs];
124 for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
126 // Fill outputs array
127 vout[nCurrentOutput] = new CTxOut();
128 vout[nCurrentOutput].nValue = BitConverter.ToInt64(wBytes.Get(8), 0);
130 int nScriptPKLen = (int)wBytes.GetVarInt();
131 vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
134 nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
138 /// Read transactions array which is encoded in the block body.
140 /// <param name="wTxBytes">Bytes sequence</param>
141 /// <returns>Transactions array</returns>
142 public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
144 // Read amount of transactions
145 int nTransactions = (int)wTxBytes.GetVarInt();
146 var tx = new CTransaction[nTransactions];
148 for (int nTx = 0; nTx < nTransactions; nTx++)
150 // Fill the transactions array
151 tx[nTx] = new CTransaction();
153 tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
154 tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
157 tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
160 tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
162 tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
168 public bool IsCoinBase
170 get { return (vin.Length == 1 && vin[0].prevout.IsNull && vout.Length >= 1); }
173 public bool IsCoinStake
177 return (vin.Length > 0 && (!vin[0].prevout.IsNull) && vout.Length >= 2 && vout[0].IsEmpty);
186 get { return Hash256.Compute256(this); }
190 /// A sequence of bytes, which corresponds to the current state of CTransaction.
192 public static implicit operator byte[] (CTransaction tx)
194 var resultBytes = new List<byte>();
196 resultBytes.AddRange(BitConverter.GetBytes(tx.nVersion));
197 resultBytes.AddRange(BitConverter.GetBytes(tx.nTime));
198 resultBytes.AddRange(VarInt.EncodeVarInt(tx.vin.LongLength));
200 foreach (var input in tx.vin)
202 resultBytes.AddRange((byte[])input);
205 resultBytes.AddRange(VarInt.EncodeVarInt(tx.vout.LongLength));
207 foreach (var output in tx.vout)
209 resultBytes.AddRange((byte[])output);
212 resultBytes.AddRange(BitConverter.GetBytes(tx.nLockTime));
214 return resultBytes.ToArray();
217 public override string ToString()
219 var sb = new StringBuilder();
221 sb.AppendFormat("CTransaction(\n nVersion={0},\n nTime={1},\n", nVersion, nTime);
223 foreach (var txin in vin)
225 sb.AppendFormat(" {0},\n", txin.ToString());
228 foreach (var txout in vout)
230 sb.AppendFormat(" {0},\n", txout.ToString());
233 sb.AppendFormat("\nnLockTime={0}\n)", nLockTime);
235 return sb.ToString();