@@ -509,17 +509,19 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
509509 return false ;
510510#ifndef QT_GUI
511511 // If default receiving address gets used, replace it with a new one
512- CScript scriptDefaultKey;
513- scriptDefaultKey.SetDestination (vchDefaultKey.GetID ());
514- BOOST_FOREACH (const CTxOut& txout, wtx.vout )
515- {
516- if (txout.scriptPubKey == scriptDefaultKey)
512+ if (vchDefaultKey.IsValid ()) {
513+ CScript scriptDefaultKey;
514+ scriptDefaultKey.SetDestination (vchDefaultKey.GetID ());
515+ BOOST_FOREACH (const CTxOut& txout, wtx.vout )
517516 {
518- CPubKey newDefaultKey;
519- if (GetKeyFromPool (newDefaultKey, false ))
517+ if (txout.scriptPubKey == scriptDefaultKey)
520518 {
521- SetDefaultKey (newDefaultKey);
522- SetAddressBookName (vchDefaultKey.GetID (), " " );
519+ CPubKey newDefaultKey;
520+ if (GetKeyFromPool (newDefaultKey, false ))
521+ {
522+ SetDefaultKey (newDefaultKey);
523+ SetAddressBookName (vchDefaultKey.GetID (), " " );
524+ }
523525 }
524526 }
525527 }
@@ -1437,7 +1439,8 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64_t> >& vecSend,
14371439 // post-backup change.
14381440
14391441 // Reserve a new key pair from key pool
1440- CPubKey vchPubKey = reservekey.GetReservedKey ();
1442+ CPubKey vchPubKey;
1443+ assert (reservekey.GetReservedKey (vchPubKey)); // should never fail, as we just unlocked
14411444
14421445 scriptChange.SetDestination (vchPubKey.GetID ());
14431446 }
@@ -2321,22 +2324,25 @@ void CWallet::DisableTransaction(const CTransaction &tx)
23212324 }
23222325}
23232326
2324- CPubKey CReserveKey::GetReservedKey ()
2327+ bool CReserveKey::GetReservedKey (CPubKey& pubkey )
23252328{
23262329 if (nIndex == -1 )
23272330 {
23282331 CKeyPool keypool;
23292332 pwallet->ReserveKeyFromKeyPool (nIndex, keypool);
23302333 if (nIndex != -1 )
23312334 vchPubKey = keypool.vchPubKey ;
2332- else
2333- {
2334- printf (" CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!" );
2335- vchPubKey = pwallet->vchDefaultKey ;
2335+ else {
2336+ if (pwallet->vchDefaultKey .IsValid ()) {
2337+ printf (" CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!" );
2338+ vchPubKey = pwallet->vchDefaultKey ;
2339+ } else
2340+ return false ;
23362341 }
23372342 }
23382343 assert (vchPubKey.IsValid ());
2339- return vchPubKey;
2344+ pubkey = vchPubKey;
2345+ return true ;
23402346}
23412347
23422348void CReserveKey::KeepKey ()
0 commit comments