Skip to content

Commit a57a1d4

Browse files
committed
test: add unit test for wallet watch-only methods involving PubKeys
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. test: add missing wallet lock for test case WatchOnlyPubKeys test: test case WatchOnlyPubKeys, suggested review changes by instagibbs test: test case WatchOnlyPubKeys, suggested review changes by achow101 test: test case WatchOnlyPubKeys, s/isPubKeyFullyValid/is_pubkey_fully_valid
1 parent d3e6721 commit a57a1d4

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
@@ -334,6 +334,84 @@ BOOST_AUTO_TEST_CASE(LoadReceiveRequests)
334334
BOOST_CHECK_EQUAL(values[1], "val_rr1");
335335
}
336336

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

0 commit comments

Comments
 (0)