Bugfix: incorrect TxOffset calculation.
[NovacoinLibrary.git] / Novacoin / CScript.cs
index 5692fa9..e2e17e0 100644 (file)
@@ -20,26 +20,10 @@ using System;
 using System.Linq;
 using System.Text;
 using System.Collections.Generic;
+using System.Diagnostics.Contracts;
 
 namespace Novacoin
 {
-    public class CScriptException : Exception
-    {
-        public CScriptException()
-        {
-        }
-
-        public CScriptException(string message)
-            : base(message)
-        {
-        }
-
-        public CScriptException(string message, Exception inner)
-            : base(message, inner)
-        {
-        }
-    }
-
     /// <summary>
     /// Representation of script code
     /// </summary>
@@ -68,7 +52,7 @@ namespace Novacoin
         /// Return a new instance of ByteQueue object for current code bytes
         /// </summary>
         /// <returns></returns>
-        public ByteQueue GetByteQUeue()
+        public ByteQueue GetByteQueue()
         {
              return new ByteQueue(codeBytes);
         }
@@ -79,10 +63,7 @@ namespace Novacoin
         /// <param name="opcode"></param>
         public void AddInstruction(instruction opcode)
         {
-            if (opcode < instruction.OP_0 || opcode > instruction.OP_INVALIDOPCODE)
-            {
-                throw new CScriptException("CScript::AddInstruction() : invalid instruction");
-            }
+            Contract.Requires<ArgumentException>(opcode >= instruction.OP_0 && opcode <= instruction.OP_INVALIDOPCODE, "Invalid instruction.");
 
             codeBytes.Add((byte)opcode);
         }
@@ -96,7 +77,7 @@ namespace Novacoin
         public void AddHash(Hash160 hash)
         {
             codeBytes.Add((byte)hash.hashSize);
-            codeBytes.AddRange(hash.hashBytes);
+            codeBytes.AddRange((byte[])hash);
         }
 
         /// <summary>
@@ -108,7 +89,7 @@ namespace Novacoin
         public void AddHash(Hash256 hash)
         {
             codeBytes.Add((byte)hash.hashSize);
-            codeBytes.AddRange(hash.hashBytes);
+            codeBytes.AddRange((byte[])hash);
         }
 
         /// <summary>
@@ -132,7 +113,7 @@ namespace Novacoin
             }
             else if (nCount < 0xffff)
             {
-                // OP_PUSHDATA1 0x00 0x01 [0x5a]
+                // OP_PUSHDATA1 0x01 0x00 [0x5a]
                 codeBytes.Add((byte)instruction.OP_PUSHDATA2);
 
                 var szBytes = BitConverter.GetBytes((ushort)nCount);
@@ -140,7 +121,7 @@ namespace Novacoin
             }
             else if (nCount < 0xffffffff)
             {
-                // OP_PUSHDATA1 0x00 0x00 0x00 0x01 [0x5a]
+                // OP_PUSHDATA1 0x01 0x00 0x00 0x00 [0x5a]
                 codeBytes.Add((byte)instruction.OP_PUSHDATA4);
 
                 var szBytes = BitConverter.GetBytes((uint)nCount);
@@ -420,7 +401,7 @@ namespace Novacoin
             // This is a pay-to-script-hash scriptPubKey;
             // get the last item that the scriptSig
             // pushes onto the stack:
-            ByteQueue wScriptSig = scriptSig.GetByteQUeue();
+            ByteQueue wScriptSig = scriptSig.GetByteQueue();
 
             instruction opcode; // Current instruction
             byte[] pushArgs; // OP_PUSHDATAn argument
@@ -447,7 +428,7 @@ namespace Novacoin
         public void SetDestination(CPubKey pubKey)
         {
             codeBytes.Clear();
-            PushData(pubKey.PublicBytes);
+            PushData(pubKey);
             AddInstruction(instruction.OP_CHECKSIG);
         }
 
@@ -497,7 +478,7 @@ namespace Novacoin
 
             foreach (var key in keys)
             {
-                PushData(key.PublicBytes);
+                PushData(key);
             }
 
             AddInstruction(ScriptCode.EncodeOP_N(keys.Length));
@@ -507,9 +488,17 @@ namespace Novacoin
         /// <summary>
         /// Access to script code.
         /// </summary>
-        public byte[] Bytes
+        public static implicit operator byte[] (CScript script)
+        {
+            return script.codeBytes.ToArray();
+        }
+
+        /// <summary>
+        /// Script size
+        /// </summary>
+        public int Size
         {
-            get { return codeBytes.ToArray(); }
+            get { return codeBytes.Count; }
         }
 
         public CScriptID ScriptID