Skip to content

Commit

Permalink
wallet/spkman_tests: Add GetLabelledSPDestination unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Eunovo committed Sep 27, 2024
1 parent 9a4b93c commit b54e623
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/wallet/test/scriptpubkeyman_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,21 @@
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.

#include <addresstype.h>
#include <coins.h>
#include <common/bip352.h>
#include <key.h>
#include <primitives/transaction.h>
#include <test/util/setup_common.h>
#include <script/solver.h>
#include <uint256.h>
#include <wallet/scriptpubkeyman.h>
#include <wallet/wallet.h>
#include <wallet/walletutil.h>
#include <wallet/test/util.h>

#include <variant>

#include <boost/test/unit_test.hpp>

namespace wallet {
Expand Down Expand Up @@ -40,5 +48,45 @@ BOOST_AUTO_TEST_CASE(CanProvide)
BOOST_CHECK(keyman.CanProvide(p2sh_script, data));
}

// Test SilentPaymentDescriptorScriptPubKeyMan::GetLabelledSPDestination
// returns the correct label for the given Taproot spk
BOOST_AUTO_TEST_CASE(GetLabelledSPDestination)
{
CWallet wallet(m_node.chain.get(), "", CreateMockableWalletDatabase());
wallet.SetWalletFlag(WALLET_FLAG_DESCRIPTORS);
wallet.SetWalletFlag(WALLET_FLAG_SILENT_PAYMENTS);
{
LOCK(wallet.cs_wallet);
wallet.SetupDescriptorScriptPubKeyMans();
}
SilentPaymentDescriptorScriptPubKeyMan* sp_spk_man = *wallet.GetSilentPaymentsSPKMs().begin();
uint64_t index;
auto labelled_dest = sp_spk_man->GetNewLabelledDestination(index);

CKey private_key = GenerateRandomKey();
CPubKey pubkey = private_key.GetPubKey();
COutPoint prevout(Txid::FromUint256(*uint256::FromHex("daec98e9311f843277e8c59f9dccb42efd4d40881bff215ae82e5724fa754c50")), 0);
WitnessV0KeyHash p2wpkh(pubkey);
CScript scriptPubKey = GetScriptForDestination(p2wpkh);
CScript scriptSig;
auto sp_dest = std::get_if<V0SilentPaymentDestination>(&labelled_dest.value());
auto destinations = BIP352::GenerateSilentPaymentTaprootDestinations(
std::map<size_t, V0SilentPaymentDestination>{{0, *sp_dest}},
std::vector{private_key},
std::vector<KeyPair>(),
prevout
);
auto taproot_dest = destinations.value()[0];

CTxIn tx_in(prevout, scriptSig);
tx_in.scriptWitness.stack.push_back(std::vector<unsigned char>(pubkey.begin(), pubkey.end()));
auto public_data = BIP352::GetSilentPaymentsPublicData(
std::vector{tx_in},
std::map<COutPoint, Coin>{{prevout, Coin(CTxOut(10000, scriptPubKey), 1, false)}}
);
auto retrieved_labelled_dest = sp_spk_man->GetLabelledSPDestination(taproot_dest, *public_data);
BOOST_CHECK(*labelled_dest == *retrieved_labelled_dest);
}

BOOST_AUTO_TEST_SUITE_END()
} // namespace wallet

0 comments on commit b54e623

Please sign in to comment.