/// </summary>
public uint nLockTime = 0;
- public CTransaction ()
+ /// <summary>
+ /// Parse byte sequence and initialize new instance of CTransaction
+ /// </summary>
+ /// <param name="txBytes"></param>
+ public CTransaction (IList<byte> txBytes)
{
+ WrappedList<byte> wBytes = new WrappedList<byte>(txBytes);
+ nVersion = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+ nTime = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+
+ int nInputs = (int)VarInt.ReadVarInt(wBytes);
+ inputs = new CTxIn[nInputs];
+
+ for (int nCurrentInput = 0; nCurrentInput < nInputs; nCurrentInput++)
+ {
+ // Fill inputs array
+ inputs[nCurrentInput].txID = new Hash256(wBytes.GetItems(32));
+ inputs[nCurrentInput].n = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+ inputs[nCurrentInput].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(wBytes));
+ inputs[nCurrentInput].nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4));
+ }
+
+ int nOutputs = (int)VarInt.ReadVarInt(wBytes);
+ outputs = new CTxOut[nOutputs];
+
+ for (int nCurrentOutput = 0; nCurrentOutput < nInputs; nCurrentOutput++)
+ {
+ // Fill outputs array
+ outputs[nCurrentOutput].nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8));
+ outputs[nCurrentOutput].scriptPubKey = wBytes.GetItems((int)VarInt.ReadVarInt(wBytes));
+ }
+
+ nLockTime = Interop.LEBytesToUInt32(wBytes.GetItems(4));
}
IList<byte> ToBytes()
/// <summary>
/// Hash of parent transaction.
/// </summary>
- private Hash256 txID = new Hash256();
+ public Hash256 txID = new Hash256();
/// <summary>
/// Parent input number.
/// </summary>
- private uint nInput = 0;
+ public uint n = 0;
/// <summary>
/// First half of script, signatures for the scriptPubKey
/// </summary>
- private byte[] scriptSig;
+ public byte[] scriptSig;
/// <summary>
/// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default.
/// </summary>
- private uint nSequence = 0xffffffff;
+ public uint nSequence = 0xffffffff;
/// <summary>
/// Initialize new CTxIn instance as copy of another one.
public CTxIn(CTxIn i)
{
txID = i.txID;
- nInput = i.nInput;
+ n = i.n;
scriptSig = i.scriptSig;
nSequence = i.nSequence;
}
/// <summary>
- /// Decode byte sequence and initialize new instance of CTxIn class.
- /// </summary>
- /// <param name="bytes">Byte sequence</param>
- public CTxIn (IList<byte> bytes)
- {
- WrappedList<byte> wBytes = new WrappedList<byte>(bytes);
-
- txID = new Hash256(wBytes.GetItems(32));
- nInput = Interop.LEBytesToUInt32(wBytes.GetItems(4));
- int ssLength = (int)VarInt.ReadVarInt(wBytes);
- scriptSig = wBytes.GetItems(ssLength);
- nSequence = Interop.LEBytesToUInt32(wBytes.GetItems(4));
- }
-
- /// <summary>
/// Get raw bytes representation of our input.
/// </summary>
/// <returns>Byte sequence.</returns>
List<byte> inputBytes = new List<byte>();
inputBytes.AddRange(txID.hashBytes); // Input transaction id
- inputBytes.AddRange(Interop.LEBytes(nInput)); // Input number
- inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // Scriptsig length
- inputBytes.AddRange(scriptSig); // ScriptSig
+ inputBytes.AddRange(Interop.LEBytes(n)); // Output number
+ inputBytes.AddRange(VarInt.EncodeVarInt(scriptSig.LongLength)); // scriptSig length
+ inputBytes.AddRange(scriptSig); // scriptSig
inputBytes.AddRange(Interop.LEBytes(nSequence)); // Sequence
return inputBytes;
public override string ToString ()
{
StringBuilder sb = new StringBuilder ();
- sb.AppendFormat ("CTxIn(txId={0},n={1},scriptSig={2}", nInput, nInput, scriptSig.ToString());
+ sb.AppendFormat ("CTxIn(txId={0},n={1},scriptSig={2}", txID.ToString(), n, scriptSig.ToString());
return sb.ToString ();
}
/// <summary>
/// Input value.
/// </summary>
- private ulong nValue;
+ public ulong nValue;
/// <summary>
/// Second half of script which contains spending instructions.
/// </summary>
- private byte[] scriptPubKey;
+ public byte[] scriptPubKey;
/// <summary>
/// Initialize new CTxOut instance as a copy of another instance.
}
/// <summary>
- /// Parse input byte sequence and initialize new CTxOut instance.
- /// </summary>
- /// <param name="bytes">Byte sequence.</param>
- public CTxOut(IList<byte> bytes)
- {
- WrappedList<byte> wBytes = new WrappedList<byte>(bytes);
-
- nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8));
- int spkLength = (int)VarInt.ReadVarInt(wBytes);
-
- scriptPubKey = wBytes.GetItems(spkLength);
- }
-
- /// <summary>
/// Get raw bytes representation of our output.
/// </summary>
/// <returns>Byte sequence.</returns>