From 79788d8ea96f28697226316f6ab5bb32e6bee880 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Mon, 17 Aug 2015 02:22:02 +0300 Subject: [PATCH] CTransaction constructor. Also, trying to remove some excessive code. --- Novacoin/CTransaction.cs | 33 ++++++++++++++++++++++++++++++++- Novacoin/CTxIn.cs | 33 +++++++++------------------------ Novacoin/CTxOut.cs | 18 ++---------------- 3 files changed, 43 insertions(+), 41 deletions(-) diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index b58dd7e..77d6a62 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -33,9 +33,40 @@ namespace Novacoin /// public uint nLockTime = 0; - public CTransaction () + /// + /// Parse byte sequence and initialize new instance of CTransaction + /// + /// + public CTransaction (IList txBytes) { + WrappedList wBytes = new WrappedList(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 ToBytes() diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs index b87e594..d079d15 100644 --- a/Novacoin/CTxIn.cs +++ b/Novacoin/CTxIn.cs @@ -12,22 +12,22 @@ namespace Novacoin /// /// Hash of parent transaction. /// - private Hash256 txID = new Hash256(); + public Hash256 txID = new Hash256(); /// /// Parent input number. /// - private uint nInput = 0; + public uint n = 0; /// /// First half of script, signatures for the scriptPubKey /// - private byte[] scriptSig; + public byte[] scriptSig; /// /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default. /// - private uint nSequence = 0xffffffff; + public uint nSequence = 0xffffffff; /// /// Initialize new CTxIn instance as copy of another one. @@ -36,27 +36,12 @@ namespace Novacoin public CTxIn(CTxIn i) { txID = i.txID; - nInput = i.nInput; + n = i.n; scriptSig = i.scriptSig; nSequence = i.nSequence; } /// - /// Decode byte sequence and initialize new instance of CTxIn class. - /// - /// Byte sequence - public CTxIn (IList bytes) - { - WrappedList wBytes = new WrappedList(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)); - } - - /// /// Get raw bytes representation of our input. /// /// Byte sequence. @@ -65,9 +50,9 @@ namespace Novacoin List inputBytes = new List(); 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; @@ -76,7 +61,7 @@ namespace Novacoin 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 (); } diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index 42d2b8d..aec5a88 100644 --- a/Novacoin/CTxOut.cs +++ b/Novacoin/CTxOut.cs @@ -12,12 +12,12 @@ namespace Novacoin /// /// Input value. /// - private ulong nValue; + public ulong nValue; /// /// Second half of script which contains spending instructions. /// - private byte[] scriptPubKey; + public byte[] scriptPubKey; /// /// Initialize new CTxOut instance as a copy of another instance. @@ -30,20 +30,6 @@ namespace Novacoin } /// - /// Parse input byte sequence and initialize new CTxOut instance. - /// - /// Byte sequence. - public CTxOut(IList bytes) - { - WrappedList wBytes = new WrappedList(bytes); - - nValue = Interop.LEBytesToUInt64(wBytes.GetItems(8)); - int spkLength = (int)VarInt.ReadVarInt(wBytes); - - scriptPubKey = wBytes.GetItems(spkLength); - } - - /// /// Get raw bytes representation of our output. /// /// Byte sequence. -- 1.7.1