using System;
using System.Text;
using System.Collections.Generic;
namespace Novacoin
{
///
/// Transaction input.
///
public class CTxIn
{
///
/// Hash of parent transaction.
///
public Hash256 txID = new Hash256();
///
/// Parent input number.
///
public uint n = 0;
///
/// First half of script, signatures for the scriptPubKey
///
public byte[] scriptSig;
///
/// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
///
public uint nSequence = 0xffffffff;
///
/// Initialize new CTxIn instance as copy of another one.
///
/// CTxIn instance.
public CTxIn(CTxIn i)
{
txID = i.txID;
n = i.n;
scriptSig = i.scriptSig;
nSequence = i.nSequence;
}
///
/// Initialize an empty instance of CTxIn class
///
public CTxIn()
{
}
///
/// Read vin list from byte sequence.
///
/// Reference to byte sequence
/// Inputs array
public static CTxIn[] ReadTxInList(ref WrappedList wBytes)
{
CTxIn[] vin;
// Get amount
int nInputs = (int)VarInt.ReadVarInt(ref wBytes);
vin = new CTxIn[nInputs];
for (int nIndex = 0; nIndex < nInputs; nIndex++)
{
// Fill inputs array
vin[nIndex] = new CTxIn();
vin[nIndex].txID = new Hash256(wBytes.GetItems(32));
vin[nIndex].n = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
vin[nIndex].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes));
vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0);
}
// Return inputs array
return vin;
}
///
/// Get raw bytes representation of our input.
///
/// Byte sequence.
public IList ToBytes()
{
List inputBytes = new List();
inputBytes.AddRange(txID.hashBytes); // Input transaction id
inputBytes.AddRange(BitConverter.GetBytes(n)); // Output number
inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // scriptSig length
inputBytes.AddRange(scriptSig); // scriptSig
inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence
return inputBytes;
}
public bool IsCoinBase()
{
return txID.IsZero();
}
public override string ToString ()
{
StringBuilder sb = new StringBuilder ();
if (IsCoinBase())
{
sb.AppendFormat("CTxIn(txId={0}, coinbase={2}, nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig), nSequence);
}
else
{
sb.AppendFormat("CTxIn(txId={0}, n={1}, scriptSig={2}, nSequence={3})", txID.ToString(), n, (new CScript(scriptSig)).ToString(), nSequence);
}
return sb.ToString ();
}
}
}