14 #include <boost/algorithm/string/replace.hpp>
15 #include <openssl/rand.h>
30 bool operator()(
const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t1,
31 const pair<int64_t, pair<const CWalletTx*, unsigned int> >& t2)
const
33 return t1.first < t2.first;
40 std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(hash);
41 if (it == mapWallet.end())
62 int64_t nCreationTime =
GetTime();
64 if (!nTimeFirstKey || nCreationTime < nTimeFirstKey)
65 nTimeFirstKey = nCreationTime;
67 if (!AddKeyPubKey(secret, pubkey))
68 throw std::runtime_error(
"CWallet::GenerateNewKey() : AddKey failed");
81 if (HaveWatchOnly(script))
82 RemoveWatchOnly(script);
89 mapKeyMetadata[pubkey.
GetID()]);
95 const vector<unsigned char> &vchCryptedSecret)
103 if (pwalletdbEncryption)
104 return pwalletdbEncryption->WriteCryptedKey(vchPubKey,
106 mapKeyMetadata[vchPubKey.
GetID()]);
110 mapKeyMetadata[vchPubKey.
GetID()]);
121 mapKeyMetadata[pubkey.
GetID()] = meta;
144 if (redeemScript.size() > MAX_SCRIPT_ELEMENT_SIZE)
147 LogPrintf(
"%s: Warning: This wallet contains a redeemScript of size %i which exceeds maximum size %i thus can never be redeemed. Do not use address %s.\n",
148 __func__, redeemScript.size(), MAX_SCRIPT_ELEMENT_SIZE, strAddr);
160 NotifyWatchonlyChanged(
true);
171 if (!HaveWatchOnly())
172 NotifyWatchonlyChanged(
false);
192 BOOST_FOREACH(
const MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
194 if(!crypter.
SetKeyFromPassphrase(strWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
196 if (!crypter.
Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
207 bool fWasLocked = IsLocked();
215 BOOST_FOREACH(MasterKeyMap::value_type& pMasterKey, mapMasterKeys)
217 if(!crypter.
SetKeyFromPassphrase(strOldWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
219 if (!crypter.
Decrypt(pMasterKey.second.vchCryptedKey, vMasterKey))
224 crypter.
SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
225 pMasterKey.second.nDeriveIterations = pMasterKey.second.nDeriveIterations * (100 / ((double)(
GetTimeMillis() - nStartTime)));
228 crypter.
SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod);
229 pMasterKey.second.nDeriveIterations = (pMasterKey.second.nDeriveIterations + pMasterKey.second.nDeriveIterations * 100 / ((double)(
GetTimeMillis() - nStartTime))) / 2;
231 if (pMasterKey.second.nDeriveIterations < 25000)
232 pMasterKey.second.nDeriveIterations = 25000;
234 LogPrintf(
"Wallet passphrase changed to an nDeriveIterations of %i\n", pMasterKey.second.nDeriveIterations);
236 if (!crypter.
SetKeyFromPassphrase(strNewWalletPassphrase, pMasterKey.second.vchSalt, pMasterKey.second.nDeriveIterations, pMasterKey.second.nDerivationMethod))
238 if (!crypter.
Encrypt(vMasterKey, pMasterKey.second.vchCryptedKey))
260 if (nWalletVersion >= nVersion)
264 if (fExplicit && nVersion > nWalletMaxVersion)
267 nWalletVersion = nVersion;
269 if (nVersion > nWalletMaxVersion)
270 nWalletMaxVersion = nVersion;
275 if (nWalletVersion > 40000)
288 if (nWalletVersion > nVersion)
291 nWalletMaxVersion = nVersion;
301 std::map<uint256, CWalletTx>::const_iterator it = mapWallet.find(txid);
302 if (it == mapWallet.end())
306 std::pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
308 BOOST_FOREACH(
const CTxIn& txin, wtx.
vin)
310 if (mapTxSpends.count(txin.
prevout) <= 1)
312 range = mapTxSpends.equal_range(txin.
prevout);
313 for (TxSpends::const_iterator it = range.first; it != range.second; ++it)
314 result.insert(it->second);
325 int nMinOrderPos = std::numeric_limits<int>::max();
327 for (TxSpends::iterator it = range.first; it != range.second; ++it)
329 const uint256& hash = it->second;
330 int n = mapWallet[hash].nOrderPos;
331 if (n < nMinOrderPos)
334 copyFrom = &mapWallet[hash];
338 for (TxSpends::iterator it = range.first; it != range.second; ++it)
340 const uint256& hash = it->second;
342 if (copyFrom == copyTo)
continue;
360 pair<TxSpends::const_iterator, TxSpends::const_iterator> range;
361 range = mapTxSpends.equal_range(outpoint);
363 for (TxSpends::const_iterator it = range.first; it != range.second; ++it)
365 const uint256& wtxid = it->second;
366 std::map<uint256, CWalletTx>::const_iterator mit = mapWallet.find(wtxid);
367 if (mit != mapWallet.end() && mit->second.GetDepthInMainChain() >= 0)
375 mapTxSpends.insert(make_pair(outpoint, wtxid));
377 pair<TxSpends::iterator, TxSpends::iterator> range;
378 range = mapTxSpends.equal_range(outpoint);
385 assert(mapWallet.count(wtxid));
387 if (thisTx.IsCoinBase())
390 BOOST_FOREACH(
const CTxIn& txin, thisTx.vin)
391 AddToSpends(txin.
prevout, wtxid);
432 mapMasterKeys[++nMasterKeyMaxID] = kMasterKey;
436 if (!pwalletdbEncryption->TxnBegin())
438 pwalletdbEncryption->WriteMasterKey(nMasterKeyMaxID, kMasterKey);
441 if (!EncryptKeys(vMasterKey))
444 pwalletdbEncryption->TxnAbort();
453 if (!pwalletdbEncryption->TxnCommit())
456 delete pwalletdbEncryption;
457 pwalletdbEncryption = NULL;
461 Unlock(strWalletPassphrase);
470 NotifyStatusChanged(
this);
478 int64_t nRet = nOrderPosNext++;
497 for (map<uint256, CWalletTx>::iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
517 item.second.MarkDirty();
527 mapWallet[hash] = wtxIn;
535 pair<map<uint256, CWalletTx>::iterator,
bool> ret = mapWallet.insert(make_pair(hash, wtxIn));
538 bool fInsertedNew = ret.second;
550 unsigned int latestEntry = 0;
553 int64_t latestTolerated = latestNow + 300;
554 std::list<CAccountingEntry> acentries;
555 TxItems txOrdered = OrderedTxItems(acentries);
556 for (TxItems::reverse_iterator it = txOrdered.rbegin(); it != txOrdered.rend(); ++it)
558 CWalletTx *
const pwtx = (*it).second.first;
570 nSmartTime = pacentry->
nTime;
571 if (nSmartTime <= latestTolerated)
573 latestEntry = nSmartTime;
574 if (nSmartTime > latestNow)
575 latestNow = nSmartTime;
582 wtx.
nTimeSmart = std::max(latestEntry, std::min(blocktime, latestNow));
585 LogPrintf(
"AddToWallet() : found %s in block %s not in index\n",
592 bool fUpdated =
false;
615 LogPrintf(
"AddToWallet %s %s%s\n", wtxIn.
GetHash().
ToString(), (fInsertedNew ?
"new" :
""), (fUpdated ?
"update" :
""));
618 if (fInsertedNew || fUpdated)
629 std::string strCmd =
GetArg(
"-walletnotify",
"");
631 if ( !strCmd.empty())
648 bool fExisted = mapWallet.count(hash);
649 if (fExisted && !fUpdate)
return false;
650 if (fExisted ||
IsMine(tx) || IsFromMe(tx))
655 wtx.SetMerkleBranch(pblock);
656 return AddToWallet(wtx);
665 if (!AddToWalletIfInvolvingMe(hash, tx, pblock,
true))
671 BOOST_FOREACH(
const CTxIn& txin, tx.
vin)
684 if (mapWallet.erase(hash))
695 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.
prevout.
hash);
696 if (mi != mapWallet.end())
710 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(txin.
prevout.
hash);
711 if (mi != mapWallet.end())
738 if (!mapAddressBook.count(address))
746 int64_t n = nTimeSmart;
747 return n ? n : nTimeReceived;
755 LOCK(pwallet->cs_wallet);
761 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
762 if (mi != pwallet->mapRequestCount.end())
763 nRequests = (*mi).second;
769 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(GetHash());
770 if (mi != pwallet->mapRequestCount.end())
772 nRequests = (*mi).second;
775 if (nRequests == 0 && hashBlock != 0)
777 map<uint256, int>::const_iterator mi = pwallet->mapRequestCount.find(hashBlock);
778 if (mi != pwallet->mapRequestCount.end())
779 nRequests = (*mi).second;
790 list<pair<CTxDestination, int64_t> >& listSent, int64_t& nFee,
string& strSentAccount,
const isminefilter& filter)
const
793 listReceived.clear();
795 strSentAccount = strFromAccount;
798 int64_t nDebit = GetDebit(filter);
801 int64_t nValueOut = GetValueOut();
802 nFee = nDebit - nValueOut;
806 BOOST_FOREACH(
const CTxOut& txout, vout)
816 if (pwallet->IsChange(txout))
819 else if (!(fIsMine & filter))
826 LogPrintf(
"CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
827 this->GetHash().ToString());
833 listSent.push_back(make_pair(address, txout.
nValue));
836 if (fIsMine & filter)
837 listReceived.push_back(make_pair(address, txout.
nValue));
843 int64_t& nSent, int64_t& nFee,
const isminefilter& filter)
const
845 nReceived = nSent = nFee = 0;
848 string strSentAccount;
849 list<pair<CTxDestination, int64_t> > listReceived;
850 list<pair<CTxDestination, int64_t> > listSent;
851 GetAmounts(listReceived, listSent, allFee, strSentAccount, filter);
853 if (strAccount == strSentAccount)
860 LOCK(pwallet->cs_wallet);
863 if (pwallet->mapAddressBook.count(r.first))
865 map<CTxDestination, CAddressBookData>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
866 if (mi != pwallet->mapAddressBook.end() && (*mi).second.name == strAccount)
867 nReceived += r.second;
869 else if (strAccount.empty())
871 nReceived += r.second;
897 while (pindex && nTimeFirstKey && (pindex->
nTime < (nTimeFirstKey - 7200)))
900 ShowProgress(
_(
"Rescanning..."), 0);
905 if (pindex->
nHeight % 100 == 0 && dProgressTip - dProgressStart > 0.0)
912 if (AddToWalletIfInvolvingMe(tx.
GetHash(), tx, &block, fUpdate))
921 ShowProgress(
_(
"Rescanning..."), 100);
931 const uint256& wtxid = item.first;
933 assert(wtx.
GetHash() == wtxid);
950 if (GetDepthInMainChain() == 0) {
964 result = pwallet->GetConflicts(myHash);
965 result.erase(myHash);
976 bool fFirst = (nNextResend == 0);
987 LogPrintf(
"ResendWalletTransactions()\n");
991 multimap<unsigned int, CWalletTx*> mapSorted;
1024 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1040 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1055 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1069 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1085 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1100 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1116 for (map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); ++it)
1118 const uint256& wtxid = it->first;
1124 if (fOnlyConfirmed && !pcoin->
IsTrusted())
1134 for (
unsigned int i = 0; i < pcoin->
vout.size(); i++) {
1136 if (!(IsSpent(wtxid, i)) && mine !=
ISMINE_NO &&
1137 !IsLockedCoin((*it).first, i) && pcoin->
vout[i].nValue > 0 &&
1145 static void ApproximateBestSubset(vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > >vValue, int64_t nTotalLower, int64_t nTargetValue,
1146 vector<char>& vfBest, int64_t& nBest,
int iterations = 1000)
1148 vector<char> vfIncluded;
1150 vfBest.assign(vValue.size(),
true);
1151 nBest = nTotalLower;
1155 for (
int nRep = 0; nRep < iterations && nBest != nTargetValue; nRep++)
1157 vfIncluded.assign(vValue.size(),
false);
1159 bool fReachedTarget =
false;
1160 for (
int nPass = 0; nPass < 2 && !fReachedTarget; nPass++)
1162 for (
unsigned int i = 0; i < vValue.size(); i++)
1170 if (nPass == 0 ? insecure_rand()&1 : !vfIncluded[i])
1172 nTotal += vValue[i].first;
1173 vfIncluded[i] =
true;
1174 if (nTotal >= nTargetValue)
1176 fReachedTarget =
true;
1180 vfBest = vfIncluded;
1182 nTotal -= vValue[i].first;
1183 vfIncluded[i] =
false;
1192 set<pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet)
const
1194 setCoinsRet.clear();
1198 pair<int64_t, pair<const CWalletTx*,unsigned int> > coinLowestLarger;
1199 coinLowestLarger.first = std::numeric_limits<int64_t>::max();
1200 coinLowestLarger.second.first = NULL;
1201 vector<pair<int64_t, pair<const CWalletTx*,unsigned int> > > vValue;
1202 int64_t nTotalLower = 0;
1204 random_shuffle(vCoins.begin(), vCoins.end(),
GetRandInt);
1206 BOOST_FOREACH(
const COutput &output, vCoins)
1217 int64_t n = pcoin->
vout[i].nValue;
1219 pair<int64_t,pair<const CWalletTx*,unsigned int> > coin = make_pair(n,make_pair(pcoin, i));
1221 if (n == nTargetValue)
1223 setCoinsRet.insert(coin.second);
1224 nValueRet += coin.first;
1227 else if (n < nTargetValue + CENT)
1229 vValue.push_back(coin);
1232 else if (n < coinLowestLarger.first)
1234 coinLowestLarger = coin;
1238 if (nTotalLower == nTargetValue)
1240 for (
unsigned int i = 0; i < vValue.size(); ++i)
1242 setCoinsRet.insert(vValue[i].second);
1243 nValueRet += vValue[i].first;
1248 if (nTotalLower < nTargetValue)
1250 if (coinLowestLarger.second.first == NULL)
1252 setCoinsRet.insert(coinLowestLarger.second);
1253 nValueRet += coinLowestLarger.first;
1259 vector<char> vfBest;
1262 ApproximateBestSubset(vValue, nTotalLower, nTargetValue, vfBest, nBest, 1000);
1263 if (nBest != nTargetValue && nTotalLower >= nTargetValue + CENT)
1264 ApproximateBestSubset(vValue, nTotalLower, nTargetValue + CENT, vfBest, nBest, 1000);
1268 if (coinLowestLarger.second.first &&
1269 ((nBest != nTargetValue && nBest < nTargetValue + CENT) || coinLowestLarger.first <= nBest))
1271 setCoinsRet.insert(coinLowestLarger.second);
1272 nValueRet += coinLowestLarger.first;
1275 for (
unsigned int i = 0; i < vValue.size(); i++)
1278 setCoinsRet.insert(vValue[i].second);
1279 nValueRet += vValue[i].first;
1282 LogPrint(
"selectcoins",
"SelectCoins() best subset: ");
1283 for (
unsigned int i = 0; i < vValue.size(); i++)
1285 LogPrint(
"selectcoins",
"%s ",
FormatMoney(vValue[i].first));
1286 LogPrint(
"selectcoins",
"total %s\n",
FormatMoney(nBest));
1294 vector<COutput> vCoins;
1295 AvailableCoins(vCoins,
true, coinControl);
1300 BOOST_FOREACH(
const COutput& out, vCoins)
1304 nValueRet += out.
tx->
vout[out.
i].nValue;
1305 setCoinsRet.insert(make_pair(out.
tx, out.
i));
1307 return (nValueRet >= nTargetValue);
1310 return (SelectCoinsMinConf(nTargetValue, 1, 6, vCoins, setCoinsRet, nValueRet) ||
1311 SelectCoinsMinConf(nTargetValue, 1, 1, vCoins, setCoinsRet, nValueRet) ||
1312 (
bSpendZeroConfChange && SelectCoinsMinConf(nTargetValue, 0, 1, vCoins, setCoinsRet, nValueRet)));
1326 strFailReason =
_(
"Transaction amounts must be positive");
1331 if (vecSend.empty() || nValue < 0)
1333 strFailReason =
_(
"Transaction amounts must be positive");
1346 wtxNew.
vout.clear();
1349 int64_t nTotalValue = nValue + nFeeRet;
1350 double dPriority = 0;
1354 CTxOut txout(s.second, s.first);
1357 strFailReason =
_(
"Transaction amount too small");
1360 wtxNew.
vout.push_back(txout);
1364 set<pair<const CWalletTx*,unsigned int> > setCoins;
1365 int64_t nValueIn = 0;
1366 if (!SelectCoins(nTotalValue, setCoins, nValueIn, coinControl))
1368 strFailReason =
_(
"Insufficient funds");
1373 int64_t nCredit = pcoin.first->vout[pcoin.second].nValue;
1377 dPriority += (double)nCredit * (pcoin.first->GetDepthInMainChain()+1);
1380 int64_t nChange = nValueIn - nValue - nFeeRet;
1385 if (nFeeRet < CTransaction::nMinTxFee && nChange > 0 && nChange < CENT)
1388 nChange -= nMoveToFee;
1389 nFeeRet += nMoveToFee;
1400 if (coinControl && !boost::get<CNoDestination>(&coinControl->
destChange))
1422 CTxOut newTxOut(nChange, scriptChange);
1434 vector<CTxOut>::iterator position = wtxNew.
vout.begin()+
GetRandInt(wtxNew.
vout.size()+1);
1435 wtxNew.
vout.insert(position, newTxOut);
1443 wtxNew.vin.push_back(
CTxIn(coin.first->GetHash(),coin.second));
1450 strFailReason =
_(
"Signing transaction failed");
1456 if (nBytes >= MAX_STANDARD_TX_SIZE)
1458 strFailReason =
_(
"Transaction too large");
1467 if (nFeeRet < max(nPayFee, nMinFee))
1469 nFeeRet = max(nPayFee, nMinFee);
1485 vector< pair<CScript, int64_t> > vecSend;
1486 vecSend.push_back(make_pair(scriptPubKey, nValue));
1487 return CreateTransaction(vecSend, wtxNew, reservekey, nFeeRet, strFailReason, coinControl);
1507 AddToWallet(wtxNew);
1510 set<CWalletTx*> setCoins;
1511 BOOST_FOREACH(
const CTxIn& txin, wtxNew.
vin)
1523 mapRequestCount[wtxNew.
GetHash()] = 0;
1529 LogPrintf(
"CommitTransaction() : Error: Transaction not valid");
1543 int64_t nFeeRequired;
1547 string strError =
_(
"Error: Wallet locked, unable to create transaction!");
1548 LogPrintf(
"SendMoney() : %s", strError);
1552 if (!CreateTransaction(scriptPubKey, nValue, wtxNew, reservekey, nFeeRequired, strError))
1554 if (nValue + nFeeRequired > GetBalance())
1555 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));
1556 LogPrintf(
"SendMoney() : %s\n", strError);
1560 if (!CommitTransaction(wtxNew, reservekey))
1561 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.");
1572 return _(
"Invalid amount");
1574 return _(
"Insufficient funds");
1580 return SendMoney(scriptPubKey, nValue, wtxNew);
1590 fFirstRunRet =
false;
1605 return nLoadWalletRet;
1606 fFirstRunRet = !vchDefaultKey.IsValid();
1632 return nZapWalletTxRet;
1640 bool fUpdated =
false;
1643 std::map<CTxDestination, CAddressBookData>::iterator mi = mapAddressBook.find(address);
1644 fUpdated = mi != mapAddressBook.end();
1645 mapAddressBook[address].name = strName;
1646 if (!strPurpose.empty())
1647 mapAddressBook[address].purpose = strPurpose;
1649 NotifyAddressBookChanged(
this, address, strName, ::
IsMine(*
this, address),
1667 BOOST_FOREACH(
const PAIRTYPE(
string,
string) &item, mapAddressBook[address].destdata)
1672 mapAddressBook.erase(address);
1675 NotifyAddressBookChanged(
this, address,
"", ::
IsMine(*
this, address),
"",
CT_DELETED);
1690 vchDefaultKey = vchPubKey;
1703 BOOST_FOREACH(int64_t nIndex, setKeyPool)
1710 int64_t nKeys = max(
GetArg(
"-keypool", 100), (int64_t)0);
1711 for (
int i = 0; i < nKeys; i++)
1713 int64_t nIndex = i+1;
1715 setKeyPool.insert(nIndex);
1717 LogPrintf(
"CWallet::NewKeyPool wrote %d new keys\n", nKeys);
1733 unsigned int nTargetSize;
1735 nTargetSize = kpSize;
1737 nTargetSize = max(
GetArg(
"-keypool", 100), (int64_t) 0);
1739 while (setKeyPool.size() < (nTargetSize + 1))
1742 if (!setKeyPool.empty())
1743 nEnd = *(--setKeyPool.end()) + 1;
1745 throw runtime_error(
"TopUpKeyPool() : writing generated key failed");
1746 setKeyPool.insert(nEnd);
1747 LogPrintf(
"keypool added key %d, size=%u\n", nEnd, setKeyPool.size());
1764 if(setKeyPool.empty())
1769 nIndex = *(setKeyPool.begin());
1770 setKeyPool.erase(setKeyPool.begin());
1771 if (!walletdb.
ReadPool(nIndex, keypool))
1772 throw runtime_error(
"ReserveKeyFromKeyPool() : read failed");
1774 throw runtime_error(
"ReserveKeyFromKeyPool() : unknown key in key pool");
1776 LogPrintf(
"keypool reserve %d\n", nIndex);
1786 int64_t nIndex = 1 + *(--setKeyPool.end());
1787 if (!walletdb.
WritePool(nIndex, keypool))
1788 throw runtime_error(
"AddReserveKey() : writing added key failed");
1789 setKeyPool.insert(nIndex);
1811 setKeyPool.insert(nIndex);
1813 LogPrintf(
"keypool return %d\n", nIndex);
1822 ReserveKeyFromKeyPool(nIndex, keypool);
1825 if (IsLocked())
return false;
1826 result = GenerateNewKey();
1839 ReserveKeyFromKeyPool(nIndex, keypool);
1843 return keypool.
nTime;
1848 map<CTxDestination, int64_t> balances;
1866 for (
unsigned int i = 0; i < pcoin->
vout.size(); i++)
1874 int64_t n = IsSpent(walletEntry.first, i) ? 0 : pcoin->
vout[i].nValue;
1876 if (!balances.count(addr))
1878 balances[addr] += n;
1889 set< set<CTxDestination> > groupings;
1890 set<CTxDestination> grouping;
1896 if (pcoin->
vin.size() > 0)
1898 bool any_mine =
false;
1900 BOOST_FOREACH(
CTxIn txin, pcoin->
vin)
1907 grouping.insert(address);
1915 if (IsChange(txout))
1920 grouping.insert(txoutAddr);
1923 if (grouping.size() > 0)
1925 groupings.insert(grouping);
1931 for (
unsigned int i = 0; i < pcoin->
vout.size(); i++)
1937 grouping.insert(address);
1938 groupings.insert(grouping);
1943 set< set<CTxDestination>* > uniqueGroupings;
1944 map< CTxDestination, set<CTxDestination>* > setmap;
1945 BOOST_FOREACH(set<CTxDestination> grouping, groupings)
1948 set< set<CTxDestination>* > hits;
1949 map< CTxDestination, set<CTxDestination>* >::iterator it;
1951 if ((it = setmap.find(address)) != setmap.end())
1952 hits.insert((*it).second);
1955 set<CTxDestination>* merged =
new set<CTxDestination>(grouping);
1956 BOOST_FOREACH(set<CTxDestination>* hit, hits)
1958 merged->insert(hit->begin(), hit->end());
1959 uniqueGroupings.erase(hit);
1962 uniqueGroupings.insert(merged);
1966 setmap[element] = merged;
1969 set< set<CTxDestination> > ret;
1970 BOOST_FOREACH(set<CTxDestination>* uniqueGrouping, uniqueGroupings)
1972 ret.insert(*uniqueGrouping);
1973 delete uniqueGrouping;
1982 set<CTxDestination> result;
1986 const string& strName = item.second.
name;
1987 if (strName == strAccount)
1988 result.insert(address);
1998 pwallet->ReserveKeyFromKeyPool(nIndex, keypool);
2002 if (pwallet->vchDefaultKey.IsValid()) {
2003 LogPrintf(
"CReserveKey::GetReservedKey(): Warning: Using default key instead of a new key, top up your keypool!");
2004 vchPubKey = pwallet->vchDefaultKey;
2017 pwallet->KeepKey(nIndex);
2025 pwallet->ReturnKey(nIndex);
2037 BOOST_FOREACH(
const int64_t&
id, setKeyPool)
2040 if (!walletdb.
ReadPool(
id, keypool))
2041 throw runtime_error(
"GetAllReserveKeyHashes() : read failed");
2044 if (!HaveKey(keyID))
2045 throw runtime_error(
"GetAllReserveKeyHashes() : unknown key in key pool");
2046 setAddress.insert(keyID);
2055 map<uint256, CWalletTx>::const_iterator mi = mapWallet.find(hashTx);
2056 if (mi != mapWallet.end())
2057 NotifyTransactionChanged(
this, hashTx,
CT_UPDATED);
2064 setLockedCoins.insert(output);
2070 setLockedCoins.erase(output);
2076 setLockedCoins.clear();
2084 return (setLockedCoins.count(outpt) > 0);
2090 for (std::set<COutPoint>::iterator it = setLockedCoins.begin();
2091 it != setLockedCoins.end(); it++) {
2093 vOutpts.push_back(outpt);
2099 mapKeyBirth.clear();
2102 for (std::map<CKeyID, CKeyMetadata>::const_iterator it = mapKeyMetadata.begin(); it != mapKeyMetadata.end(); it++)
2103 if (it->second.nCreateTime)
2104 mapKeyBirth[it->first] = it->second.nCreateTime;
2108 std::map<CKeyID, CBlockIndex*> mapKeyFirstBlock;
2109 std::set<CKeyID> setKeys;
2111 BOOST_FOREACH(
const CKeyID &keyid, setKeys) {
2112 if (mapKeyBirth.count(keyid) == 0)
2113 mapKeyFirstBlock[keyid] = pindexMax;
2118 if (mapKeyFirstBlock.empty())
2122 std::vector<CKeyID> vAffected;
2123 for (std::map<uint256, CWalletTx>::const_iterator it = mapWallet.begin(); it != mapWallet.end(); it++) {
2129 int nHeight = blit->second->nHeight;
2130 BOOST_FOREACH(
const CTxOut &txout, wtx.
vout) {
2133 BOOST_FOREACH(
const CKeyID &keyid, vAffected) {
2135 std::map<CKeyID, CBlockIndex*>::iterator rit = mapKeyFirstBlock.find(keyid);
2136 if (rit != mapKeyFirstBlock.end() && nHeight < rit->second->nHeight)
2137 rit->second = blit->second;
2145 for (std::map<CKeyID, CBlockIndex*>::const_iterator it = mapKeyFirstBlock.begin(); it != mapKeyFirstBlock.end(); it++)
2146 mapKeyBirth[it->first] = it->second->nTime - 7200;
2151 if (boost::get<CNoDestination>(&dest))
2154 mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
2162 if (!mapAddressBook[dest].destdata.erase(key))
2171 mapAddressBook[dest].destdata.insert(std::make_pair(key, value));
2177 std::map<CTxDestination, CAddressBookData>::const_iterator i = mapAddressBook.find(dest);
2178 if(i != mapAddressBook.end())
2180 CAddressBookData::StringMap::const_iterator j = i->second.destdata.find(key);
2181 if(j != i->second.destdata.end())
bool LoadCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
int64_t GetImmatureWatchOnlyCredit(const bool &fUseCache=true) const
CClientUIInterface uiInterface
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
bool WriteMinVersion(int nVersion)
bool SetKeyFromPassphrase(const SecureString &strKeyData, const std::vector< unsigned char > &chSalt, const unsigned int nRounds, const unsigned int nDerivationMethod)
uint64_t GetRand(uint64_t nMax)
void GetAmounts(std::list< std::pair< CTxDestination, int64_t > > &listReceived, std::list< std::pair< CTxDestination, int64_t > > &listSent, int64_t &nFee, std::string &strSentAccount, const isminefilter &filter) const
int64_t AddReserveKey(const CKeyPool &keypool)
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
void BindWallet(CWallet *pwalletIn)
unsigned int nDerivationMethod
bool SetAddressBook(const CTxDestination &address, const std::string &strName, const std::string &purpose)
bool LoadDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
Adds a destination data tuple to the store, without saving it to disk.
isminetype IsMine(const CTxIn &txin) const
int64_t GetImmatureBalance() const
bool RemoveWatchOnly(const CScript &dest)
void KeepKey(int64_t nIndex)
bool Encrypt(const CKeyingMaterial &vchPlaintext, std::vector< unsigned char > &vchCiphertext)
bool AddToWalletIfInvolvingMe(const uint256 &hash, const CTransaction &tx, const CBlock *pblock, bool fUpdate)
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
const unsigned int WALLET_CRYPTO_KEY_SIZE
bool LoadKeyMetadata(const CPubKey &pubkey, const CKeyMetadata &metadata)
int64_t GetUnconfirmedWatchOnlyBalance() const
double ComputePriority(double dPriorityInputs, unsigned int nTxSize=0) const
bool SelectCoinsMinConf(int64_t nTargetValue, int nConfMine, int nConfTheirs, std::vector< COutput > vCoins, std::set< std::pair< const CWalletTx *, unsigned int > > &setCoinsRet, int64_t &nValueRet) const
void ListLockedCoins(std::vector< COutPoint > &vOutpts)
static bool Rewrite(const std::string &strFile, const char *pszSkip=NULL)
Encryption/decryption context with key information.
isminetype IsMine(const CKeyStore &keystore, const CTxDestination &dest)
std::vector< unsigned char > vchCryptedKey
bool bSpendZeroConfChange
std::string strFromAccount
WalletFeature
(client) version numbers for particular wallet features
bool WriteMasterKey(unsigned int nID, const CMasterKey &kMasterKey)
bool WriteCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret, const CKeyMetadata &keyMeta)
std::map< CTxDestination, int64_t > GetAddressBalances()
Master key for wallet encryption.
bool SignSignature(const CKeyStore &keystore, const CScript &fromPubKey, CTransaction &txTo, unsigned int nIn, int nHashType)
void seed_insecure_rand(bool fDeterministic)
Seed insecure_rand using the random pool.
std::string strWalletFile
std::vector< unsigned char, secure_allocator< unsigned char > > CKeyingMaterial
bool IsSelected(const uint256 &hash, unsigned int n) const
int64_t GetMinFee(const CTransaction &tx, unsigned int nBytes, bool fAllowFree, enum GetMinFee_mode mode)
void GetAllReserveKeys(std::set< CKeyID > &setAddress) const
boost::signals2::signal< void(CWallet *wallet)> LoadWallet
A wallet has been loaded.
void ListAccountCreditDebit(const std::string &strAccount, std::list< CAccountingEntry > &acentries)
bool WriteWatchOnly(const CScript &script)
uint160 Hash160(const T1 pbegin, const T1 pend)
bool ErasePurpose(const std::string &strAddress)
bool AddWatchOnly(const CScript &dest)
bool SetMaxVersion(int nVersion)
virtual bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
bool AllowFree(double dPriority)
DBErrors
Error statuses for the wallet database.
void GetKeyBirthTimes(std::map< CKeyID, int64_t > &mapKeyBirth) const
void RandAddSeedPerfmon()
static int64_t nMinTxFee
Fees smaller than this (in satoshi) are considered zero fee (for transaction creation) ...
string FormatMoney(int64_t n, bool fPlus)
void ReacceptWalletTransactions()
CChain chainActive
The currently-connected chain of blocks.
std::set< uint256 > GetConflicts(const uint256 &txid) const
bool SetDefaultKey(const CPubKey &vchPubKey)
std::string SendMoney(CScript scriptPubKey, int64_t nValue, CWalletTx &wtxNew)
bool WriteOrderPosNext(int64_t nOrderPosNext)
int64_t GetDebit(const CTxIn &txin, const isminefilter &filter) const
int64_t IncOrderPosNext(CWalletDB *pwalletdb=NULL)
Increment the next transaction order id.
void SetBestChain(const CBlockLocator &loc)
void EraseFromWallet(const uint256 &hash)
bool EraseTx(uint256 hash)
void SetDestination(const CTxDestination &address)
virtual bool AddCScript(const CScript &redeemScript)
isminetype
IsMine() return codes.
int64_t GetWatchOnlyBalance() const
std::multimap< int64_t, TxPair > TxItems
bool SetMinVersion(enum WalletFeature, CWalletDB *pwalletdbIn=NULL, bool fExplicit=false)
int64_t GetBalance() const
#define AssertLockHeld(cs)
bool CreateTransaction(const std::vector< std::pair< CScript, int64_t > > &vecSend, CWalletTx &wtxNew, CReserveKey &reservekey, int64_t &nFeeRet, std::string &strFailReason, const CCoinControl *coinControl=NULL)
int GetBlocksToMaturity() const
CBlockIndex * Tip() const
Returns the index entry for the tip of this chain, or NULL if none.
int GetRequestCount() const
int Height() const
Return the maximal height in the chain.
DBErrors ZapWalletTx(CWallet *pwallet)
bool IsSpent(const uint256 &hash, unsigned int n) const
void SyncMetaData(std::pair< TxSpends::iterator, TxSpends::iterator >)
int64_t GetAdjustedTime()
void AddToSpends(const COutPoint &outpoint, const uint256 &wtxid)
CBlockIndex * Next(const CBlockIndex *pindex) const
Find the successor of a block in this chain, or NULL if the given index is not found or is the tip...
void LockCoin(COutPoint &output)
bool EraseDestData(const std::string &address, const std::string &key)
Erase destination data tuple from wallet database.
bool WritePool(int64_t nPool, const CKeyPool &keypool)
bool ChangeWalletPassphrase(const SecureString &strOldWalletPassphrase, const SecureString &strNewWalletPassphrase)
bool SelectCoins(int64_t nTargetValue, std::set< std::pair< const CWalletTx *, unsigned int > > &setCoinsRet, int64_t &nValueRet, const CCoinControl *coinControl=NULL) const
unsigned int GetSerializeSize(char a, int, int=0)
An input of a transaction.
int64_t GetOldestKeyPoolTime()
const CWalletTx * GetWalletTx(const uint256 &hash) const
CPubKey GetPubKey() const
bool WriteName(const std::string &strAddress, const std::string &strName)
CPrivKey GetPrivKey() const
std::vector< CTxOut > vout
DBErrors LoadWallet(CWallet *pwallet)
int64_t GetAvailableWatchOnlyCredit(const bool &fUseCache=true) const
CTxDestination destChange
void UnlockCoin(COutPoint &output)
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
An encapsulated public key.
bool Unlock(const CKeyingMaterial &vMasterKeyIn)
bool Decrypt(const std::vector< unsigned char > &vchCiphertext, CKeyingMaterial &vchPlaintext)
base58-encoded Anoncoin addresses.
std::string SendMoneyToDestination(const CTxDestination &address, int64_t nValue, CWalletTx &wtxNew)
void MakeNewKey(bool fCompressed)
std::vector< uint256 > vMerkleBranch
bool TopUpKeyPool(unsigned int kpSize=0)
bool AcceptToMemoryPool(bool fLimitFree=true)
bool WriteDestData(const std::string &address, const std::string &key, const std::string &value)
Write destination data key,value tuple to database.
void RelayTransaction(const CTransaction &tx, const uint256 &hash)
bool EraseDestData(const CTxDestination &dest, const std::string &key)
Erases a destination data tuple in the store and on disk.
bool CommitTransaction(CWalletTx &wtxNew, CReserveKey &reservekey)
std::string ToString() const
virtual bool RemoveWatchOnly(const CScript &dest)
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
void RelayWalletTransaction()
An output of a transaction.
bool GetReservedKey(CPubKey &pubkey)
std::set< std::set< CTxDestination > > GetAddressGroupings()
void AvailableCoins(std::vector< COutput > &vCoins, bool fOnlyConfirmed=true, const CCoinControl *coinControl=NULL) const
bool IsFromMe(const isminefilter &filter) const
An outpoint - a combination of a transaction hash and an index n into its vout.
bool LoadCScript(const CScript &redeemScript)
uint8_t isminefilter
used for bitflags of isminetype
std::set< CTxDestination > GetAccountAddresses(std::string strAccount) const
bool ErasePool(int64_t nPool)
std::string GetHex() const
unsigned int fTimeReceivedIsTxTime
double GuessVerificationProgress(CBlockIndex *pindex, bool fSigchecks)
bool WriteTx(uint256 hash, const CWalletTx &wtx)
std::string ToString() const
Access to the wallet database (wallet.dat)
A transaction with a bunch of additional info that only the owner cares about.
int64_t GetTxTime() const
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
void UpdatedTransaction(const uint256 &hashTx)
bool WriteBestBlock(const CBlockLocator &locator)
const unsigned int WALLET_CRYPTO_SALT_SIZE
int64_t GetImmatureWatchOnlyBalance() const
int64_t nTimeBestReceived
int64_t GetUnconfirmedBalance() const
bool ReadBlockFromDisk(CBlock &block, const CDiskBlockPos &pos)
bool LoadWatchOnly(const CScript &dest)
int ScanForWalletTransactions(CBlockIndex *pindexStart, bool fUpdate=false)
bool EncryptWallet(const SecureString &strWalletPassphrase)
A key allocated from the key pool.
The block chain is a tree shaped structure starting with the genesis block at the root...
bool ReadPool(int64_t nPool, CKeyPool &keypool)
Serialized script, used inside transaction inputs and outputs.
std::string ToString() const
std::string _(const char *psz)
Translation function: Call Translate signal on UI interface, which returns a boost::optional result...
void ReturnKey(int64_t nIndex)
A reference to a CKey: the Hash160 of its serialized public key.
bool ExtractDestination(const CScript &scriptPubKey, CTxDestination &addressRet)
bool Unlock(const SecureString &strWalletPassphrase)
bool Contains(const CBlockIndex *pindex) const
Efficiently check whether a block is present in this chain.
bool IsChange(const CTxOut &txout) const
void ResendWalletTransactions()
void ReserveKeyFromKeyPool(int64_t &nIndex, CKeyPool &keypool)
std::set< uint256 > GetConflicts() const
int GetDepthInMainChain(CBlockIndex *&pindexRet) const
std::vector< unsigned char > vchSalt
void SyncTransaction(const uint256 &hash, const CTransaction &tx, const CBlock *pblock)
static int64_t nMinRelayTxFee
Fees smaller than this (in satoshi) are considered zero fee (for relaying and mining) ...
bool AddCScript(const CScript &redeemScript)
bool operator()(const pair< int64_t, pair< const CWalletTx *, unsigned int > > &t1, const pair< int64_t, pair< const CWalletTx *, unsigned int > > &t2) const
bool AddToWallet(const CWalletTx &wtxIn, bool fFromLoadWallet=false)
DBErrors LoadWallet(bool &fFirstRunRet)
std::vector< CTransaction > vtx
std::string GetArg(const std::string &strArg, const std::string &strDefault)
Return string argument or default value.
bool GetKeyFromPool(CPubKey &key)
void GetAccountAmounts(const std::string &strAccount, int64_t &nReceived, int64_t &nSent, int64_t &nFee, const isminefilter &filter) const
unsigned int nTimeReceived
An encapsulated private key.
bool EraseName(const std::string &strAddress)
int64_t GetAvailableCredit(bool fUseCache=true) const
The basic transaction that is broadcasted on the network and contained in blocks. ...
bool AddDestData(const CTxDestination &dest, const std::string &key, const std::string &value)
Adds a destination data tuple to the store, and saves it to disk.
unsigned int nDeriveIterations
bool WriteCScript(const uint160 &hash, const CScript &redeemScript)
bool WritePurpose(const std::string &strAddress, const std::string &purpose)
bool AddCryptedKey(const CPubKey &vchPubKey, const std::vector< unsigned char > &vchCryptedSecret)
std::pair< CWalletTx *, CAccountingEntry * > TxPair
bool IsLockedCoin(uint256 hash, unsigned int n) const
bool DelAddressBook(const CTxDestination &address)
void ExtractAffectedKeys(const CKeyStore &keystore, const CScript &scriptPubKey, std::vector< CKeyID > &vKeys)
map< uint256, CBlockIndex * > mapBlockIndex
int64_t GetImmatureCredit(bool fUseCache=true) const
virtual bool AddWatchOnly(const CScript &dest)
bool WriteKey(const CPubKey &vchPubKey, const CPrivKey &vchPrivKey, const CKeyMetadata &keyMeta)
void runCommand(std::string strCommand)
std::vector< std::pair< std::string, std::string > > vOrderForm
TxItems OrderedTxItems(std::list< CAccountingEntry > &acentries, std::string strAccount="")
Get the wallet's activity log.
bool GetDestData(const CTxDestination &dest, const std::string &key, std::string *value) const
Look up a destination data tuple in the store, return true if found false otherwise.