6 #ifndef ANONCOIN_WALLET_H
7 #define ANONCOIN_WALLET_H
32 static const int64_t DEFAULT_TRANSACTION_FEE = 0;
34 static const int nHighTransactionFeeWarning = 0.01 * COIN;
70 vchPubKey = vchPubKeyIn;
94 typedef std::map<std::string, std::string>
StringMap;
104 bool SelectCoins(int64_t nTargetValue, std::set<std::pair<const CWalletTx*,unsigned int> >& setCoinsRet, int64_t& nValueRet,
const CCoinControl *coinControl = NULL)
const;
120 typedef std::multimap<COutPoint, uint256>
TxSpends;
123 void AddToSpends(
const uint256& wtxid);
125 void SyncMetaData(std::pair<TxSpends::iterator, TxSpends::iterator>);
153 strWalletFile = strWalletFileIn;
162 pwalletdbEncryption = NULL;
187 void AvailableCoins(std::vector<COutput>& vCoins,
bool fOnlyConfirmed=
true,
const CCoinControl *coinControl = NULL)
const;
188 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;
190 bool IsSpent(
const uint256& hash,
unsigned int n)
const;
192 bool IsLockedCoin(
uint256 hash,
unsigned int n)
const;
195 void UnlockAllCoins();
196 void ListLockedCoins(std::vector<COutPoint>& vOutpts);
202 bool AddKeyPubKey(
const CKey& key,
const CPubKey &pubkey);
208 bool LoadMinVersion(
int nVersion) {
AssertLockHeld(cs_wallet); nWalletVersion = nVersion; nWalletMaxVersion = std::max(nWalletMaxVersion, nVersion);
return true; }
211 bool AddCryptedKey(
const CPubKey &vchPubKey,
const std::vector<unsigned char> &vchCryptedSecret);
213 bool LoadCryptedKey(
const CPubKey &vchPubKey,
const std::vector<unsigned char> &vchCryptedSecret);
214 bool AddCScript(
const CScript& redeemScript);
215 bool LoadCScript(
const CScript& redeemScript);
218 bool AddDestData(
const CTxDestination &dest,
const std::string &key,
const std::string &value);
220 bool EraseDestData(
const CTxDestination &dest,
const std::string &key);
222 bool LoadDestData(
const CTxDestination &dest,
const std::string &key,
const std::string &value);
224 bool GetDestData(
const CTxDestination &dest,
const std::string &key, std::string *value)
const;
227 bool AddWatchOnly(
const CScript &dest);
228 bool RemoveWatchOnly(
const CScript &dest);
230 bool LoadWatchOnly(
const CScript &dest);
233 bool ChangeWalletPassphrase(
const SecureString& strOldWalletPassphrase,
const SecureString& strNewWalletPassphrase);
234 bool EncryptWallet(
const SecureString& strWalletPassphrase);
236 void GetKeyBirthTimes(std::map<CKeyID, int64_t> &mapKeyBirth)
const;
241 int64_t IncOrderPosNext(
CWalletDB *pwalletdb = NULL);
243 typedef std::pair<CWalletTx*, CAccountingEntry*>
TxPair;
244 typedef std::multimap<int64_t, TxPair >
TxItems;
250 TxItems OrderedTxItems(std::list<CAccountingEntry>& acentries, std::string strAccount =
"");
253 bool AddToWallet(
const CWalletTx& wtxIn,
bool fFromLoadWallet=
false);
256 void EraseFromWallet(
const uint256 &hash);
257 int ScanForWalletTransactions(
CBlockIndex* pindexStart,
bool fUpdate =
false);
258 void ReacceptWalletTransactions();
259 void ResendWalletTransactions();
260 int64_t GetBalance()
const;
261 int64_t GetUnconfirmedBalance()
const;
262 int64_t GetImmatureBalance()
const;
263 int64_t GetWatchOnlyBalance()
const;
264 int64_t GetUnconfirmedWatchOnlyBalance()
const;
265 int64_t GetImmatureWatchOnlyBalance()
const;
266 bool CreateTransaction(
const std::vector<std::pair<CScript, int64_t> >& vecSend,
268 bool CreateTransaction(
CScript scriptPubKey, int64_t nValue,
271 std::string SendMoney(
CScript scriptPubKey, int64_t nValue,
CWalletTx& wtxNew);
275 bool TopUpKeyPool(
unsigned int kpSize = 0);
276 int64_t AddReserveKey(
const CKeyPool& keypool);
277 void ReserveKeyFromKeyPool(int64_t& nIndex,
CKeyPool& keypool);
278 void KeepKey(int64_t nIndex);
279 void ReturnKey(int64_t nIndex);
280 bool GetKeyFromPool(
CPubKey &key);
281 int64_t GetOldestKeyPoolTime();
282 void GetAllReserveKeys(std::set<CKeyID>& setAddress)
const;
284 std::set< std::set<CTxDestination> > GetAddressGroupings();
285 std::map<CTxDestination, int64_t> GetAddressBalances();
287 std::set<CTxDestination> GetAccountAddresses(std::string strAccount)
const;
298 throw std::runtime_error(
"CWallet::GetCredit() : value out of range");
301 bool IsChange(
const CTxOut& txout)
const;
305 throw std::runtime_error(
"CWallet::GetChange() : value out of range");
306 return (IsChange(txout) ? txout.
nValue : 0);
322 BOOST_FOREACH(
const CTxIn& txin, tx.
vin)
324 nDebit += GetDebit(txin, filter);
326 throw std::runtime_error(
"CWallet::GetDebit() : value out of range");
335 nCredit += GetCredit(txout, filter);
337 throw std::runtime_error(
"CWallet::GetCredit() : value out of range");
346 nChange += GetChange(txout);
348 throw std::runtime_error(
"CWallet::GetChange() : value out of range");
354 DBErrors LoadWallet(
bool& fFirstRunRet);
357 bool SetAddressBook(
const CTxDestination& address,
const std::string& strName,
const std::string& purpose);
361 void UpdatedTransaction(
const uint256 &hashTx);
367 std::map<uint256, int>::iterator mi = mapRequestCount.find(hash);
368 if (mi != mapRequestCount.end())
376 return setKeyPool.size();
379 bool SetDefaultKey(
const CPubKey &vchPubKey);
385 bool SetMaxVersion(
int nVersion);
391 std::set<uint256> GetConflicts(
const uint256& txid)
const;
397 &address,
const std::string &label,
bool isMine,
398 const std::string &purpose,
404 boost::signals2::signal<void (
CWallet *wallet,
const uint256 &hashTx,
408 boost::signals2::signal<void (const std::string &title, int nProgress)>
ShowProgress;
434 bool GetReservedKey(
CPubKey &pubkey);
442 static void ReadOrderPos(int64_t& nOrderPos, mapValue_t& mapValue)
444 if (!mapValue.count(
"n"))
449 nOrderPos =
atoi64(mapValue[
"n"].c_str());
453 static void WriteOrderPos(
const int64_t& nOrderPos, mapValue_t& mapValue)
457 mapValue[
"n"] =
i64tostr(nOrderPos);
471 std::vector<std::pair<std::string, std::string> >
vOrderForm;
524 fTimeReceivedIsTxTime =
false;
528 strFromAccount.clear();
529 fDebitCached =
false;
530 fCreditCached =
false;
531 fImmatureCreditCached =
false;
532 fAvailableCreditCached =
false;
533 fWatchDebitCached =
false;
534 fWatchCreditCached =
false;
535 fImmatureWatchCreditCached =
false;
536 fAvailableWatchCreditCached =
false;
537 fChangeCached =
false;
540 nImmatureCreditCached = 0;
541 nAvailableCreditCached = 0;
542 nWatchDebitCached = 0;
543 nWatchCreditCached = 0;
544 nAvailableWatchCreditCached = 0;
545 nImmatureWatchCreditCached = 0;
552 CWalletTx* pthis = const_cast<CWalletTx*>(
this);
559 pthis->mapValue[
"fromaccount"] = pthis->strFromAccount;
561 WriteOrderPos(pthis->nOrderPos, pthis->mapValue);
564 pthis->mapValue[
"timesmart"] =
strprintf(
"%u", nTimeSmart);
568 std::vector<CMerkleTx> vUnused;
579 pthis->strFromAccount = pthis->mapValue[
"fromaccount"];
581 ReadOrderPos(pthis->nOrderPos, pthis->mapValue);
583 pthis->nTimeSmart = mapValue.count(
"timesmart") ? (
unsigned int)
atoi64(pthis->mapValue[
"timesmart"]) : 0;
586 pthis->mapValue.erase(
"fromaccount");
587 pthis->mapValue.erase(
"version");
588 pthis->mapValue.erase(
"spent");
589 pthis->mapValue.erase(
"n");
590 pthis->mapValue.erase(
"timesmart");
596 fCreditCached =
false;
597 fAvailableCreditCached =
false;
598 fWatchDebitCached =
false;
599 fWatchCreditCached =
false;
600 fAvailableWatchCreditCached =
false;
601 fImmatureWatchCreditCached =
false;
602 fDebitCached =
false;
603 fChangeCached =
false;
622 debit += nDebitCached;
625 nDebitCached = pwallet->
GetDebit(*
this, ISMINE_SPENDABLE);
627 debit += nDebitCached;
632 if(fWatchDebitCached)
633 debit += nWatchDebitCached;
636 nWatchDebitCached = pwallet->
GetDebit(*
this, ISMINE_WATCH_ONLY);
637 fWatchDebitCached =
true;
638 debit += nWatchDebitCached;
647 if (IsCoinBase() && GetBlocksToMaturity() > 0)
655 credit += nCreditCached;
658 nCreditCached = pwallet->
GetCredit(*
this, ISMINE_SPENDABLE);
659 fCreditCached =
true;
660 credit += nCreditCached;
665 if(fWatchCreditCached)
666 credit += nWatchCreditCached;
669 nWatchCreditCached = pwallet->
GetCredit(*
this, ISMINE_WATCH_ONLY);
670 fWatchCreditCached =
true;
671 credit += nWatchCreditCached;
679 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
681 if (fUseCache && fImmatureCreditCached)
682 return nImmatureCreditCached;
684 fImmatureCreditCached =
true;
685 return nImmatureCreditCached;
697 if (IsCoinBase() && GetBlocksToMaturity() > 0)
700 if (fUseCache && fAvailableCreditCached)
701 return nAvailableCreditCached;
705 for (
unsigned int i = 0; i < vout.size(); i++)
707 if (!pwallet->
IsSpent(hashTx, i))
709 const CTxOut &txout = vout[i];
712 throw std::runtime_error(
"CWalletTx::GetAvailableCredit() : value out of range");
716 nAvailableCreditCached = nCredit;
717 fAvailableCreditCached =
true;
723 if (IsCoinBase() && GetBlocksToMaturity() > 0 && IsInMainChain())
725 if (fUseCache && fImmatureWatchCreditCached)
726 return nImmatureWatchCreditCached;
728 fImmatureWatchCreditCached =
true;
729 return nImmatureWatchCreditCached;
741 if (IsCoinBase() && GetBlocksToMaturity() > 0)
744 if (fUseCache && fAvailableWatchCreditCached)
745 return nAvailableWatchCreditCached;
748 for (
unsigned int i = 0; i < vout.size(); i++)
750 if (!pwallet->
IsSpent(GetHash(), i))
752 const CTxOut &txout = vout[i];
755 throw std::runtime_error(
"CWalletTx::GetAvailableCredit() : value out of range");
759 nAvailableWatchCreditCached = nCredit;
760 fAvailableWatchCreditCached =
true;
767 return nChangeCached;
768 nChangeCached = pwallet->
GetChange(*
this);
769 fChangeCached =
true;
770 return nChangeCached;
773 void GetAmounts(std::list<std::pair<CTxDestination, int64_t> >& listReceived,
774 std::list<std::pair<CTxDestination, int64_t> >& listSent, int64_t& nFee, std::string& strSentAccount,
const isminefilter& filter)
const;
776 void GetAccountAmounts(
const std::string& strAccount, int64_t& nReceived,
777 int64_t& nSent, int64_t& nFee,
const isminefilter& filter)
const;
781 return (GetDebit(filter) > 0);
789 int nDepth = GetDepthInMainChain();
798 BOOST_FOREACH(
const CTxIn& txin, vin)
813 int64_t GetTxTime()
const;
814 int GetRequestCount()
const;
816 void RelayWalletTransaction();
818 std::set<uint256> GetConflicts()
const;
834 tx = txIn; i = iIn; nDepth = nDepthIn; fSpendable = fSpendableIn;
864 nTimeCreated = (nExpires ?
GetTime() : 0);
865 nTimeExpires = nExpires;
870 if (!(nType & SER_GETHASH))
904 if (!(nType & SER_GETHASH))
937 strOtherAccount.clear();
945 if (!(nType & SER_GETHASH))
954 WriteOrderPos(nOrderPos, me.mapValue);
956 if (!(mapValue.empty() && _ssExtra.empty()))
959 ss.insert(ss.begin(),
'\0');
961 ss.insert(ss.end(), _ssExtra.begin(), _ssExtra.end());
962 me.strComment.append(ss.str());
968 size_t nSepPos = strComment.find(
"\0", 0, 1);
972 if (std::string::npos != nSepPos)
974 CDataStream ss(std::vector<char>(strComment.begin() + nSepPos + 1, strComment.end()), nType, nVersion);
976 me._ssExtra = std::vector<char>(ss.begin(), ss.end());
978 ReadOrderPos(me.nOrderPos, me.mapValue);
980 if (std::string::npos != nSepPos)
981 me.strComment.erase(nSepPos);
983 me.mapValue.erase(
"n");
987 std::vector<char> _ssExtra;
int64_t GetImmatureWatchOnlyCredit(const bool &fUseCache=true) const
bool IsFinalTx(const CTransaction &tx, int nBlockHeight, int64_t nBlockTime)
bool IsMine(const CTransaction &tx) const
int64_t GetCredit(const CTxOut &txout, const isminefilter &filter) const
std::set< int64_t > setKeyPool
void BindWallet(CWallet *pwalletIn)
isminetype IsMine(const CTxIn &txin) const
unsigned int SerReadWrite(Stream &s, const T &obj, int nType, int nVersion, CSerActionGetSerializeSize ser_action)
int64_t GetChange(const CTransaction &tx) const
Describes a place in the block chain to another node such that if the other node doesn't have the sam...
int64_t GetChange() const
int64_t GetCredit(const CTransaction &tx, const isminefilter &filter) const
std::map< CTxDestination, CAddressBookData > mapAddressBook
CCriticalSection cs_wallet
Main wallet lock.
isminetype IsMine(const CKeyStore &keystore, const CTxDestination &dest)
bool fImmatureCreditCached
std::string strFromAccount
WalletFeature
(client) version numbers for particular wallet features
CWalletDB * pwalletdbEncryption
std::multimap< COutPoint, uint256 > TxSpends
bool IsFromMe(const CTransaction &tx) const
int64_t nImmatureCreditCached
std::string i64tostr(int64_t n)
bool MoneyRange(int64_t nValue)
int64_t GetChange(const CTxOut &txout) const
#define IMPLEMENT_SERIALIZE(statements)
unsigned int GetKeyPoolSize()
std::map< unsigned int, CMasterKey > MasterKeyMap
Double ended buffer combining vector and stream-like interfaces.
CWalletKey(int64_t nExpires=0)
COutput(const CWalletTx *txIn, int iIn, int nDepthIn, bool fSpendableIn)
int64_t nAvailableWatchCreditCached
DBErrors
Error statuses for the wallet database.
Keystore which keeps the private keys encrypted.
string FormatMoney(int64_t n, bool fPlus)
bool CanSupportFeature(enum WalletFeature wf)
bool fAvailableCreditCached
int64_t GetDebit(const CTxIn &txin, const isminefilter &filter) const
isminetype
IsMine() return codes.
std::multimap< int64_t, TxPair > TxItems
bool fAvailableWatchCreditCached
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.
#define AssertLockHeld(cs)
int64_t GetDebit(const CTransaction &tx, const isminefilter &filter) const
bool IsSpent(const uint256 &hash, unsigned int n) const
unsigned int nMasterKeyMaxID
ChangeType
General change type (added, updated, removed).
An input of a transaction.
const CWalletTx * GetWalletTx(const uint256 &hash) const
std::vector< CTxOut > vout
int64_t atoi64(const char *psz)
int64_t GetAvailableWatchOnlyCredit(const bool &fUseCache=true) const
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
std::map< std::string, std::string > StringMap
bool LoadKey(const CKey &key, const CPubKey &pubkey)
bool bSpendZeroConfChange
bool AddKeyPubKey(const CKey &key, const CPubKey &pubkey)
An output of a transaction.
std::map< uint256, int > mapRequestCount
bool IsFromMe(const isminefilter &filter) const
An outpoint - a combination of a transaction hash and an index n into its vout.
uint8_t isminefilter
used for bitflags of isminetype
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)
int64_t nWatchCreditCached
boost::variant< CNoDestination, CKeyID, CScriptID > CTxDestination
A txout script template with a specific destination.
int64_t GetCredit(const isminefilter &filter) const
int64_t GetDebit(const isminefilter &filter) const
void Init(const CWallet *pwalletIn)
CWallet(std::string strWalletFileIn)
isminetype IsMine(const CTxOut &txout) const
boost::signals2::signal< void(CWallet *wallet, const CTxDestination &address, const std::string &label, bool isMine, const std::string &purpose, ChangeType status)> NotifyAddressBookChanged
Address book entry changed.
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
CWalletTx(const CWallet *pwalletIn)
Private key that includes an expiration date in case it never gets used.
#define LIMITED_STRING(obj, n)
A CWallet is an extension of a keystore, which also maintains a set of transactions and balances...
CKeyPool(const CPubKey &vchPubKeyIn)
std::map< CKeyID, CKeyMetadata > mapKeyMetadata
CReserveKey(CWallet *pwalletIn)
std::map< uint256, CWalletTx > mapWallet
boost::signals2::signal< void(const std::string &title, int nProgress)> ShowProgress
Show progress e.g.
CWalletTx(const CWallet *pwalletIn, const CTransaction &txIn)
unsigned int nTimeReceived
An encapsulated private key.
int64_t GetAvailableCredit(bool fUseCache=true) const
The basic transaction that is broadcasted on the network and contained in blocks. ...
bool fImmatureWatchCreditCached
std::pair< CWalletTx *, CAccountingEntry * > TxPair
int64_t nImmatureWatchCreditCached
boost::signals2::signal< void(bool fHaveWatchOnly)> NotifyWatchonlyChanged
Watch-only address added.
int64_t nAvailableCreditCached
std::string strOtherAccount
int64_t nWatchDebitCached
int64_t GetImmatureCredit(bool fUseCache=true) const
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