X-Git-Url: https://git.novaco.in/?a=blobdiff_plain;f=Novacoin%2FCTransaction.cs;h=ab1a5efb27d41d5144c93e17e9be348a5a657d22;hb=f1d2e217ae6e24a84edfa98ca3471a8a836ecaa7;hp=343968a1c674d9feab3a860f7b1a9a3b2dd02ab0;hpb=e817cfa60d6a7d7d724e6cd3ec3a04239aae1ac2;p=NovacoinLibrary.git
diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs
index 343968a..ab1a5ef 100644
--- a/Novacoin/CTransaction.cs
+++ b/Novacoin/CTransaction.cs
@@ -22,6 +22,24 @@ using System.Collections.Generic;
namespace Novacoin
{
+ [Serializable]
+ public class TransactionConstructorException : Exception
+ {
+ public TransactionConstructorException()
+ {
+ }
+
+ public TransactionConstructorException(string message)
+ : base(message)
+ {
+ }
+
+ public TransactionConstructorException(string message, Exception inner)
+ : base(message, inner)
+ {
+ }
+ }
+
///
/// Represents the transaction. Any transaction must provide one input and one output at least.
///
@@ -93,6 +111,27 @@ namespace Novacoin
nLockTime = tx.nLockTime;
}
+ public bool VerifyScripts()
+ {
+ if (IsCoinBase)
+ {
+ return true;
+ }
+
+ CTransaction txPrev = null;
+ for (int i = 0; i < vin.Length; i++)
+ {
+ var outpoint = vin[i].prevout;
+
+ if (!CBlockStore.Instance.GetTransaction(outpoint.hash, ref txPrev))
+ return false;
+
+ if (!ScriptCode.VerifyScript(vin[i].scriptSig, txPrev.vout[outpoint.n].scriptPubKey, this, i, (int)scriptflag.SCRIPT_VERIFY_P2SH, 0))
+ return false;
+ }
+
+ return true;
+ }
///
/// Parse byte sequence and initialize new instance of CTransaction
@@ -100,41 +139,48 @@ namespace Novacoin
/// Byte sequence
public CTransaction(byte[] txBytes)
{
- var wBytes = new ByteQueue(txBytes);
+ try
+ {
+ var wBytes = new ByteQueue(txBytes);
- nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
- nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ nVersion = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ nTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
- int nInputs = (int)wBytes.GetVarInt();
- vin = new CTxIn[nInputs];
+ int nInputs = (int)wBytes.GetVarInt();
+ vin = new CTxIn[nInputs];
- for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
- {
- // Fill inputs array
- vin[nCurrentInput] = new CTxIn();
-
- vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
+ for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
+ {
+ // Fill inputs array
+ vin[nCurrentInput] = new CTxIn();
- int nScriptSigLen = (int)wBytes.GetVarInt();
- vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
+ vin[nCurrentInput].prevout = new COutPoint(wBytes.Get(36));
- vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
- }
+ int nScriptSigLen = (int)wBytes.GetVarInt();
+ vin[nCurrentInput].scriptSig = new CScript(wBytes.Get(nScriptSigLen));
- int nOutputs = (int)wBytes.GetVarInt();
- vout = new CTxOut[nOutputs];
+ vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ }
- for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
- {
- // Fill outputs array
- vout[nCurrentOutput] = new CTxOut();
- vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
+ int nOutputs = (int)wBytes.GetVarInt();
+ vout = new CTxOut[nOutputs];
- int nScriptPKLen = (int)wBytes.GetVarInt();
- vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
- }
+ for (int nCurrentOutput = 0; nCurrentOutput < nOutputs; nCurrentOutput++)
+ {
+ // Fill outputs array
+ vout[nCurrentOutput] = new CTxOut();
+ vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.Get(8), 0);
+
+ int nScriptPKLen = (int)wBytes.GetVarInt();
+ vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.Get(nScriptPKLen));
+ }
- nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ nLockTime = BitConverter.ToUInt32(wBytes.Get(4), 0);
+ }
+ catch (Exception e)
+ {
+ throw new TransactionConstructorException("Deserialization failed", e);
+ }
}
///
@@ -170,28 +216,36 @@ namespace Novacoin
/// Transactions array
public static CTransaction[] ReadTransactionsList(ref ByteQueue wTxBytes)
{
- // Read amount of transactions
- int nTransactions = (int)wTxBytes.GetVarInt();
- var tx = new CTransaction[nTransactions];
-
- for (int nTx = 0; nTx < nTransactions; nTx++)
+ try
{
- // Fill the transactions array
- tx[nTx] = new CTransaction();
+ // Read amount of transactions
+ int nTransactions = (int)wTxBytes.GetVarInt();
+ var tx = new CTransaction[nTransactions];
- tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
- tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ for (int nTx = 0; nTx < nTransactions; nTx++)
+ {
+ // Fill the transactions array
+ tx[nTx] = new CTransaction();
- // Inputs array
- tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
+ tx[nTx].nVersion = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ tx[nTx].nTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
- // outputs array
- tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
+ // Inputs array
+ tx[nTx].vin = CTxIn.ReadTxInList(ref wTxBytes);
- tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
- }
+ // outputs array
+ tx[nTx].vout = CTxOut.ReadTxOutList(ref wTxBytes);
+
+ tx[nTx].nLockTime = BitConverter.ToUInt32(wTxBytes.Get(4), 0);
+ }
+
+ return tx;
- return tx;
+ }
+ catch (Exception e)
+ {
+ throw new TransactionConstructorException("Deserialization failed", e);
+ }
}
public bool IsCoinBase