Use implicit type casting operator for serialization.
[NovacoinLibrary.git] / Novacoin / ScriptCode.cs
index 43cf9d4..52c36f7 100644 (file)
@@ -268,31 +268,32 @@ namespace Novacoin
             if (opcode <= instruction.OP_PUSHDATA4)
             {
                 var szBytes = new byte[4] { 0, 0, 0, 0 }; // Zero length
+                int nSize = 0;
 
                 try
                 {
                     if (opcode < instruction.OP_PUSHDATA1)
                     {
                         // Zero value instructions (OP_0, OP_FALSE)
-                        szBytes[3] = (byte)opcode;
+                        nSize = (int) opcode;
                     }
                     else if (opcode == instruction.OP_PUSHDATA1)
                     {
                         // The next byte contains the number of bytes to be pushed onto the stack, 
                         //    i.e. you have something like OP_PUSHDATA1 0x01 [0x5a]
-                        szBytes[3] = codeBytes.Get();
+                        nSize = codeBytes.Get();
                     }
                     else if (opcode == instruction.OP_PUSHDATA2)
                     {
                         // The next two bytes contain the number of bytes to be pushed onto the stack,
-                        //    i.e. now your operation will seem like this: OP_PUSHDATA2 0x00 0x01 [0x5a]
-                        codeBytes.Get(2).CopyTo(szBytes, 2);
+                        //    i.e. now your operation will seem like this: OP_PUSHDATA2 0x01 0x00 [0x5a]
+                        nSize = BitConverter.ToInt16(codeBytes.Get(2), 0);
                     }
                     else if (opcode == instruction.OP_PUSHDATA4)
                     {
                         // The next four bytes contain the number of bytes to be pushed onto the stack,
-                        //   OP_PUSHDATA4 0x00 0x00 0x00 0x01 [0x5a]
-                        szBytes = codeBytes.Get(4);
+                        //   OP_PUSHDATA4 0x01 0x00 0x00 0x00 [0x5a]
+                        nSize = BitConverter.ToInt32(codeBytes.Get(4), 0);
                     }
                 }
                 catch (ByteQueueException)
@@ -301,8 +302,6 @@ namespace Novacoin
                     return false;
                 }
 
-                int nSize = (int)Interop.BEBytesToUInt32(szBytes);
-
                 if (nSize > 0)
                 {
                     // If nSize is greater than zero then there is some data available
@@ -337,7 +336,7 @@ namespace Novacoin
 
             if (bytes.Length <= 4)
             {
-                sb.Append(Interop.BEBytesToUInt32(bytes));
+                sb.Append(new BigInteger(bytes));
             }
             else
             {
@@ -479,6 +478,8 @@ namespace Novacoin
         /// <returns>Result</returns>
         public static bool Solver(CScript scriptPubKey, out txnouttype typeRet, out IList<byte[]> solutions)
         {
+            var scriptBytes = ((byte[])scriptPubKey);
+
             solutions = new List<byte[]>();
 
             // There are shortcuts for pay-to-script-hash and pay-to-pubkey-hash, which are more constrained than the other types.
@@ -489,7 +490,7 @@ namespace Novacoin
                 typeRet = txnouttype.TX_SCRIPTHASH;
 
                 // Take 20 bytes with offset of 2 bytes
-                var hashBytes = scriptPubKey.Bytes.Skip(2).Take(20);
+                var hashBytes = scriptBytes.Skip(2).Take(20);
                 solutions.Add(hashBytes.ToArray());
 
                 return true;
@@ -501,7 +502,7 @@ namespace Novacoin
                 typeRet = txnouttype.TX_PUBKEYHASH;
 
                 // Take 20 bytes with offset of 3 bytes
-                var hashBytes = scriptPubKey.Bytes.Skip(3).Take(20);
+                var hashBytes = scriptBytes.Skip(3).Take(20);
                 solutions.Add(hashBytes.ToArray());
 
                 return true;
@@ -561,8 +562,8 @@ namespace Novacoin
 
                 byte[] args1, args2;
 
-                int last1 = script1.Bytes.Count() -1;
-                int last2 = script2.Bytes.Count() - 1;
+                int last1 = ((byte[])script1).Length -1;
+                int last2 = ((byte[])script2).Length - 1;
 
                 while (true)
                 {
@@ -685,7 +686,7 @@ namespace Novacoin
 
             // In case concatenating two scripts ends up with two codeseparators,
             // or an extra one at the end, this prevents all those possible incompatibilities.
-            script.RemovePattern(new byte[] { (byte)instruction.OP_CODESEPARATOR });
+            script.RemoveInstruction(instruction.OP_CODESEPARATOR);
 
             // Blank out other inputs' signatures
             for (int i = 0; i < txTmp.vin.Length; i++)
@@ -744,7 +745,7 @@ namespace Novacoin
             }
 
             // Concatenate and hash
-            var txBytes = txTmp.Bytes;
+            var txBytes = (byte[])txTmp;
             var nHashTypeBytes = BitConverter.GetBytes(nHashType);
 
             return Hash256.Compute256(ref txBytes, ref nHashTypeBytes);
@@ -867,7 +868,9 @@ namespace Novacoin
         /// <returns></returns>
         public static bool EvalScript(ref List<byte[]> stack, CScript script, CTransaction txTo, int nIn, int flags, int nHashType)
         {
-            if (script.Bytes.Count() > 10000)
+            var scriptBytes = ((byte[])script);
+
+            if (scriptBytes.Length > 10000)
             {
                 return false; // Size limit failed
             }
@@ -1548,7 +1551,7 @@ namespace Novacoin
                                             break;
                                     }
                                     popstack(ref stack);
-                                    stack.Add(hash.hashBytes);
+                                    stack.Add(hash);
                                 }
                                 break;
 
@@ -1572,7 +1575,7 @@ namespace Novacoin
                                     var pubkeyBytes = stacktop(ref stack, -1);
 
                                     // Subset of script starting at the most recent codeseparator
-                                    var scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin).ToArray());
+                                    var scriptCode = new CScript(scriptBytes.Skip(nCodeHashBegin).ToArray());
 
                                     // There's no way for a signature to sign itself
                                     scriptCode.RemovePattern(sigBytes);
@@ -1639,7 +1642,7 @@ namespace Novacoin
                                     }
 
                                     // Subset of script starting at the most recent codeseparator
-                                    var scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin).ToArray());
+                                    var scriptCode = new CScript(scriptBytes.Skip(nCodeHashBegin).ToArray());
 
                                     // There is no way for a signature to sign itself, so we need to drop the signatures
                                     for (int k = 0; k < nSigsCount; k++)