diff options
author | w0xlt <woltx@protonmail.com> | 2022-08-27 03:09:38 -0300 |
---|---|---|
committer | w0xlt <woltx@protonmail.com> | 2022-08-27 18:59:13 -0300 |
commit | 1b77db265317a6470d0914b520f04eb64b3c0942 (patch) | |
tree | 2f3fb23a758bbc005dfbb350d96513a290d96e62 /src/wallet/test | |
parent | e191fac4f3c37820f0618f72f0a8e8b524531ab8 (diff) |
test: add `ismine` test for descriptor scriptpubkeyman
Diffstat (limited to 'src/wallet/test')
-rw-r--r-- | src/wallet/test/ismine_tests.cpp | 297 |
1 files changed, 281 insertions, 16 deletions
diff --git a/src/wallet/test/ismine_tests.cpp b/src/wallet/test/ismine_tests.cpp index dd5cd0af46..126fba9928 100644 --- a/src/wallet/test/ismine_tests.cpp +++ b/src/wallet/test/ismine_tests.cpp @@ -3,6 +3,7 @@ // file COPYING or http://www.opensource.org/licenses/mit-license.php. #include <key.h> +#include <key_io.h> #include <node/context.h> #include <script/script.h> #include <script/standard.h> @@ -16,6 +17,25 @@ namespace wallet { BOOST_FIXTURE_TEST_SUITE(ismine_tests, BasicTestingSetup) +wallet::ScriptPubKeyMan* CreateDescriptor(CWallet& keystore, const std::string& desc_str, const bool success) +{ + keystore.SetWalletFlag(WALLET_FLAG_DESCRIPTORS); + + FlatSigningProvider keys; + std::string error; + std::unique_ptr<Descriptor> parsed_desc = Parse(desc_str, keys, error, false); + BOOST_CHECK(success == (parsed_desc != nullptr)); + if (!success) return nullptr; + + const int64_t range_start = 0, range_end = 1, next_index = 0, timestamp = 1; + + WalletDescriptor w_desc(std::move(parsed_desc), timestamp, range_start, range_end, next_index); + + LOCK(keystore.cs_wallet); + + return Assert(keystore.AddWalletDescriptor(w_desc, keys,/*label=*/"", /*internal=*/false)); +}; + BOOST_AUTO_TEST_CASE(ismine_standard) { CKey keys[2]; @@ -33,7 +53,7 @@ BOOST_AUTO_TEST_CASE(ismine_standard) CScript scriptPubKey; isminetype result; - // P2PK compressed + // P2PK compressed - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -50,7 +70,19 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } - // P2PK uncompressed + // P2PK compressed - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "pk(" + EncodeSecret(keys[0]) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + scriptPubKey = GetScriptForRawPubKey(pubkeys[0]); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // P2PK uncompressed - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -67,7 +99,19 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } - // P2PKH compressed + // P2PK uncompressed - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "pk(" + EncodeSecret(uncompressedKey) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + scriptPubKey = GetScriptForRawPubKey(uncompressedPubkey); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // P2PKH compressed - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -84,7 +128,19 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } - // P2PKH uncompressed + // P2PKH compressed - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "pkh(" + EncodeSecret(keys[0]) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + scriptPubKey = GetScriptForDestination(PKHash(pubkeys[0])); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // P2PKH uncompressed - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -101,7 +157,19 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } - // P2SH + // P2PKH uncompressed - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "pkh(" + EncodeSecret(uncompressedKey) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + scriptPubKey = GetScriptForDestination(PKHash(uncompressedPubkey)); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // P2SH - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -125,7 +193,20 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } - // (P2PKH inside) P2SH inside P2SH (invalid) + // P2SH - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "sh(pkh(" + EncodeSecret(keys[0]) + "))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0])); + scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript)); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // (P2PKH inside) P2SH inside P2SH (invalid) - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -143,7 +224,16 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_NO); } - // (P2PKH inside) P2SH inside P2WSH (invalid) + // (P2PKH inside) P2SH inside P2SH (invalid) - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "sh(sh(" + EncodeSecret(keys[0]) + "))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, false); + BOOST_CHECK_EQUAL(spk_manager, nullptr); + } + + // (P2PKH inside) P2SH inside P2WSH (invalid) - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -161,7 +251,16 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_NO); } - // P2WPKH inside P2WSH (invalid) + // (P2PKH inside) P2SH inside P2WSH (invalid) - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "wsh(sh(" + EncodeSecret(keys[0]) + "))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, false); + BOOST_CHECK_EQUAL(spk_manager, nullptr); + } + + // P2WPKH inside P2WSH (invalid) - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -177,7 +276,16 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_NO); } - // (P2PKH inside) P2WSH inside P2WSH (invalid) + // P2WPKH inside P2WSH (invalid) - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "wsh(wpkh(" + EncodeSecret(keys[0]) + "))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, false); + BOOST_CHECK_EQUAL(spk_manager, nullptr); + } + + // (P2PKH inside) P2WSH inside P2WSH (invalid) - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -195,7 +303,16 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_NO); } - // P2WPKH compressed + // (P2PKH inside) P2WSH inside P2WSH (invalid) - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "wsh(wsh(" + EncodeSecret(keys[0]) + "))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, false); + BOOST_CHECK_EQUAL(spk_manager, nullptr); + } + + // P2WPKH compressed - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -210,7 +327,19 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } - // P2WPKH uncompressed + // P2WPKH compressed - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "wpkh(" + EncodeSecret(keys[0]) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0])); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // P2WPKH uncompressed - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -229,7 +358,16 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_NO); } - // scriptPubKey multisig + // P2WPKH uncompressed (invalid) - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "wpkh(" + EncodeSecret(uncompressedKey) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, false); + BOOST_CHECK_EQUAL(spk_manager, nullptr); + } + + // scriptPubKey multisig - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -260,7 +398,19 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_NO); } - // P2SH multisig + // scriptPubKey multisig - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + std::string desc_str = "multi(2, " + EncodeSecret(uncompressedKey) + ", " + EncodeSecret(keys[1]) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + scriptPubKey = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]}); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // P2SH multisig - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -281,7 +431,21 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } - // P2WSH multisig with compressed keys + // P2SH multisig - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + + std::string desc_str = "sh(multi(2, " + EncodeSecret(uncompressedKey) + ", " + EncodeSecret(keys[1]) + "))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + CScript redeemScript = GetScriptForMultisig(2, {uncompressedPubkey, pubkeys[1]}); + scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript)); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // P2WSH multisig with compressed keys - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -307,7 +471,21 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } - // P2WSH multisig with uncompressed key + // P2WSH multisig with compressed keys - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + + std::string desc_str = "wsh(multi(2, " + EncodeSecret(keys[0]) + ", " + EncodeSecret(keys[1]) + "))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + CScript redeemScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]}); + scriptPubKey = GetScriptForDestination(WitnessV0ScriptHash(redeemScript)); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // P2WSH multisig with uncompressed key - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -333,7 +511,17 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_NO); } - // P2WSH multisig wrapped in P2SH + // P2WSH multisig with uncompressed key (invalid) - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + + std::string desc_str = "wsh(multi(2, " + EncodeSecret(uncompressedKey) + ", " + EncodeSecret(keys[1]) + "))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, false); + BOOST_CHECK_EQUAL(spk_manager, nullptr); + } + + // P2WSH multisig wrapped in P2SH - Legacy { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); keystore.SetupLegacyScriptPubKeyMan(); @@ -360,6 +548,83 @@ BOOST_AUTO_TEST_CASE(ismine_standard) BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); } + // P2WSH multisig wrapped in P2SH - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + + std::string desc_str = "sh(wsh(multi(2, " + EncodeSecret(keys[0]) + ", " + EncodeSecret(keys[1]) + ")))"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + CScript witnessScript = GetScriptForMultisig(2, {pubkeys[0], pubkeys[1]}); + CScript redeemScript = GetScriptForDestination(WitnessV0ScriptHash(witnessScript)); + scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript)); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + + // Combo - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + + std::string desc_str = "combo(" + EncodeSecret(keys[0]) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + // Test P2PK + result = spk_manager->IsMine(GetScriptForRawPubKey(pubkeys[0])); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + + // Test P2PKH + result = spk_manager->IsMine(GetScriptForDestination(PKHash(pubkeys[0]))); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + + // Test P2SH (combo descriptor does not describe P2SH) + CScript redeemScript = GetScriptForDestination(PKHash(pubkeys[0])); + scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript)); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_NO); + + // Test P2WPKH + scriptPubKey = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0])); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + + // P2SH-P2WPKH output + redeemScript = GetScriptForDestination(WitnessV0KeyHash(pubkeys[0])); + scriptPubKey = GetScriptForDestination(ScriptHash(redeemScript)); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + + // Test P2TR (combo descriptor does not describe P2TR) + XOnlyPubKey xpk(pubkeys[0]); + Assert(xpk.IsFullyValid()); + TaprootBuilder builder; + builder.Finalize(xpk); + WitnessV1Taproot output = builder.GetOutput(); + scriptPubKey = GetScriptForDestination(output); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_NO); + } + + // Taproot - Descriptor + { + CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); + + std::string desc_str = "tr(" + EncodeSecret(keys[0]) + ")"; + + auto spk_manager = CreateDescriptor(keystore, desc_str, true); + + XOnlyPubKey xpk(pubkeys[0]); + Assert(xpk.IsFullyValid()); + TaprootBuilder builder; + builder.Finalize(xpk); + WitnessV1Taproot output = builder.GetOutput(); + scriptPubKey = GetScriptForDestination(output); + result = spk_manager->IsMine(scriptPubKey); + BOOST_CHECK_EQUAL(result, ISMINE_SPENDABLE); + } + // OP_RETURN { CWallet keystore(chain.get(), "", m_args, CreateDummyWalletDatabase()); |