public void CanCalculateMerkleRoot() { var block = new Block(); block.ReadWrite(Encoders.Hex.DecodeData(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("block169482.txt")))); Assert.Equal(block.Header.HashMerkleRoot, block.GetMerkleRoot().Hash); }
public void CanCalculateMerkleRoot() { Block block = Network.Main.CreateBlock(); block.ReadWrite(Encoders.Hex.DecodeData(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("block169482.txt"))), Network.Main.Consensus.ConsensusFactory); Assert.Equal(block.Header.HashMerkleRoot, block.GetMerkleRoot().Hash); }
public void DeterministicSignatureTestVectors() { foreach (DeterministicSigTest test in ParseTestsDump(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("determiniticECDSA.txt")))) { TestSig(test); } }
public void CanCalculateDifficulty() { var main = new ConcurrentChain(LoadMainChain()); var histories = File.ReadAllText(TestDataLocations.GetFileFromDataFolder("targethistory.csv")).Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries); foreach (var history in histories) { var height = int.Parse(history.Split(',')[0]); var expectedTarget = new Target(new BouncyCastle.Math.BigInteger(history.Split(',')[1], 10)); var block = main.GetBlock(height).Header; Assert.Equal(expectedTarget, block.Bits); var target = main.GetWorkRequired(Network.Main, height); Assert.Equal(expectedTarget, target); } }
public void CanCalculateDifficulty() { var main = new ChainIndexer(this.network).Load(this.LoadMainChain()); // The state of the line separators may be affected by copy operations - so do an environment independent line split... string[] histories = File.ReadAllText(TestDataLocations.GetFileFromDataFolder("targethistory.csv")).Split(new string[] { "\r\n", "\r", "\n" }, StringSplitOptions.RemoveEmptyEntries); foreach (string history in histories) { int height = int.Parse(history.Split(',')[0]); var expectedTarget = new Target(new BouncyCastle.Math.BigInteger(history.Split(',')[1], 10)); BlockHeader block = main.GetHeader(height).Header; Assert.Equal(expectedTarget, block.Bits); Target target = main.GetHeader(height).GetWorkRequired(network); Assert.Equal(expectedTarget, target); } }
public void JapTest() { JArray test = JArray.Parse(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("bip39_JP.json"), Encoding.UTF32)); foreach (JObject unitTest in test.OfType <JObject>()) { byte[] entropy = Encoders.Hex.DecodeData(unitTest["entropy"].ToString()); string mnemonicStr = unitTest["mnemonic"].ToString(); string seed = unitTest["seed"].ToString(); string passphrase = unitTest["passphrase"].ToString(); var mnemonic = new Mnemonic(mnemonicStr, Wordlist.Japanese); Assert.True(mnemonic.IsValidChecksum); Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed(passphrase))); string bip32 = unitTest["bip32_xprv"].ToString(); string bip32Actual = mnemonic.DeriveExtKey(passphrase).ToString(KnownNetworks.Main); Assert.Equal(bip32, bip32Actual.ToString()); mnemonic = new Mnemonic(Wordlist.Japanese, entropy); Assert.True(mnemonic.IsValidChecksum); bip32Actual = mnemonic.DeriveExtKey(passphrase).ToString(KnownNetworks.Main); Assert.Equal(bip32, bip32Actual.ToString()); } }
public void EngTest() { JObject test = JObject.Parse(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("bip39_vectors.json"))); foreach (JProperty language in test.Properties()) { Wordlist lang = GetList(language.Name); foreach (JArray langTest in ((JArray)language.Value).OfType <JArray>().Take(2)) { byte[] entropy = Encoders.Hex.DecodeData(langTest[0].ToString()); string mnemonicStr = langTest[1].ToString(); string seed = langTest[2].ToString(); var mnemonic = new Mnemonic(mnemonicStr, lang); Assert.True(mnemonic.IsValidChecksum); Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed("TREZOR"))); mnemonic = new Mnemonic(lang, entropy); Assert.True(mnemonic.IsValidChecksum); Assert.Equal(seed, Encoders.Hex.EncodeData(mnemonic.DeriveSeed("TREZOR"))); } } }
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); } }
public ColoredCoinTester(Network network, [CallerMemberName] string test = null) { TestCase testcase = JsonConvert.DeserializeObject <TestCase[]>(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("openasset-known-tx.json"))) .First(t => t.Test == test); var repository = new NoSqlTransactionRepository(network); foreach (string tx in testcase.Txs) { Transaction txObj = network.CreateTransaction(tx); repository.Put(txObj.GetHash(), txObj); } this.TestedTxId = uint256.Parse(testcase.Testedtx); this.Repository = new NullColoredTransactionRepository(repository); }
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 ColoredCoinTester([CallerMemberName] string test = null) { var testcase = JsonConvert.DeserializeObject <TestCase[]>(File.ReadAllText(TestDataLocations.GetFileFromDataFolder("openasset-known-tx.json"))) .First(t => t.test == test); NoSqlTransactionRepository repository = new NoSqlTransactionRepository(); foreach (var tx in testcase.txs) { var txObj = Transaction.Parse(tx); repository.Put(txObj.GetHash(), txObj); } TestedTxId = uint256.Parse(testcase.testedtx); Repository = new NullColoredTransactionRepository(repository); }