CScript: implement new AddRawData method, allow RemoveInstruction to process the...
[NovacoinLibrary.git] / Novacoin / ScriptCode.cs
index eaafed1..e1964b3 100644 (file)
@@ -1,17 +1,19 @@
 /**
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ *  Novacoin classes library
+ *  Copyright (C) 2015 Alex D. (balthazar.ad@gmail.com)
+
+ *  This program is free software: you can redistribute it and/or modify
+ *  it under the terms of the GNU Affero General Public License as
+ *  published by the Free Software Foundation, either version 3 of the
+ *  License, or (at your option) any later version.
+
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU Affero General Public License for more details.
+
+ *  You should have received a copy of the GNU Affero General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 using System;
@@ -265,32 +267,33 @@ namespace Novacoin
             // Immediate operand
             if (opcode <= instruction.OP_PUSHDATA4)
             {
-                byte[] szBytes = new byte[4] { 0, 0, 0, 0 }; // Zero length
+                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)
@@ -299,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
@@ -335,7 +336,7 @@ namespace Novacoin
 
             if (bytes.Length <= 4)
             {
-                sb.Append(Interop.BEBytesToUInt32(bytes));
+                sb.Append(new BigInteger(bytes));
             }
             else
             {
@@ -683,7 +684,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++)
@@ -1566,11 +1567,11 @@ namespace Novacoin
                                         return false;
                                     }
 
-                                    byte[] sigBytes = stacktop(ref stack, -2);
-                                    byte[] pubkeyBytes = stacktop(ref stack, -1);
+                                    var sigBytes = stacktop(ref stack, -2);
+                                    var pubkeyBytes = stacktop(ref stack, -1);
 
                                     // Subset of script starting at the most recent codeseparator
-                                    CScript scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin));
+                                    var scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin).ToArray());
 
                                     // There's no way for a signature to sign itself
                                     scriptCode.RemovePattern(sigBytes);
@@ -1637,7 +1638,7 @@ namespace Novacoin
                                     }
 
                                     // Subset of script starting at the most recent codeseparator
-                                    CScript scriptCode = new CScript(script.Bytes.Skip(nCodeHashBegin));
+                                    var scriptCode = new CScript(script.Bytes.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++)