From 0afd7876b5538f75852557443c3e7bbcefa519d1 Mon Sep 17 00:00:00 2001 From: CryptoManiac Date: Fri, 21 Aug 2015 03:18:11 +0300 Subject: [PATCH] Use CScript for vin and vout, --- Novacoin/CBlock.cs | 2 +- Novacoin/CTransaction.cs | 16 ++++++++++------ Novacoin/CTxIn.cs | 15 +++++++++------ Novacoin/CTxOut.cs | 15 ++++++++++----- 4 files changed, 30 insertions(+), 18 deletions(-) diff --git a/Novacoin/CBlock.cs b/Novacoin/CBlock.cs index 46ca9d5..17ffe6a 100644 --- a/Novacoin/CBlock.cs +++ b/Novacoin/CBlock.cs @@ -28,7 +28,7 @@ namespace Novacoin /// Parse byte sequence and initialize new block instance /// /// - public CBlock (List blockBytes) + public CBlock (IList blockBytes) { header = new CBlockHeader(); diff --git a/Novacoin/CTransaction.cs b/Novacoin/CTransaction.cs index 4467ebe..89c4b11 100644 --- a/Novacoin/CTransaction.cs +++ b/Novacoin/CTransaction.cs @@ -71,7 +71,7 @@ namespace Novacoin /// /// Parse byte sequence and initialize new instance of CTransaction /// - /// + /// Byte sequence public CTransaction (IList txBytes) { WrappedList wBytes = new WrappedList(txBytes); @@ -88,9 +88,11 @@ namespace Novacoin vin[nCurrentInput] = new CTxIn(); vin[nCurrentInput].txID = new Hash256(wBytes.GetItems(32)); - vin[nCurrentInput].n = BitConverter.ToUInt32(wBytes.GetItems(4),0); - vin[nCurrentInput].scriptSig = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)); - vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4),0); + vin[nCurrentInput].n = BitConverter.ToUInt32(wBytes.GetItems(4), 0); + + int nScriptSigLen = (int)VarInt.ReadVarInt(ref wBytes); + vin[nCurrentInput].scriptSig = new CScript(wBytes.GetItems(nScriptSigLen)); + vin[nCurrentInput].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0); } int nOutputs = (int)VarInt.ReadVarInt(ref wBytes); @@ -100,8 +102,10 @@ namespace Novacoin { // Fill outputs array vout[nCurrentOutput] = new CTxOut(); - vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.GetItems(8),0); - vout[nCurrentOutput].scriptPubKey = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)); + vout[nCurrentOutput].nValue = BitConverter.ToUInt64(wBytes.GetItems(8), 0); + + int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes); + vout[nCurrentOutput].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen)); } nLockTime = BitConverter.ToUInt32(wBytes.GetItems(4), 0); diff --git a/Novacoin/CTxIn.cs b/Novacoin/CTxIn.cs index 7f6aeee..59ddc40 100644 --- a/Novacoin/CTxIn.cs +++ b/Novacoin/CTxIn.cs @@ -22,7 +22,7 @@ namespace Novacoin /// /// First half of script, signatures for the scriptPubKey /// - public byte[] scriptSig; + public CScript scriptSig; /// /// Transaction variant number, irrelevant if nLockTime isn't specified. Its value is 0xffffffff by default. @@ -68,7 +68,7 @@ namespace Novacoin 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].scriptSig = new CScript(wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes))); vin[nIndex].nSequence = BitConverter.ToUInt32(wBytes.GetItems(4), 0); } @@ -88,8 +88,11 @@ namespace Novacoin 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 + + List s = new List(scriptSig.Bytes); + + inputBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptSig length + inputBytes.AddRange(s); // scriptSig inputBytes.AddRange(BitConverter.GetBytes(nSequence)); // Sequence return inputBytes; @@ -107,11 +110,11 @@ namespace Novacoin if (IsCoinBase) { - sb.AppendFormat("CTxIn(txId={0}, coinbase={2}, nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig), nSequence); + sb.AppendFormat("CTxIn(txId={0}, coinbase={2}, nSequence={3})", txID.ToString(), n, Interop.ToHex(scriptSig.Bytes), nSequence); } else { - sb.AppendFormat("CTxIn(txId={0}, n={1}, scriptSig={2}, nSequence={3})", txID.ToString(), n, (new CScript(scriptSig)).ToString(), nSequence); + sb.AppendFormat("CTxIn(txId={0}, n={1}, scriptSig={2}, nSequence={3})", txID.ToString(), n, scriptSig.ToString(), nSequence); } return sb.ToString (); diff --git a/Novacoin/CTxOut.cs b/Novacoin/CTxOut.cs index 9b40a8f..51ae16f 100644 --- a/Novacoin/CTxOut.cs +++ b/Novacoin/CTxOut.cs @@ -17,7 +17,7 @@ namespace Novacoin /// /// Second half of script which contains spending instructions. /// - public byte[] scriptPubKey; + public CScript scriptPubKey; /// /// Initialize new CTxOut instance as a copy of another instance. @@ -51,7 +51,9 @@ namespace Novacoin // Fill outputs array vout[nIndex] = new CTxOut(); vout[nIndex].nValue = BitConverter.ToUInt32(wBytes.GetItems(8), 0); - vout[nIndex].scriptPubKey = wBytes.GetItems((int)VarInt.ReadVarInt(ref wBytes)); + + int nScriptPKLen = (int)VarInt.ReadVarInt(ref wBytes); + vout[nIndex].scriptPubKey = new CScript(wBytes.GetItems(nScriptPKLen)); } return vout; @@ -68,8 +70,11 @@ namespace Novacoin List resultBytes = new List(); resultBytes.AddRange(BitConverter.GetBytes(nValue)); // txout value - resultBytes.AddRange(VarInt.EncodeVarInt(scriptPubKey.LongLength)); // scriptPubKey length - resultBytes.AddRange(scriptPubKey); // scriptPubKey + + List s = new List(scriptPubKey.Bytes); + + resultBytes.AddRange(VarInt.EncodeVarInt(s.Count)); // scriptPubKey length + resultBytes.AddRange(s); // scriptPubKey return resultBytes; } @@ -78,7 +83,7 @@ namespace Novacoin public override string ToString () { StringBuilder sb = new StringBuilder (); - sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, (new CScript(scriptPubKey)).ToString()); + sb.AppendFormat ("CTxOut(nValue={0}, scriptPubKey={1})", nValue, scriptPubKey.ToString()); return sb.ToString (); } -- 1.7.1