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"
14 //////////////////////////////////////////////////////////////////////////////
19 std::vector<unsigned char> CWallet::GenerateNewKey()
21 bool fCompressed = CanSupportFeature(FEATURE_COMPRPUBKEY); // default to compressed public keys if we want 0.6.0 wallets
25 key.MakeNewKey(fCompressed);
27 // Compressed public keys were introduced in version 0.6.0
29 SetMinVersion(FEATURE_COMPRPUBKEY);
32 throw std::runtime_error("CWallet::GenerateNewKey() : AddKey failed");
33 return key.GetPubKey();
36 bool CWallet::AddKey(const CKey& key)
38 if (!CCryptoKeyStore::AddKey(key))
43 return CWalletDB(strWalletFile).WriteKey(key.GetPubKey(), key.GetPrivKey());
47 bool CWallet::AddCryptedKey(const vector<unsigned char> &vchPubKey, const vector<unsigned char> &vchCryptedSecret)
49 if (!CCryptoKeyStore::AddCryptedKey(vchPubKey, vchCryptedSecret))
55 if (pwalletdbEncryption)
56 return pwalletdbEncryption->WriteCryptedKey(vchPubKey, vchCryptedSecret);
58 return CWalletDB(strWalletFile).WriteCryptedKey(vchPubKey, vchCryptedSecret);
63 bool CWallet::AddCScript(const CScript& redeemScript)
65 if (!CCryptoKeyStore::AddCScript(redeemScript))
69 return CWalletDB(strWalletFile).WriteCScript(Hash160(redeemScript), redeemScript);
72 bool CWallet::Unlock(const SecureString& strWalletPassphrase)
78 CKeyingMaterial vMasterKey;
82 BOOST_FOREACH(const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
84 if(!crypter.SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
86 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
88 if (CCryptoKeyStore::Unlock(vMasterKey))
95 bool CWallet::ChangeWalletPassphrase(const SecureString& strOldWalletPassphrase, const SecureString& strNewWalletPassphrase)
97 bool fWasLocked = IsLocked();
104 CKeyingMaterial vMasterKey;
105 BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
107 if(!crypter.SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
109 if (!crypter.Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
111 if (CCryptoKeyStore::Unlock(vMasterKey))
113 int64 nStartTime = GetTimeMillis();
114 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
115 pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(GetTimeMillis() - nStartTime)));
117 nStartTime = GetTimeMillis();
118 crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
119 pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
121 if (pMasterKey.second.nDeriveIterations < 25000)
122 pMasterKey.second.nDeriveIterations = 25000;
124 printf("Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
126 if (!crypter.SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
128 if (!crypter.Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
130 CWalletDB(strWalletFile).WriteMasterKey(pMasterKey.first, pMasterKey.second);
141 void CWallet::SetBestChain(const CBlockLocator& loc)
143 CWalletDB walletdb(strWalletFile);
144 walletdb.WriteBestBlock(loc);
147 // This class implements an addrIncoming entry that causes pre-0.4
148 // clients to crash on startup if reading a private-key-encrypted wallet.
149 class CCorruptAddress
154 if (nType & SER_DISK)
159 bool CWallet::SetMinVersion(enum WalletFeature nVersion, CWalletDB* pwalletdbIn, bool fExplicit)
161 if (nWalletVersion >= nVersion)
164 // when doing an explicit upgrade, if we pass the max version permitted, upgrade all the way
165 if (fExplicit && nVersion > nWalletMaxVersion)
166 nVersion = FEATURE_LATEST;
168 nWalletVersion = nVersion;
170 if (nVersion > nWalletMaxVersion)
171 nWalletMaxVersion = nVersion;
175 CWalletDB* pwalletdb = pwalletdbIn ? pwalletdbIn : new CWalletDB(strWalletFile);
176 if (nWalletVersion >= 40000)
178 // Versions prior to 0.4.0 did not support the "minversion" record.
179 // Use a CCorruptAddress to make them crash instead.
180 CCorruptAddress corruptAddress;
181 pwalletdb->WriteSetting("addrIncoming", corruptAddress);
183 if (nWalletVersion > 40000)
184 pwalletdb->WriteMinVersion(nWalletVersion);
192 bool CWallet::SetMaxVersion(int nVersion)
194 // cannot downgrade below current version
195 if (nWalletVersion > nVersion)
198 nWalletMaxVersion = nVersion;
203 bool CWallet::EncryptWallet(const SecureString& strWalletPassphrase)
208 CKeyingMaterial vMasterKey;
209 RandAddSeedPerfmon();
211 vMasterKey.resize(WALLET_CRYPTO_KEY_SIZE);
212 RAND_bytes(&vMasterKey[0], WALLET_CRYPTO_KEY_SIZE);
214 CMasterKey kMasterKey;
216 RandAddSeedPerfmon();
217 kMasterKey.vchSalt.resize(WALLET_CRYPTO_SALT_SIZE);
218 RAND_bytes(&kMasterKey.vchSalt[0], WALLET_CRYPTO_SALT_SIZE);
221 int64 nStartTime = GetTimeMillis();
222 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, 25000, kMasterKey.nDerivationMethod);
223 kMasterKey.nDeriveIterations = 2500000 / ((double)(GetTimeMillis() - nStartTime));
225 nStartTime = GetTimeMillis();
226 crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod);
227 kMasterKey.nDeriveIterations = (kMasterKey.nDeriveIterations + kMasterKey.nDeriveIterations * 100 / ((double)(GetTimeMillis() - nStartTime))) / 2;
229 if (kMasterKey.nDeriveIterations < 25000)
230 kMasterKey.nDeriveIterations = 25000;
232 printf("Encrypting Wallet with an nDeriveIterations of %i\n", kMasterKey.nDeriveIterations);
234 if (!crypter.SetKeyFromPassphrase(strWalletPassphrase, kMasterKey.vchSalt, kMasterKey.nDeriveIterations, kMasterKey.nDerivationMethod))
236 if (!crypter.Encrypt(vMasterKey, kMasterKey.vchCryptedKey))
241 mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
244 pwalletdbEncryption = new CWalletDB(strWalletFile);
245 if (!pwalletdbEncryption->TxnBegin())
247 pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
250 if (!EncryptKeys(vMasterKey))
253 pwalletdbEncryption->TxnAbort();
254 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.
257 // Encryption was introduced in version 0.4.0
258 SetMinVersion(FEATURE_WALLETCRYPT, pwalletdbEncryption, true);
262 if (!pwalletdbEncryption->TxnCommit())
263 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.
265 delete pwalletdbEncryption;
266 pwalletdbEncryption = NULL;
270 Unlock(strWalletPassphrase);
274 // Need to completely rewrite the wallet file; if we don't, bdb might keep
275 // bits of the unencrypted private key in slack space in the database file.
276 CDB::Rewrite(strWalletFile);
282 void CWallet::WalletUpdateSpent(const CTransaction &tx)
284 // Anytime a signature is successfully verified, it's proof the outpoint is spent.
285 // Update the wallet spent flag if it doesn't know due to wallet.dat being
286 // restored from backup or the user making copies of wallet.dat.
289 BOOST_FOREACH(const CTxIn& txin, tx.vin)
291 map<uint256, CWalletTx>::iterator mi = mapWallet.find(txin.prevout.hash);
292 if (mi != mapWallet.end())
294 CWalletTx& wtx = (*mi).second;
295 if (!wtx.IsSpent(txin.prevout.n) && IsMine(wtx.vout[txin.prevout.n]))
297 printf("WalletUpdateSpent found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
298 wtx.MarkSpent(txin.prevout.n);
300 vWalletUpdated.push_back(txin.prevout.hash);
307 void CWallet::MarkDirty()
311 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
312 item.second.MarkDirty();
316 bool CWallet::AddToWallet(const CWalletTx& wtxIn)
318 uint256 hash = wtxIn.GetHash();
321 // Inserts only if not already there, returns tx inserted or tx found
322 pair<map<uint256, CWalletTx>::iterator, bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
323 CWalletTx& wtx = (*ret.first).second;
324 wtx.BindWallet(this);
325 bool fInsertedNew = ret.second;
327 wtx.nTimeReceived = GetAdjustedTime();
329 bool fUpdated = false;
333 if (wtxIn.hashBlock != 0 && wtxIn.hashBlock != wtx.hashBlock)
335 wtx.hashBlock = wtxIn.hashBlock;
338 if (wtxIn.nIndex != -1 && (wtxIn.vMerkleBranch != wtx.vMerkleBranch || wtxIn.nIndex != wtx.nIndex))
340 wtx.vMerkleBranch = wtxIn.vMerkleBranch;
341 wtx.nIndex = wtxIn.nIndex;
344 if (wtxIn.fFromMe && wtxIn.fFromMe != wtx.fFromMe)
346 wtx.fFromMe = wtxIn.fFromMe;
349 fUpdated |= wtx.UpdateSpent(wtxIn.vfSpent);
353 printf("AddToWallet %s %s%s\n", wtxIn.GetHash().ToString().substr(0,10).c_str(), (fInsertedNew ? "new" : ""), (fUpdated ? "update" : ""));
356 if (fInsertedNew || fUpdated)
357 if (!wtx.WriteToDisk())
360 // If default receiving address gets used, replace it with a new one
361 CScript scriptDefaultKey;
362 scriptDefaultKey.SetBitcoinAddress(vchDefaultKey);
363 BOOST_FOREACH(const CTxOut& txout, wtx.vout)
365 if (txout.scriptPubKey == scriptDefaultKey)
367 std::vector<unsigned char> newDefaultKey;
368 if (GetKeyFromPool(newDefaultKey, false))
370 SetDefaultKey(newDefaultKey);
371 SetAddressBookName(CBitcoinAddress(vchDefaultKey), "");
377 vWalletUpdated.push_back(hash);
379 // since AddToWallet is called directly for self-originating transactions, check for consumption of own coins
380 WalletUpdateSpent(wtx);
388 // Add a transaction to the wallet, or update it.
389 // pblock is optional, but should be provided if the transaction is known to be in a block.
390 // If fUpdate is true, existing transactions will be updated.
391 bool CWallet::AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate, bool fFindBlock)
393 uint256 hash = tx.GetHash();
396 bool fExisted = mapWallet.count(hash);
397 if (fExisted && !fUpdate) return false;
398 if (fExisted || IsMine(tx) || IsFromMe(tx))
400 CWalletTx wtx(this,tx);
401 // Get merkle branch if transaction was found in a block
403 wtx.SetMerkleBranch(pblock);
404 return AddToWallet(wtx);
407 WalletUpdateSpent(tx);
412 bool CWallet::EraseFromWallet(uint256 hash)
418 if (mapWallet.erase(hash))
419 CWalletDB(strWalletFile).EraseTx(hash);
425 bool CWallet::IsMine(const CTxIn &txin) const
429 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
430 if (mi != mapWallet.end())
432 const CWalletTx& prev = (*mi).second;
433 if (txin.prevout.n < prev.vout.size())
434 if (IsMine(prev.vout[txin.prevout.n]))
441 int64 CWallet::GetDebit(const CTxIn &txin) const
445 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.prevout.hash);
446 if (mi != mapWallet.end())
448 const CWalletTx& prev = (*mi).second;
449 if (txin.prevout.n < prev.vout.size())
450 if (IsMine(prev.vout[txin.prevout.n]))
451 return prev.vout[txin.prevout.n].nValue;
457 bool CWallet::IsChange(const CTxOut& txout) const
459 CBitcoinAddress address;
461 // TODO: fix handling of 'change' outputs. The assumption is that any
462 // payment to a TX_PUBKEYHASH that is mine but isn't in the address book
463 // is change. That assumption is likely to break when we implement multisignature
464 // wallets that return change back into a multi-signature-protected address;
465 // a better way of identifying which outputs are 'the send' and which are
466 // 'the change' will need to be implemented (maybe extend CWalletTx to remember
467 // which output, if any, was change).
468 if (ExtractAddress(txout.scriptPubKey, address) && HaveKey(address))
471 if (!mapAddressBook.count(address))
477 int64 CWalletTx::GetTxTime() const
479 return nTimeReceived;
482 int CWalletTx::GetRequestCount() const
484 // Returns -1 if it wasn't being tracked
487 LOCK(pwallet->cs_wallet);
493 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
494 if (mi != pwallet->mapRequestCount.end())
495 nRequests = (*mi).second;
500 // Did anyone request this transaction?
501 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
502 if (mi != pwallet->mapRequestCount.end())
504 nRequests = (*mi).second;
506 // How about the block it's in?
507 if (nRequests == 0 && hashBlock != 0)
509 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
510 if (mi != pwallet->mapRequestCount.end())
511 nRequests = (*mi).second;
513 nRequests = 1; // If it's in someone else's block it must have got out
521 void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list<pair<CBitcoinAddress, int64> >& listReceived,
522 list<pair<CBitcoinAddress, int64> >& listSent, int64& nFee, string& strSentAccount) const
524 nGeneratedImmature = nGeneratedMature = nFee = 0;
525 listReceived.clear();
527 strSentAccount = strFromAccount;
531 if (GetBlocksToMaturity() > 0)
532 nGeneratedImmature = pwallet->GetCredit(*this);
534 nGeneratedMature = GetCredit();
539 int64 nDebit = GetDebit();
540 if (nDebit > 0) // debit>0 means we signed/sent this transaction
542 int64 nValueOut = GetValueOut();
543 nFee = nDebit - nValueOut;
547 BOOST_FOREACH(const CTxOut& txout, vout)
549 CBitcoinAddress address;
550 vector<unsigned char> vchPubKey;
551 if (!ExtractAddress(txout.scriptPubKey, address))
553 printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
554 this->GetHash().ToString().c_str());
555 address = " unknown ";
558 // Don't report 'change' txouts
559 if (nDebit > 0 && pwallet->IsChange(txout))
563 listSent.push_back(make_pair(address, txout.nValue));
565 if (pwallet->IsMine(txout))
566 listReceived.push_back(make_pair(address, txout.nValue));
571 void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, int64& nReceived,
572 int64& nSent, int64& nFee) const
574 nGenerated = nReceived = nSent = nFee = 0;
576 int64 allGeneratedImmature, allGeneratedMature, allFee;
577 allGeneratedImmature = allGeneratedMature = allFee = 0;
578 string strSentAccount;
579 list<pair<CBitcoinAddress, int64> > listReceived;
580 list<pair<CBitcoinAddress, int64> > listSent;
581 GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount);
583 if (strAccount == "")
584 nGenerated = allGeneratedMature;
585 if (strAccount == strSentAccount)
587 BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& s, listSent)
592 LOCK(pwallet->cs_wallet);
593 BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listReceived)
595 if (pwallet->mapAddressBook.count(r.first))
597 map<CBitcoinAddress, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
598 if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount)
599 nReceived += r.second;
601 else if (strAccount.empty())
603 nReceived += r.second;
609 void CWalletTx::AddSupportingTransactions(CTxDB& txdb)
613 const int COPY_DEPTH = 3;
614 if (SetMerkleBranch() < COPY_DEPTH)
616 vector<uint256> vWorkQueue;
617 BOOST_FOREACH(const CTxIn& txin, vin)
618 vWorkQueue.push_back(txin.prevout.hash);
620 // This critsect is OK because txdb is already open
622 LOCK(pwallet->cs_wallet);
623 map<uint256, const CMerkleTx*> mapWalletPrev;
624 set<uint256> setAlreadyDone;
625 for (unsigned int i = 0; i < vWorkQueue.size(); i++)
627 uint256 hash = vWorkQueue[i];
628 if (setAlreadyDone.count(hash))
630 setAlreadyDone.insert(hash);
633 map<uint256, CWalletTx>::const_iterator mi = pwallet->mapWallet.find(hash);
634 if (mi != pwallet->mapWallet.end())
637 BOOST_FOREACH(const CMerkleTx& txWalletPrev, (*mi).second.vtxPrev)
638 mapWalletPrev[txWalletPrev.GetHash()] = &txWalletPrev;
640 else if (mapWalletPrev.count(hash))
642 tx = *mapWalletPrev[hash];
644 else if (!fClient && txdb.ReadDiskTx(hash, tx))
650 printf("ERROR: AddSupportingTransactions() : unsupported transaction\n");
654 int nDepth = tx.SetMerkleBranch();
655 vtxPrev.push_back(tx);
657 if (nDepth < COPY_DEPTH)
659 BOOST_FOREACH(const CTxIn& txin, tx.vin)
660 vWorkQueue.push_back(txin.prevout.hash);
666 reverse(vtxPrev.begin(), vtxPrev.end());
669 bool CWalletTx::WriteToDisk()
671 return CWalletDB(pwallet->strWalletFile).WriteTx(GetHash(), *this);
674 // Scan the block chain (starting in pindexStart) for transactions
675 // from or to us. If fUpdate is true, found transactions that already
676 // exist in the wallet will be updated.
677 int CWallet::ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate)
681 CBlockIndex* pindex = pindexStart;
687 block.ReadFromDisk(pindex, true);
688 BOOST_FOREACH(CTransaction& tx, block.vtx)
690 if (AddToWalletIfInvolvingMe(tx, &block, fUpdate))
693 pindex = pindex->pnext;
699 int CWallet::ScanForWalletTransaction(const uint256& hashTx)
702 tx.ReadFromDisk(COutPoint(hashTx, 0));
703 if (AddToWalletIfInvolvingMe(tx, NULL, true, true))
708 void CWallet::ReacceptWalletTransactions()
716 vector<CDiskTxPos> vMissingTx;
717 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
719 CWalletTx& wtx = item.second;
720 if (wtx.IsCoinBase() && wtx.IsSpent(0))
724 bool fUpdated = false;
725 if (txdb.ReadTxIndex(wtx.GetHash(), txindex))
727 // Update fSpent if a tx got spent somewhere else by a copy of wallet.dat
728 if (txindex.vSpent.size() != wtx.vout.size())
730 printf("ERROR: ReacceptWalletTransactions() : txindex.vSpent.size() %d != wtx.vout.size() %d\n", txindex.vSpent.size(), wtx.vout.size());
733 for (unsigned int i = 0; i < txindex.vSpent.size(); i++)
737 if (!txindex.vSpent[i].IsNull() && IsMine(wtx.vout[i]))
741 vMissingTx.push_back(txindex.vSpent[i]);
746 printf("ReacceptWalletTransactions found spent coin %sbc %s\n", FormatMoney(wtx.GetCredit()).c_str(), wtx.GetHash().ToString().c_str());
753 // Reaccept any txes of ours that aren't already in a block
754 if (!wtx.IsCoinBase())
755 wtx.AcceptWalletTransaction(txdb, false);
758 if (!vMissingTx.empty())
760 // TODO: optimize this to scan just part of the block chain?
761 if (ScanForWalletTransactions(pindexGenesisBlock))
762 fRepeat = true; // Found missing transactions: re-do Reaccept.
767 void CWalletTx::RelayWalletTransaction(CTxDB& txdb)
769 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
771 if (!tx.IsCoinBase())
773 uint256 hash = tx.GetHash();
774 if (!txdb.ContainsTx(hash))
775 RelayMessage(CInv(MSG_TX, hash), (CTransaction)tx);
780 uint256 hash = GetHash();
781 if (!txdb.ContainsTx(hash))
783 printf("Relaying wtx %s\n", hash.ToString().substr(0,10).c_str());
784 RelayMessage(CInv(MSG_TX, hash), (CTransaction)*this);
789 void CWalletTx::RelayWalletTransaction()
792 RelayWalletTransaction(txdb);
795 void CWallet::ResendWalletTransactions()
797 // Do this infrequently and randomly to avoid giving away
798 // that these are our transactions.
799 static int64 nNextTime;
800 if (GetTime() < nNextTime)
802 bool fFirst = (nNextTime == 0);
803 nNextTime = GetTime() + GetRand(30 * 60);
807 // Only do it if there's been a new block since last time
808 static int64 nLastTime;
809 if (nTimeBestReceived < nLastTime)
811 nLastTime = GetTime();
813 // Rebroadcast any of our txes that aren't in a block yet
814 printf("ResendWalletTransactions()\n");
818 // Sort them in chronological order
819 multimap<unsigned int, CWalletTx*> mapSorted;
820 BOOST_FOREACH(PAIRTYPE(const uint256, CWalletTx)& item, mapWallet)
822 CWalletTx& wtx = item.second;
823 // Don't rebroadcast until it's had plenty of time that
824 // it should have gotten in already by now.
825 if (nTimeBestReceived - (int64)wtx.nTimeReceived > 5 * 60)
826 mapSorted.insert(make_pair(wtx.nTimeReceived, &wtx));
828 BOOST_FOREACH(PAIRTYPE(const unsigned int, CWalletTx*)& item, mapSorted)
830 CWalletTx& wtx = *item.second;
831 wtx.RelayWalletTransaction(txdb);
841 //////////////////////////////////////////////////////////////////////////////
847 int64 CWallet::GetBalance() const
852 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
854 const CWalletTx* pcoin = &(*it).second;
855 if (!pcoin->IsFinal() || !pcoin->IsConfirmed())
857 nTotal += pcoin->GetAvailableCredit();
864 int64 CWallet::GetUnconfirmedBalance() const
869 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
871 const CWalletTx* pcoin = &(*it).second;
872 if (pcoin->IsFinal() && pcoin->IsConfirmed())
874 nTotal += pcoin->GetAvailableCredit();
880 bool CWallet::SelectCoinsMinConf(int64 nTargetValue, int nConfMine, int nConfTheirs, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
885 // List of values less than target
886 pair<int64, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
887 coinLowestLarger.first = std::numeric_limits<int64>::max();
888 coinLowestLarger.second.first = NULL;
889 vector<pair<int64, pair<const CWalletTx*,unsigned int> > > vValue;
890 int64 nTotalLower = 0;
894 vector<const CWalletTx*> vCoins;
895 vCoins.reserve(mapWallet.size());
896 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
897 vCoins.push_back(&(*it).second);
898 random_shuffle(vCoins.begin(), vCoins.end(), GetRandInt);
900 BOOST_FOREACH(const CWalletTx* pcoin, vCoins)
902 if (!pcoin->IsFinal() || !pcoin->IsConfirmed())
905 if (pcoin->IsCoinBase() && pcoin->GetBlocksToMaturity() > 0)
908 int nDepth = pcoin->GetDepthInMainChain();
909 if (nDepth < (pcoin->IsFromMe() ? nConfMine : nConfTheirs))
912 for (unsigned int i = 0; i < pcoin->vout.size(); i++)
914 if (pcoin->IsSpent(i) || !IsMine(pcoin->vout[i]))
917 int64 n = pcoin->vout[i].nValue;
922 pair<int64,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin,i));
924 if (n == nTargetValue)
926 setCoinsRet.insert(coin.second);
927 nValueRet += coin.first;
930 else if (n < nTargetValue + CENT)
932 vValue.push_back(coin);
935 else if (n < coinLowestLarger.first)
937 coinLowestLarger = coin;
943 if (nTotalLower == nTargetValue || nTotalLower == nTargetValue + CENT)
945 for (unsigned int i = 0; i < vValue.size(); ++i)
947 setCoinsRet.insert(vValue[i].second);
948 nValueRet += vValue[i].first;
953 if (nTotalLower < nTargetValue + (coinLowestLarger.second.first ? CENT : 0))
955 if (coinLowestLarger.second.first == NULL)
957 setCoinsRet.insert(coinLowestLarger.second);
958 nValueRet += coinLowestLarger.first;
962 if (nTotalLower >= nTargetValue + CENT)
963 nTargetValue += CENT;
965 // Solve subset sum by stochastic approximation
966 sort(vValue.rbegin(), vValue.rend());
967 vector<char> vfIncluded;
968 vector<char> vfBest(vValue.size(), true);
969 int64 nBest = nTotalLower;
971 for (int nRep = 0; nRep < 1000 && nBest != nTargetValue; nRep++)
973 vfIncluded.assign(vValue.size(), false);
975 bool fReachedTarget = false;
976 for (int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
978 for (unsigned int i = 0; i < vValue.size(); i++)
980 if (nPass == 0 ? rand() % 2 : !vfIncluded[i])
982 nTotal += vValue[i].first;
983 vfIncluded[i] = true;
984 if (nTotal >= nTargetValue)
986 fReachedTarget = true;
992 nTotal -= vValue[i].first;
993 vfIncluded[i] = false;
1000 // If the next larger is still closer, return it
1001 if (coinLowestLarger.second.first && coinLowestLarger.first - nTargetValue <= nBest - nTargetValue)
1003 setCoinsRet.insert(coinLowestLarger.second);
1004 nValueRet += coinLowestLarger.first;
1007 for (unsigned int i = 0; i < vValue.size(); i++)
1010 setCoinsRet.insert(vValue[i].second);
1011 nValueRet += vValue[i].first;
1015 printf("SelectCoins() best subset: ");
1016 for (unsigned int i = 0; i < vValue.size(); i++)
1018 printf("%s ", FormatMoney(vValue[i].first).c_str());
1019 printf("total %s\n", FormatMoney(nBest).c_str());
1025 bool CWallet::SelectCoins(int64 nTargetValue, set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64& nValueRet) const
1027 return (SelectCoinsMinConf(nTargetValue, 1, 6, setCoinsRet, nValueRet) ||
1028 SelectCoinsMinConf(nTargetValue, 1, 1, setCoinsRet, nValueRet) ||
1029 SelectCoinsMinConf(nTargetValue, 0, 1, setCoinsRet, nValueRet));
1035 bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet)
1038 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1044 if (vecSend.empty() || nValue < 0)
1047 wtxNew.BindWallet(this);
1050 LOCK2(cs_main, cs_wallet);
1051 // txdb must be opened before the mapWallet lock
1054 nFeeRet = nTransactionFee;
1058 wtxNew.vout.clear();
1059 wtxNew.fFromMe = true;
1061 int64 nTotalValue = nValue + nFeeRet;
1062 double dPriority = 0;
1063 // vouts to the payees
1064 BOOST_FOREACH (const PAIRTYPE(CScript, int64)& s, vecSend)
1065 wtxNew.vout.push_back(CTxOut(s.second, s.first));
1067 // Choose coins to use
1068 set<pair<const CWalletTx*,unsigned int> > setCoins;
1070 if (!SelectCoins(nTotalValue, setCoins, nValueIn))
1072 BOOST_FOREACH(PAIRTYPE(const CWalletTx*, unsigned int) pcoin, setCoins)
1074 int64 nCredit = pcoin.first->vout[pcoin.second].nValue;
1075 dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain();
1078 int64 nChange = nValueIn - nValue - nFeeRet;
1079 // if sub-cent change is required, the fee must be raised to at least MIN_TX_FEE
1080 // or until nChange becomes zero
1081 // NOTE: this depends on the exact behaviour of GetMinFee
1082 if (nFeeRet < MIN_TX_FEE && nChange > 0 && nChange < CENT)
1084 int64 nMoveToFee = min(nChange, MIN_TX_FEE - nFeeRet);
1085 nChange -= nMoveToFee;
1086 nFeeRet += nMoveToFee;
1091 // Note: We use a new key here to keep it from being obvious which side is the change.
1092 // The drawback is that by not reusing a previous key, the change may be lost if a
1093 // backup is restored, if the backup doesn't have the new private key for the change.
1094 // If we reused the old key, it would be possible to add code to look for and
1095 // rediscover unknown transactions that were written with keys of ours to recover
1096 // post-backup change.
1098 // Reserve a new key pair from key pool
1099 vector<unsigned char> vchPubKey = reservekey.GetReservedKey();
1100 // assert(mapKeys.count(vchPubKey));
1102 // Fill a vout to ourself
1103 // TODO: pass in scriptChange instead of reservekey so
1104 // change transaction isn't always pay-to-bitcoin-address
1105 CScript scriptChange;
1106 scriptChange.SetBitcoinAddress(vchPubKey);
1108 // Insert change txn at random position:
1109 vector<CTxOut>::iterator position = wtxNew.vout.begin()+GetRandInt(wtxNew.vout.size());
1110 wtxNew.vout.insert(position, CTxOut(nChange, scriptChange));
1113 reservekey.ReturnKey();
1116 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1117 wtxNew.vin.push_back(CTxIn(coin.first->GetHash(),coin.second));
1121 BOOST_FOREACH(const PAIRTYPE(const CWalletTx*,unsigned int)& coin, setCoins)
1122 if (!SignSignature(*this, *coin.first, wtxNew, nIn++))
1126 unsigned int nBytes = ::GetSerializeSize(*(CTransaction*)&wtxNew, SER_NETWORK, PROTOCOL_VERSION);
1127 if (nBytes >= MAX_BLOCK_SIZE_GEN/5)
1129 dPriority /= nBytes;
1131 // Check that enough fee is included
1132 int64 nPayFee = nTransactionFee * (1 + (int64)nBytes / 1000);
1133 bool fAllowFree = CTransaction::AllowFree(dPriority);
1134 int64 nMinFee = wtxNew.GetMinFee(1, fAllowFree, GMF_SEND);
1135 if (nFeeRet < max(nPayFee, nMinFee))
1137 nFeeRet = max(nPayFee, nMinFee);
1141 // Fill vtxPrev by copying from previous transactions vtxPrev
1142 wtxNew.AddSupportingTransactions(txdb);
1143 wtxNew.fTimeReceivedIsTxTime = true;
1152 bool CWallet::CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet)
1154 vector< pair<CScript, int64> > vecSend;
1155 vecSend.push_back(make_pair(scriptPubKey, nValue));
1156 return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet);
1159 // Call after CreateTransaction unless you want to abort
1160 bool CWallet::CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey)
1163 LOCK2(cs_main, cs_wallet);
1164 printf("CommitTransaction:\n%s", wtxNew.ToString().c_str());
1166 // This is only to keep the database open to defeat the auto-flush for the
1167 // duration of this scope. This is the only place where this optimization
1168 // maybe makes sense; please don't do it anywhere else.
1169 CWalletDB* pwalletdb = fFileBacked ? new CWalletDB(strWalletFile,"r") : NULL;
1171 // Take key pair from key pool so it won't be used again
1172 reservekey.KeepKey();
1174 // Add tx to wallet, because if it has change it's also ours,
1175 // otherwise just for transaction history.
1176 AddToWallet(wtxNew);
1178 // Mark old coins as spent
1179 set<CWalletTx*> setCoins;
1180 BOOST_FOREACH(const CTxIn& txin, wtxNew.vin)
1182 CWalletTx &coin = mapWallet[txin.prevout.hash];
1183 coin.BindWallet(this);
1184 coin.MarkSpent(txin.prevout.n);
1186 vWalletUpdated.push_back(coin.GetHash());
1193 // Track how many getdata requests our transaction gets
1194 mapRequestCount[wtxNew.GetHash()] = 0;
1197 if (!wtxNew.AcceptToMemoryPool())
1199 // This must not fail. The transaction has already been signed and recorded.
1200 printf("CommitTransaction() : Error: Transaction not valid");
1203 wtxNew.RelayWalletTransaction();
1212 string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
1214 CReserveKey reservekey(this);
1219 string strError = _("Error: Wallet locked, unable to create transaction ");
1220 printf("SendMoney() : %s", strError.c_str());
1223 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired))
1226 if (nValue + nFeeRequired > GetBalance())
1227 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());
1229 strError = _("Error: Transaction creation failed ");
1230 printf("SendMoney() : %s", strError.c_str());
1234 if (fAskFee && !ThreadSafeAskFee(nFeeRequired, _("Sending...")))
1237 if (!CommitTransaction(wtxNew, reservekey))
1238 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.");
1246 string CWallet::SendMoneyToBitcoinAddress(const CBitcoinAddress& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
1250 return _("Invalid amount");
1251 if (nValue + nTransactionFee > GetBalance())
1252 return _("Insufficient funds");
1254 // Parse bitcoin address
1255 CScript scriptPubKey;
1256 scriptPubKey.SetBitcoinAddress(address);
1258 return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
1264 int CWallet::LoadWallet(bool& fFirstRunRet)
1268 fFirstRunRet = false;
1269 int nLoadWalletRet = CWalletDB(strWalletFile,"cr+").LoadWallet(this);
1270 if (nLoadWalletRet == DB_NEED_REWRITE)
1272 if (CDB::Rewrite(strWalletFile, "\x04pool"))
1275 // Note: can't top-up keypool here, because wallet is locked.
1276 // User will be prompted to unlock wallet the next operation
1277 // the requires a new key.
1279 nLoadWalletRet = DB_NEED_REWRITE;
1282 if (nLoadWalletRet != DB_LOAD_OK)
1283 return nLoadWalletRet;
1284 fFirstRunRet = vchDefaultKey.empty();
1286 CreateThread(ThreadFlushWalletDB, &strWalletFile);
1291 bool CWallet::SetAddressBookName(const CBitcoinAddress& address, const string& strName)
1293 mapAddressBook[address] = strName;
1294 AddressBookRepaint();
1297 return CWalletDB(strWalletFile).WriteName(address.ToString(), strName);
1300 bool CWallet::DelAddressBookName(const CBitcoinAddress& address)
1302 mapAddressBook.erase(address);
1303 AddressBookRepaint();
1306 return CWalletDB(strWalletFile).EraseName(address.ToString());
1310 void CWallet::PrintWallet(const CBlock& block)
1314 if (mapWallet.count(block.vtx[0].GetHash()))
1316 CWalletTx& wtx = mapWallet[block.vtx[0].GetHash()];
1317 printf(" mine: %d %d %d", wtx.GetDepthInMainChain(), wtx.GetBlocksToMaturity(), wtx.GetCredit());
1323 bool CWallet::GetTransaction(const uint256 &hashTx, CWalletTx& wtx)
1327 map<uint256, CWalletTx>::iterator mi = mapWallet.find(hashTx);
1328 if (mi != mapWallet.end())
1337 bool CWallet::SetDefaultKey(const std::vector<unsigned char> &vchPubKey)
1341 if (!CWalletDB(strWalletFile).WriteDefaultKey(vchPubKey))
1344 vchDefaultKey = vchPubKey;
1348 bool GetWalletFile(CWallet* pwallet, string &strWalletFileOut)
1350 if (!pwallet->fFileBacked)
1352 strWalletFileOut = pwallet->strWalletFile;
1357 // Mark old keypool keys as used,
1358 // and generate all new keys
1360 bool CWallet::NewKeyPool()
1364 CWalletDB walletdb(strWalletFile);
1365 BOOST_FOREACH(int64 nIndex, setKeyPool)
1366 walletdb.ErasePool(nIndex);
1372 int64 nKeys = max(GetArg("-keypool", 100), (int64)0);
1373 for (int i = 0; i < nKeys; i++)
1376 walletdb.WritePool(nIndex, CKeyPool(GenerateNewKey()));
1377 setKeyPool.insert(nIndex);
1379 printf("CWallet::NewKeyPool wrote %"PRI64d" new keys\n", nKeys);
1384 bool CWallet::TopUpKeyPool()
1392 CWalletDB walletdb(strWalletFile);
1395 unsigned int nTargetSize = max(GetArg("-keypool", 100), 0LL);
1396 while (setKeyPool.size() < (nTargetSize + 1))
1399 if (!setKeyPool.empty())
1400 nEnd = *(--setKeyPool.end()) + 1;
1401 if (!walletdb.WritePool(nEnd, CKeyPool(GenerateNewKey())))
1402 throw runtime_error("TopUpKeyPool() : writing generated key failed");
1403 setKeyPool.insert(nEnd);
1404 printf("keypool added key %"PRI64d", size=%d\n", nEnd, setKeyPool.size());
1410 void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
1413 keypool.vchPubKey.clear();
1420 // Get the oldest key
1421 if(setKeyPool.empty())
1424 CWalletDB walletdb(strWalletFile);
1426 nIndex = *(setKeyPool.begin());
1427 setKeyPool.erase(setKeyPool.begin());
1428 if (!walletdb.ReadPool(nIndex, keypool))
1429 throw runtime_error("ReserveKeyFromKeyPool() : read failed");
1430 if (!HaveKey(Hash160(keypool.vchPubKey)))
1431 throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
1432 assert(!keypool.vchPubKey.empty());
1433 printf("keypool reserve %"PRI64d"\n", nIndex);
1437 int64 CWallet::AddReserveKey(const CKeyPool& keypool)
1440 LOCK2(cs_main, cs_wallet);
1441 CWalletDB walletdb(strWalletFile);
1443 int64 nIndex = 1 + *(--setKeyPool.end());
1444 if (!walletdb.WritePool(nIndex, keypool))
1445 throw runtime_error("AddReserveKey() : writing added key failed");
1446 setKeyPool.insert(nIndex);
1452 void CWallet::KeepKey(int64 nIndex)
1454 // Remove from key pool
1457 CWalletDB walletdb(strWalletFile);
1458 walletdb.ErasePool(nIndex);
1460 printf("keypool keep %"PRI64d"\n", nIndex);
1463 void CWallet::ReturnKey(int64 nIndex)
1465 // Return to key pool
1468 setKeyPool.insert(nIndex);
1470 printf("keypool return %"PRI64d"\n", nIndex);
1473 bool CWallet::GetKeyFromPool(vector<unsigned char>& result, bool fAllowReuse)
1479 ReserveKeyFromKeyPool(nIndex, keypool);
1482 if (fAllowReuse && !vchDefaultKey.empty())
1484 result = vchDefaultKey;
1487 if (IsLocked()) return false;
1488 result = GenerateNewKey();
1492 result = keypool.vchPubKey;
1497 int64 CWallet::GetOldestKeyPoolTime()
1501 ReserveKeyFromKeyPool(nIndex, keypool);
1505 return keypool.nTime;
1508 vector<unsigned char> CReserveKey::GetReservedKey()
1513 pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
1515 vchPubKey = keypool.vchPubKey;
1518 printf("CReserveKey::GetReservedKey(): Warning: using default key instead of a new key, top up your keypool.");
1519 vchPubKey = pwallet->vchDefaultKey;
1522 assert(!vchPubKey.empty());
1526 void CReserveKey::KeepKey()
1529 pwallet->KeepKey(nIndex);
1534 void CReserveKey::ReturnKey()
1537 pwallet->ReturnKey(nIndex);
1542 void CWallet::GetAllReserveAddresses(set<CBitcoinAddress>& setAddress)
1546 CWalletDB walletdb(strWalletFile);
1548 LOCK2(cs_main, cs_wallet);
1549 BOOST_FOREACH(const int64& id, setKeyPool)
1552 if (!walletdb.ReadPool(id, keypool))
1553 throw runtime_error("GetAllReserveKeyHashes() : read failed");
1554 CBitcoinAddress address(keypool.vchPubKey);
1555 assert(!keypool.vchPubKey.empty());
1556 if (!HaveKey(address))
1557 throw runtime_error("GetAllReserveKeyHashes() : unknown key in key pool");
1558 setAddress.insert(address);