public void GenerateTestVectorsTest() { var tests = TestCase.read_json("data/bip158_vectors.json"); foreach (var test in tests.Skip(1)) { var i = 0; var testBlockHeight = test[i++]; var testBlockHash = uint256.Parse((string)test[i++]); var testBlock = Block.Parse((string)test[i++]); var testPreviousBasicHeader = uint256.Parse((string)test[i++]); var testPreviousExtHeader = uint256.Parse((string)test[i++]); var testBasicFilter = (string)test[i++]; var testExtFilter = (string)test[i++]; var testBasicHeader = (string)test[i++]; var testExtHeader = (string)test[i++]; var message = (string)test[i++]; var basicFilter = GolombRiceFilterBuilder.BuildBasicFilter(testBlock); Assert.Equal(testBasicFilter, basicFilter.ToString()); Assert.Equal(testBasicHeader, basicFilter.GetHeader(testPreviousBasicHeader).ToString()); var extFilter = GolombRiceFilterBuilder.BuildExtendedFilter(testBlock); Assert.Equal(testExtFilter, extFilter.ToString()); Assert.Equal(testExtHeader, extFilter.GetHeader(testPreviousExtHeader).ToString()); var deserializedBasicFilter = GolombRiceFilter.Parse(testBasicFilter); Assert.Equal(testBasicFilter, deserializedBasicFilter.ToString()); var deserializedExtFilter = GolombRiceFilter.Parse(testExtFilter); Assert.Equal(testExtFilter, deserializedExtFilter.ToString()); } }
public void base58_DecodeBase58() { var tests = TestCase.read_json("Data\\base58_encode_decode.json"); byte[] result = null; foreach (var test in tests) { var strTest = test.ToString(); if (test.Count < 2) // Allow for extra stuff (useful for comments) { Assert.False(true, "Bad test: " + strTest); continue; } var expected = Encoders.Hex.DecodeData(test.GetValue <string>(0)); var base58string = test.GetValue <string>(1); result = Encoders.Base58.DecodeData(base58string); AssertEx.CollectionEquals(result, expected); } Assert.Throws <FormatException>(() => Encoders.Base58.DecodeData("invalid")); // check that DecodeBase58 skips whitespace, but still fails with unexpected non-whitespace at the end. Assert.Throws <FormatException>(() => Encoders.Base58.DecodeData(" \t\n\v\f\r skip \r\f\v\n\t a")); result = Encoders.Base58.DecodeData(" \t\n\v\f\r skip \r\f\v\n\t "); var expected2 = Encoders.Hex.DecodeData("971a55"); AssertEx.CollectionEquals(result, expected2); }
public void tx_valid() { // Read tests from test/data/tx_valid.json // Format is an array of arrays // Inner arrays are either [ "comment" ] // or [[[prevout hash, prevout index, prevout scriptPubKey], [input 2], ...],"], serializedTransaction, enforceP2SH // ... where all scripts are stringified scripts. var tests = TestCase.read_json("data/tx_valid.json"); foreach (var test in tests) { string strTest = test.ToString(); //Skip comments if (!(test[0] is JArray)) { continue; } JArray inputs = (JArray)test[0]; if (test.Count != 3 || !(test[1] is string) || !(test[2] is string)) { Assert.False(true, "Bad test: " + strTest); continue; } Dictionary <OutPoint, Script> mapprevOutScriptPubKeys = new Dictionary <OutPoint, Script>(); foreach (var vinput in inputs) { mapprevOutScriptPubKeys[new OutPoint(new uint256(vinput[0].ToString()), int.Parse(vinput[1].ToString()))] = script_tests.ParseScript(vinput[2].ToString()); } Transaction tx = new Transaction((string)test[1]); ValidationState state = Network.Main.CreateValidationState(); Assert.True(state.CheckTransaction(tx), strTest); Assert.True(state.IsValid); for (int i = 0; i < tx.Inputs.Count; i++) { if (!mapprevOutScriptPubKeys.ContainsKey(tx.Inputs[i].PrevOut)) { Assert.False(true, "Bad test: " + strTest); continue; } var valid = Script.VerifyScript( tx.Inputs[i].ScriptSig, mapprevOutScriptPubKeys[tx.Inputs[i].PrevOut], tx, i, ParseFlags(test[2].ToString()) , 0); Assert.True(valid, strTest + " failed"); } } }
public void sighash_from_data() { var tests = TestCase.read_json("Data/sighash.json"); foreach (var test in tests) { var strTest = test.ToString(); if (test.Count < 1) // Allow for extra stuff (useful for comments) { Assert.True(false, "Bad test: " + strTest); continue; } if (test.Count == 1) { continue; // comment } string raw_tx, raw_script, sigHashHex; int nIn, nHashType; Transaction tx = new Transaction(); Script scriptCode = new Script(); // deserialize test data raw_tx = (string)test[0]; raw_script = (string)test[1]; nIn = (int)(long)test[2]; nHashType = (int)(long)test[3]; sigHashHex = (string)test[4]; tx.ReadWrite(ParseHex(raw_tx)); ValidationState state = Network.Main.CreateValidationState(); Assert.True(state.CheckTransaction(tx), strTest); Assert.True(state.IsValid); var raw = ParseHex(raw_script); scriptCode = new Script(raw); var sh = scriptCode.SignatureHash(tx, nIn, (SigHash)nHashType); Assert.True(sh.ToString() == sigHashHex, strTest); } }
public void script_invalid() { var tests = TestCase.read_json("data/script_invalid.json"); foreach (var test in tests) { var comment = test.Count == 3 ? (string)test[2] : "no comment"; var scriptSig = ParseScript((string)test[0]); var scriptPubKey = ParseScript((string)test[1]); Assert.Equal(scriptSig.ToString(), new Script(scriptSig.ToString()).ToString()); Assert.Equal(scriptPubKey.ToString(), new Script(scriptPubKey.ToString()).ToString()); var transaction = new Transaction(); Assert.True(!Script.VerifyScript(scriptSig, scriptPubKey, transaction, 0, flags, SigHash.None), "Test : " + test.Index + " " + comment); } }
public void CanDecodeAndEncodeRawTransaction() { var tests = TestCase.read_json("data/tx_raw.json"); foreach (var test in tests) { var format = (RawFormat)Enum.Parse(typeof(RawFormat), (string)test[0], true); var network = ((string)test[1]) == "Main" ? Network.Main : Network.TestNet; var testData = ((JObject)test[2]).ToString(); Transaction raw = Transaction.Parse(testData, format, network); AssertJsonEquals(raw.ToString(format, network), testData); var raw3 = Transaction.Parse(raw.ToString(format, network), format); Assert.Equal(raw.ToString(format, network), raw3.ToString(format, network)); } }
public void base58_keys_invalid() { var tests = TestCase.read_json("data/base58_keys_invalid.json"); // Negative testcases foreach (var test in tests) { string strTest = tests.ToString(); if (test.Count < 1) // Allow for extra stuff (useful for comments) { Assert.False(true, "Bad test: " + strTest); continue; } string exp_base58string = (string)test[0]; // must be invalid as public and as private key Assert.Throws <FormatException>(() => Network.Main.CreateBitcoinAddress(exp_base58string)); Assert.Throws <FormatException>(() => Network.Main.CreateBitcoinSecret(exp_base58string)); } }
public void base58_EncodeBase58() { var tests = TestCase.read_json("Data\\base58_encode_decode.json"); foreach (var test in tests) { var strTest = test.ToString(); if (test.Count < 2) // Allow for extra stuff (useful for comments) { Assert.False(true, "Bad test: " + strTest); continue; } var sourcedata = Encoders.Hex.DecodeData(test.GetValue <string>(0)); var base58string = test.GetValue <string>(1); Assert.True( Encoders.Base58.EncodeData(sourcedata) == base58string, strTest); } }
//Compare between new old implementation of signature in reference bitcoin. But NBitcoin is like the old one, so we don't care about this test //[Fact] //public void sighash_test() //{ // int nRandomTests = 50000; // for(int i = 0 ; i < nRandomTests ; i++) // { // int nHashType = rand.Next(); // Transaction txTo = RandomTransaction((nHashType & 0x1f) == SigHash.Single); // Script scriptCode = RandomScript(); // int nIn = rand.Next() % txTo.VIn.Length; // var sho = SignatureHashOld(scriptCode, txTo, nIn, nHashType); // var sh = scriptCode.SignatureHash(txTo, nIn, (SigHash)nHashType); // Assert.True(sh == sho); // } //} // Goal: check that SignatureHash generates correct hash //[Fact] //[Trait("Core", "Core")] public void sighash_from_data() { // test diabled for now as it requires specific test data var tests = TestCase.read_json(TestDataLocations.DataFolder(@"sighash.json")); foreach (var test in tests) { var strTest = test.ToString(); if (test.Count < 1) // Allow for extra stuff (useful for comments) { Assert.True(false, "Bad test: " + strTest); continue; } if (test.Count == 1) { continue; // comment } string raw_tx, raw_script, sigHashHex; int nIn, nHashType; Transaction tx = new Transaction(); Script scriptCode = new Script(); // deserialize test data raw_tx = (string)test[0]; raw_script = (string)test[1]; nIn = (int)(long)test[2]; nHashType = (int)(long)test[3]; sigHashHex = (string)test[4]; tx.ReadWrite(ParseHex(raw_tx)); var raw = ParseHex(raw_script); scriptCode = new Script(raw); var sh = Script.SignatureHash(scriptCode, tx, nIn, (SigHash)nHashType); Assert.True(sh.ToString() == sigHashHex, strTest); } }
public void script_valid() { var tests = TestCase.read_json("data/script_valid.json"); foreach (var test in tests) { if (test.Count == 1) { continue; } var comment = test.Count == 4 ? (string)test[3] : "no comment"; var flag = ParseFlag((string)test[2]); var scriptSig = ParseScript((string)test[0]); var scriptPubKey = ParseScript((string)test[1]); Assert.Equal(scriptSig.ToString(), new Script(scriptSig.ToString()).ToString()); Assert.Equal(scriptPubKey.ToString(), new Script(scriptPubKey.ToString()).ToString()); AssertVerifyScript(scriptSig, scriptPubKey, flag, test.Index, comment, true); } }
public void sighash_from_data() { TestCase[] tests = TestCase.read_json(TestDataLocations.GetFileFromDataFolder("sighash.json")); foreach (TestCase test in tests) { string strTest = test.ToString(); if (test.Count < 1) // Allow for extra stuff (useful for comments) { Assert.True(false, "Bad test: " + strTest); continue; } if (test.Count == 1) { continue; // comment } string raw_tx, raw_script, sigHashHex; int nIn, nHashType; var tx = new Transaction(); var scriptCode = new Script(); // deserialize test data raw_tx = (string)test[0]; raw_script = (string)test[1]; nIn = (int)(long)test[2]; nHashType = (int)(long)test[3]; sigHashHex = (string)test[4]; tx.ReadWrite(ParseHex(raw_tx), this.networkMain.Consensus.ConsensusFactory); byte[] raw = ParseHex(raw_script); scriptCode = new Script(raw); uint256 sh = Script.SignatureHash(KnownNetworks.Main, scriptCode, tx, nIn, (SigHash)nHashType); Assert.True(sh.ToString() == sigHashHex, strTest); } }
//http://brainwallet.org/#tx public void CanParseTransaction() { var tests = TestCase.read_json("data/can_parse_transaction.json"); foreach (var test in tests.Select(t => t.GetDynamic(0))) { string raw = test.Raw; Transaction tx = new Transaction(raw); Assert.Equal((int)test.JSON.vin_sz, tx.Inputs.Count); Assert.Equal((int)test.JSON.vout_sz, tx.Outputs.Count); Assert.Equal((uint)test.JSON.lock_time, tx.LockTime); for (int i = 0; i < tx.Inputs.Count; i++) { var actualVIn = tx.Inputs[i]; var expectedVIn = test.JSON.@in[i]; Assert.Equal(new uint256((string)expectedVIn.prev_out.hash), actualVIn.PrevOut.Hash); Assert.Equal((uint)expectedVIn.prev_out.n, actualVIn.PrevOut.N); if (expectedVIn.sequence != null) { Assert.Equal((uint)expectedVIn.sequence, actualVIn.Sequence); } Assert.Equal((string)expectedVIn.scriptSig, actualVIn.ScriptSig.ToString()); //Can parse the string Assert.Equal((string)expectedVIn.scriptSig, (string)expectedVIn.scriptSig.ToString()); } for (int i = 0; i < tx.Outputs.Count; i++) { var actualVOut = tx.Outputs[i]; var expectedVOut = test.JSON.@out[i]; Assert.Equal((string)expectedVOut.scriptPubKey, actualVOut.ScriptPubKey.ToString()); Assert.Equal(Money.Parse((string)expectedVOut.value), actualVOut.Value); } var hash = (string)test.JSON.hash; var expectedHash = new uint256(Encoders.Hex.DecodeData(hash), false); Assert.Equal(expectedHash, tx.GetHash()); } }
public void base58_keys_valid_parse() { TestCase[] tests = TestCase.read_json(TestDataLocations.GetFileFromDataFolder("base58_keys_valid.json")); Network network; foreach (TestCase test in tests) { string strTest = test.ToString(); if (test.Count < 3) // Allow for extra stuff (useful for comments) { Assert.True(false, "Bad test " + strTest); continue; } string exp_base58string = (string)test[0]; byte[] exp_payload = TestUtils.ParseHex((string)test[1]); //const Object &metadata = test[2].get_obj(); bool isPrivkey = (bool)test.GetDynamic(2).isPrivkey; bool isTestnet = (bool)test.GetDynamic(2).isTestnet; if (isTestnet) { network = KnownNetworks.TestNet; } else { network = KnownNetworks.Main; } if (isPrivkey) { bool isCompressed = (bool)test.GetDynamic(2).isCompressed; // Must be valid private key // Note: CBitcoinSecret::SetString tests isValid, whereas CBitcoinAddress does not! BitcoinSecret secret = network.CreateBitcoinSecret(exp_base58string); //If not valid exception would throw Key privkey = secret.PrivateKey; Assert.True(privkey.IsCompressed == isCompressed, "compressed mismatch:" + strTest); Assert.True(Utils.ArrayEqual(privkey.ToBytes(), exp_payload), "key mismatch:" + strTest); // Private key must be invalid public key Assert.Throws <FormatException>(() => network.CreateBitcoinAddress(exp_base58string)); } else { string exp_addrType = (string)test.GetDynamic(2).addrType; // "script" or "pubkey" // Must be valid public key BitcoinAddress addr = network.CreateBitcoinAddress(exp_base58string); Assert.True((addr is BitcoinScriptAddress) == (exp_addrType == "script"), "isScript mismatch" + strTest); if (exp_addrType == "script") { Assert.True(addr.GetType() == typeof(BitcoinScriptAddress)); } if (exp_addrType == "pubkey") { Assert.True(addr.GetType() == typeof(BitcoinPubKeyAddress)); } Assert.Throws <FormatException>(() => network.CreateBitcoinSecret(exp_base58string)); } } }
public void base58_keys_valid_gen() { TestCase[] tests = TestCase.read_json(TestDataLocations.GetFileFromDataFolder("base58_keys_valid.json")); tests = tests.Concat(TestCase.read_json(TestDataLocations.GetFileFromDataFolder("base58_keys_valid2.json"))).ToArray(); Network network = null; foreach (TestCase test in tests) { string strTest = test.ToString(); if (test.Count < 3) // Allow for extra stuff (useful for comments) { Assert.False(true, "Bad test: " + strTest); continue; } string exp_base58string = (string)test[0]; byte[] exp_payload = TestUtils.ParseHex((string)test[1]); dynamic metadata = test.GetDynamic(2); bool isPrivkey = (bool)metadata.isPrivkey; bool isTestnet = (bool)metadata.isTestnet; if (isTestnet) { network = KnownNetworks.TestNet; } else { network = KnownNetworks.Main; } if (isPrivkey) { bool isCompressed = metadata.isCompressed; var key = new Key(exp_payload, fCompressedIn: isCompressed); BitcoinSecret secret = network.CreateBitcoinSecret(key); Assert.True(secret.ToString() == exp_base58string, "result mismatch: " + strTest); } else { string exp_addrType = (string)metadata.addrType; TxDestination dest; if (exp_addrType == "pubkey") { dest = new KeyId(new uint160(exp_payload)); } else if (exp_addrType == "script") { dest = new ScriptId(new uint160(exp_payload)); } else if (exp_addrType == "p2wpkh") { dest = new WitKeyId(new uint160(exp_payload)); } else if (exp_addrType == "p2wsh") { dest = new WitScriptId(exp_payload); } else if (exp_addrType == "none") { continue; } else { Assert.True(false, "Bad addrtype: " + strTest); continue; } try { BitcoinAddress addrOut = dest.GetAddress(network); Assert.True(addrOut.ToString() == exp_base58string, "mismatch: " + strTest); Assert.True(addrOut.ScriptPubKey == dest.ScriptPubKey); Assert.True(dest.ScriptPubKey.GetDestination(KnownNetworks.Main) == dest); } catch (ArgumentException) { Assert.True(dest.GetType() == typeof(TxDestination)); } } } }
public void base58_keys_valid_gen() { var tests = TestCase.read_json("data/base58_keys_valid.json"); Network network = null; foreach (var test in tests) { string strTest = test.ToString(); if (test.Count < 3) // Allow for extra stuff (useful for comments) { Assert.False(true, "Bad test: " + strTest); continue; } string exp_base58string = (string)test[0]; byte[] exp_payload = TestUtils.ParseHex((string)test[1]); dynamic metadata = test.GetDynamic(2); bool isPrivkey = (bool)metadata.isPrivkey; bool isTestnet = (bool)metadata.isTestnet; if (isTestnet) { network = Network.TestNet; } else { network = Network.Main; } if (isPrivkey) { bool isCompressed = metadata.isCompressed; Key key = new Key(exp_payload, fCompressedIn: isCompressed); BitcoinSecret secret = network.CreateBitcoinSecret(key); Assert.True(secret.ToString() == exp_base58string, "result mismatch: " + strTest); } else { string exp_addrType = (string)metadata.addrType; TxDestination dest; if (exp_addrType == "pubkey") { dest = new KeyId(new uint160(exp_payload)); } else if (exp_addrType == "script") { dest = new ScriptId(new uint160(exp_payload)); } else if (exp_addrType == "none") { dest = new TxDestination(0); } else { Assert.True(false, "Bad addrtype: " + strTest); continue; } try { BitcoinAddress addrOut = network.CreateBitcoinAddress(dest); Assert.True(addrOut.ToString() == exp_base58string, "mismatch: " + strTest); } catch (ArgumentException) { Assert.True(dest.GetType() == typeof(TxDestination)); } } } // Visiting a CNoDestination must fail TxDestination nodest = new TxDestination(); Assert.Throws <ArgumentException>(() => network.CreateBitcoinAddress(nodest)); }