1 // Copyright (c) 2009-2011 Satoshi Nakamoto & Bitcoin developers
2 // Distributed under the MIT/X11 software license, see the accompanying
3 // file license.txt or http://www.opensource.org/licenses/mit-license.php.
4 #ifndef BITCOIN_WALLET_H
5 #define BITCOIN_WALLET_H
14 extern std::map<uint256, CWalletTx> mapWallet;
15 extern std::vector<uint256> vWalletUpdated;
16 extern CCriticalSection cs_mapWallet;
18 extern std::map<uint256, int> mapRequestCount;
19 extern CCriticalSection cs_mapRequestCount;
21 extern std::map<std::string, std::string> mapAddressBook;
22 extern CCriticalSection cs_mapAddressBook;
24 extern std::vector<unsigned char> vchDefaultKey;
27 bool AddToWallet(const CWalletTx& wtxIn);
28 bool AddToWalletIfInvolvingMe(const CTransaction& tx, const CBlock* pblock, bool fUpdate = false);
29 bool EraseFromWallet(uint256 hash);
30 void WalletUpdateSpent(const COutPoint& prevout);
31 int ScanForWalletTransactions(CBlockIndex* pindexStart, bool fUpdate = false);
32 void ReacceptWalletTransactions();
33 void ResendWalletTransactions();
35 bool CreateTransaction(const std::vector<std::pair<CScript, int64> >& vecSend, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet);
36 bool CreateTransaction(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, CReserveKey& reservekey, int64& nFeeRet);
37 bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
38 bool BroadcastTransaction(CWalletTx& wtxNew);
39 std::string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
40 std::string SendMoneyToBitcoinAddress(std::string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
45 // A transaction with a bunch of additional info that only the owner cares
46 // about. It includes any unrecorded transactions needed to link it back
47 // to the block chain.
49 class CWalletTx : public CMerkleTx
52 std::vector<CMerkleTx> vtxPrev;
53 std::map<std::string, std::string> mapValue;
54 std::vector<std::pair<std::string, std::string> > vOrderForm;
55 unsigned int fTimeReceivedIsTxTime;
56 unsigned int nTimeReceived; // time received by this node
58 std::string strFromAccount;
59 std::vector<char> vfSpent;
62 mutable char fDebitCached;
63 mutable char fCreditCached;
64 mutable char fAvailableCreditCached;
65 mutable char fChangeCached;
66 mutable int64 nDebitCached;
67 mutable int64 nCreditCached;
68 mutable int64 nAvailableCreditCached;
69 mutable int64 nChangeCached;
71 // memory only UI hints
72 mutable unsigned int nTimeDisplayed;
73 mutable int nLinesDisplayed;
74 mutable char fConfirmedDisplayed;
82 CWalletTx(const CMerkleTx& txIn) : CMerkleTx(txIn)
87 CWalletTx(const CTransaction& txIn) : CMerkleTx(txIn)
97 fTimeReceivedIsTxTime = false;
100 strFromAccount.clear();
102 fDebitCached = false;
103 fCreditCached = false;
104 fAvailableCreditCached = false;
105 fChangeCached = false;
108 nAvailableCreditCached = 0;
112 fConfirmedDisplayed = false;
117 CWalletTx* pthis = const_cast<CWalletTx*>(this);
124 pthis->mapValue["fromaccount"] = pthis->strFromAccount;
127 BOOST_FOREACH(char f, vfSpent)
129 str += (f ? '1' : '0');
133 pthis->mapValue["spent"] = str;
136 nSerSize += SerReadWrite(s, *(CMerkleTx*)this, nType, nVersion,ser_action);
139 READWRITE(vOrderForm);
140 READWRITE(fTimeReceivedIsTxTime);
141 READWRITE(nTimeReceived);
147 pthis->strFromAccount = pthis->mapValue["fromaccount"];
149 if (mapValue.count("spent"))
150 BOOST_FOREACH(char c, pthis->mapValue["spent"])
151 pthis->vfSpent.push_back(c != '0');
153 pthis->vfSpent.assign(vout.size(), fSpent);
156 pthis->mapValue.erase("fromaccount");
157 pthis->mapValue.erase("version");
158 pthis->mapValue.erase("spent");
161 // marks certain txout's as spent
162 // returns true if any update took place
163 bool UpdateSpent(const std::vector<char>& vfNewSpent)
165 bool fReturn = false;
166 for (int i=0; i < vfNewSpent.size(); i++)
168 if (i == vfSpent.size())
171 if (vfNewSpent[i] && !vfSpent[i])
175 fAvailableCreditCached = false;
183 fCreditCached = false;
184 fAvailableCreditCached = false;
185 fDebitCached = false;
186 fChangeCached = false;
189 void MarkSpent(unsigned int nOut)
191 if (nOut >= vout.size())
192 throw std::runtime_error("CWalletTx::MarkSpent() : nOut out of range");
193 vfSpent.resize(vout.size());
196 vfSpent[nOut] = true;
197 fAvailableCreditCached = false;
201 bool IsSpent(unsigned int nOut) const
203 if (nOut >= vout.size())
204 throw std::runtime_error("CWalletTx::IsSpent() : nOut out of range");
205 if (nOut >= vfSpent.size())
207 return (!!vfSpent[nOut]);
210 int64 GetDebit() const
216 nDebitCached = CTransaction::GetDebit();
221 int64 GetCredit(bool fUseCache=true) const
223 // Must wait until coinbase is safely deep enough in the chain before valuing it
224 if (IsCoinBase() && GetBlocksToMaturity() > 0)
227 // GetBalance can assume transactions in mapWallet won't change
228 if (fUseCache && fCreditCached)
229 return nCreditCached;
230 nCreditCached = CTransaction::GetCredit();
231 fCreditCached = true;
232 return nCreditCached;
235 int64 GetAvailableCredit(bool fUseCache=true) const
237 // Must wait until coinbase is safely deep enough in the chain before valuing it
238 if (IsCoinBase() && GetBlocksToMaturity() > 0)
241 if (fUseCache && fAvailableCreditCached)
242 return nAvailableCreditCached;
245 for (int i = 0; i < vout.size(); i++)
249 const CTxOut &txout = vout[i];
250 nCredit += txout.GetCredit();
251 if (!MoneyRange(nCredit))
252 throw std::runtime_error("CWalletTx::GetAvailableCredit() : value out of range");
256 nAvailableCreditCached = nCredit;
257 fAvailableCreditCached = true;
262 int64 GetChange() const
265 return nChangeCached;
266 nChangeCached = CTransaction::GetChange();
267 fChangeCached = true;
268 return nChangeCached;
271 void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list<std::pair<std::string /* address */, int64> >& listReceived,
272 std::list<std::pair<std::string /* address */, int64> >& listSent, int64& nFee, std::string& strSentAccount) const;
274 void GetAccountAmounts(const std::string& strAccount, int64& nGenerated, int64& nReceived,
275 int64& nSent, int64& nFee) const;
277 bool IsFromMe() const
279 return (GetDebit() > 0);
282 bool IsConfirmed() const
284 // Quick answer in most cases
287 if (GetDepthInMainChain() >= 1)
289 if (!IsFromMe()) // using wtx's cached debit
292 // If no confirmations but it's from us, we can still
293 // consider it confirmed if all dependencies are confirmed
294 std::map<uint256, const CMerkleTx*> mapPrev;
295 std::vector<const CMerkleTx*> vWorkQueue;
296 vWorkQueue.reserve(vtxPrev.size()+1);
297 vWorkQueue.push_back(this);
298 for (int i = 0; i < vWorkQueue.size(); i++)
300 const CMerkleTx* ptx = vWorkQueue[i];
304 if (ptx->GetDepthInMainChain() >= 1)
306 if (!ptx->IsFromMe())
310 BOOST_FOREACH(const CMerkleTx& tx, vtxPrev)
311 mapPrev[tx.GetHash()] = &tx;
313 BOOST_FOREACH(const CTxIn& txin, ptx->vin)
315 if (!mapPrev.count(txin.prevout.hash))
317 vWorkQueue.push_back(mapPrev[txin.prevout.hash]);
325 int64 GetTxTime() const;
326 int GetRequestCount() const;
328 void AddSupportingTransactions(CTxDB& txdb);
330 bool AcceptWalletTransaction(CTxDB& txdb, bool fCheckInputs=true);
331 bool AcceptWalletTransaction();
333 void RelayWalletTransaction(CTxDB& txdb);
334 void RelayWalletTransaction();
339 // Private key that includes an expiration date in case it never gets used.
347 std::string strComment;
348 //// todo: add something to note what created it (user, getnewaddress, change)
349 //// maybe should have a map<string, string> property map
351 CWalletKey(int64 nExpires=0)
353 nTimeCreated = (nExpires ? GetTime() : 0);
354 nTimeExpires = nExpires;
359 if (!(nType & SER_GETHASH))
361 READWRITE(vchPrivKey);
362 READWRITE(nTimeCreated);
363 READWRITE(nTimeExpires);
364 READWRITE(strComment);
374 // Account information.
375 // Stored in wallet with key "acc"+string account name
380 std::vector<unsigned char> vchPubKey;
394 if (!(nType & SER_GETHASH))
396 READWRITE(vchPubKey);
403 // Internal transfers.
404 // Database key is acentry<account><counter>
406 class CAccountingEntry
409 std::string strAccount;
412 std::string strOtherAccount;
413 std::string strComment;
425 strOtherAccount.clear();
431 if (!(nType & SER_GETHASH))
433 // Note: strAccount is serialized as part of the key, not here.
434 READWRITE(nCreditDebit);
436 READWRITE(strOtherAccount);
437 READWRITE(strComment);