public void QuickTestsOnKeyIdBytes() { var a = new KeyId("93e5d305cad2588d5fb254065fe48ce446028ba3"); var b = new ScriptId("93e5d305cad2588d5fb254065fe48ce446028ba3"); var b2 = new WitKeyId("93e5d305cad2588d5fb254065fe48ce446028ba3"); Assert.Equal(a.ToString(), b.ToString()); Assert.Equal(a.ToString(), b2.ToString()); Assert.Equal("93e5d305cad2588d5fb254065fe48ce446028ba3", a.ToString()); var bytes = Encoders.Hex.DecodeData("93e5d305cad2588d5fb254065fe48ce446028ba3"); Assert.True(StructuralComparisons.StructuralComparer.Compare(a.ToBytes(), b.ToBytes()) == 0); Assert.True(StructuralComparisons.StructuralComparer.Compare(b2.ToBytes(), b.ToBytes()) == 0); var c = new KeyId(bytes); var d = new ScriptId(bytes); var d2 = new WitKeyId(bytes); Assert.Equal(a, c); Assert.Equal(d, b); Assert.Equal(b2, d2); Assert.Equal(c.ToString(), d.ToString()); Assert.Equal(c.ToString(), d2.ToString()); Assert.Equal("93e5d305cad2588d5fb254065fe48ce446028ba3", c.ToString()); Assert.True(StructuralComparisons.StructuralComparer.Compare(c.ToBytes(), d.ToBytes()) == 0); Assert.True(StructuralComparisons.StructuralComparer.Compare(c.ToBytes(), d2.ToBytes()) == 0); var e = new WitScriptId("93e5d305cad2588d5fb254065fe48ce446028ba380e6ee663baea9cd10550089"); Assert.Equal("93e5d305cad2588d5fb254065fe48ce446028ba380e6ee663baea9cd10550089", e.ToString()); bytes = Encoders.Hex.DecodeData("93e5d305cad2588d5fb254065fe48ce446028ba380e6ee663baea9cd10550089"); var e2 = new WitScriptId(bytes); Assert.Equal(e, e2); }
/// <summary> /// Extract witness redeem from WitScript /// </summary> /// <param name="witScript">Witscript to extract information from</param> /// <param name="expectedScriptId">Expected redeem hash</param> /// <returns>The witness redeem</returns> public Script ExtractWitScriptParameters(WitScript witScript, WitScriptId expectedScriptId = null) { if (witScript.PushCount == 0) { return(null); } byte[] last = witScript.GetUnsafePush(witScript.PushCount - 1); var redeem = new Script(last); if (expectedScriptId != null) { if (expectedScriptId != redeem.WitHash) { return(null); } } return(redeem); }
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"); tests = tests.Concat(TestCase.read_json("data/base58_keys_valid2.json")).ToArray(); 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 == "p2wpkh") { network = network == Network.TestNet ? Network.SegNet : network; dest = new WitKeyId(new uint160(exp_payload)); } else if(exp_addrType == "p2wsh") { network = network == Network.TestNet ? Network.SegNet : network; 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() == dest); } catch(ArgumentException) { Assert.True(dest.GetType() == typeof(TxDestination)); } } } }
public Script GenerateScriptPubKey(WitScriptId scriptHash) { return(scriptHash.ScriptPubKey); }