Skip to content

Commit 6927407

Browse files
committed
wallet/spkman_tests: Add GetLabelledSPDestination unit test
1 parent 6f2120b commit 6927407

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

src/wallet/test/scriptpubkeyman_tests.cpp

+48
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,21 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <addresstype.h>
6+
#include <coins.h>
7+
#include <common/bip352.h>
58
#include <key.h>
9+
#include <primitives/transaction.h>
610
#include <test/util/setup_common.h>
711
#include <script/solver.h>
12+
#include <uint256.h>
813
#include <wallet/scriptpubkeyman.h>
914
#include <wallet/wallet.h>
15+
#include <wallet/walletutil.h>
1016
#include <wallet/test/util.h>
1117

18+
#include <variant>
19+
1220
#include <boost/test/unit_test.hpp>
1321

1422
namespace wallet {
@@ -40,5 +48,45 @@ BOOST_AUTO_TEST_CASE(CanProvide)
4048
BOOST_CHECK(keyman.CanProvide(p2sh_script, data));
4149
}
4250

51+
// Test SilentPaymentDescriptorScriptPubKeyMan::GetLabelledSPDestination
52+
// returns the correct label for the given Taproot spk
53+
BOOST_AUTO_TEST_CASE(GetLabelledSPDestination)
54+
{
55+
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
56+
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
57+
wallet.SetWalletFlag(WALLET_FLAG_SILENT_PAYMENTS);
58+
{
59+
LOCK(wallet.cs_wallet);
60+
wallet.SetupDescriptorScriptPubKeyMans();
61+
}
62+
SilentPaymentDescriptorScriptPubKeyMan* sp_spk_man = *wallet.GetSilentPaymentsSPKMs().begin();
63+
uint64_t index;
64+
auto labelled_dest = sp_spk_man->GetNewLabelledDestination(index);
65+
66+
CKey private_key = GenerateRandomKey();
67+
CPubKey pubkey = private_key.GetPubKey();
68+
COutPoint prevout(Txid::FromUint256(*uint256::FromHex("daec98e9311f843277e8c59f9dccb42efd4d40881bff215ae82e5724fa754c50")), 0);
69+
WitnessV0KeyHash p2wpkh(pubkey);
70+
CScript scriptPubKey = GetScriptForDestination(p2wpkh);
71+
CScript scriptSig;
72+
auto sp_dest = std::get_if<V0SilentPaymentDestination>(&labelled_dest.value());
73+
auto destinations = BIP352::GenerateSilentPaymentTaprootDestinations(
74+
std::map<size_t, V0SilentPaymentDestination>{{0, *sp_dest}},
75+
std::vector{private_key},
76+
std::vector<KeyPair>(),
77+
prevout
78+
);
79+
auto taproot_dest = destinations.value()[0];
80+
81+
CTxIn tx_in(prevout, scriptSig);
82+
tx_in.scriptWitness.stack.push_back(std::vector<unsigned char>(pubkey.begin(), pubkey.end()));
83+
auto public_data = BIP352::GetSilentPaymentsPublicData(
84+
std::vector{tx_in},
85+
std::map<COutPoint, Coin>{{prevout, Coin(CTxOut(10000, scriptPubKey), 1, false)}}
86+
);
87+
auto retrieved_labelled_dest = sp_spk_man->GetLabelledSPDestination(taproot_dest, *public_data);
88+
BOOST_CHECK(*labelled_dest == *retrieved_labelled_dest);
89+
}
90+
4391
BOOST_AUTO_TEST_SUITE_END()
4492
} // namespace wallet

0 commit comments

Comments
 (0)