SetDestination fixes and tests
authorCryptoManiac <balthazar@yandex.ru>
Thu, 20 Aug 2015 20:37:21 +0000 (23:37 +0300)
committerCryptoManiac <balthazar@yandex.ru>
Thu, 20 Aug 2015 20:37:21 +0000 (23:37 +0300)
Novacoin/CScript.cs
NovacoinTest/Program.cs

index 044f8b9..bb0e5a5 100644 (file)
@@ -96,8 +96,8 @@ namespace Novacoin
         /// <summary>
         /// Create new OP_PUSHDATAn operator and add it to opcode bytes list
         /// </summary>
-        /// <param name="dataBytes">List of data bytes</param>
-        public void PushData(IList<byte> dataBytes)
+        /// <param name="dataBytes">Set of data bytes</param>
+        public void PushData(IEnumerable<byte> dataBytes)
         {
             long nCount = dataBytes.LongCount();
 
@@ -356,15 +356,35 @@ namespace Novacoin
 
         }
 
+        /// <summary>
+        /// Set pay-to-pubkey destination.
+        /// </summary>
+        /// <param name="pubKey">Instance of CPubKey.</param>
+        public void SetDestination(CPubKey pubKey)
+        {
+            codeBytes.Clear();
+            PushData(pubKey.PublicBytes);
+            AddOp(opcodetype.OP_CHECKSIG);
+        }
+
+        /// <summary>
+        /// Set pay-to-pubkeyhash destination
+        /// </summary>
+        /// <param name="ID">Public key hash</param>
         public void SetDestination(CKeyID ID)
         {
             codeBytes.Clear();
             AddOp(opcodetype.OP_DUP);
             AddOp(opcodetype.OP_HASH160);
             AddHash(ID);
-            AddOp(opcodetype.OP_EQUAL);
+            AddOp(opcodetype.OP_EQUALVERIFY);
+            AddOp(opcodetype.OP_CHECKSIG);
         }
 
+        /// <summary>
+        /// Set pay-to-scripthash destination
+        /// </summary>
+        /// <param name="ID">Script hash</param>
         public void SetDestination(CScriptID ID)
         {
             codeBytes.Clear();
@@ -381,6 +401,11 @@ namespace Novacoin
             codeBytes.Clear();
         }
 
+        /// <summary>
+        /// Set multisig destination.
+        /// </summary>
+        /// <param name="nRequired">Amount of required signatures.</param>
+        /// <param name="keys">Set of public keys.</param>
         public void SetMultiSig(int nRequired, IEnumerable<CPubKey> keys)
         {
             codeBytes.Clear();
index 71c7741..4459f0a 100644 (file)
@@ -112,11 +112,33 @@ namespace NovacoinTest
             txnouttype typeRet;
             IList<IEnumerable<byte>> solutions;
 
-            Console.WriteLine("scriptPubKey solved: {0}", ScriptCode.Solver(scriptPubKey, out typeRet, out solutions));
-            Console.WriteLine("scriptPubKey address: {0}", new CPubKey(solutions.First()).KeyID.ToString());
+            Console.WriteLine("\nscriptPubKey solved: {0}", ScriptCode.Solver(scriptPubKey, out typeRet, out solutions));
+            Console.WriteLine("scriptPubKey address: {0}\n", new CPubKey(solutions.First()).KeyID.ToString());
 
             Console.WriteLine("scriptPubKeyHash solved: {0}", ScriptCode.Solver(scriptPubKeyHash, out typeRet, out solutions));
-            Console.WriteLine("scriptPubKeyHash address: {0}", new CKeyID(new Hash160(solutions.First())).ToString());
+            Console.WriteLine("scriptPubKeyHash address: {0}\n", new CKeyID(new Hash160(solutions.First())).ToString());
+
+            /// Some SetDestination tests
+            CScript scriptDestinationTest = new CScript();
+
+            
+            Console.WriteLine("Creating and decoding new destination with {0} as public key.\n", keyPair1.PubKey.ToString());
+
+            Console.WriteLine("Pay-to-Pubkey:");
+
+            scriptDestinationTest.SetDestination(keyPair1.PubKey);
+
+            Console.WriteLine("\tscriptDestinationTest solved: {0}", ScriptCode.Solver(scriptDestinationTest, out typeRet, out solutions));
+            Console.WriteLine("\tscriptDestinationTest address: {0}\n", new CPubKey(solutions.First()).KeyID.ToString());
+
+            Console.WriteLine("Pay-to-PubkeyHash:");
+
+            scriptDestinationTest.SetDestination(keyPair1.PubKey.KeyID);
+
+            Console.WriteLine("\tscriptDestinationTest solved: {0}", ScriptCode.Solver(scriptDestinationTest, out typeRet, out solutions));
+            Console.WriteLine("\tscriptDestinationTest address: {0}\n", new CKeyID(new Hash160(solutions.First())).ToString());
+
+
 
             Console.ReadLine();
         }