Multisig destination parsing example
[NovacoinLibrary.git] / NovacoinTest / Program.cs
index 01f9b9d..eca8cbd 100644 (file)
@@ -1,13 +1,12 @@
 \feffusing System;
-using System.Collections.Generic;
 using System.Linq;
 using System.Text;
-using System.Threading.Tasks;
 
 
 namespace NovacoinTest
 {
     using Novacoin;
+    using System.Collections.Generic;
 
     class Program
     {
@@ -28,26 +27,134 @@ namespace NovacoinTest
             string strBlock1 = "0600000086e539d77573abc0d81feb7896e1aef41a866001bc78bd24f5fe1a0000000000f5822cea59d999f37d896f66899c86e01e764ed6014706f3ceb58281ed55d0e55ab7d155ada3001d0000000005010000005ab7d155010000000000000000000000000000000000000000000000000000000000000000ffffffff0e0363ff02026d05062f503253482fffffffff0100000000000000000000000000010000005ab7d15501a768f8ed022f4080e3c8866bbe8292c7610b826cd467c49a06a1d0ff2ef7cdd6000000006b483045022100dce689d8cda64ebaffd6b96321952f16df34494256c58d2fd83069db7bce40e5022016020f55dc747d845d2057547c650412aa27d7d628e72238579f72e572dafdfe012102916e12c72a41913a5307bf7477db80dd499ea20f1a6bd99a2bdae6229f5aa093ffffffff03000000000000000000d0f1440300000000232102916e12c72a41913a5307bf7477db80dd499ea20f1a6bd99a2bdae6229f5aa093acc23f450300000000232102916e12c72a41913a5307bf7477db80dd499ea20f1a6bd99a2bdae6229f5aa093ac000000000100000091b4d15502c252c9130b1fd1dc8ef59cdb550ed398c4fe12c7ebf3eb917076bbda039b769d010000004847304402204bee0faac004364cdf6483d492333d00ad6f7c925faa3750fef2c79a9065a28102204a5e2b970f776ea1af2c2c03e36e6381d3d69b529d90b512363ae44815a321c601ffffffffc252c9130b1fd1dc8ef59cdb550ed398c4fe12c7ebf3eb917076bbda039b769d02000000494830450221008bf152a838f6f14f0ed1b2afc27821717e43a528e27aec3569ab42fc82f468aa02202cf6c962ef97db6e5ba32ccdd235afdc9a3cbb7907bfe879f8109446485d66dc01ffffffff0116467210000000001976a914edbf189bece45d4afa9848276e949183936bf6a488ac000000000100000017b5d1550229c74fb0004d45fba5baaefed1d9c229a8f1c85c36590cedf3ce6635335963d5000000006a4730440220319a4dfcf1607682d493c6d90087dc35d778a8bfcebe3549bae0af69e8daecb902206e6622367be30d9ccd4fdd27ed09c2fbcc9e5c858b26dfcdd927a8aba637b327012103b103f5d7e9717bc37cc99984b23babc3fff4677728be6b9c1847f6ce78e557f5ffffffff24b91fa6e9c160cc8da306e485942ee76137117aa8adecf531f6af1aef4e9b680000000049483045022100c9b311b7a7f5adeb0e72f962fb81b4cc1d105e32cfd7b1a7641a0fcc014d67c50220527161371a17301448bae87a26df201598b46d00ff452893177e9aed665c357c01ffffffff028e380000000000001976a91400afc350f81916a642a88b5ce8f73508663b531188ac67f46b00000000001976a91420c10f267f55ff4e05a083a8e1f4e882fbca1f4988ac0000000001000000efb6d15501626835db281e1fe6271620b8f67999f2174bb96df0eb3935fc99771e4ff45acf000000006a47304402206c34deb9c07c5477c47d398eaf91dbdf74aff5229c448e82ed0c1d8e2ee30e2d02203fe609434844b3eee21e747e313bcbf98efa4326727db6d2efba7bb627d2e0ce0121030c86c72f59c66824297aa78e433fe7057fd064e03e44c62ec49201ee0184149bffffffff028be30300000000001976a91481fc5cfb7f41afb3baf4138626022b3081b84e1788ac6abd0000000000001976a91499346dcd8ddfa10326697d5387b7df765004f4e388ac0000000046304402205189911c97354edb2965b4a119e6d76281f4c5da8fcead19c97bf6bcc9990fe102200f56d9dd967b036627b32b1e3ef2f819deaaafcc3244332472df7acfe19f1aa5";
             CBlock b1 = new CBlock(Interop.ParseHex(strBlock1).ToList());
 
-            string strBlock1Bytes = Interop.ToHex(b1.ToBytes());
+            string strBlock1Bytes = Interop.ToHex(b1.Bytes);
 
             string strBlock2 = "06000000eb5ab262c7382e7e009ad0b65c707131b8b6b846f8920a1a6697d929203a22f70e8cbd6bee1c0519a9d06b749b5eb6e599c154b12b732170807e603b6c326abbe0b7d15560e2211b15085b8f0101000000e0b7d155010000000000000000000000000000000000000000000000000000000000000000ffffffff270364ff02062f503253482f04c7b7d15508300000032b0000000d2f6e6f64655374726174756d2f0000000002f87d6b000000000023210287753c456abfc248d1bd155f44742d2ea72a2f29a5290c815fea0e9c55c4e2d0ac488a0000000000001976a914276cdbe21aaab75d58e151e01efea2860d3ef3d088ac0000000000";
             CBlock b2 = new CBlock(Interop.ParseHex(strBlock2).ToList());
 
-            string strBlock2Bytes = Interop.ToHex(b2.ToBytes());
+            string strBlock2Bytes = Interop.ToHex(b2.Bytes);
 
             Console.WriteLine(b1.ToString());
             Console.WriteLine("OK: {0}\n", strBlock1 == strBlock1Bytes);
 
             Console.WriteLine(b2.ToString());
-            Console.WriteLine("OK: {0}\n", strBlock2 == strBlock2Bytes);
+            Console.WriteLine("Reserialization is OK: {0}\n", strBlock2 == strBlock2Bytes);
 
             /// ECDSA keypair generation test
 
             CKeyPair keyPair1 = new CKeyPair();
-            CKeyPair keyPair2 = new CKeyPair(keyPair1.Secret);
-       
-            Console.WriteLine(keyPair1.ToString());
-            Console.WriteLine("OK: {0}\n", keyPair1.ToString() == keyPair2.ToString());
+            CKeyPair keyPair2 = new CKeyPair(keyPair1.SecretBytes);
+            CPubKey pubKey = keyPair2.PubKey;
+
+            string strPrivKeyBase58 = keyPair1.ToString();
+
+            Console.WriteLine("Privkey in Base58: {0}", strPrivKeyBase58);
+            Console.WriteLine("Privkey in Hex: {0}", keyPair1.ToHex());
+
+            CKeyPair keyPair3 = new CKeyPair(strPrivKeyBase58);
+            Console.WriteLine("Privkey base58 deserialization is OK: {0}", keyPair3.KeyID.ToString() == keyPair1.KeyID.ToString());
+
+            Console.WriteLine("Pubkey in Base58: {0}", pubKey.ToString());
+            Console.WriteLine("Pubkey in Hex: {0}", pubKey.ToHex());
+
+            Console.WriteLine("Reinitialization is OK: {0}\n", keyPair1.ToString() == keyPair2.ToString());
+
+            /// Address generation test
+
+            CKeyID keyID = keyPair1.KeyID;
+            Console.WriteLine("Key ID: {0}", Interop.ToHex(keyID.hashBytes));
+            Console.WriteLine("Novacoin address: {0}\n", keyID.ToString());
+
+            /// Privkey deserialization test
+            CKeyPair keyPair4 = new CKeyPair("MEP3qCtFGmWo3Gurf8fMnUNaDHGNf637DqjoeG8rKium2jSj51sf");
+            Console.WriteLine("\nHard-coded privkey in Hex: {0}", keyPair4.ToHex());
+            Console.WriteLine("Hard-Coded privkey address: {0}", keyPair4.KeyID.ToString());
+            Console.WriteLine("Hard-Coded privkey: {0}\n", keyPair4.ToString());
+
+            // Privkey hex deserialization test
+            CKeyPair keyPair5 = new CKeyPair(keyPair4.SecretBytes.ToArray());
+            Console.WriteLine("Decoded privkey in Hex: {0}", keyPair5.ToHex());
+            Console.WriteLine("Decoded privkey address: {0}\n", keyPair5.KeyID.ToString());
+
+            /// ECDSA keypair signing test
+
+            string data = "Превед!";
+            byte[] dataBytes = Encoding.UTF8.GetBytes(data);
+            byte[] signature = keyPair1.Sign(dataBytes).ToArray();
+
+            Console.WriteLine("Signature: {0}", Interop.ToHex(signature));
+            Console.WriteLine("Signature is OK: {0} (CKeyPair)", keyPair1.VerifySignature(dataBytes, signature));
+            Console.WriteLine("Signature is OK: {0} (CPubKey)", pubKey.VerifySignature(dataBytes, signature));
+
+            /// Donation address
+
+            string strPubKeyTest = "029780fac8b85b4a47a616acb4e19d7958eaf02acc5123f65e7824ce720b1ae788";
+            CPubKey pubKeyTest = new CPubKey(Interop.ParseHex(strPubKeyTest));
+            string strDonationAddress = pubKeyTest.KeyID.ToString();
+            Console.WriteLine("\nDonations may be sent to: {0}", strDonationAddress);
+            Console.WriteLine("Address generation is OK: {0}", strDonationAddress == "4T2t8uiDtyHceMwMjMHPn88TyJB3trCg3o");
+
+            /// Address deserialization test
+
+            CNovacoinAddress donationAddress = new CNovacoinAddress(strDonationAddress);
+            Console.WriteLine("Address reserialization is OK: {0}", donationAddress.ToString() == pubKeyTest.KeyID.ToString());
+
+            /// Block header hashing test
+            IEnumerable<byte> dataBytesForScrypt = b1.header.Bytes;
+            ScryptHash256 scryptHash = ScryptHash256.Compute256(dataBytesForScrypt);
+
+            Console.WriteLine("\nblock1 header hash: {0}", scryptHash.ToString());
+
+            /// Solver tests
+            CScript scriptPubKey = new CScript(Interop.ParseHex("21021ad6ae76a602310e86957d4ca752c81a8725f142fd2fc40f6a7fc2310bb2c749ac"));
+            CScript scriptPubKeyHash = new CScript(Interop.ParseHex("76a914edbf189bece45d4afa9848276e949183936bf6a488ac"));
+
+            txnouttype typeRet;
+            IList<IEnumerable<byte>> solutions;
+
+            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}\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.WriteLine("Multisig with three random keys:");
+
+            CKeyPair k1 = new CKeyPair(), k2 = new CKeyPair(), k3 = new CKeyPair();
+
+            scriptDestinationTest.SetMultiSig(2, new CPubKey[] { k1.PubKey, k2.PubKey, k3.PubKey });
+            Console.WriteLine("\tscriptDestinationTest solved: {0}", ScriptCode.Solver(scriptDestinationTest, out typeRet, out solutions));
+            Console.WriteLine("\tscriptDestinationTest addresses: \n");
+
+            int nRequired = solutions.First().First();
+            int nKeys = solutions.Last().First();
+
+            foreach (IEnumerable<byte> keyBytes in solutions.Skip(1).Take(nKeys))
+            {
+                Console.WriteLine("\t\t{0}", (new CPubKey(keyBytes)).KeyID.ToString());
+            }
+
+            Console.WriteLine("\n\tnRequired={0}", nRequired);
 
             Console.ReadLine();
         }