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 public class TransactionConstructorException : Exception
28 public TransactionConstructorException()
32 public TransactionConstructorException(string message)
37 public TransactionConstructorException(string message, Exception inner)
38 : base(message, inner)
44 /// Represents the transaction. Any transaction must provide one input and one output at least.
46 public class CTransaction
49 /// Version of transaction schema.
54 /// Transaction timestamp.
59 /// Array of transaction inputs
64 /// Array of transaction outputs
69 /// Block height or timestamp when transaction is final
71 public uint nLockTime;
74 /// Initialize an empty instance
78 // Initialize empty input and output arrays. Please note that such
79 // configuration is not valid for real transaction, you have to supply
80 // at least one input and one output.
89 /// Initialize new instance as a copy of another transaction
91 /// <param name="tx">Transaction to copy from</param>
92 public CTransaction(CTransaction tx)
94 nVersion = tx.nVersion;
97 vin = new CTxIn[tx.vin.Length];
99 for (int i = 0; i < vin.Length; i++)
101 vin[i] = new CTxIn(tx.vin[i]);
104 vout = new CTxOut[tx.vout.Length];
106 for (int i = 0; i < vout.Length; i++)
108 vout[i] = new CTxOut(tx.vout[i]);
111 nLockTime = tx.nLockTime;
114 public bool VerifyScripts()
121 CTransaction txPrev = null;
122 for (int i = 0; i < vin.Length; i++)
124 var outpoint = vin[i].prevout;
126 if (!CBlockStore.Instance.GetTransaction(outpoint.hash, ref txPrev))
129 if (!ScriptCode.VerifyScript(vin[i].scriptSig, txPrev.vout[outpoint.n].scriptPubKey, this, i, (int)scriptflag.SCRIPT_VERIFY_P2SH, 0))
137 /// Parse byte sequence and initialize new instance of CTransaction
139 /// <param name="txBytes">Byte sequence</param>
140 public CTransaction(byte[] txBytes)
144 var wBytes = new ByteQueue(txBytes);
146 nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
147 nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
149 int nInputs = (int)wBytes.GetVarInt();
150 vin = new CTxIn[nInputs];
152 for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
155 vin[nCurrentInput] = new CTxIn();
157 vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
159 int nScriptSigLen = (int)wBytes.GetVarInt();
160 vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
162 vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
165 int nOutputs = (int)wBytes.GetVarInt();
166 vout = new CTxOut[nOutputs];
168 for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
170 // Fill outputs array
171 vout[nCurrentOutput] = new CTxOut();
172 vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
174 int nScriptPKLen = (int)wBytes.GetVarInt();
175 vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
178 nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
182 throw new TransactionConstructorException("Deserialization failed", e);
193 int nSize = 12; // nVersion, nTime, nLockLime
195 nSize += VarInt.GetEncodedSize(vin.Length);
196 nSize += VarInt.GetEncodedSize(vout.Length);
198 foreach (var input in vin)
203 foreach (var output in vout)
205 nSize += output.Size;
213 /// Read transactions array which is encoded in the block body.
215 /// <param name="wTxBytes">Bytes sequence</param>
216 /// <returns>Transactions array</returns>
217 public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
221 // Read amount of transactions
222 int nTransactions = (int)wTxBytes.GetVarInt();
223 var tx = new CTransaction[nTransactions];
225 for (int nTx = 0; nTx < nTransactions; nTx++)
227 // Fill the transactions array
228 tx[nTx] = new CTransaction();
230 tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
231 tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
234 tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
237 tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
239 tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
247 throw new TransactionConstructorException("Deserealization failed", e);
251 public bool IsCoinBase
253 get { return (vin.Length == 1 && vin[0].prevout.IsNull && vout.Length >= 1); }
256 public bool IsCoinStake
260 return (vin.Length > 0 && (!vin[0].prevout.IsNull) && vout.Length >= 2 && vout[0].IsEmpty);
269 get { return Hash256.Compute256(this); }
273 /// A sequence of bytes, which corresponds to the current state of CTransaction.
275 public static implicit operator byte[] (CTransaction tx)
277 var resultBytes = new List<byte>();
279 resultBytes.AddRange(BitConverter.GetBytes(tx.nVersion));
280 resultBytes.AddRange(BitConverter.GetBytes(tx.nTime));
281 resultBytes.AddRange(VarInt.EncodeVarInt(tx.vin.LongLength));
283 foreach (var input in tx.vin)
285 resultBytes.AddRange((byte[])input);
288 resultBytes.AddRange(VarInt.EncodeVarInt(tx.vout.LongLength));
290 foreach (var output in tx.vout)
292 resultBytes.AddRange((byte[])output);
295 resultBytes.AddRange(BitConverter.GetBytes(tx.nLockTime));
297 return resultBytes.ToArray();
300 public override string ToString()
302 var sb = new StringBuilder();
304 sb.AppendFormat("CTransaction(\n nVersion={0},\n nTime={1},\n", nVersion, nTime);
306 foreach (var txin in vin)
308 sb.AppendFormat(" {0},\n", txin.ToString());
311 foreach (var txout in vout)
313 sb.AppendFormat(" {0},\n", txout.ToString());
316 sb.AppendFormat("\nnLockTime={0}\n)", nLockTime);
318 return sb.ToString();