5 #ifndef BITCOIN_WALLET_H
6 #define BITCOIN_WALLET_H
54 vchPubKey = vchPubKeyIn;
72 bool SelectCoins(
int64 nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet,
int64& nValueRet,
const CCoinControl *coinControl=NULL)
const;
101 pwalletdbEncryption = NULL;
108 strWalletFile = strWalletFileIn;
111 pwalletdbEncryption = NULL;
128 void AvailableCoins(std::vector<COutput>& vCoins,
bool fOnlyConfirmed=
true,
const CCoinControl *coinControl=NULL)
const;
129 bool SelectCoinsMinConf(
int64 nTargetValue,
int nConfMine,
int nConfTheirs, std::vector<COutput> vCoins, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet,
int64& nValueRet)
const;
130 bool IsLockedCoin(
uint256 hash,
unsigned int n)
const;
133 void UnlockAllCoins();
134 void ListLockedCoins(std::vector<COutPoint>& vOutpts);
140 bool AddKeyPubKey(
const CKey& key,
const CPubKey &pubkey);
144 bool LoadMinVersion(
int nVersion) { nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion);
return true; }
147 bool AddCryptedKey(
const CPubKey &vchPubKey,
const std::vector<unsigned char> &vchCryptedSecret);
149 bool LoadCryptedKey(
const CPubKey &vchPubKey,
const std::vector<unsigned char> &vchCryptedSecret);
150 bool AddCScript(
const CScript& redeemScript);
154 bool ChangeWalletPassphrase(
const SecureString& strOldWalletPassphrase,
const SecureString& strNewWalletPassphrase);
155 bool EncryptWallet(
const SecureString& strWalletPassphrase);
162 typedef std::pair<CWalletTx*, CAccountingEntry*>
TxPair;
163 typedef std::multimap<int64, TxPair >
TxItems;
169 TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount =
"");
172 bool AddToWallet(
const CWalletTx& wtxIn);
173 bool AddToWalletIfInvolvingMe(
const uint256 &
hash,
const CTransaction& tx,
const CBlock* pblock,
bool fUpdate =
false,
bool fFindBlock =
false);
176 int ScanForWalletTransactions(
CBlockIndex* pindexStart,
bool fUpdate =
false);
177 void ReacceptWalletTransactions();
178 void ResendWalletTransactions();
179 int64 GetBalance()
const;
180 int64 GetUnconfirmedBalance()
const;
181 int64 GetImmatureBalance()
const;
182 bool CreateTransaction(
const std::vector<std::pair<CScript, int64> >& vecSend,
184 bool CreateTransaction(
CScript scriptPubKey,
int64 nValue,
193 void ReserveKeyFromKeyPool(
int64& nIndex,
CKeyPool& keypool);
194 void KeepKey(
int64 nIndex);
195 void ReturnKey(
int64 nIndex);
196 bool GetKeyFromPool(
CPubKey &key,
bool fAllowReuse=
true);
197 int64 GetOldestKeyPoolTime();
198 void GetAllReserveKeys(std::set<CKeyID>& setAddress);
200 std::set< std::set<CTxDestination> > GetAddressGroupings();
201 std::map<CTxDestination, int64> GetAddressBalances();
212 throw std::runtime_error(
"CWallet::GetCredit() : value out of range");
215 bool IsChange(
const CTxOut& txout)
const;
219 throw std::runtime_error(
"CWallet::GetChange() : value out of range");
220 return (IsChange(txout) ? txout.
nValue : 0);
231 return (GetDebit(tx) > 0);
236 BOOST_FOREACH(
const CTxIn& txin, tx.
vin)
238 nDebit += GetDebit(txin);
240 throw std::runtime_error(
"CWallet::GetDebit() : value out of range");
249 nCredit += GetCredit(txout);
251 throw std::runtime_error(
"CWallet::GetCredit() : value out of range");
260 nChange += GetChange(txout);
262 throw std::runtime_error(
"CWallet::GetChange() : value out of range");
268 DBErrors LoadWallet(
bool& fFirstRunRet);
270 bool SetAddressBookName(
const CTxDestination& address,
const std::string& strName);
274 void UpdatedTransaction(
const uint256 &hashTx);
276 void PrintWallet(
const CBlock& block);
282 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
283 if (mi != mapRequestCount.end())
290 return setKeyPool.size();
295 bool SetDefaultKey(
const CPubKey &vchPubKey);
301 bool SetMaxVersion(
int nVersion);
309 boost::signals2::signal<void (CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)>
NotifyAddressBookChanged;
337 bool GetReservedKey(
CPubKey &pubkey);
345 static void ReadOrderPos(
int64& nOrderPos, mapValue_t& mapValue)
347 if (!mapValue.count(
"n"))
352 nOrderPos =
atoi64(mapValue[
"n"].c_str());
356 static void WriteOrderPos(
const int64& nOrderPos, mapValue_t& mapValue)
360 mapValue[
"n"] =
i64tostr(nOrderPos);
375 std::vector<std::pair<std::string, std::string> >
vOrderForm;
422 fTimeReceivedIsTxTime =
false;
426 strFromAccount.clear();
428 fDebitCached =
false;
429 fCreditCached =
false;
430 fImmatureCreditCached =
false;
431 fAvailableCreditCached =
false;
432 fChangeCached =
false;
435 nImmatureCreditCached = 0;
436 nAvailableCreditCached = 0;
443 CWalletTx* pthis = const_cast<CWalletTx*>(
this);
450 pthis->mapValue[
"fromaccount"] = pthis->strFromAccount;
453 BOOST_FOREACH(
char f, vfSpent)
455 str += (f ?
'1' :
'0');
459 pthis->mapValue[
"spent"] = str;
461 WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
464 pthis->mapValue[
"timesmart"] =
strprintf(
"%u", nTimeSmart);
478 pthis->strFromAccount = pthis->mapValue[
"fromaccount"];
480 if (mapValue.count(
"spent"))
481 BOOST_FOREACH(
char c, pthis->mapValue[
"spent"])
482 pthis->vfSpent.push_back(c !=
'0');
484 pthis->vfSpent.assign(vout.size(), fSpent);
486 ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
488 pthis->nTimeSmart = mapValue.count(
"timesmart") ? (
unsigned int)
atoi64(pthis->mapValue[
"timesmart"]) : 0;
491 pthis->mapValue.erase(
"fromaccount");
492 pthis->mapValue.erase(
"version");
493 pthis->mapValue.erase(
"spent");
494 pthis->mapValue.erase(
"n");
495 pthis->mapValue.erase(
"timesmart");
500 bool UpdateSpent(
const std::vector<char>& vfNewSpent)
502 bool fReturn =
false;
503 for (
unsigned int i = 0; i < vfNewSpent.size(); i++)
505 if (i == vfSpent.size())
508 if (vfNewSpent[i] && !vfSpent[i])
512 fAvailableCreditCached =
false;
521 fCreditCached =
false;
522 fAvailableCreditCached =
false;
523 fDebitCached =
false;
524 fChangeCached =
false;
535 if (nOut >= vout.size())
536 throw std::runtime_error(
"CWalletTx::MarkSpent() : nOut out of range");
537 vfSpent.resize(vout.size());
540 vfSpent[nOut] =
true;
541 fAvailableCreditCached =
false;
547 if (nOut >= vout.size())
548 throw std::runtime_error(
"CWalletTx::IsSpent() : nOut out of range");
549 if (nOut >= vfSpent.size())
551 return (!!vfSpent[nOut]);
560 nDebitCached = pwallet->
GetDebit(*
this);
568 if (IsCoinBase() && GetBlocksToMaturity() > 0)
572 if (fUseCache && fCreditCached)
573 return nCreditCached;
574 nCreditCached = pwallet->
GetCredit(*
this);
575 fCreditCached =
true;
576 return nCreditCached;
581 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
583 if (fUseCache && fImmatureCreditCached)
584 return nImmatureCreditCached;
585 nImmatureCreditCached = pwallet->
GetCredit(*
this);
586 fImmatureCreditCached =
true;
587 return nImmatureCreditCached;
596 if (IsCoinBase() && GetBlocksToMaturity() > 0)
599 if (fUseCache && fAvailableCreditCached)
600 return nAvailableCreditCached;
603 for (
unsigned int i = 0; i < vout.size(); i++)
607 const CTxOut &txout = vout[i];
610 throw std::runtime_error(
"CWalletTx::GetAvailableCredit() : value out of range");
614 nAvailableCreditCached = nCredit;
615 fAvailableCreditCached =
true;
623 return nChangeCached;
624 nChangeCached = pwallet->
GetChange(*
this);
625 fChangeCached =
true;
626 return nChangeCached;
629 void GetAmounts(std::list<std::pair<CTxDestination, int64> >& listReceived,
630 std::list<std::pair<CTxDestination, int64> >& listSent,
int64& nFee, std::string& strSentAccount)
const;
632 void GetAccountAmounts(
const std::string& strAccount,
int64& nReceived,
637 return (GetDebit() > 0);
645 if (GetDepthInMainChain() >= 1)
652 std::map<uint256, const CMerkleTx*> mapPrev;
653 std::vector<const CMerkleTx*> vWorkQueue;
654 vWorkQueue.reserve(vtxPrev.size()+1);
655 vWorkQueue.push_back(
this);
656 for (
unsigned int i = 0; i < vWorkQueue.size(); i++)
669 BOOST_FOREACH(
const CMerkleTx& tx, vtxPrev)
673 BOOST_FOREACH(
const CTxIn& txin, ptx->
vin)
685 int64 GetTxTime()
const;
686 int GetRequestCount()
const;
688 void AddSupportingTransactions();
689 bool AcceptWalletTransaction(
bool fCheckInputs=
true);
690 void RelayWalletTransaction();
705 tx = txIn; i = iIn; nDepth = nDepthIn;
715 printf(
"%s\n", ToString().c_str());
735 nTimeCreated = (nExpires ?
GetTime() : 0);
736 nTimeExpires = nExpires;
741 if (!(nType & SER_GETHASH))
775 if (!(nType & SER_GETHASH))
808 strOtherAccount.clear();
816 if (!(nType & SER_GETHASH))
825 WriteOrderPos(nOrderPos, me.mapValue);
827 if (!(mapValue.empty() && _ssExtra.empty()))
830 ss.insert(ss.begin(),
'\0');
832 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
833 me.strComment.append(ss.str());
839 size_t nSepPos = strComment.find(
"\0", 0, 1);
843 if (std::string::npos != nSepPos)
845 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
847 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
849 ReadOrderPos(me.nOrderPos, me.mapValue);
851 if (std::string::npos != nSepPos)
852 me.strComment.erase(nSepPos);
854 me.mapValue.erase(
"n");
858 std::vector<char> _ssExtra;
bool IsMine(const CTransaction &tx) const
bool IsFinal(int nBlockHeight=0, int64 nBlockTime=0) const
#define strprintf(format,...)
void BindWallet(CWallet *pwalletIn)
unsigned int SerReadWrite(Stream &s, const T &obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
COutput(const CWalletTx *txIn, int iIn, int nDepthIn)
CCriticalSection cs_wallet
bool fImmatureCreditCached
std::string strFromAccount
WalletFeature
(client) version numbers for particular wallet features
CWalletDB * pwalletdbEncryption
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
std::vector< CMerkleTx > vtxPrev
bool IsFromMe(const CTransaction &tx) const
bool IsSpent(unsigned int nOut) const
#define IMPLEMENT_SERIALIZE(statements)
int64 GetChange(const CTransaction &tx) const
std::map< unsigned int, CMasterKey > MasterKeyMap
Double ended buffer combining vector and stream-like interfaces.
unsigned long long uint64
int64 GetDebit(const CTxIn &txin) const
std::vector< char > vfSpent
DBErrors
Error statuses for the wallet database.
Keystore which keeps the private keys encrypted.
bool IsMine(const CTxOut &txout) const
bool CanSupportFeature(enum WalletFeature wf)
bool fAvailableCreditCached
int64 GetDebit(const CTransaction &tx) const
int64 atoi64(const char *psz)
int64 GetCredit(bool fUseCache=true) const
virtual bool AddCScript(const CScript &redeemScript)
std::vector< unsigned char, secure_allocator< unsigned char > > CPrivKey
boost::signals2::signal< void(CWallet *wallet, const uint256 &hashTx, ChangeType status)> NotifyTransactionChanged
Wallet transaction added, removed or updated.
int64 GetCredit(const CTransaction &tx) const
unsigned int nMasterKeyMaxID
int64 GetAvailableCredit(bool fUseCache=true) const
int64 GetChange(const CTxOut &txout) const
bool SetBestChain(CValidationState &state, CBlockIndex *pindexNew)
Connect/disconnect blocks until pindexNew is the new tip of the active block chain.
int64 GetImmatureCredit(bool fUseCache=true) const
An input of a transaction.
std::vector< CTxOut > vout
std::string ToString() const
std::basic_string< char, std::char_traits< char >, secure_allocator< char > > SecureString
An encapsulated public key.
void Inventory(const uint256 &hash)
std::set< COutPoint > setLockedCoins
bool GetWalletFile(CWallet *pwallet, std::string &strWalletFileOut)
bool LoadKey(const CKey &key, const CPubKey &pubkey)
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
An output of a transaction.
std::map< uint256, int > mapRequestCount
An outpoint - a combination of a transaction hash and an index n into its vout.
bool LoadCScript(const CScript &redeemScript)
int64 GetCredit(const CTxOut &txout) const
unsigned int fTimeReceivedIsTxTime
bool LoadMinVersion(int nVersion)
Access to the wallet database (wallet.dat)
A transaction with a bunch of additional info that only the owner cares about.
std::string strWalletFile
CWalletTx(const CWallet *pwalletIn, const CMerkleTx &txIn)
void Init(const CWallet *pwalletIn)
CWallet(std::string strWalletFileIn)
std::set< int64 > setKeyPool
MasterKeyMap mapMasterKeys
A key allocated from the key pool.
The block chain is a tree shaped structure starting with the genesis block at the root...
Serialized script, used inside transaction inputs and outputs.
std::string ToString() const
string FormatMoney(int64 n, bool fPlus)
CWalletTx(const CWallet *pwalletIn)
bool IsMine(const CKeyStore &keystore, const CTxDestination &dest)
Private key that includes an expiration date in case it never gets used.
int64 nAvailableCreditCached
int64 nImmatureCreditCached
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
bool GetTransaction(const uint256 &hash, CTransaction &txOut, uint256 &hashBlock, bool fAllowSlow)
Retrieve a transaction (from memory pool, or from disk, if possible)
int GetDepthInMainChain(CBlockIndex *&pindexRet) const
CKeyPool(const CPubKey &vchPubKeyIn)
CReserveKey(CWallet *pwalletIn)
std::map< CTxDestination, std::string > mapAddressBook
std::map< uint256, CWalletTx > mapWallet
boost::signals2::signal< void(CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, ChangeType status)> NotifyAddressBookChanged
Address book entry changed.
CWalletTx(const CWallet *pwalletIn, const CTransaction &txIn)
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
unsigned int nTimeReceived
An encapsulated private key.
CWalletKey(int64 nExpires=0)
The basic transaction that is broadcasted on the network and contained in blocks. ...
std::string i64tostr(int64 n)
std::pair< CWalletTx *, CAccountingEntry * > TxPair
std::string strOtherAccount
void MarkSpent(unsigned int nOut)
std::multimap< int64, TxPair > TxItems
bool MoneyRange(int64 nValue)
A transaction with a merkle branch linking it to the block chain.
std::map< std::string, std::string > mapValue_t
std::vector< std::pair< std::string, std::string > > vOrderForm