1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2012 The Bitcoin developers
3 // Distributed under the MIT/X11 software license, see the accompanying
4 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
9 #include "ui_interface.h"
11 #include <boost/algorithm/string/replace.hpp>
16 //////////////////////////////////////////////////////////////////////////////
21 struct CompareValueOnly
23 bool operator()(const pair<int64, pair<const CWalletTx*, unsigned int> >& t1,
24 const pair<int64, pair<const CWalletTx*, unsigned int> >& t2) const
26 return t1.first < t2.first;
30 CPubKey CWallet::GenerateNewKey()
32 bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
36 secret.MakeNewKey(fCompressed);
38 // Compressed public keys were introduced in version 0.6.0
40 SetMinVersion(FEATURE_COMPRPUBKEY);
42 CPubKey pubkey = secret.GetPubKey();
44 // Create new metadata
45 int64 nCreationTime = GetTime();
46 mapKeyMetadata[pubkey.GetID()] = CKeyMetadata(nCreationTime);
47 if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
48 nTimeFirstKey = nCreationTime;
50 if (!AddKeyPubKey(secret, pubkey))
51 throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
55 bool CWallet::AddKeyPubKey(const CKey& secret, const CPubKey &pubkey)
57 if (!CCryptoKeyStore::AddKeyPubKey(secret, pubkey))
62 return CWalletDB(strWalletFile).WriteKey(pubkey,
64 mapKeyMetadata[pubkey.GetID()]);
69 bool CWallet::AddCryptedKey(const CPubKey &vchPubKey,
70 const vector<unsigned char> &vchCryptedSecret)
72 if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
78 if (pwalletdbEncryption)
79 return pwalletdbEncryption->WriteCryptedKey(vchPubKey,
81 mapKeyMetadata[vchPubKey.GetID()]);
83 return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey,
85 mapKeyMetadata[vchPubKey.GetID()]);
90 bool CWallet::LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &meta)
92 if (meta.nCreateTime && (!nTimeFirstKey || meta.nCreateTime < nTimeFirstKey))
93 nTimeFirstKey = meta.nCreateTime;
95 mapKeyMetadata[pubkey.GetID()] = meta;
99 bool CWallet::LoadCryptedKey(const CPubKey &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
101 return CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret);
104 bool CWallet::AddCScript(const CScript& redeemScript)
106 if (!CCryptoKeyStore::AddCScript(redeemScript))
110 return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
113 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
116 CKeyingMaterial vMasterKey;
120 BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
122 if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
124 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
125 continue; // try another master key
126 if (CCryptoKeyStore::Unlock(vMasterKey))
133 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
135 bool fWasLocked = IsLocked();
142 CKeyingMaterial vMasterKey;
143 BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
145 if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
147 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
149 if (CCryptoKeyStore::Unlock(vMasterKey))
151 int64 nStartTime = GetTimeMillis();
152 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
153 pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
155 nStartTime = GetTimeMillis();
156 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
157 pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
159 if (pMasterKey.second.nDeriveIterations < 25000)
160 pMasterKey.second.nDeriveIterations = 25000;
162 printf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
164 if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
166 if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
168 CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
179 void CWallet::SetBestChain(const CBlockLocator& loc)
181 CWalletDB walletdb(strWalletFile);
182 walletdb.WriteBestBlock(loc);
185 // This class implements an addrIncoming entry that causes pre-0.4
186 // clients to crash on startup if reading a private-key-encrypted wallet.
187 class CCorruptAddress
192 if (nType & SER_DISK)
197 bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
199 if (nWalletVersion >= nVersion)
202 // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
203 if (fExplicit && nVersion > nWalletMaxVersion)
204 nVersion = FEATURE_LATEST;
206 nWalletVersion = nVersion;
208 if (nVersion > nWalletMaxVersion)
209 nWalletMaxVersion = nVersion;
213 CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
214 if (nWalletVersion >= 40000)
216 // Versions prior to 0.4.0 did not support the "minversion" record.
217 // Use a CCorruptAddress to make them crash instead.
218 CCorruptAddress corruptAddress;
219 pwalletdb->WriteSetting("addrIncoming", corruptAddress);
221 if (nWalletVersion > 40000)
222 pwalletdb->WriteMinVersion(nWalletVersion);
230 bool CWallet::SetMaxVersion(int nVersion)
232 // cannot downgrade below current version
233 if (nWalletVersion > nVersion)
236 nWalletMaxVersion = nVersion;
241 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
246 CKeyingMaterial vMasterKey;
247 RandAddSeedPerfmon();
249 vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
250 RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
252 CMasterKey kMasterKey;
254 RandAddSeedPerfmon();
255 kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
256 RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
259 int64 nStartTime = GetTimeMillis();
260 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
261 kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
263 nStartTime = GetTimeMillis();
264 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
265 kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
267 if (kMasterKey.nDeriveIterations < 25000)
268 kMasterKey.nDeriveIterations = 25000;
270 printf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
272 if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
274 if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
279 mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
282 pwalletdbEncryption = new CWalletDB(strWalletFile);
283 if (!pwalletdbEncryption->TxnBegin())
285 pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
288 if (!EncryptKeys(vMasterKey))
291 pwalletdbEncryption->TxnAbort();
292 exit(1); //We now probably have half of our keys encrypted in memory, and half not...die and let the user reload their unencrypted wallet.
295 // Encryption was introduced in version 0.4.0
296 SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
300 if (!pwalletdbEncryption->TxnCommit())
301 exit(1); //We now have keys encrypted in memory, but no on disk...die to avoid confusion and let the user reload their unencrypted wallet.
303 delete pwalletdbEncryption;
304 pwalletdbEncryption = NULL;
308 Unlock(strWalletPassphrase);
312 // Need to completely rewrite the wallet file; if we don't, bdb might keep
313 // bits of the unencrypted private key in slack space in the database file.
314 CDB::Rewrite(strWalletFile);
317 NotifyStatusChanged(this);
322 int64 CWallet::IncOrderPosNext(CWalletDB *pwalletdb)
324 int64 nRet = nOrderPosNext++;
326 pwalletdb->WriteOrderPosNext(nOrderPosNext);
328 CWalletDB(strWalletFile).WriteOrderPosNext(nOrderPosNext);
333 CWallet::TxItems CWallet::OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount)
335 CWalletDB walletdb(strWalletFile);
337 // First: get all CWalletTx and CAccountingEntry into a sorted-by-order multimap.
340 // Note: maintaining indices in the database of (account,time) --> txid and (account, time) --> acentry
341 // would make this much faster for applications that do this a lot.
342 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
344 CWalletTx* wtx = &((*it).second);
345 txOrdered.insert(make_pair(wtx->nOrderPos, TxPair(wtx, (CAccountingEntry*)0)));
348 walletdb.ListAccountCreditDebit(strAccount, acentries);
349 BOOST_FOREACH(CAccountingEntry& entry, acentries)
351 txOrdered.insert(make_pair(entry.nOrderPos, TxPair((CWalletTx*)0, &entry)));
357 void CWallet::WalletUpdateSpent(const CTransaction &tx)
359 // Anytime a signature is successfully verified, it's proof the outpoint is spent.
360 // Update the wallet spent flag if it doesn't know due to wallet.dat being
361 // restored from backup or the user making copies of wallet.dat.
364 BOOST_FOREACH(const CTxIn& txin, tx.vin)
366 map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
367 if (mi != mapWallet.end())
369 CWalletTx& wtx = (*mi).second;
370 if (txin.prevout.n >= wtx.vout.size())
371 printf("WalletUpdateSpent: bad wtx %s\n", wtx.GetHash().ToString().c_str());
372 else if (!wtx.IsSpent(txin.prevout.n) && IsMine(wtx.vout[txin.prevout.n]))
374 printf("WalletUpdateSpent found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
375 wtx.MarkSpent(txin.prevout.n);
377 NotifyTransactionChanged(this, txin.prevout.hash, CT_UPDATED);
384 void CWallet::MarkDirty()
388 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
389 item.second.MarkDirty();
393 bool CWallet::AddToWallet(const CWalletTx& wtxIn)
395 uint256 hash = wtxIn.GetHash();
398 // Inserts only if not already there, returns tx inserted or tx found
399 pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
400 CWalletTx& wtx = (*ret.first).second;
401 wtx.BindWallet(this);
402 bool fInsertedNew = ret.second;
405 wtx.nTimeReceived = GetAdjustedTime();
406 wtx.nOrderPos = IncOrderPosNext();
408 wtx.nTimeSmart = wtx.nTimeReceived;
409 if (wtxIn.hashBlock != 0)
411 if (mapBlockIndex.count(wtxIn.hashBlock))
413 unsigned int latestNow = wtx.nTimeReceived;
414 unsigned int latestEntry = 0;
416 // Tolerate times up to the last timestamp in the wallet not more than 5 minutes into the future
417 int64 latestTolerated = latestNow + 300;
418 std::list<CAccountingEntry> acentries;
419 TxItems txOrdered = OrderedTxItems(acentries);
420 for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
422 CWalletTx *const pwtx = (*it).second.first;
425 CAccountingEntry *const pacentry = (*it).second.second;
429 nSmartTime = pwtx->nTimeSmart;
431 nSmartTime = pwtx->nTimeReceived;
434 nSmartTime = pacentry->nTime;
435 if (nSmartTime <= latestTolerated)
437 latestEntry = nSmartTime;
438 if (nSmartTime > latestNow)
439 latestNow = nSmartTime;
445 unsigned int& blocktime = mapBlockIndex[wtxIn.hashBlock]->nTime;
446 wtx.nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
449 printf("AddToWallet() : found %s in block %s not in index\n",
450 wtxIn.GetHash().ToString().c_str(),
451 wtxIn.hashBlock.ToString().c_str());
455 bool fUpdated = false;
459 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
461 wtx.hashBlock = wtxIn.hashBlock;
464 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
466 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
467 wtx.nIndex = wtxIn.nIndex;
470 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
472 wtx.fFromMe = wtxIn.fFromMe;
475 fUpdated |= wtx.UpdateSpent(wtxIn.vfSpent);
479 printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
482 if (fInsertedNew || fUpdated)
483 if (!wtx.WriteToDisk())
487 // If default receiving address gets used, replace it with a new one
488 if (vchDefaultKey.IsValid()) {
489 CScript scriptDefaultKey;
490 scriptDefaultKey.SetDestination(vchDefaultKey.GetID());
491 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
493 if (txout.scriptPubKey == scriptDefaultKey)
495 CPubKey newDefaultKey;
496 if (GetKeyFromPool(newDefaultKey, false))
498 SetDefaultKey(newDefaultKey);
499 SetAddressBook(vchDefaultKey.GetID(), "", "receive");
505 // since AddToWallet is called directly for self-originating transactions, check for consumption of own coins
506 WalletUpdateSpent(wtx);
508 // Notify UI of new or updated transaction
509 NotifyTransactionChanged(this, hash, fInsertedNew ? CT_NEW : CT_UPDATED);
511 // notify an external script when a wallet transaction comes in or is updated
512 std::string strCmd = GetArg("-walletnotify", "");
514 if ( !strCmd.empty())
516 boost::replace_all(strCmd, "%s", wtxIn.GetHash().GetHex());
517 boost::thread t(runCommand, strCmd); // thread runs free
524 // Add a transaction to the wallet, or update it.
525 // pblock is optional, but should be provided if the transaction is known to be in a block.
526 // If fUpdate is true, existing transactions will be updated.
527 bool CWallet::AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction& tx, const CBlock* pblock, bool fUpdate, bool fFindBlock)
531 bool fExisted = mapWallet.count(hash);
532 if (fExisted && !fUpdate) return false;
533 if (fExisted || IsMine(tx) || IsFromMe(tx))
535 CWalletTx wtx(this,tx);
536 // Get merkle branch if transaction was found in a block
538 wtx.SetMerkleBranch(pblock);
539 return AddToWallet(wtx);
542 WalletUpdateSpent(tx);
547 bool CWallet::EraseFromWallet(uint256 hash)
553 if (mapWallet.erase(hash))
554 CWalletDB(strWalletFile).EraseTx(hash);
560 bool CWallet::IsMine(const CTxIn &txin) const
564 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
565 if (mi != mapWallet.end())
567 const CWalletTx& prev = (*mi).second;
568 if (txin.prevout.n < prev.vout.size())
569 if (IsMine(prev.vout[txin.prevout.n]))
576 int64 CWallet::GetDebit(const CTxIn &txin) const
580 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
581 if (mi != mapWallet.end())
583 const CWalletTx& prev = (*mi).second;
584 if (txin.prevout.n < prev.vout.size())
585 if (IsMine(prev.vout[txin.prevout.n]))
586 return prev.vout[txin.prevout.n].nValue;
592 bool CWallet::IsChange(const CTxOut& txout) const
594 CTxDestination address;
596 // TODO: fix handling of 'change' outputs. The assumption is that any
597 // payment to a TX_PUBKEYHASH that is mine but isn't in the address book
598 // is change. That assumption is likely to break when we implement multisignature
599 // wallets that return change back into a multi-signature-protected address;
600 // a better way of identifying which outputs are 'the send' and which are
601 // 'the change' will need to be implemented (maybe extend CWalletTx to remember
602 // which output, if any, was change).
603 if (ExtractDestination(txout.scriptPubKey, address) && ::IsMine(*this, address))
606 if (!mapAddressBook.count(address))
612 int64 CWalletTx::GetTxTime() const
614 int64 n = nTimeSmart;
615 return n ? n : nTimeReceived;
618 int CWalletTx::GetRequestCount() const
620 // Returns -1 if it wasn't being tracked
623 LOCK(pwallet->cs_wallet);
629 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
630 if (mi != pwallet->mapRequestCount.end())
631 nRequests = (*mi).second;
636 // Did anyone request this transaction?
637 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
638 if (mi != pwallet->mapRequestCount.end())
640 nRequests = (*mi).second;
642 // How about the block it's in?
643 if (nRequests == 0 && hashBlock != 0)
645 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
646 if (mi != pwallet->mapRequestCount.end())
647 nRequests = (*mi).second;
649 nRequests = 1; // If it's in someone else's block it must have got out
657 void CWalletTx::GetAmounts(list<pair<CTxDestination, int64> >& listReceived,
658 list<pair<CTxDestination, int64> >& listSent, int64& nFee, string& strSentAccount) const
661 listReceived.clear();
663 strSentAccount = strFromAccount;
666 int64 nDebit = GetDebit();
667 if (nDebit > 0) // debit>0 means we signed/sent this transaction
669 int64 nValueOut = GetValueOut(*this);
670 nFee = nDebit - nValueOut;
674 BOOST_FOREACH(const CTxOut& txout, vout)
676 CTxDestination address;
677 vector<unsigned char> vchPubKey;
678 if (!ExtractDestination(txout.scriptPubKey, address))
680 printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
681 this->GetHash().ToString().c_str());
684 // Don't report 'change' txouts
685 if (nDebit > 0 && pwallet->IsChange(txout))
689 listSent.push_back(make_pair(address, txout.nValue));
691 if (pwallet->IsMine(txout))
692 listReceived.push_back(make_pair(address, txout.nValue));
697 void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nReceived,
698 int64& nSent, int64& nFee) const
700 nReceived = nSent = nFee = 0;
703 string strSentAccount;
704 list<pair<CTxDestination, int64> > listReceived;
705 list<pair<CTxDestination, int64> > listSent;
706 GetAmounts(listReceived, listSent, allFee, strSentAccount);
708 if (strAccount == strSentAccount)
710 BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& s, listSent)
715 LOCK(pwallet->cs_wallet);
716 BOOST_FOREACH(const PAIRTYPE(CTxDestination,int64)& r, listReceived)
718 if (pwallet->mapAddressBook.count(r.first))
720 map<CTxDestination, CAddressBookData>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
721 if (mi != pwallet->mapAddressBook.end() && (*mi).second.name == strAccount)
722 nReceived += r.second;
724 else if (strAccount.empty())
726 nReceived += r.second;
732 void CWalletTx::AddSupportingTransactions()
736 const int COPY_DEPTH = 3;
737 if (SetMerkleBranch() < COPY_DEPTH)
739 vector<uint256> vWorkQueue;
740 BOOST_FOREACH(const CTxIn& txin, vin)
741 vWorkQueue.push_back(txin.prevout.hash);
744 LOCK(pwallet->cs_wallet);
745 map<uint256, const CMerkleTx*> mapWalletPrev;
746 set<uint256> setAlreadyDone;
747 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
749 uint256 hash = vWorkQueue[i];
750 if (setAlreadyDone.count(hash))
752 setAlreadyDone.insert(hash);
755 map<uint256, CWalletTx>::const_iterator mi = pwallet->mapWallet.find(hash);
756 if (mi != pwallet->mapWallet.end())
759 BOOST_FOREACH(const CMerkleTx& txWalletPrev, (*mi).second.vtxPrev)
760 mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
762 else if (mapWalletPrev.count(hash))
764 tx = *mapWalletPrev[hash];
767 int nDepth = tx.SetMerkleBranch();
768 vtxPrev.push_back(tx);
770 if (nDepth < COPY_DEPTH)
772 BOOST_FOREACH(const CTxIn& txin, tx.vin)
773 vWorkQueue.push_back(txin.prevout.hash);
779 reverse(vtxPrev.begin(), vtxPrev.end());
782 bool CWalletTx::WriteToDisk()
784 return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
787 // Scan the block chain (starting in pindexStart) for transactions
788 // from or to us. If fUpdate is true, found transactions that already
789 // exist in the wallet will be updated.
790 int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
794 CBlockIndex* pindex = pindexStart;
799 // no need to read and scan block, if block was created before
800 // our wallet birthday (as adjusted for block time variability)
801 if (nTimeFirstKey && (pindex->nTime < (nTimeFirstKey - 7200))) {
802 pindex = pindex->GetNextInMainChain();
807 ReadBlockFromDisk(block, pindex);
808 BOOST_FOREACH(CTransaction& tx, block.vtx)
810 if (AddToWalletIfInvolvingMe(tx.GetHash(), tx, &block, fUpdate))
813 pindex = pindex->GetNextInMainChain();
819 void CWallet::ReacceptWalletTransactions()
826 bool fMissing = false;
827 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
829 CWalletTx& wtx = item.second;
830 if (wtx.IsCoinBase() && wtx.IsSpent(0))
834 bool fUpdated = false;
835 bool fFound = pcoinsTip->GetCoins(wtx.GetHash(), coins);
836 if (fFound || wtx.GetDepthInMainChain() > 0)
838 // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
839 for (unsigned int i = 0; i < wtx.vout.size(); i++)
843 if ((i >= coins.vout.size() || coins.vout[i].IsNull()) && IsMine(wtx.vout[i]))
852 printf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
859 // Re-accept any txes of ours that aren't already in a block
860 if (!wtx.IsCoinBase())
861 wtx.AcceptWalletTransaction();
866 // TODO: optimize this to scan just part of the block chain?
867 if (ScanForWalletTransactions(pindexGenesisBlock))
868 fRepeat = true; // Found missing transactions: re-do re-accept.
873 void CWalletTx::RelayWalletTransaction()
875 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
877 if (!tx.IsCoinBase())
878 if (tx.GetDepthInMainChain() == 0)
879 RelayTransaction((CTransaction)tx, tx.GetHash());
883 if (GetDepthInMainChain() == 0) {
884 uint256 hash = GetHash();
885 printf("Relaying wtx %s\n", hash.ToString().c_str());
886 RelayTransaction((CTransaction)*this, hash);
891 void CWallet::ResendWalletTransactions()
893 // Do this infrequently and randomly to avoid giving away
894 // that these are our transactions.
895 if (GetTime() < nNextResend)
897 bool fFirst = (nNextResend == 0);
898 nNextResend = GetTime() + GetRand(30 * 60);
902 // Only do it if there's been a new block since last time
903 if (nTimeBestReceived < nLastResend)
905 nLastResend = GetTime();
907 // Rebroadcast any of our txes that aren't in a block yet
908 printf("ResendWalletTransactions()\n");
911 // Sort them in chronological order
912 multimap<unsigned int, CWalletTx*> mapSorted;
913 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
915 CWalletTx& wtx = item.second;
916 // Don't rebroadcast until it's had plenty of time that
917 // it should have gotten in already by now.
918 if (nTimeBestReceived - (int64)wtx.nTimeReceived > 5 * 60)
919 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
921 BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
923 CWalletTx& wtx = *item.second;
924 wtx.RelayWalletTransaction();
934 //////////////////////////////////////////////////////////////////////////////
940 int64 CWallet::GetBalance() const
945 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
947 const CWalletTx* pcoin = &(*it).second;
948 if (pcoin->IsConfirmed())
949 nTotal += pcoin->GetAvailableCredit();
956 int64 CWallet::GetUnconfirmedBalance() const
961 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
963 const CWalletTx* pcoin = &(*it).second;
964 if (!IsFinalTx(*pcoin) || !pcoin->IsConfirmed())
965 nTotal += pcoin->GetAvailableCredit();
971 int64 CWallet::GetImmatureBalance() const
976 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
978 const CWalletTx* pcoin = &(*it).second;
979 nTotal += pcoin->GetImmatureCredit();
985 // populate vCoins with vector of spendable COutputs
986 void CWallet::AvailableCoins(vector<COutput>& vCoins, bool fOnlyConfirmed) const
992 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
994 const CWalletTx* pcoin = &(*it).second;
996 if (!IsFinalTx(*pcoin))
999 if (fOnlyConfirmed && !pcoin->IsConfirmed())
1002 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
1005 for (unsigned int i = 0; i < pcoin->vout.size(); i++) {
1006 if (!(pcoin->IsSpent(i)) && IsMine(pcoin->vout[i]) &&
1007 !IsLockedCoin((*it).first, i) && pcoin->vout[i].nValue > 0)
1008 vCoins.push_back(COutput(pcoin, i, pcoin->GetDepthInMainChain()));
1014 static void ApproximateBestSubset(vector<pair<int64, pair<const CWalletTx*,unsigned int> > >vValue, int64 nTotalLower, int64 nTargetValue,
1015 vector<char>& vfBest, int64& nBest, int iterations = 1000)
1017 vector<char> vfIncluded;
1019 vfBest.assign(vValue.size(), true);
1020 nBest = nTotalLower;
1022 seed_insecure_rand();
1024 for (int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
1026 vfIncluded.assign(vValue.size(), false);
1028 bool fReachedTarget = false;
1029 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
1031 for (unsigned int i = 0; i < vValue.size(); i++)
1033 //The solver here uses a randomized algorithm,
1034 //the randomness serves no real security purpose but is just
1035 //needed to prevent degenerate behavior and it is important
1036 //that the rng fast. We do not use a constant random sequence,
1037 //because there may be some privacy improvement by making
1038 //the selection random.
1039 if (nPass == 0 ? insecure_rand()&1 : !vfIncluded[i])
1041 nTotal += vValue[i].first;
1042 vfIncluded[i] = true;
1043 if (nTotal >= nTargetValue)
1045 fReachedTarget = true;
1049 vfBest = vfIncluded;
1051 nTotal -= vValue[i].first;
1052 vfIncluded[i] = false;
1060 bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, vector<COutput> vCoins,
1061 set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1063 setCoinsRet.clear();
1066 // List of values less than target
1067 pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
1068 coinLowestLarger.first = std::numeric_limits<int64>::max();
1069 coinLowestLarger.second.first = NULL;
1070 vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
1071 int64 nTotalLower = 0;
1073 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
1075 BOOST_FOREACH(COutput output, vCoins)
1077 const CWalletTx *pcoin = output.tx;
1079 if (output.nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
1083 int64 n = pcoin->vout[i].nValue;
1085 pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
1087 if (n == nTargetValue)
1089 setCoinsRet.insert(coin.second);
1090 nValueRet += coin.first;
1093 else if (n < nTargetValue + CENT)
1095 vValue.push_back(coin);
1098 else if (n < coinLowestLarger.first)
1100 coinLowestLarger = coin;
1104 if (nTotalLower == nTargetValue)
1106 for (unsigned int i = 0; i < vValue.size(); ++i)
1108 setCoinsRet.insert(vValue[i].second);
1109 nValueRet += vValue[i].first;
1114 if (nTotalLower < nTargetValue)
1116 if (coinLowestLarger.second.first == NULL)
1118 setCoinsRet.insert(coinLowestLarger.second);
1119 nValueRet += coinLowestLarger.first;
1123 // Solve subset sum by stochastic approximation
1124 sort(vValue.rbegin(), vValue.rend(), CompareValueOnly());
1125 vector<char> vfBest;
1128 ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
1129 if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
1130 ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
1132 // If we have a bigger coin and (either the stochastic approximation didn't find a good solution,
1133 // or the next bigger coin is closer), return the bigger coin
1134 if (coinLowestLarger.second.first &&
1135 ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
1137 setCoinsRet.insert(coinLowestLarger.second);
1138 nValueRet += coinLowestLarger.first;
1141 for (unsigned int i = 0; i < vValue.size(); i++)
1144 setCoinsRet.insert(vValue[i].second);
1145 nValueRet += vValue[i].first;
1149 printf("SelectCoins() best subset: ");
1150 for (unsigned int i = 0; i < vValue.size(); i++)
1152 printf("%s ", FormatMoney(vValue[i].first).c_str());
1153 printf("total %s\n", FormatMoney(nBest).c_str());
1159 bool CWallet::SelectCoins(int64 nTargetValue, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1161 vector<COutput> vCoins;
1162 AvailableCoins(vCoins);
1164 return (SelectCoinsMinConf(nTargetValue, 1, 6, vCoins, setCoinsRet, nValueRet) ||
1165 SelectCoinsMinConf(nTargetValue, 1, 1, vCoins, setCoinsRet, nValueRet) ||
1166 SelectCoinsMinConf(nTargetValue, 0, 1, vCoins, setCoinsRet, nValueRet));
1172 bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend,
1173 CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, std::string& strFailReason)
1176 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1180 strFailReason = _("Transaction amounts must be positive");
1185 if (vecSend.empty() || nValue < 0)
1187 strFailReason = _("Transaction amounts must be positive");
1191 wtxNew.BindWallet(this);
1194 LOCK2(cs_main, cs_wallet);
1196 nFeeRet = nTransactionFee;
1200 wtxNew.vout.clear();
1201 wtxNew.fFromMe = true;
1203 int64 nTotalValue = nValue + nFeeRet;
1204 double dPriority = 0;
1205 // vouts to the payees
1206 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1208 CTxOut txout(s.second, s.first);
1209 if (txout.IsDust(CTransaction::nMinRelayTxFee))
1211 strFailReason = _("Transaction amount too small");
1214 wtxNew.vout.push_back(txout);
1217 // Choose coins to use
1218 set<pair<const CWalletTx*,unsigned int> > setCoins;
1220 if (!SelectCoins(nTotalValue, setCoins, nValueIn))
1222 strFailReason = _("Insufficient funds");
1225 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1227 int64 nCredit = pcoin.first->vout[pcoin.second].nValue;
1228 //The priority after the next block (depth+1) is used instead of the current,
1229 //reflecting an assumption the user would accept a bit more delay for
1230 //a chance at a free transaction.
1231 dPriority += (double)nCredit * (pcoin.first->GetDepthInMainChain()+1);
1234 int64 nChange = nValueIn - nValue - nFeeRet;
1235 // if sub-cent change is required, the fee must be raised to at least nMinTxFee
1236 // or until nChange becomes zero
1237 // NOTE: this depends on the exact behaviour of GetMinFee
1238 if (nFeeRet < CTransaction::nMinTxFee && nChange > 0 && nChange < CENT)
1240 int64 nMoveToFee = min(nChange, CTransaction::nMinTxFee - nFeeRet);
1241 nChange -= nMoveToFee;
1242 nFeeRet += nMoveToFee;
1247 // Note: We use a new key here to keep it from being obvious which side is the change.
1248 // The drawback is that by not reusing a previous key, the change may be lost if a
1249 // backup is restored, if the backup doesn't have the new private key for the change.
1250 // If we reused the old key, it would be possible to add code to look for and
1251 // rediscover unknown transactions that were written with keys of ours to recover
1252 // post-backup change.
1254 // Reserve a new key pair from key pool
1256 assert(reservekey.GetReservedKey(vchPubKey)); // should never fail, as we just unlocked
1258 // Fill a vout to ourself
1259 // TODO: pass in scriptChange instead of reservekey so
1260 // change transaction isn't always pay-to-bitcoin-address
1261 CScript scriptChange;
1262 scriptChange.SetDestination(vchPubKey.GetID());
1264 CTxOut newTxOut(nChange, scriptChange);
1266 // Never create dust outputs; if we would, just
1267 // add the dust to the fee.
1268 if (newTxOut.IsDust(CTransaction::nMinRelayTxFee))
1271 reservekey.ReturnKey();
1275 // Insert change txn at random position:
1276 vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size()+1);
1277 wtxNew.vout.insert(position, newTxOut);
1281 reservekey.ReturnKey();
1284 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1285 wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
1289 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1290 if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
1292 strFailReason = _("Signing transaction failed");
1297 unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
1298 if (nBytes >= MAX_STANDARD_TX_SIZE)
1300 strFailReason = _("Transaction too large");
1303 dPriority /= nBytes;
1305 // Check that enough fee is included
1306 int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
1307 bool fAllowFree = AllowFree(dPriority);
1308 int64 nMinFee = GetMinFee(wtxNew, fAllowFree, GMF_SEND);
1309 if (nFeeRet < max(nPayFee, nMinFee))
1311 nFeeRet = max(nPayFee, nMinFee);
1315 // Fill vtxPrev by copying from previous transactions vtxPrev
1316 wtxNew.AddSupportingTransactions();
1317 wtxNew.fTimeReceivedIsTxTime = true;
1326 bool CWallet::CreateTransaction(CScript scriptPubKey, int64 nValue,
1327 CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet, std::string& strFailReason)
1329 vector< pair<CScript, int64> > vecSend;
1330 vecSend.push_back(make_pair(scriptPubKey, nValue));
1331 return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, strFailReason);
1334 // Call after CreateTransaction unless you want to abort
1335 bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
1338 LOCK2(cs_main, cs_wallet);
1339 printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
1341 // This is only to keep the database open to defeat the auto-flush for the
1342 // duration of this scope. This is the only place where this optimization
1343 // maybe makes sense; please don't do it anywhere else.
1344 CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
1346 // Take key pair from key pool so it won't be used again
1347 reservekey.KeepKey();
1349 // Add tx to wallet, because if it has change it's also ours,
1350 // otherwise just for transaction history.
1351 AddToWallet(wtxNew);
1353 // Mark old coins as spent
1354 set<CWalletTx*> setCoins;
1355 BOOST_FOREACH(const CTxIn& txin, wtxNew.vin)
1357 CWalletTx &coin = mapWallet[txin.prevout.hash];
1358 coin.BindWallet(this);
1359 coin.MarkSpent(txin.prevout.n);
1361 NotifyTransactionChanged(this, coin.GetHash(), CT_UPDATED);
1368 // Track how many getdata requests our transaction gets
1369 mapRequestCount[wtxNew.GetHash()] = 0;
1372 if (!wtxNew.AcceptToMemoryPool(false))
1374 // This must not fail. The transaction has already been signed and recorded.
1375 printf("CommitTransaction() : Error: Transaction not valid");
1378 wtxNew.RelayWalletTransaction();
1386 string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
1388 CReserveKey reservekey(this);
1393 string strError = _("Error: Wallet locked, unable to create transaction!");
1394 printf("SendMoney() : %s", strError.c_str());
1398 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError))
1400 if (nValue + nFeeRequired > GetBalance())
1401 strError = strprintf(_("Error: This transaction requires a transaction fee of at least %s because of its amount, complexity, or use of recently received funds!"), FormatMoney(nFeeRequired).c_str());
1402 printf("SendMoney() : %s\n", strError.c_str());
1406 if (fAskFee && !uiInterface.ThreadSafeAskFee(nFeeRequired))
1409 if (!CommitTransaction(wtxNew, reservekey))
1410 return _("Error: The transaction was rejected! This might happen if some of the coins in your wallet were already spent, such as if you used a copy of wallet.dat and coins were spent in the copy but not marked as spent here.");
1417 string CWallet::SendMoneyToDestination(const CTxDestination& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
1421 return _("Invalid amount");
1422 if (nValue + nTransactionFee > GetBalance())
1423 return _("Insufficient funds");
1425 // Parse Bitcoin address
1426 CScript scriptPubKey;
1427 scriptPubKey.SetDestination(address);
1429 return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
1435 DBErrors CWallet::LoadWallet(bool& fFirstRunRet)
1439 fFirstRunRet = false;
1440 DBErrors nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
1441 if (nLoadWalletRet == DB_NEED_REWRITE)
1443 if (CDB::Rewrite(strWalletFile, "\x04pool"))
1446 // Note: can't top-up keypool here, because wallet is locked.
1447 // User will be prompted to unlock wallet the next operation
1448 // the requires a new key.
1452 if (nLoadWalletRet != DB_LOAD_OK)
1453 return nLoadWalletRet;
1454 fFirstRunRet = !vchDefaultKey.IsValid();
1460 bool CWallet::SetAddressBook(const CTxDestination& address, const string& strName, const string& strPurpose)
1462 std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
1463 mapAddressBook[address].name = strName;
1464 NotifyAddressBookChanged(this, address, strName, ::IsMine(*this, address), (mi == mapAddressBook.end()) ? CT_NEW : CT_UPDATED);
1467 if (!strPurpose.empty() && !CWalletDB(strWalletFile).WritePurpose(CBitcoinAddress(address).ToString(), strPurpose))
1469 return CWalletDB(strWalletFile).WriteName(CBitcoinAddress(address).ToString(), strName);
1472 bool CWallet::DelAddressBook(const CTxDestination& address)
1474 mapAddressBook.erase(address);
1475 NotifyAddressBookChanged(this, address, "", ::IsMine(*this, address), CT_DELETED);
1478 CWalletDB(strWalletFile).ErasePurpose(CBitcoinAddress(address).ToString());
1479 return CWalletDB(strWalletFile).EraseName(CBitcoinAddress(address).ToString());
1482 void CWallet::PrintWallet(const CBlock& block)
1486 if (mapWallet.count(block.vtx[0].GetHash()))
1488 CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
1489 printf(" mine: %d %d %"PRI64d"", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
1495 bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx)
1499 map<uint256, CWalletTx>::iterator mi = mapWallet.find(hashTx);
1500 if (mi != mapWallet.end())
1509 bool CWallet::SetDefaultKey(const CPubKey &vchPubKey)
1513 if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
1516 vchDefaultKey = vchPubKey;
1520 bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
1522 if (!pwallet->fFileBacked)
1524 strWalletFileOut = pwallet->strWalletFile;
1529 // Mark old keypool keys as used,
1530 // and generate all new keys
1532 bool CWallet::NewKeyPool()
1536 CWalletDB walletdb(strWalletFile);
1537 BOOST_FOREACH(int64 nIndex, setKeyPool)
1538 walletdb.ErasePool(nIndex);
1544 int64 nKeys = max(GetArg("-keypool", 100), (int64)0);
1545 for (int i = 0; i < nKeys; i++)
1548 walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
1549 setKeyPool.insert(nIndex);
1551 printf("CWallet::NewKeyPool wrote %"PRI64d" new keys\n", nKeys);
1556 bool CWallet::TopUpKeyPool(unsigned int kpSize)
1564 CWalletDB walletdb(strWalletFile);
1567 unsigned int nTargetSize;
1569 nTargetSize = kpSize;
1571 nTargetSize = max(GetArg("-keypool", 100), 0LL);
1573 while (setKeyPool.size() < (nTargetSize + 1))
1576 if (!setKeyPool.empty())
1577 nEnd = *(--setKeyPool.end()) + 1;
1578 if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
1579 throw runtime_error("TopUpKeyPool() : writing generated key failed");
1580 setKeyPool.insert(nEnd);
1581 printf("keypool added key %"PRI64d", size=%"PRIszu"\n", nEnd, setKeyPool.size());
1587 void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
1590 keypool.vchPubKey = CPubKey();
1597 // Get the oldest key
1598 if(setKeyPool.empty())
1601 CWalletDB walletdb(strWalletFile);
1603 nIndex = *(setKeyPool.begin());
1604 setKeyPool.erase(setKeyPool.begin());
1605 if (!walletdb.ReadPool(nIndex, keypool))
1606 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
1607 if (!HaveKey(keypool.vchPubKey.GetID()))
1608 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
1609 assert(keypool.vchPubKey.IsValid());
1610 printf("keypool reserve %"PRI64d"\n", nIndex);
1614 int64 CWallet::AddReserveKey(const CKeyPool& keypool)
1617 LOCK2(cs_main, cs_wallet);
1618 CWalletDB walletdb(strWalletFile);
1620 int64 nIndex = 1 + *(--setKeyPool.end());
1621 if (!walletdb.WritePool(nIndex, keypool))
1622 throw runtime_error("AddReserveKey() : writing added key failed");
1623 setKeyPool.insert(nIndex);
1629 void CWallet::KeepKey(int64 nIndex)
1631 // Remove from key pool
1634 CWalletDB walletdb(strWalletFile);
1635 walletdb.ErasePool(nIndex);
1637 printf("keypool keep %"PRI64d"\n", nIndex);
1640 void CWallet::ReturnKey(int64 nIndex)
1642 // Return to key pool
1645 setKeyPool.insert(nIndex);
1647 printf("keypool return %"PRI64d"\n", nIndex);
1650 bool CWallet::GetKeyFromPool(CPubKey& result, bool fAllowReuse)
1656 ReserveKeyFromKeyPool(nIndex, keypool);
1659 if (fAllowReuse && vchDefaultKey.IsValid())
1661 result = vchDefaultKey;
1664 if (IsLocked()) return false;
1665 result = GenerateNewKey();
1669 result = keypool.vchPubKey;
1674 int64 CWallet::GetOldestKeyPoolTime()
1678 ReserveKeyFromKeyPool(nIndex, keypool);
1682 return keypool.nTime;
1685 std::map<CTxDestination, int64> CWallet::GetAddressBalances()
1687 map<CTxDestination, int64> balances;
1691 BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
1693 CWalletTx *pcoin = &walletEntry.second;
1695 if (!IsFinalTx(*pcoin) || !pcoin->IsConfirmed())
1698 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
1701 int nDepth = pcoin->GetDepthInMainChain();
1702 if (nDepth < (pcoin->IsFromMe() ? 0 : 1))
1705 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
1707 CTxDestination addr;
1708 if (!IsMine(pcoin->vout[i]))
1710 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, addr))
1713 int64 n = pcoin->IsSpent(i) ? 0 : pcoin->vout[i].nValue;
1715 if (!balances.count(addr))
1717 balances[addr] += n;
1725 set< set<CTxDestination> > CWallet::GetAddressGroupings()
1727 set< set<CTxDestination> > groupings;
1728 set<CTxDestination> grouping;
1730 BOOST_FOREACH(PAIRTYPE(uint256, CWalletTx) walletEntry, mapWallet)
1732 CWalletTx *pcoin = &walletEntry.second;
1734 if (pcoin->vin.size() > 0)
1736 bool any_mine = false;
1737 // group all input addresses with each other
1738 BOOST_FOREACH(CTxIn txin, pcoin->vin)
1740 CTxDestination address;
1741 if(!IsMine(txin)) /* If this input isn't mine, ignore it */
1743 if(!ExtractDestination(mapWallet[txin.prevout.hash].vout[txin.prevout.n].scriptPubKey, address))
1745 grouping.insert(address);
1749 // group change with input addresses
1752 BOOST_FOREACH(CTxOut txout, pcoin->vout)
1753 if (IsChange(txout))
1755 CTxDestination txoutAddr;
1756 if(!ExtractDestination(txout.scriptPubKey, txoutAddr))
1758 grouping.insert(txoutAddr);
1761 if (grouping.size() > 0)
1763 groupings.insert(grouping);
1768 // group lone addrs by themselves
1769 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
1770 if (IsMine(pcoin->vout[i]))
1772 CTxDestination address;
1773 if(!ExtractDestination(pcoin->vout[i].scriptPubKey, address))
1775 grouping.insert(address);
1776 groupings.insert(grouping);
1781 set< set<CTxDestination>* > uniqueGroupings; // a set of pointers to groups of addresses
1782 map< CTxDestination, set<CTxDestination>* > setmap; // map addresses to the unique group containing it
1783 BOOST_FOREACH(set<CTxDestination> grouping, groupings)
1785 // make a set of all the groups hit by this new group
1786 set< set<CTxDestination>* > hits;
1787 map< CTxDestination, set<CTxDestination>* >::iterator it;
1788 BOOST_FOREACH(CTxDestination address, grouping)
1789 if ((it = setmap.find(address)) != setmap.end())
1790 hits.insert((*it).second);
1792 // merge all hit groups into a new single group and delete old groups
1793 set<CTxDestination>* merged = new set<CTxDestination>(grouping);
1794 BOOST_FOREACH(set<CTxDestination>* hit, hits)
1796 merged->insert(hit->begin(), hit->end());
1797 uniqueGroupings.erase(hit);
1800 uniqueGroupings.insert(merged);
1803 BOOST_FOREACH(CTxDestination element, *merged)
1804 setmap[element] = merged;
1807 set< set<CTxDestination> > ret;
1808 BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings)
1810 ret.insert(*uniqueGrouping);
1811 delete uniqueGrouping;
1817 set<CTxDestination> CWallet::GetAccountAddresses(string strAccount) const
1819 set<CTxDestination> result;
1820 BOOST_FOREACH(const PAIRTYPE(CTxDestination, CAddressBookData)& item, mapAddressBook)
1822 const CTxDestination& address = item.first;
1823 const string& strName = item.second.name;
1824 if (strName == strAccount)
1825 result.insert(address);
1830 bool CReserveKey::GetReservedKey(CPubKey& pubkey)
1835 pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
1837 vchPubKey = keypool.vchPubKey;
1839 if (pwallet->vchDefaultKey.IsValid()) {
1840 printf("CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
1841 vchPubKey = pwallet->vchDefaultKey;
1846 assert(vchPubKey.IsValid());
1851 void CReserveKey::KeepKey()
1854 pwallet->KeepKey(nIndex);
1856 vchPubKey = CPubKey();
1859 void CReserveKey::ReturnKey()
1862 pwallet->ReturnKey(nIndex);
1864 vchPubKey = CPubKey();
1867 void CWallet::GetAllReserveKeys(set<CKeyID>& setAddress) const
1871 CWalletDB walletdb(strWalletFile);
1873 LOCK2(cs_main, cs_wallet);
1874 BOOST_FOREACH(const int64& id, setKeyPool)
1877 if (!walletdb.ReadPool(id, keypool))
1878 throw runtime_error("GetAllReserveKeyHashes() : read failed");
1879 assert(keypool.vchPubKey.IsValid());
1880 CKeyID keyID = keypool.vchPubKey.GetID();
1881 if (!HaveKey(keyID))
1882 throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
1883 setAddress.insert(keyID);
1887 void CWallet::UpdatedTransaction(const uint256 &hashTx)
1891 // Only notify UI if this transaction is in this wallet
1892 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx);
1893 if (mi != mapWallet.end())
1894 NotifyTransactionChanged(this, hashTx, CT_UPDATED);
1898 void CWallet::LockCoin(COutPoint& output)
1900 setLockedCoins.insert(output);
1903 void CWallet::UnlockCoin(COutPoint& output)
1905 setLockedCoins.erase(output);
1908 void CWallet::UnlockAllCoins()
1910 setLockedCoins.clear();
1913 bool CWallet::IsLockedCoin(uint256 hash, unsigned int n) const
1915 COutPoint outpt(hash, n);
1917 return (setLockedCoins.count(outpt) > 0);
1920 void CWallet::ListLockedCoins(std::vector<COutPoint>& vOutpts)
1922 for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
1923 it != setLockedCoins.end(); it++) {
1924 COutPoint outpt = (*it);
1925 vOutpts.push_back(outpt);
1929 void CWallet::GetKeyBirthTimes(std::map<CKeyID, int64> &mapKeyBirth) const {
1930 mapKeyBirth.clear();
1932 // get birth times for keys with metadata
1933 for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
1934 if (it->second.nCreateTime)
1935 mapKeyBirth[it->first] = it->second.nCreateTime;
1937 // map in which we'll infer heights of other keys
1938 CBlockIndex *pindexMax = FindBlockByHeight(std::max(0, nBestHeight - 144)); // the tip can be reorganised; use a 144-block safety margin
1939 std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
1940 std::set<CKeyID> setKeys;
1942 BOOST_FOREACH(const CKeyID &keyid, setKeys) {
1943 if (mapKeyBirth.count(keyid) == 0)
1944 mapKeyFirstBlock[keyid] = pindexMax;
1948 // if there are no such keys, we're done
1949 if (mapKeyFirstBlock.empty())
1952 // find first block that affects those keys, if there are any left
1953 std::vector<CKeyID> vAffected;
1954 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
1955 // iterate over all wallet transactions...
1956 const CWalletTx &wtx = (*it).second;
1957 std::map<uint256, CBlockIndex*>::const_iterator blit = mapBlockIndex.find(wtx.hashBlock);
1958 if (blit != mapBlockIndex.end() && blit->second->IsInMainChain()) {
1959 // ... which are already in a block
1960 int nHeight = blit->second->nHeight;
1961 BOOST_FOREACH(const CTxOut &txout, wtx.vout) {
1962 // iterate over all their outputs
1963 ::ExtractAffectedKeys(*this, txout.scriptPubKey, vAffected);
1964 BOOST_FOREACH(const CKeyID &keyid, vAffected) {
1965 // ... and all their affected keys
1966 std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid);
1967 if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight)
1968 rit->second = blit->second;
1975 // Extract block timestamps for those keys
1976 for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
1977 mapKeyBirth[it->first] = it->second->nTime - 7200; // block times can be 2h off