SigHash tests
authorCryptoManiac <balthazar.ad@gmail.com>
Fri, 21 Aug 2015 07:31:47 +0000 (10:31 +0300)
committerCryptoManiac <balthazar.ad@gmail.com>
Fri, 21 Aug 2015 07:31:47 +0000 (10:31 +0300)
Not verified yet, though.

Novacoin/Interop.cs
Novacoin/ScriptCode.cs
NovacoinTest/Program.cs

index f0a05cf..f9766f4 100644 (file)
@@ -115,13 +115,18 @@ namespace Novacoin
             return BitConverter.ToUInt64(bytes, 0);
         }
 
-        public static IEnumerable<byte> ParseHex(string hex)
+        public static IEnumerable<byte> HexToEnumerable(string hex)
         {
             return Enumerable.Range(0, hex.Length)
                              .Where(x => x % 2 == 0)
                              .Select(x => Convert.ToByte(hex.Substring(x, 2), 16));
         }
 
+        public static IList<byte> HexToList(string hex)
+        {
+            return HexToEnumerable(hex).ToList();
+        }
+
         public static string ToHex(IEnumerable<byte> bytes)
         {
             StringBuilder sb = new StringBuilder();
index 6f50fc0..c5b92a3 100644 (file)
@@ -180,6 +180,19 @@ namespace Novacoin
 
     public static class ScriptCode
     {
+        public static string GetTxnOutputType(txnouttype t)
+        {
+            switch (t)
+            {
+                case txnouttype.TX_NONSTANDARD: return "nonstandard";
+                case txnouttype.TX_PUBKEY: return "pubkey";
+                case txnouttype.TX_PUBKEYHASH: return "pubkeyhash";
+                case txnouttype.TX_SCRIPTHASH: return "scripthash";
+                case txnouttype.TX_MULTISIG: return "multisig";
+                case txnouttype.TX_NULL_DATA: return "nulldata";
+            }
+            return string.Empty;
+        }
 
         /// <summary>
         /// Get the name of supplied opcode
@@ -623,7 +636,12 @@ namespace Novacoin
             return -1;
         }
 
-
+        /// <summary>
+        /// Is it a standart type of scriptPubKey?
+        /// </summary>
+        /// <param name="scriptPubKey">CScript instance</param>
+        /// <param name="whichType">utut type</param>
+        /// <returns>Checking result</returns>
         public static bool IsStandard(CScript scriptPubKey, out txnouttype whichType)
         {
             IList<IEnumerable<byte>> solutions = new List<IEnumerable<byte>>();
@@ -636,6 +654,7 @@ namespace Novacoin
 
             if (whichType == txnouttype.TX_MULTISIG)
             {
+                // Additional verification of OP_CHECKMULTISIG arguments
                 byte m = solutions.First().First();
                 byte n = solutions.Last().First();
 
@@ -858,7 +877,7 @@ namespace Novacoin
             if ((nHashType & 0x1f) == (int)sigflag.SIGHASH_NONE)
             {
                 // Wildcard payee
-                txTmp.vout = null;
+                txTmp.vout = new CTxOut[0];
 
                 // Let the others update at will
                 for (int i = 0; i < txTmp.vin.Length; i++)
index ed0213a..9fce6e0 100644 (file)
@@ -17,20 +17,20 @@ namespace NovacoinTest
 
             string strCoinbaseTx = "010000002926d155010000000000000000000000000000000000000000000000000000000000000000ffffffff27030cff02062f503253482f042926d155081ffffffdf60100000d2f6e6f64655374726174756d2f0000000003c04d6a00000000002321021ad6ae76a602310e86957d4ca752c81a8725f142fd2fc40f6a7fc2310bb2c749acd89e0100000000001976a914ecf809f1ec0ba4faa909d5175e405902a21282be88aca81b0000000000001976a91422851477d63a085dbc2398c8430af1c09e7343f688ac00000000";
 
-            CTransaction tx = new CTransaction(Interop.ParseHex(strUserTx).ToList());
-            CTransaction txCoinbase = new CTransaction(Interop.ParseHex(strCoinbaseTx).ToList());
+            CTransaction tx = new CTransaction(Interop.HexToList(strUserTx));
+            CTransaction txCoinbase = new CTransaction(Interop.HexToList(strCoinbaseTx));
 
             Console.WriteLine("User TX:{0}\n", tx.ToString());
             Console.WriteLine("Coinbase TX: {0}\n", txCoinbase.ToString());
 
             /// Block encoding/decoding tests
             string strBlock1 = "0600000086e539d77573abc0d81feb7896e1aef41a866001bc78bd24f5fe1a0000000000f5822cea59d999f37d896f66899c86e01e764ed6014706f3ceb58281ed55d0e55ab7d155ada3001d0000000005010000005ab7d155010000000000000000000000000000000000000000000000000000000000000000ffffffff0e0363ff02026d05062f503253482fffffffff0100000000000000000000000000010000005ab7d15501a768f8ed022f4080e3c8866bbe8292c7610b826cd467c49a06a1d0ff2ef7cdd6000000006b483045022100dce689d8cda64ebaffd6b96321952f16df34494256c58d2fd83069db7bce40e5022016020f55dc747d845d2057547c650412aa27d7d628e72238579f72e572dafdfe012102916e12c72a41913a5307bf7477db80dd499ea20f1a6bd99a2bdae6229f5aa093ffffffff03000000000000000000d0f1440300000000232102916e12c72a41913a5307bf7477db80dd499ea20f1a6bd99a2bdae6229f5aa093acc23f450300000000232102916e12c72a41913a5307bf7477db80dd499ea20f1a6bd99a2bdae6229f5aa093ac000000000100000091b4d15502c252c9130b1fd1dc8ef59cdb550ed398c4fe12c7ebf3eb917076bbda039b769d010000004847304402204bee0faac004364cdf6483d492333d00ad6f7c925faa3750fef2c79a9065a28102204a5e2b970f776ea1af2c2c03e36e6381d3d69b529d90b512363ae44815a321c601ffffffffc252c9130b1fd1dc8ef59cdb550ed398c4fe12c7ebf3eb917076bbda039b769d02000000494830450221008bf152a838f6f14f0ed1b2afc27821717e43a528e27aec3569ab42fc82f468aa02202cf6c962ef97db6e5ba32ccdd235afdc9a3cbb7907bfe879f8109446485d66dc01ffffffff0116467210000000001976a914edbf189bece45d4afa9848276e949183936bf6a488ac000000000100000017b5d1550229c74fb0004d45fba5baaefed1d9c229a8f1c85c36590cedf3ce6635335963d5000000006a4730440220319a4dfcf1607682d493c6d90087dc35d778a8bfcebe3549bae0af69e8daecb902206e6622367be30d9ccd4fdd27ed09c2fbcc9e5c858b26dfcdd927a8aba637b327012103b103f5d7e9717bc37cc99984b23babc3fff4677728be6b9c1847f6ce78e557f5ffffffff24b91fa6e9c160cc8da306e485942ee76137117aa8adecf531f6af1aef4e9b680000000049483045022100c9b311b7a7f5adeb0e72f962fb81b4cc1d105e32cfd7b1a7641a0fcc014d67c50220527161371a17301448bae87a26df201598b46d00ff452893177e9aed665c357c01ffffffff028e380000000000001976a91400afc350f81916a642a88b5ce8f73508663b531188ac67f46b00000000001976a91420c10f267f55ff4e05a083a8e1f4e882fbca1f4988ac0000000001000000efb6d15501626835db281e1fe6271620b8f67999f2174bb96df0eb3935fc99771e4ff45acf000000006a47304402206c34deb9c07c5477c47d398eaf91dbdf74aff5229c448e82ed0c1d8e2ee30e2d02203fe609434844b3eee21e747e313bcbf98efa4326727db6d2efba7bb627d2e0ce0121030c86c72f59c66824297aa78e433fe7057fd064e03e44c62ec49201ee0184149bffffffff028be30300000000001976a91481fc5cfb7f41afb3baf4138626022b3081b84e1788ac6abd0000000000001976a91499346dcd8ddfa10326697d5387b7df765004f4e388ac0000000046304402205189911c97354edb2965b4a119e6d76281f4c5da8fcead19c97bf6bcc9990fe102200f56d9dd967b036627b32b1e3ef2f819deaaafcc3244332472df7acfe19f1aa5";
-            CBlock b1 = new CBlock(Interop.ParseHex(strBlock1).ToList());
+            CBlock b1 = new CBlock(Interop.HexToList(strBlock1));
 
             string strBlock1Bytes = Interop.ToHex(b1.Bytes);
 
             string strBlock2 = "06000000eb5ab262c7382e7e009ad0b65c707131b8b6b846f8920a1a6697d929203a22f70e8cbd6bee1c0519a9d06b749b5eb6e599c154b12b732170807e603b6c326abbe0b7d15560e2211b15085b8f0101000000e0b7d155010000000000000000000000000000000000000000000000000000000000000000ffffffff270364ff02062f503253482f04c7b7d15508300000032b0000000d2f6e6f64655374726174756d2f0000000002f87d6b000000000023210287753c456abfc248d1bd155f44742d2ea72a2f29a5290c815fea0e9c55c4e2d0ac488a0000000000001976a914276cdbe21aaab75d58e151e01efea2860d3ef3d088ac0000000000";
-            CBlock b2 = new CBlock(Interop.ParseHex(strBlock2).ToList());
+            CBlock b2 = new CBlock(Interop.HexToList(strBlock2));
 
             string strBlock2Bytes = Interop.ToHex(b2.Bytes);
 
@@ -89,7 +89,7 @@ namespace NovacoinTest
             /// Donation address
 
             string strPubKeyTest = "029780fac8b85b4a47a616acb4e19d7958eaf02acc5123f65e7824ce720b1ae788";
-            CPubKey pubKeyTest = new CPubKey(Interop.ParseHex(strPubKeyTest));
+            CPubKey pubKeyTest = new CPubKey(Interop.HexToEnumerable(strPubKeyTest));
             string strDonationAddress = pubKeyTest.KeyID.ToString();
             Console.WriteLine("\nDonations may be sent to: {0}", strDonationAddress);
             Console.WriteLine("Address generation is OK: {0}", strDonationAddress == "4T2t8uiDtyHceMwMjMHPn88TyJB3trCg3o");
@@ -106,8 +106,8 @@ namespace NovacoinTest
             Console.WriteLine("\nblock1 header hash: {0}", scryptHash.ToString());
 
             /// Solver tests
-            CScript scriptPubKey = new CScript(Interop.ParseHex("21021ad6ae76a602310e86957d4ca752c81a8725f142fd2fc40f6a7fc2310bb2c749ac"));
-            CScript scriptPubKeyHash = new CScript(Interop.ParseHex("76a914edbf189bece45d4afa9848276e949183936bf6a488ac"));
+            CScript scriptPubKey = new CScript(Interop.HexToEnumerable("21021ad6ae76a602310e86957d4ca752c81a8725f142fd2fc40f6a7fc2310bb2c749ac"));
+            CScript scriptPubKeyHash = new CScript(Interop.HexToEnumerable("76a914edbf189bece45d4afa9848276e949183936bf6a488ac"));
 
             txnouttype typeRet;
             IList<IEnumerable<byte>> solutions;
@@ -167,7 +167,18 @@ namespace NovacoinTest
             Console.WriteLine("\tscriptP2SHTest solved: {0}", ScriptCode.Solver(scriptP2SHTest, out typeRet, out solutions));
             Console.WriteLine("\tscriptP2SHTest address: {0}\n", new CScriptID(new Hash160(solutions.First())).ToString());
 
+            // SignatureHash tests
+            CTransaction txS = new CTransaction(Interop.HexToEnumerable("01000000ccfe9e550d083902781746c80954e3af56e930235befb798f987667021a2f32dc0099499cd010000006b483045022100b5f6783af4f7f60866c889fd668c93ee110ecc3751208fe0b49cc7ace47e52e8022075652e1e960a50b27436ab04f2728b3bba09d07a858691559d99c1ac5dd74f16012103fe065856d7fa8cd41d0047600af2ec1ebe8c6198c1a889e90d8ce6b2f1f8afd7ffffffff2c6009a7494f38f7797bb9ef2aeafb093ae433208171a504367373df4164399d010000004847304402205f1b74bbc37219918f3de13ff645ecc7093512fecda4fcbcac2174c44144361102202149f1adcfcd473ec8a662b5b166b600208d92596e30b33fb402b4720bac3da101ffffffffbf1dd11394d2c0d3cbd4e0b56c74e7463ed5a19f22fe219ee700c61f834b3948010000006a473044022004824a9e071c40707e5309e510fd2cc105fd430504ceefce48aeed1c8fcf3bd7022023f4a43c58e4012284d8df25b55940199d19d4ca664053e2d5c1cc93ef441c3c012103fe065856d7fa8cd41d0047600af2ec1ebe8c6198c1a889e90d8ce6b2f1f8afd7ffffffffea06d18d8034a3645a8d5da75ed5c5f68a9dd09a798a876bef5d2cc9db8819390100000048473044022018e016973d87a53d6f14ae9929aa3c426d3d3a76eb81b3f1e996f0ec24ebacb302203668f165e6e9d5818eb3d108d23e2390213a6921ddfd51dbfca4ffebad73029601ffffffff5bda9a2a98debbda4ddad400a340190fcba4f4b3268f0a9d88eb5541bd7dadfc0100000049483045022100c4d210a6cd3edc6bc9cbfee1a8506ff239ef60baf7ebd46ffefb43e20a575d6c022019ea10cf480dadbb6332a03a404a3991437ffc9fef044c07112e2d15f3de74de01ffffffff5bda9a2a98debbda4ddad400a340190fcba4f4b3268f0a9d88eb5541bd7dadfc020000004948304502210084f5781ff88c201caca29b724f89fad5d72320a578239a3a2834ba669ea92b7e02207651ef9f7c60c2cc4fe187c98587252f3196fb1c31ed8f6c1f1f41e9a90d75ff01ffffffff61a9d75745092786bcbd48cc5860845beea607b8994790e9734f9fa68951bb66010000006b483045022100d2d3f925472970b9a0d365a120a9a6c9b7b0b3b3aacedaa40532397c6252da2f02206939d3cade4bada339799a4d651a7a33cb381640f6acef5fbecbe55ae1fa2364012103fe065856d7fa8cd41d0047600af2ec1ebe8c6198c1a889e90d8ce6b2f1f8afd7ffffffff635c711ba32bd587d349521475d2bd133a402c178543183c027cc1414a7837500100000049483045022100aece1ca9d902eaece08ec9704005196046f3a0b6f561cd17e9b09c01fed1447602207e68e21be4fcb895f045337741b42f43d218bb5c681f8e2eac5f9f3ffc8caf8301ffffffff7ddd7385fd7b81f19ccaa6ccd629bd2ab2a0af7ca69831c6dbb3b02c31de95db0100000049483045022100b260f2065dea407006e424d3cbb20009c807f422cee4ac8fb3553e4a81d62a7702204c67c99e792542cfe19a6956b101b4fd754a01fb1538b54e5f2141210729f09b01ffffffff8faebe377bc4211e41bd7e4a551e1de530040f8a55797f82e12d4d3c6a0b9fff010000004847304402204637911286c073fa0a8211e8427a6c63201bdac73e7b2760d3d9c7d748c9267c02205df709fdd06e3fb600ab81a17a1becc829769f1cb117b1520755d4f2a38429f001fffffffff1be969005bfcab4bcdaf835470680e2a309290b97d79fe63f7cbe904560b2d601000000484730440220352ad1a1ea5d92ddc13b7507a05180574c7309822f684ecf7321b7e925e5104302201e6a06e2f2d05a3d665cc6180fafacb658514a2f1bb632de99e88e4c26149e2501fffffffffa2019204a766fb4614be3d12bfb1ab35ad756e144193249e83660ca78898b3b0200000048473044022024f21eaf955291a9aec2cd45f42add62d8a30626aa9246664226fb3b56bf632f02205a3b46ec2857fec2fcb73663a57f99e4fcda328e8f1283198e8e9c5b4a2a3e0f01ffffffffd25e023fbdcd571ae346bf7aa142f5e32ca1aec23adae314ee209af22572cf1f000000006a4730440220551627592cbb7d970222a4d57a32aed50f1e93e81ae69958f26e56ca3b561715022019b12e560ff31013d0941ca2100ecdf9a3c3602b5c76b83d3b3c87d723d32ce3012103fe065856d7fa8cd41d0047600af2ec1ebe8c6198c1a889e90d8ce6b2f1f8afd7ffffffff02402d0000000000001976a91479f1d300be0da277e7ae217e99c6cc8a4f8717fe88ac00943577000000001976a914cbc5a055ae068d34b4a93e4c9adb9cb10262ae4f88ac00000000").ToList());
 
+            Hash256 sigHashAll = ScriptCode.SignatureHash(txS.vout[0].scriptPubKey, txS, 1, (int)sigflag.SIGHASH_ALL);
+            Hash256 sigHashNone = ScriptCode.SignatureHash(txS.vout[0].scriptPubKey, txS, 1, (int)sigflag.SIGHASH_NONE);
+            Hash256 sigHashSingle = ScriptCode.SignatureHash(txS.vout[0].scriptPubKey, txS, 1, (int)sigflag.SIGHASH_SINGLE);
+            Hash256 sigHashAnyone = ScriptCode.SignatureHash(txS.vout[0].scriptPubKey, txS, 1, (int)sigflag.SIGHASH_ANYONECANPAY);
+
+            Console.WriteLine("sigHashAll={0}", sigHashAll.ToString());
+            Console.WriteLine("sigHashNone={0}", sigHashNone.ToString());
+            Console.WriteLine("sigHashSingle={0}", sigHashSingle.ToString());
+            Console.WriteLine("sigHashAnyone={0}", sigHashAnyone.ToString());
 
             Console.ReadLine();
         }