Skip to content

Commit 59f0687

Browse files
author
MarcoFalke
committed
Merge #16786: test: add unit test for wallet watch-only methods involving PubKeys
a57a1d4 test: add unit test for wallet watch-only methods involving PubKeys (Sebastian Falbesoner) Pull request description: The motivation for this addition was to unit test the function `wallet.cpp:ExtractPubKey()` (see recent change in commit 798a589) which is however static and only indirectly available via the public methods `AddWatchOnly()`, `LoadWatchOnly()` and `RemoveWatchOnly()`. Since the first of those methods also stores the addresses to the disk, the second, simpler one was chosen which only operates in memory. ACKs for top commit: Sjors: ACK a57a1d4 instagibbs: reACK bitcoin/bitcoin@a57a1d4 Sjors: re-ACK a57a1d4 Tree-SHA512: 92a242204ab533022cd848662997372c41815b1265d07b3d96305697f801db29a5ba5668337faf4bea702bec1451972529afd6665927fb142aaf91700a338b26
2 parents 2a01640 + a57a1d4 commit 59f0687

File tree

1 file changed

+78
-0
lines changed

1 file changed

+78
-0
lines changed

src/wallet/test/wallet_tests.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,84 @@ BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
338338
BOOST_CHECK_EQUAL(values[1], "val_rr1");
339339
}
340340

341+
// Test some watch-only wallet methods by the procedure of loading (LoadWatchOnly),
342+
// checking (HaveWatchOnly), getting (GetWatchPubKey) and removing (RemoveWatchOnly) a
343+
// given PubKey, resp. its corresponding P2PK Script. Results of the the impact on
344+
// the address -> PubKey map is dependent on whether the PubKey is a point on the curve
345+
static void TestWatchOnlyPubKey(CWallet& wallet, const CPubKey& add_pubkey)
346+
{
347+
CScript p2pk = GetScriptForRawPubKey(add_pubkey);
348+
CKeyID add_address = add_pubkey.GetID();
349+
CPubKey found_pubkey;
350+
LOCK(wallet.cs_wallet);
351+
352+
// all Scripts (i.e. also all PubKeys) are added to the general watch-only set
353+
BOOST_CHECK(!wallet.HaveWatchOnly(p2pk));
354+
wallet.LoadWatchOnly(p2pk);
355+
BOOST_CHECK(wallet.HaveWatchOnly(p2pk));
356+
357+
// only PubKeys on the curve shall be added to the watch-only address -> PubKey map
358+
bool is_pubkey_fully_valid = add_pubkey.IsFullyValid();
359+
if (is_pubkey_fully_valid) {
360+
BOOST_CHECK(wallet.GetWatchPubKey(add_address, found_pubkey));
361+
BOOST_CHECK(found_pubkey == add_pubkey);
362+
} else {
363+
BOOST_CHECK(!wallet.GetWatchPubKey(add_address, found_pubkey));
364+
BOOST_CHECK(found_pubkey == CPubKey()); // passed key is unchanged
365+
}
366+
367+
wallet.RemoveWatchOnly(p2pk);
368+
BOOST_CHECK(!wallet.HaveWatchOnly(p2pk));
369+
370+
if (is_pubkey_fully_valid) {
371+
BOOST_CHECK(!wallet.GetWatchPubKey(add_address, found_pubkey));
372+
BOOST_CHECK(found_pubkey == add_pubkey); // passed key is unchanged
373+
}
374+
}
375+
376+
// Cryptographically invalidate a PubKey whilst keeping length and first byte
377+
static void PollutePubKey(CPubKey& pubkey)
378+
{
379+
std::vector<unsigned char> pubkey_raw(pubkey.begin(), pubkey.end());
380+
std::fill(pubkey_raw.begin()+1, pubkey_raw.end(), 0);
381+
pubkey = CPubKey(pubkey_raw);
382+
assert(!pubkey.IsFullyValid());
383+
assert(pubkey.IsValid());
384+
}
385+
386+
// Test watch-only wallet logic for PubKeys
387+
BOOST_AUTO_TEST_CASE(WatchOnlyPubKeys)
388+
{
389+
CKey key;
390+
CPubKey pubkey;
391+
392+
BOOST_CHECK(!m_wallet.HaveWatchOnly());
393+
394+
// uncompressed valid PubKey
395+
key.MakeNewKey(false);
396+
pubkey = key.GetPubKey();
397+
assert(!pubkey.IsCompressed());
398+
TestWatchOnlyPubKey(m_wallet, pubkey);
399+
400+
// uncompressed cryptographically invalid PubKey
401+
PollutePubKey(pubkey);
402+
TestWatchOnlyPubKey(m_wallet, pubkey);
403+
404+
// compressed valid PubKey
405+
key.MakeNewKey(true);
406+
pubkey = key.GetPubKey();
407+
assert(pubkey.IsCompressed());
408+
TestWatchOnlyPubKey(m_wallet, pubkey);
409+
410+
// compressed cryptographically invalid PubKey
411+
PollutePubKey(pubkey);
412+
TestWatchOnlyPubKey(m_wallet, pubkey);
413+
414+
// invalid empty PubKey
415+
pubkey = CPubKey();
416+
TestWatchOnlyPubKey(m_wallet, pubkey);
417+
}
418+
341419
class ListCoinsTestingSetup : public TestChain100Setup
342420
{
343421
public:

0 commit comments

Comments
 (0)