Bitcoin Core  0.20.99
P2P Digital Currency
scriptpubkeyman_tests.cpp
Go to the documentation of this file.
1 // Copyright (c) 2020 The Bitcoin Core developers
2 // Distributed under the MIT software license, see the accompanying
3 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 
5 #include <key.h>
6 #include <script/standard.h>
7 #include <test/util/setup_common.h>
9 #include <wallet/wallet.h>
10 
11 #include <boost/test/unit_test.hpp>
12 
13 BOOST_FIXTURE_TEST_SUITE(scriptpubkeyman_tests, BasicTestingSetup)
14 
15 // Test LegacyScriptPubKeyMan::CanProvide behavior, making sure it returns true
16 // for recognized scripts even when keys may not be available for signing.
18 {
19  // Set up wallet and keyman variables.
20  NodeContext node;
21  std::unique_ptr<interfaces::Chain> chain = interfaces::MakeChain(node);
22  CWallet wallet(chain.get(), "", CreateDummyWalletDatabase());
23  LegacyScriptPubKeyMan& keyman = *wallet.GetOrCreateLegacyScriptPubKeyMan();
24 
25  // Make a 1 of 2 multisig script
26  std::vector<CKey> keys(2);
27  std::vector<CPubKey> pubkeys;
28  for (CKey& key : keys) {
29  key.MakeNewKey(true);
30  pubkeys.emplace_back(key.GetPubKey());
31  }
32  CScript multisig_script = GetScriptForMultisig(1, pubkeys);
33  CScript p2sh_script = GetScriptForDestination(ScriptHash(multisig_script));
34  SignatureData data;
35 
36  // Verify the p2sh(multisig) script is not recognized until the multisig
37  // script is added to the keystore to make it solvable
38  BOOST_CHECK(!keyman.CanProvide(p2sh_script, data));
39  keyman.AddCScript(multisig_script);
40  BOOST_CHECK(keyman.CanProvide(p2sh_script, data));
41 }
42 
std::unique_ptr< Chain > MakeChain(NodeContext &node)
Return implementation of Chain interface.
Definition: chain.cpp:415
NodeContext struct containing references to chain state and connection state.
Definition: context.h:36
BOOST_AUTO_TEST_CASE(CanProvide)
std::unique_ptr< WalletDatabase > CreateDummyWalletDatabase()
Return object for accessing dummy database with no read/write capabilities.
Definition: walletdb.cpp:1036
CScript GetScriptForDestination(const CTxDestination &dest)
Generate a Bitcoin scriptPubKey for the given CTxDestination.
Definition: standard.cpp:295
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:390
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:622
CScript GetScriptForMultisig(int nRequired, const std::vector< CPubKey > &keys)
Generate a multisig script.
Definition: standard.cpp:305
An encapsulated private key.
Definition: key.h:27
#define BOOST_CHECK(expr)
Definition: object.cpp:17