Merge remote branch 'upstream/master'

Conflicts:
	src/bitcoinrpc.cpp
This commit is contained in:
Wladimir J. van der Laan 2011-07-26 15:38:31 +02:00
commit 491ad6db50
17 changed files with 399 additions and 349 deletions

View File

@ -159,52 +159,148 @@ inline bool DecodeBase58Check(const std::string& str, std::vector<unsigned char>
#define ADDRESSVERSION ((unsigned char)(fTestNet ? 111 : 0)) class CBase58Data
inline std::string Hash160ToAddress(uint160 hash160)
{ {
// add 1-byte version number to the front protected:
std::vector<unsigned char> vch(1, ADDRESSVERSION); unsigned char nVersion;
vch.insert(vch.end(), UBEGIN(hash160), UEND(hash160)); std::vector<unsigned char> vchData;
return EncodeBase58Check(vch);
}
inline bool AddressToHash160(const char* psz, uint160& hash160Ret) CBase58Data()
{
nVersion = 0;
vchData.clear();
}
~CBase58Data()
{
if (!vchData.empty())
memset(&vchData[0], 0, vchData.size());
}
void SetData(int nVersionIn, const void* pdata, size_t nSize)
{
nVersion = nVersionIn;
vchData.resize(nSize);
if (!vchData.empty())
memcpy(&vchData[0], pdata, nSize);
}
void SetData(int nVersionIn, const unsigned char *pbegin, const unsigned char *pend)
{
SetData(nVersionIn, (void*)pbegin, pend - pbegin);
}
public:
bool SetString(const char* psz)
{
std::vector<unsigned char> vchTemp;
DecodeBase58Check(psz, vchTemp);
if (vchTemp.empty())
{
vchData.clear();
nVersion = 0;
return false;
}
nVersion = vchTemp[0];
vchData.resize(vchTemp.size() - 1);
if (!vchData.empty())
memcpy(&vchData[0], &vchTemp[1], vchData.size());
memset(&vchTemp[0], 0, vchTemp.size());
return true;
}
bool SetString(const std::string& str)
{
return SetString(str.c_str());
}
std::string ToString() const
{
std::vector<unsigned char> vch(1, nVersion);
vch.insert(vch.end(), vchData.begin(), vchData.end());
return EncodeBase58Check(vch);
}
int CompareTo(const CBase58Data& b58) const
{
if (nVersion < b58.nVersion) return -1;
if (nVersion > b58.nVersion) return 1;
if (vchData < b58.vchData) return -1;
if (vchData > b58.vchData) return 1;
return 0;
}
bool operator==(const CBase58Data& b58) const { return CompareTo(b58) == 0; }
bool operator<=(const CBase58Data& b58) const { return CompareTo(b58) <= 0; }
bool operator>=(const CBase58Data& b58) const { return CompareTo(b58) >= 0; }
bool operator< (const CBase58Data& b58) const { return CompareTo(b58) < 0; }
bool operator> (const CBase58Data& b58) const { return CompareTo(b58) > 0; }
};
class CBitcoinAddress : public CBase58Data
{ {
std::vector<unsigned char> vch; public:
if (!DecodeBase58Check(psz, vch)) bool SetHash160(const uint160& hash160)
return false; {
if (vch.empty()) SetData(fTestNet ? 111 : 0, &hash160, 20);
return false; }
unsigned char nVersion = vch[0];
if (vch.size() != sizeof(hash160Ret) + 1)
return false;
memcpy(&hash160Ret, &vch[1], sizeof(hash160Ret));
return (nVersion <= ADDRESSVERSION);
}
inline bool AddressToHash160(const std::string& str, uint160& hash160Ret) bool SetPubKey(const std::vector<unsigned char>& vchPubKey)
{ {
return AddressToHash160(str.c_str(), hash160Ret); return SetHash160(Hash160(vchPubKey));
} }
inline bool IsValidBitcoinAddress(const char* psz) bool IsValid() const
{ {
uint160 hash160; int nExpectedSize = 20;
return AddressToHash160(psz, hash160); bool fExpectTestNet = false;
} switch(nVersion)
{
case 0:
break;
inline bool IsValidBitcoinAddress(const std::string& str) case 111:
{ fExpectTestNet = true;
return IsValidBitcoinAddress(str.c_str()); break;
}
default:
return false;
}
return fExpectTestNet == fTestNet && vchData.size() == nExpectedSize;
}
CBitcoinAddress()
{
}
CBitcoinAddress(uint160 hash160In)
{
SetHash160(hash160In);
}
inline std::string PubKeyToAddress(const std::vector<unsigned char>& vchPubKey) CBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
{ {
return Hash160ToAddress(Hash160(vchPubKey)); SetPubKey(vchPubKey);
} }
CBitcoinAddress(const std::string& strAddress)
{
SetString(strAddress);
}
CBitcoinAddress(const char* pszAddress)
{
SetString(pszAddress);
}
uint160 GetHash160() const
{
assert(vchData.size() == 20);
uint160 hash160;
memcpy(&hash160, &vchData[0], 20);
return hash160;
}
};
#endif #endif

View File

@ -342,7 +342,7 @@ Value getnewaddress(const Array& params, bool fHelp)
strAccount = AccountFromValue(params[0]); strAccount = AccountFromValue(params[0]);
// Generate a new key that is added to wallet // Generate a new key that is added to wallet
string strAddress = PubKeyToAddress(pwalletMain->GetOrReuseKeyFromPool()); string strAddress = CBitcoinAddress(pwalletMain->GetOrReuseKeyFromPool()).ToString();
// This could be done in the same main CS as GetKeyFromKeyPool. // This could be done in the same main CS as GetKeyFromKeyPool.
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
@ -353,7 +353,7 @@ Value getnewaddress(const Array& params, bool fHelp)
// requires cs_main, cs_mapWallet, cs_mapAddressBook locks // requires cs_main, cs_mapWallet, cs_mapAddressBook locks
string GetAccountAddress(string strAccount, bool bForceNew=false) CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
{ {
string strAddress; string strAddress;
@ -393,16 +393,14 @@ string GetAccountAddress(string strAccount, bool bForceNew=false)
else else
{ {
account.vchPubKey = pwalletMain->GetOrReuseKeyFromPool(); account.vchPubKey = pwalletMain->GetOrReuseKeyFromPool();
string strAddress = PubKeyToAddress(account.vchPubKey); string strAddress = CBitcoinAddress(account.vchPubKey).ToString();
pwalletMain->SetAddressBookName(strAddress, strAccount); pwalletMain->SetAddressBookName(strAddress, strAccount);
walletdb.WriteAccount(strAccount, account); walletdb.WriteAccount(strAccount, account);
} }
} }
} }
strAddress = PubKeyToAddress(account.vchPubKey); return CBitcoinAddress(account.vchPubKey);
return strAddress;
} }
Value getaccountaddress(const Array& params, bool fHelp) Value getaccountaddress(const Array& params, bool fHelp)
@ -421,7 +419,7 @@ Value getaccountaddress(const Array& params, bool fHelp)
CRITICAL_BLOCK(pwalletMain->cs_mapWallet) CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
ret = GetAccountAddress(strAccount); ret = GetAccountAddress(strAccount).ToString();
} }
return ret; return ret;
@ -437,9 +435,8 @@ Value setaccount(const Array& params, bool fHelp)
"Sets the account associated with the given address."); "Sets the account associated with the given address.");
string strAddress = params[0].get_str(); string strAddress = params[0].get_str();
uint160 hash160; CBitcoinAddress address(strAddress);
bool isValid = AddressToHash160(strAddress, hash160); if (!address.IsValid())
if (!isValid)
throw JSONRPCError(-5, "Invalid bitcoin address"); throw JSONRPCError(-5, "Invalid bitcoin address");
@ -452,10 +449,10 @@ Value setaccount(const Array& params, bool fHelp)
CRITICAL_BLOCK(pwalletMain->cs_mapWallet) CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
if (pwalletMain->mapAddressBook.count(strAddress)) if (pwalletMain->mapAddressBook.count(address))
{ {
string strOldAccount = pwalletMain->mapAddressBook[strAddress]; string strOldAccount = pwalletMain->mapAddressBook[address];
if (strAddress == GetAccountAddress(strOldAccount)) if (address == GetAccountAddress(strOldAccount))
GetAccountAddress(strOldAccount, true); GetAccountAddress(strOldAccount, true);
} }
@ -474,11 +471,12 @@ Value getaccount(const Array& params, bool fHelp)
"Returns the account associated with the given address."); "Returns the account associated with the given address.");
string strAddress = params[0].get_str(); string strAddress = params[0].get_str();
CBitcoinAddress address(strAddress);
string strAccount; string strAccount;
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
map<string, string>::iterator mi = pwalletMain->mapAddressBook.find(strAddress); map<CBitcoinAddress, string>::iterator mi = pwalletMain->mapAddressBook.find(address);
if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty())
strAccount = (*mi).second; strAccount = (*mi).second;
} }
@ -499,17 +497,12 @@ Value getaddressesbyaccount(const Array& params, bool fHelp)
Array ret; Array ret;
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
{ {
const string& strAddress = item.first; const CBitcoinAddress& address = item.first;
const string& strName = item.second; const string& strName = item.second;
if (strName == strAccount) if (strName == strAccount)
{ ret.push_back(address.ToString());
// We're only adding valid bitcoin addresses and not ip addresses
CScript scriptPubKey;
if (scriptPubKey.SetBitcoinAddress(strAddress))
ret.push_back(strAddress);
}
} }
} }
return ret; return ret;
@ -578,10 +571,11 @@ Value getreceivedbyaddress(const Array& params, bool fHelp)
"Returns the total amount received by <bitcoinaddress> in transactions with at least [minconf] confirmations."); "Returns the total amount received by <bitcoinaddress> in transactions with at least [minconf] confirmations.");
// Bitcoin address // Bitcoin address
string strAddress = params[0].get_str(); CBitcoinAddress address = CBitcoinAddress(params[0].get_str());
CScript scriptPubKey; CScript scriptPubKey;
if (!scriptPubKey.SetBitcoinAddress(strAddress)) if (!address.IsValid())
throw JSONRPCError(-5, "Invalid bitcoin address"); throw JSONRPCError(-5, "Invalid bitcoin address");
scriptPubKey.SetBitcoinAddress(address);
if (!IsMine(*pwalletMain,scriptPubKey)) if (!IsMine(*pwalletMain,scriptPubKey))
return (double)0.0; return (double)0.0;
@ -611,22 +605,16 @@ Value getreceivedbyaddress(const Array& params, bool fHelp)
} }
void GetAccountPubKeys(string strAccount, set<CScript>& setPubKey) void GetAccountAddresses(string strAccount, set<CBitcoinAddress>& setAddress)
{ {
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
{ {
const string& strAddress = item.first; const CBitcoinAddress& address = item.first;
const string& strName = item.second; const string& strName = item.second;
if (strName == strAccount) if (strName == strAccount)
{ setAddress.insert(address);
// We're only counting our own valid bitcoin addresses and not ip addresses
CScript scriptPubKey;
if (scriptPubKey.SetBitcoinAddress(strAddress))
if (IsMine(*pwalletMain,scriptPubKey))
setPubKey.insert(scriptPubKey);
}
} }
} }
} }
@ -646,8 +634,8 @@ Value getreceivedbyaccount(const Array& params, bool fHelp)
// Get the set of pub keys that have the label // Get the set of pub keys that have the label
string strAccount = AccountFromValue(params[0]); string strAccount = AccountFromValue(params[0]);
set<CScript> setPubKey; set<CBitcoinAddress> setAddress;
GetAccountPubKeys(strAccount, setPubKey); GetAccountAddresses(strAccount, setAddress);
// Tally // Tally
int64 nAmount = 0; int64 nAmount = 0;
@ -660,9 +648,12 @@ Value getreceivedbyaccount(const Array& params, bool fHelp)
continue; continue;
BOOST_FOREACH(const CTxOut& txout, wtx.vout) BOOST_FOREACH(const CTxOut& txout, wtx.vout)
if (setPubKey.count(txout.scriptPubKey)) {
CBitcoinAddress address;
if (ExtractAddress(txout.scriptPubKey, pwalletMain, address) && setAddress.count(address))
if (wtx.GetDepthInMainChain() >= nMinDepth) if (wtx.GetDepthInMainChain() >= nMinDepth)
nAmount += txout.nValue; nAmount += txout.nValue;
}
} }
} }
@ -733,13 +724,13 @@ Value getbalance(const Array& params, bool fHelp)
int64 allGeneratedImmature, allGeneratedMature, allFee; int64 allGeneratedImmature, allGeneratedMature, allFee;
allGeneratedImmature = allGeneratedMature = allFee = 0; allGeneratedImmature = allGeneratedMature = allFee = 0;
string strSentAccount; string strSentAccount;
list<pair<string, int64> > listReceived; list<pair<CBitcoinAddress, int64> > listReceived;
list<pair<string, int64> > listSent; list<pair<CBitcoinAddress, int64> > listSent;
wtx.GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount); wtx.GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount);
if (wtx.GetDepthInMainChain() >= nMinDepth) if (wtx.GetDepthInMainChain() >= nMinDepth)
BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listReceived) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listReceived)
nBalance += r.second; nBalance += r.second;
BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listSent) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listSent)
nBalance -= r.second; nBalance -= r.second;
nBalance -= allFee; nBalance -= allFee;
nBalance += allGeneratedMature; nBalance += allGeneratedMature;
@ -874,22 +865,22 @@ Value sendmany(const Array& params, bool fHelp)
if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty()) if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty())
wtx.mapValue["comment"] = params[3].get_str(); wtx.mapValue["comment"] = params[3].get_str();
set<string> setAddress; set<CBitcoinAddress> setAddress;
vector<pair<CScript, int64> > vecSend; vector<pair<CScript, int64> > vecSend;
int64 totalAmount = 0; int64 totalAmount = 0;
BOOST_FOREACH(const Pair& s, sendTo) BOOST_FOREACH(const Pair& s, sendTo)
{ {
uint160 hash160; CBitcoinAddress address(s.name_);
string strAddress = s.name_; if (!address.IsValid())
throw JSONRPCError(-5, string("Invalid bitcoin address:")+s.name_);
if (setAddress.count(strAddress)) if (setAddress.count(address))
throw JSONRPCError(-8, string("Invalid parameter, duplicated address: ")+strAddress); throw JSONRPCError(-8, string("Invalid parameter, duplicated address: ")+s.name_);
setAddress.insert(strAddress); setAddress.insert(address);
CScript scriptPubKey; CScript scriptPubKey;
if (!scriptPubKey.SetBitcoinAddress(strAddress)) scriptPubKey.SetBitcoinAddress(address);
throw JSONRPCError(-5, string("Invalid bitcoin address:")+strAddress);
int64 nAmount = AmountFromValue(s.value_); int64 nAmount = AmountFromValue(s.value_);
totalAmount += nAmount; totalAmount += nAmount;
@ -950,7 +941,7 @@ Value ListReceived(const Array& params, bool fByAccounts)
fIncludeEmpty = params[1].get_bool(); fIncludeEmpty = params[1].get_bool();
// Tally // Tally
map<uint160, tallyitem> mapTally; map<CBitcoinAddress, tallyitem> mapTally;
CRITICAL_BLOCK(pwalletMain->cs_mapWallet) CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
{ {
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it) for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
@ -965,12 +956,11 @@ Value ListReceived(const Array& params, bool fByAccounts)
BOOST_FOREACH(const CTxOut& txout, wtx.vout) BOOST_FOREACH(const CTxOut& txout, wtx.vout)
{ {
// Only counting our own bitcoin addresses and not ip addresses CBitcoinAddress address;
uint160 hash160 = txout.scriptPubKey.GetBitcoinAddressHash160(); if (!ExtractAddress(txout.scriptPubKey, pwalletMain, address) || !address.IsValid())
if (hash160 == 0 || !mapPubKeys.count(hash160)) // IsMine
continue; continue;
tallyitem& item = mapTally[hash160]; tallyitem& item = mapTally[address];
item.nAmount += txout.nValue; item.nAmount += txout.nValue;
item.nConf = min(item.nConf, nDepth); item.nConf = min(item.nConf, nDepth);
} }
@ -982,14 +972,11 @@ Value ListReceived(const Array& params, bool fByAccounts)
map<string, tallyitem> mapAccountTally; map<string, tallyitem> mapAccountTally;
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
{ {
const string& strAddress = item.first; const CBitcoinAddress& address = item.first;
const string& strAccount = item.second; const string& strAccount = item.second;
uint160 hash160; map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
if (!AddressToHash160(strAddress, hash160))
continue;
map<uint160, tallyitem>::iterator it = mapTally.find(hash160);
if (it == mapTally.end() && !fIncludeEmpty) if (it == mapTally.end() && !fIncludeEmpty)
continue; continue;
@ -1010,7 +997,7 @@ Value ListReceived(const Array& params, bool fByAccounts)
else else
{ {
Object obj; Object obj;
obj.push_back(Pair("address", strAddress)); obj.push_back(Pair("address", address.ToString()));
obj.push_back(Pair("account", strAccount)); obj.push_back(Pair("account", strAccount));
obj.push_back(Pair("label", strAccount)); // deprecated obj.push_back(Pair("label", strAccount)); // deprecated
obj.push_back(Pair("amount", ValueFromAmount(nAmount))); obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
@ -1073,8 +1060,8 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
{ {
int64 nGeneratedImmature, nGeneratedMature, nFee; int64 nGeneratedImmature, nGeneratedMature, nFee;
string strSentAccount; string strSentAccount;
list<pair<string, int64> > listReceived; list<pair<CBitcoinAddress, int64> > listReceived;
list<pair<string, int64> > listSent; list<pair<CBitcoinAddress, int64> > listSent;
wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount); wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount);
bool fAllAccounts = (strAccount == string("*")); bool fAllAccounts = (strAccount == string("*"));
@ -1102,11 +1089,11 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
// Sent // Sent
if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount)) if ((!listSent.empty() || nFee != 0) && (fAllAccounts || strAccount == strSentAccount))
{ {
BOOST_FOREACH(const PAIRTYPE(string, int64)& s, listSent) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& s, listSent)
{ {
Object entry; Object entry;
entry.push_back(Pair("account", strSentAccount)); entry.push_back(Pair("account", strSentAccount));
entry.push_back(Pair("address", s.first)); entry.push_back(Pair("address", s.first.ToString()));
entry.push_back(Pair("category", "send")); entry.push_back(Pair("category", "send"));
entry.push_back(Pair("amount", ValueFromAmount(-s.second))); entry.push_back(Pair("amount", ValueFromAmount(-s.second)));
entry.push_back(Pair("fee", ValueFromAmount(-nFee))); entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
@ -1120,7 +1107,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth) if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
BOOST_FOREACH(const PAIRTYPE(string, int64)& r, listReceived) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& r, listReceived)
{ {
string account; string account;
if (pwalletMain->mapAddressBook.count(r.first)) if (pwalletMain->mapAddressBook.count(r.first))
@ -1129,7 +1116,7 @@ void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDe
{ {
Object entry; Object entry;
entry.push_back(Pair("account", account)); entry.push_back(Pair("account", account));
entry.push_back(Pair("address", r.first)); entry.push_back(Pair("address", r.first.ToString()));
entry.push_back(Pair("category", "receive")); entry.push_back(Pair("category", "receive"));
entry.push_back(Pair("amount", ValueFromAmount(r.second))); entry.push_back(Pair("amount", ValueFromAmount(r.second)));
if (fLong) if (fLong)
@ -1240,9 +1227,8 @@ Value listaccounts(const Array& params, bool fHelp)
CRITICAL_BLOCK(pwalletMain->cs_mapWallet) CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
BOOST_FOREACH(const PAIRTYPE(string, string)& entry, pwalletMain->mapAddressBook) { BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& entry, pwalletMain->mapAddressBook) {
uint160 hash160; if (pwalletMain->HaveKey(entry.first)) // This address belongs to me
if(AddressToHash160(entry.first, hash160) && mapPubKeys.count(hash160)) // This address belongs to me
mapAccountBalances[entry.second] = 0; mapAccountBalances[entry.second] = 0;
} }
@ -1251,16 +1237,16 @@ Value listaccounts(const Array& params, bool fHelp)
const CWalletTx& wtx = (*it).second; const CWalletTx& wtx = (*it).second;
int64 nGeneratedImmature, nGeneratedMature, nFee; int64 nGeneratedImmature, nGeneratedMature, nFee;
string strSentAccount; string strSentAccount;
list<pair<string, int64> > listReceived; list<pair<CBitcoinAddress, int64> > listReceived;
list<pair<string, int64> > listSent; list<pair<CBitcoinAddress, int64> > listSent;
wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount); wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount);
mapAccountBalances[strSentAccount] -= nFee; mapAccountBalances[strSentAccount] -= nFee;
BOOST_FOREACH(const PAIRTYPE(string, int64)& s, listSent) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& s, listSent)
mapAccountBalances[strSentAccount] -= s.second; mapAccountBalances[strSentAccount] -= s.second;
if (wtx.GetDepthInMainChain() >= nMinDepth) if (wtx.GetDepthInMainChain() >= nMinDepth)
{ {
mapAccountBalances[""] += nGeneratedMature; mapAccountBalances[""] += nGeneratedMature;
BOOST_FOREACH(const PAIRTYPE(string, int64)& r, listReceived) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& r, listReceived)
if (pwalletMain->mapAddressBook.count(r.first)) if (pwalletMain->mapAddressBook.count(r.first))
mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second; mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second;
else else
@ -1553,8 +1539,8 @@ Value validateaddress(const Array& params, bool fHelp)
"Return information about <bitcoinaddress>."); "Return information about <bitcoinaddress>.");
string strAddress = params[0].get_str(); string strAddress = params[0].get_str();
uint160 hash160; CBitcoinAddress address(strAddress);
bool isValid = AddressToHash160(strAddress, hash160); bool isValid = address.IsValid();
Object ret; Object ret;
ret.push_back(Pair("isvalid", isValid)); ret.push_back(Pair("isvalid", isValid));
@ -1562,13 +1548,13 @@ Value validateaddress(const Array& params, bool fHelp)
{ {
// Call Hash160ToAddress() so we always return current ADDRESSVERSION // Call Hash160ToAddress() so we always return current ADDRESSVERSION
// version of the address: // version of the address:
string currentAddress = Hash160ToAddress(hash160); string currentAddress = address.ToString();
ret.push_back(Pair("address", currentAddress)); ret.push_back(Pair("address", currentAddress));
ret.push_back(Pair("ismine", (mapPubKeys.count(hash160) > 0))); ret.push_back(Pair("ismine", (pwalletMain->HaveKey(address) > 0)));
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
if (pwalletMain->mapAddressBook.count(currentAddress)) if (pwalletMain->mapAddressBook.count(address))
ret.push_back(Pair("account", pwalletMain->mapAddressBook[currentAddress])); ret.push_back(Pair("account", pwalletMain->mapAddressBook[address]));
} }
} }
return ret; return ret;

View File

@ -424,7 +424,6 @@ bool AppInit2(int argc, char* argv[])
printf("mapBlockIndex.size() = %d\n", mapBlockIndex.size()); printf("mapBlockIndex.size() = %d\n", mapBlockIndex.size());
printf("nBestHeight = %d\n", nBestHeight); printf("nBestHeight = %d\n", nBestHeight);
printf("setKeyPool.size() = %d\n", pwalletMain->setKeyPool.size()); printf("setKeyPool.size() = %d\n", pwalletMain->setKeyPool.size());
printf("mapPubKeys.size() = %d\n", mapPubKeys.size());
printf("mapWallet.size() = %d\n", pwalletMain->mapWallet.size()); printf("mapWallet.size() = %d\n", pwalletMain->mapWallet.size());
printf("mapAddressBook.size() = %d\n", pwalletMain->mapAddressBook.size()); printf("mapAddressBook.size() = %d\n", pwalletMain->mapAddressBook.size());

View File

@ -220,6 +220,11 @@ public:
return false; return false;
return true; return true;
} }
CBitcoinAddress GetAddress() const
{
return CBitcoinAddress(GetPubKey());
}
}; };
#endif #endif

View File

@ -16,14 +16,19 @@ std::vector<unsigned char> CKeyStore::GenerateNewKey()
return key.GetPubKey(); return key.GetPubKey();
} }
bool CKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char> &vchPubKeyOut) const
{
CKey key;
if (!GetKey(address, key))
return false;
vchPubKeyOut = key.GetPubKey();
return true;
}
bool CBasicKeyStore::AddKey(const CKey& key) bool CBasicKeyStore::AddKey(const CKey& key)
{ {
CRITICAL_BLOCK(cs_mapPubKeys)
CRITICAL_BLOCK(cs_KeyStore) CRITICAL_BLOCK(cs_KeyStore)
{ mapKeys[key.GetAddress()] = key.GetSecret();
mapKeys[key.GetPubKey()] = key.GetPrivKey();
mapPubKeys[Hash160(key.GetPubKey())] = key.GetPubKey();
}
return true; return true;
} }
@ -44,11 +49,11 @@ bool CCryptoKeyStore::Unlock(const CKeyingMaterial& vMasterKeyIn)
if (!SetCrypted()) if (!SetCrypted())
return false; return false;
std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.begin(); CryptedKeyMap::const_iterator mi = mapCryptedKeys.begin();
for (; mi != mapCryptedKeys.end(); ++mi) for (; mi != mapCryptedKeys.end(); ++mi)
{ {
const std::vector<unsigned char> &vchPubKey = (*mi).first; const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second; const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
CSecret vchSecret; CSecret vchSecret;
if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret)) if(!DecryptSecret(vMasterKeyIn, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
return false; return false;
@ -88,31 +93,30 @@ bool CCryptoKeyStore::AddKey(const CKey& key)
bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret) bool CCryptoKeyStore::AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret)
{ {
CRITICAL_BLOCK(cs_mapPubKeys)
CRITICAL_BLOCK(cs_KeyStore) CRITICAL_BLOCK(cs_KeyStore)
{ {
if (!SetCrypted()) if (!SetCrypted())
return false; return false;
mapCryptedKeys[vchPubKey] = vchCryptedSecret; mapCryptedKeys[CBitcoinAddress(vchPubKey)] = make_pair(vchPubKey, vchCryptedSecret);
mapPubKeys[Hash160(vchPubKey)] = vchPubKey;
} }
return true; return true;
} }
bool CCryptoKeyStore::GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const bool CCryptoKeyStore::GetKey(const CBitcoinAddress &address, CKey& keyOut) const
{ {
CRITICAL_BLOCK(cs_vMasterKey) CRITICAL_BLOCK(cs_vMasterKey)
{ {
if (!IsCrypted()) if (!IsCrypted())
return CBasicKeyStore::GetPrivKey(vchPubKey, keyOut); return CBasicKeyStore::GetKey(address, keyOut);
std::map<std::vector<unsigned char>, std::vector<unsigned char> >::const_iterator mi = mapCryptedKeys.find(vchPubKey); CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
if (mi != mapCryptedKeys.end()) if (mi != mapCryptedKeys.end())
{ {
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second; const std::vector<unsigned char> &vchPubKey = (*mi).second.first;
const std::vector<unsigned char> &vchCryptedSecret = (*mi).second.second;
CSecret vchSecret; CSecret vchSecret;
if (!DecryptSecret(vMasterKey, (*mi).second, Hash((*mi).first.begin(), (*mi).first.end()), vchSecret)) if (!DecryptSecret(vMasterKey, vchCryptedSecret, Hash(vchPubKey.begin(), vchPubKey.end()), vchSecret))
return false; return false;
keyOut.SetSecret(vchSecret); keyOut.SetSecret(vchSecret);
return true; return true;
@ -121,6 +125,23 @@ bool CCryptoKeyStore::GetPrivKey(const std::vector<unsigned char> &vchPubKey, CK
return false; return false;
} }
bool CCryptoKeyStore::GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const
{
CRITICAL_BLOCK(cs_vMasterKey)
{
if (!IsCrypted())
return CKeyStore::GetPubKey(address, vchPubKeyOut);
CryptedKeyMap::const_iterator mi = mapCryptedKeys.find(address);
if (mi != mapCryptedKeys.end())
{
vchPubKeyOut = (*mi).second.first;
return true;
}
}
return false;
}
bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn) bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
{ {
CRITICAL_BLOCK(cs_KeyStore) CRITICAL_BLOCK(cs_KeyStore)
@ -135,10 +156,11 @@ bool CCryptoKeyStore::EncryptKeys(CKeyingMaterial& vMasterKeyIn)
{ {
if (!key.SetPrivKey(mKey.second)) if (!key.SetPrivKey(mKey.second))
return false; return false;
const std::vector<unsigned char> vchPubKey = key.GetPubKey();
std::vector<unsigned char> vchCryptedSecret; std::vector<unsigned char> vchCryptedSecret;
if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(mKey.first.begin(), mKey.first.end()), vchCryptedSecret)) if (!EncryptSecret(vMasterKeyIn, key.GetSecret(), Hash(vchPubKey.begin(), vchPubKey.end()), vchCryptedSecret))
return false; return false;
if (!AddCryptedKey(mKey.first, vchCryptedSecret)) if (!AddCryptedKey(vchPubKey, vchCryptedSecret))
return false; return false;
} }
mapKeys.clear(); mapKeys.clear();

View File

@ -12,12 +12,13 @@ public:
mutable CCriticalSection cs_KeyStore; mutable CCriticalSection cs_KeyStore;
virtual bool AddKey(const CKey& key) =0; virtual bool AddKey(const CKey& key) =0;
virtual bool HaveKey(const std::vector<unsigned char> &vchPubKey) const =0; virtual bool HaveKey(const CBitcoinAddress &address) const =0;
virtual bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const =0; virtual bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const =0;
virtual bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
virtual std::vector<unsigned char> GenerateNewKey(); virtual std::vector<unsigned char> GenerateNewKey();
}; };
typedef std::map<std::vector<unsigned char>, CPrivKey> KeyMap; typedef std::map<CBitcoinAddress, CSecret> KeyMap;
class CBasicKeyStore : public CKeyStore class CBasicKeyStore : public CKeyStore
{ {
@ -26,26 +27,28 @@ protected:
public: public:
bool AddKey(const CKey& key); bool AddKey(const CKey& key);
bool HaveKey(const std::vector<unsigned char> &vchPubKey) const bool HaveKey(const CBitcoinAddress &address) const
{ {
return (mapKeys.count(vchPubKey) > 0); return (mapKeys.count(address) > 0);
} }
bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const
{ {
std::map<std::vector<unsigned char>, CPrivKey>::const_iterator mi = mapKeys.find(vchPubKey); KeyMap::const_iterator mi = mapKeys.find(address);
if (mi != mapKeys.end()) if (mi != mapKeys.end())
{ {
keyOut.SetPrivKey((*mi).second); keyOut.SetSecret((*mi).second);
return true; return true;
} }
return false; return false;
} }
}; };
typedef std::map<CBitcoinAddress, std::pair<std::vector<unsigned char>, std::vector<unsigned char> > > CryptedKeyMap;
class CCryptoKeyStore : public CBasicKeyStore class CCryptoKeyStore : public CBasicKeyStore
{ {
private: private:
std::map<std::vector<unsigned char>, std::vector<unsigned char> > mapCryptedKeys; CryptedKeyMap mapCryptedKeys;
CKeyingMaterial vMasterKey; CKeyingMaterial vMasterKey;
@ -103,13 +106,14 @@ public:
virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret); virtual bool AddCryptedKey(const std::vector<unsigned char> &vchPubKey, const std::vector<unsigned char> &vchCryptedSecret);
std::vector<unsigned char> GenerateNewKey(); std::vector<unsigned char> GenerateNewKey();
bool AddKey(const CKey& key); bool AddKey(const CKey& key);
bool HaveKey(const std::vector<unsigned char> &vchPubKey) const bool HaveKey(const CBitcoinAddress &address) const
{ {
if (!IsCrypted()) if (!IsCrypted())
return CBasicKeyStore::HaveKey(vchPubKey); return CBasicKeyStore::HaveKey(address);
return mapCryptedKeys.count(vchPubKey) > 0; return mapCryptedKeys.count(address) > 0;
} }
bool GetPrivKey(const std::vector<unsigned char> &vchPubKey, CKey& keyOut) const; bool GetKey(const CBitcoinAddress &address, CKey& keyOut) const;
bool GetPubKey(const CBitcoinAddress &address, std::vector<unsigned char>& vchPubKeyOut) const;
}; };
#endif #endif

View File

@ -21,9 +21,6 @@ set<CWallet*> setpwalletRegistered;
CCriticalSection cs_main; CCriticalSection cs_main;
CCriticalSection cs_mapPubKeys;
map<uint160, vector<unsigned char> > mapPubKeys;
map<uint256, CTransaction> mapTransactions; map<uint256, CTransaction> mapTransactions;
CCriticalSection cs_mapTransactions; CCriticalSection cs_mapTransactions;
unsigned int nTransactionsUpdated = 0; unsigned int nTransactionsUpdated = 0;

View File

@ -1568,7 +1568,5 @@ public:
extern std::map<uint256, CTransaction> mapTransactions; extern std::map<uint256, CTransaction> mapTransactions;
extern std::map<uint160, std::vector<unsigned char> > mapPubKeys;
extern CCriticalSection cs_mapPubKeys;
#endif #endif

View File

@ -39,18 +39,18 @@ struct AddressTablePriv
{ {
cachedAddressTable.clear(); cachedAddressTable.clear();
CRITICAL_BLOCK(cs_mapPubKeys) CRITICAL_BLOCK(wallet->cs_KeyStore)
CRITICAL_BLOCK(wallet->cs_mapAddressBook) CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{ {
BOOST_FOREACH(const PAIRTYPE(std::string, std::string)& item, wallet->mapAddressBook) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, std::string)& item, wallet->mapAddressBook)
{ {
std::string strAddress = item.first; const CBitcoinAddress& address = item.first;
std::string strName = item.second; const std::string& strName = item.second;
uint160 hash160; uint160 hash160;
bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160)); bool fMine = wallet->HaveKey(address);
cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending, cachedAddressTable.append(AddressTableEntry(fMine ? AddressTableEntry::Receiving : AddressTableEntry::Sending,
QString::fromStdString(strName), QString::fromStdString(strName),
QString::fromStdString(strAddress))); QString::fromStdString(address.ToString())));
} }
} }
} }
@ -268,9 +268,8 @@ QString AddressTableModel::addRow(const QString &type, const QString &label, con
} }
else if(type == Receive) else if(type == Receive)
{ {
// Generate a new address to associate with given label, optionally // Generate a new address to associate with given label
// set as default receiving address. strAddress = CBitcoinAddress(wallet->GetOrReuseKeyFromPool()).ToString();
strAddress = PubKeyToAddress(wallet->GetOrReuseKeyFromPool());
} }
else else
{ {
@ -312,7 +311,8 @@ QString AddressTableModel::labelForAddress(const QString &address) const
{ {
CRITICAL_BLOCK(wallet->cs_mapAddressBook) CRITICAL_BLOCK(wallet->cs_mapAddressBook)
{ {
std::map<std::string, std::string>::iterator mi = wallet->mapAddressBook.find(address.toStdString()); CBitcoinAddress address_parsed(address.toStdString());
std::map<CBitcoinAddress, std::string>::iterator mi = wallet->mapAddressBook.find(address_parsed);
if (mi != wallet->mapAddressBook.end()) if (mi != wallet->mapAddressBook.end())
{ {
return QString::fromStdString(mi->second); return QString::fromStdString(mi->second);

View File

@ -125,17 +125,16 @@ string TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
{ {
if (wallet->IsMine(txout)) if (wallet->IsMine(txout))
{ {
vector<unsigned char> vchPubKey; CBitcoinAddress address;
if (ExtractPubKey(txout.scriptPubKey, wallet, vchPubKey)) if (ExtractAddress(txout.scriptPubKey, wallet, address))
{ {
string strAddress = PubKeyToAddress(vchPubKey); if (wallet->mapAddressBook.count(address))
if (wallet->mapAddressBook.count(strAddress))
{ {
strHTML += string() + _("<b>From:</b> ") + _("unknown") + "<br>"; strHTML += string() + _("<b>From:</b> ") + _("unknown") + "<br>";
strHTML += _("<b>To:</b> "); strHTML += _("<b>To:</b> ");
strHTML += HtmlEscape(strAddress); strHTML += HtmlEscape(address.ToString());
if (!wallet->mapAddressBook[strAddress].empty()) if (!wallet->mapAddressBook[address].empty())
strHTML += _(" (yours, label: ") + wallet->mapAddressBook[strAddress] + ")"; strHTML += _(" (yours, label: ") + wallet->mapAddressBook[address] + ")";
else else
strHTML += _(" (yours)"); strHTML += _(" (yours)");
strHTML += "<br>"; strHTML += "<br>";
@ -211,14 +210,13 @@ string TransactionDesc::toHTML(CWallet *wallet, CWalletTx &wtx)
if (wtx.mapValue["to"].empty()) if (wtx.mapValue["to"].empty())
{ {
// Offline transaction // Offline transaction
uint160 hash160; CBitcoinAddress address;
if (ExtractHash160(txout.scriptPubKey, hash160)) if (ExtractAddress(txout.scriptPubKey, wallet, address))
{ {
string strAddress = Hash160ToAddress(hash160);
strHTML += _("<b>To:</b> "); strHTML += _("<b>To:</b> ");
if (wallet->mapAddressBook.count(strAddress) && !wallet->mapAddressBook[strAddress].empty()) if (wallet->mapAddressBook.count(address) && !wallet->mapAddressBook[address].empty())
strHTML += wallet->mapAddressBook[strAddress] + " "; strHTML += wallet->mapAddressBook[address] + " ";
strHTML += strAddress; strHTML += address.ToString();
strHTML += "<br>"; strHTML += "<br>";
} }
} }

View File

@ -79,10 +79,10 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *
{ {
if(wallet->IsMine(txout)) if(wallet->IsMine(txout))
{ {
std::vector<unsigned char> vchPubKey; CBitcoinAddress address;
if (ExtractPubKey(txout.scriptPubKey, wallet, vchPubKey)) if (ExtractAddress(txout.scriptPubKey, wallet, address))
{ {
sub.address = PubKeyToAddress(vchPubKey); sub.address = address.ToString();
} }
break; break;
} }
@ -137,9 +137,11 @@ QList<TransactionRecord> TransactionRecord::decomposeTransaction(const CWallet *
{ {
// Sent to Bitcoin Address // Sent to Bitcoin Address
sub.type = TransactionRecord::SendToAddress; sub.type = TransactionRecord::SendToAddress;
uint160 hash160; CBitcoinAddress address;
if (ExtractHash160(txout.scriptPubKey, hash160)) if (ExtractAddress(txout.scriptPubKey, wallet, address))
sub.address = Hash160ToAddress(hash160); {
sub.address = address.ToString();
}
} }
int64 nValue = txout.nValue; int64 nValue = txout.nValue;

View File

@ -66,9 +66,8 @@ void WalletModel::update()
bool WalletModel::validateAddress(const QString &address) bool WalletModel::validateAddress(const QString &address)
{ {
uint160 hash160 = 0; CBitcoinAddress addressParsed(address.toStdString());
return addressParsed.IsValid();
return AddressToHash160(address.toStdString(), hash160);
} }
WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients) WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipient> &recipients)
@ -87,7 +86,7 @@ WalletModel::SendCoinsReturn WalletModel::sendCoins(const QList<SendCoinsRecipie
{ {
uint160 hash160 = 0; uint160 hash160 = 0;
if(!AddressToHash160(rcp.address.toUtf8().constData(), hash160)) if(!validateAddress(rcp.address))
{ {
return InvalidAddress; return InvalidAddress;
} }

View File

@ -1041,7 +1041,9 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash
// Sign // Sign
const valtype& vchPubKey = item.second; const valtype& vchPubKey = item.second;
CKey key; CKey key;
if (!keystore.GetPrivKey(vchPubKey, key)) if (!keystore.GetKey(Hash160(vchPubKey), key))
return false;
if (key.GetPubKey() != vchPubKey)
return false; return false;
if (hash != 0) if (hash != 0)
{ {
@ -1055,12 +1057,8 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash
else if (item.first == OP_PUBKEYHASH) else if (item.first == OP_PUBKEYHASH)
{ {
// Sign and give pubkey // Sign and give pubkey
map<uint160, valtype>::iterator mi = mapPubKeys.find(uint160(item.second));
if (mi == mapPubKeys.end())
return false;
const vector<unsigned char>& vchPubKey = (*mi).second;
CKey key; CKey key;
if (!keystore.GetPrivKey(vchPubKey, key)) if (!keystore.GetKey(uint160(item.second), key))
return false; return false;
if (hash != 0) if (hash != 0)
{ {
@ -1068,7 +1066,7 @@ bool Solver(const CKeyStore& keystore, const CScript& scriptPubKey, uint256 hash
if (!key.Sign(hash, vchSig)) if (!key.Sign(hash, vchSig))
return false; return false;
vchSig.push_back((unsigned char)nHashType); vchSig.push_back((unsigned char)nHashType);
scriptSigRet << vchSig << vchPubKey; scriptSigRet << vchSig << key.GetPubKey();
} }
} }
else else
@ -1102,19 +1100,16 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
{ {
if (item.first == OP_PUBKEY) if (item.first == OP_PUBKEY)
{ {
// Sign
const valtype& vchPubKey = item.second; const valtype& vchPubKey = item.second;
if (!keystore.HaveKey(vchPubKey)) vector<unsigned char> vchPubKeyFound;
if (!keystore.GetPubKey(Hash160(vchPubKey), vchPubKeyFound))
return false;
if (vchPubKeyFound != vchPubKey)
return false; return false;
} }
else if (item.first == OP_PUBKEYHASH) else if (item.first == OP_PUBKEYHASH)
{ {
// Sign and give pubkey if (!keystore.HaveKey(uint160(item.second)))
map<uint160, valtype>::iterator mi = mapPubKeys.find(uint160(item.second));
if (mi == mapPubKeys.end())
return false;
const vector<unsigned char>& vchPubKey = (*mi).second;
if (!keystore.HaveKey(vchPubKey))
return false; return false;
} }
else else
@ -1128,57 +1123,26 @@ bool IsMine(const CKeyStore &keystore, const CScript& scriptPubKey)
} }
bool ExtractPubKey(const CScript& scriptPubKey, const CKeyStore* keystore, vector<unsigned char>& vchPubKeyRet) bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* keystore, CBitcoinAddress& addressRet)
{ {
vchPubKeyRet.clear();
vector<pair<opcodetype, valtype> > vSolution; vector<pair<opcodetype, valtype> > vSolution;
if (!Solver(scriptPubKey, vSolution)) if (!Solver(scriptPubKey, vSolution))
return false; return false;
CRITICAL_BLOCK(cs_mapPubKeys) CRITICAL_BLOCK(keystore->cs_KeyStore)
{ {
BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution) BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
{ {
valtype vchPubKey; uint160 hash160;
if (item.first == OP_PUBKEY) if (item.first == OP_PUBKEY)
{ addressRet.SetPubKey(item.second);
vchPubKey = item.second;
}
else if (item.first == OP_PUBKEYHASH) else if (item.first == OP_PUBKEYHASH)
{ addressRet.SetHash160((uint160)item.second);
map<uint160, valtype>::iterator mi = mapPubKeys.find(uint160(item.second)); //if (keystore == NULL || keystore->HaveKey(addressRet))
if (mi == mapPubKeys.end())
continue;
vchPubKey = (*mi).second;
}
if (keystore == NULL || keystore->HaveKey(vchPubKey))
{
vchPubKeyRet = vchPubKey;
return true; return true;
}
} }
} }
return false;
}
bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret)
{
hash160Ret = 0;
vector<pair<opcodetype, valtype> > vSolution;
if (!Solver(scriptPubKey, vSolution))
return false;
BOOST_FOREACH(PAIRTYPE(opcodetype, valtype)& item, vSolution)
{
if (item.first == OP_PUBKEYHASH)
{
hash160Ret = uint160(item.second);
return true;
}
}
return false; return false;
} }

View File

@ -622,7 +622,7 @@ public:
} }
uint160 GetBitcoinAddressHash160() const CBitcoinAddress GetBitcoinAddress() const
{ {
opcodetype opcode; opcodetype opcode;
std::vector<unsigned char> vch; std::vector<unsigned char> vch;
@ -634,36 +634,18 @@ public:
if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0; if (!GetOp(pc, opcode, vch) || opcode != OP_EQUALVERIFY) return 0;
if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0; if (!GetOp(pc, opcode, vch) || opcode != OP_CHECKSIG) return 0;
if (pc != end()) return 0; if (pc != end()) return 0;
return hash160; return CBitcoinAddress(hash160);
} }
std::string GetBitcoinAddress() const void SetBitcoinAddress(const CBitcoinAddress& address)
{
uint160 hash160 = GetBitcoinAddressHash160();
if (hash160 == 0)
return "";
return Hash160ToAddress(hash160);
}
void SetBitcoinAddress(const uint160& hash160)
{ {
this->clear(); this->clear();
*this << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG; *this << OP_DUP << OP_HASH160 << address.GetHash160() << OP_EQUALVERIFY << OP_CHECKSIG;
} }
void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey) void SetBitcoinAddress(const std::vector<unsigned char>& vchPubKey)
{ {
SetBitcoinAddress(Hash160(vchPubKey)); SetBitcoinAddress(CBitcoinAddress(vchPubKey));
}
bool SetBitcoinAddress(const std::string& strAddress)
{
this->clear();
uint160 hash160;
if (!AddressToHash160(strAddress, hash160))
return false;
SetBitcoinAddress(hash160);
return true;
} }
@ -710,8 +692,7 @@ public:
bool IsStandard(const CScript& scriptPubKey); bool IsStandard(const CScript& scriptPubKey);
bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey); bool IsMine(const CKeyStore& keystore, const CScript& scriptPubKey);
bool ExtractPubKey(const CScript& scriptPubKey, const CKeyStore* pkeystore, std::vector<unsigned char>& vchPubKeyRet); bool ExtractAddress(const CScript& scriptPubKey, const CKeyStore* pkeystore, CBitcoinAddress& addressRet);
bool ExtractHash160(const CScript& scriptPubKey, uint160& hash160Ret);
bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript()); bool SignSignature(const CKeyStore& keystore, const CTransaction& txFrom, CTransaction& txTo, unsigned int nIn, int nHashType=SIGHASH_ALL, CScript scriptPrereq=CScript());
bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0); bool VerifySignature(const CTransaction& txFrom, const CTransaction& txTo, unsigned int nIn, int nHashType=0);

View File

@ -235,12 +235,13 @@ void SetDefaultReceivingAddress(const string& strAddress)
return; return;
if (strAddress != pframeMain->m_textCtrlAddress->GetValue()) if (strAddress != pframeMain->m_textCtrlAddress->GetValue())
{ {
uint160 hash160; CBitcoinAddress address(strAddress);
if (!AddressToHash160(strAddress, hash160)) if (!address.IsValid())
return; return;
if (!mapPubKeys.count(hash160)) vector<unsigned char> vchPubKey;
if (!pwalletMain->GetPubKey(address, vchPubKey))
return; return;
pwalletMain->SetDefaultKey(mapPubKeys[hash160]); pwalletMain->SetDefaultKey(vchPubKey);
pframeMain->m_textCtrlAddress->SetValue(strAddress); pframeMain->m_textCtrlAddress->SetValue(strAddress);
} }
} }
@ -366,7 +367,7 @@ CMainFrame::CMainFrame(wxWindow* parent) : CMainFrameBase(parent)
// Fill your address text box // Fill your address text box
vector<unsigned char> vchPubKey; vector<unsigned char> vchPubKey;
if (CWalletDB(pwalletMain->strWalletFile,"r").ReadDefaultKey(vchPubKey)) if (CWalletDB(pwalletMain->strWalletFile,"r").ReadDefaultKey(vchPubKey))
m_textCtrlAddress->SetValue(PubKeyToAddress(vchPubKey)); m_textCtrlAddress->SetValue(CBitcoinAddress(vchPubKey).ToString());
if (pwalletMain->IsCrypted()) if (pwalletMain->IsCrypted())
m_menuOptions->Remove(m_menuOptionsEncryptWallet); m_menuOptions->Remove(m_menuOptionsEncryptWallet);
@ -703,24 +704,23 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex)
{ {
if (pwalletMain->IsMine(txout)) if (pwalletMain->IsMine(txout))
{ {
vector<unsigned char> vchPubKey; CBitcoinAddress address;
if (ExtractPubKey(txout.scriptPubKey, pwalletMain, vchPubKey)) if (ExtractAddress(txout.scriptPubKey, pwalletMain, address))
{ {
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
//strDescription += _("Received payment to "); //strDescription += _("Received payment to ");
//strDescription += _("Received with address "); //strDescription += _("Received with address ");
strDescription += _("Received with: "); strDescription += _("Received with: ");
string strAddress = PubKeyToAddress(vchPubKey); map<CBitcoinAddress, string>::iterator mi = pwalletMain->mapAddressBook.find(address);
map<string, string>::iterator mi = pwalletMain->mapAddressBook.find(strAddress);
if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty()) if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.empty())
{ {
string strLabel = (*mi).second; string strLabel = (*mi).second;
strDescription += strAddress.substr(0,12) + "... "; strDescription += address.ToString().substr(0,12) + "... ";
strDescription += "(" + strLabel + ")"; strDescription += "(" + strLabel + ")";
} }
else else
strDescription += strAddress; strDescription += address.ToString();
} }
} }
break; break;
@ -785,9 +785,9 @@ bool CMainFrame::InsertTransaction(const CWalletTx& wtx, bool fNew, int nIndex)
else else
{ {
// Sent to Bitcoin Address // Sent to Bitcoin Address
uint160 hash160; CBitcoinAddress address;
if (ExtractHash160(txout.scriptPubKey, hash160)) if (ExtractAddress(txout.scriptPubKey, pwalletMain, address))
strAddress = Hash160ToAddress(hash160); strAddress = address.ToString();
} }
string strDescription = _("To: "); string strDescription = _("To: ");
@ -1114,7 +1114,7 @@ void CMainFrame::OnPaintListCtrl(wxPaintEvent& event)
m_statusBar->SetStatusText(strStatus, 2); m_statusBar->SetStatusText(strStatus, 2);
// Update receiving address // Update receiving address
string strDefaultAddress = PubKeyToAddress(pwalletMain->vchDefaultKey); string strDefaultAddress = CBitcoinAddress(pwalletMain->vchDefaultKey).ToString();
if (m_textCtrlAddress->GetValue() != strDefaultAddress) if (m_textCtrlAddress->GetValue() != strDefaultAddress)
m_textCtrlAddress->SetValue(strDefaultAddress); m_textCtrlAddress->SetValue(strDefaultAddress);
} }
@ -1393,7 +1393,7 @@ void CMainFrame::OnButtonNew(wxCommandEvent& event)
return; return;
// Generate new key // Generate new key
strAddress = PubKeyToAddress(pwalletMain->GetOrReuseKeyFromPool()); strAddress = CBitcoinAddress(pwalletMain->GetOrReuseKeyFromPool()).ToString();
if (fWasLocked) if (fWasLocked)
pwalletMain->Lock(); pwalletMain->Lock();
@ -1502,17 +1502,16 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails
{ {
if (pwalletMain->IsMine(txout)) if (pwalletMain->IsMine(txout))
{ {
vector<unsigned char> vchPubKey; CBitcoinAddress address;
if (ExtractPubKey(txout.scriptPubKey, pwalletMain, vchPubKey)) if (ExtractAddress(txout.scriptPubKey, pwalletMain, address))
{ {
string strAddress = PubKeyToAddress(vchPubKey); if (pwalletMain->mapAddressBook.count(address))
if (pwalletMain->mapAddressBook.count(strAddress))
{ {
strHTML += string() + _("<b>From:</b> ") + _("unknown") + "<br>"; strHTML += string() + _("<b>From:</b> ") + _("unknown") + "<br>";
strHTML += _("<b>To:</b> "); strHTML += _("<b>To:</b> ");
strHTML += HtmlEscape(strAddress); strHTML += HtmlEscape(address.ToString());
if (!pwalletMain->mapAddressBook[strAddress].empty()) if (!pwalletMain->mapAddressBook[address].empty())
strHTML += _(" (yours, label: ") + pwalletMain->mapAddressBook[strAddress] + ")"; strHTML += _(" (yours, label: ") + pwalletMain->mapAddressBook[address] + ")";
else else
strHTML += _(" (yours)"); strHTML += _(" (yours)");
strHTML += "<br>"; strHTML += "<br>";
@ -1588,13 +1587,13 @@ CTxDetailsDialog::CTxDetailsDialog(wxWindow* parent, CWalletTx wtx) : CTxDetails
if (wtx.mapValue["to"].empty()) if (wtx.mapValue["to"].empty())
{ {
// Offline transaction // Offline transaction
uint160 hash160; CBitcoinAddress address;
if (ExtractHash160(txout.scriptPubKey, hash160)) if (ExtractAddress(txout.scriptPubKey, pwalletMain, address))
{ {
string strAddress = Hash160ToAddress(hash160); string strAddress = address.ToString();
strHTML += _("<b>To:</b> "); strHTML += _("<b>To:</b> ");
if (pwalletMain->mapAddressBook.count(strAddress) && !pwalletMain->mapAddressBook[strAddress].empty()) if (pwalletMain->mapAddressBook.count(address) && !pwalletMain->mapAddressBook[address].empty())
strHTML += pwalletMain->mapAddressBook[strAddress] + " "; strHTML += pwalletMain->mapAddressBook[address] + " ";
strHTML += strAddress; strHTML += strAddress;
strHTML += "<br>"; strHTML += "<br>";
} }
@ -2155,8 +2154,8 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event)
} }
// Parse bitcoin address // Parse bitcoin address
uint160 hash160; CBitcoinAddress address(strAddress);
bool fBitcoinAddress = AddressToHash160(strAddress, hash160); bool fBitcoinAddress = address.IsValid();
if (fBitcoinAddress) if (fBitcoinAddress)
{ {
@ -2169,7 +2168,7 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event)
// Send to bitcoin address // Send to bitcoin address
CScript scriptPubKey; CScript scriptPubKey;
scriptPubKey << OP_DUP << OP_HASH160 << hash160 << OP_EQUALVERIFY << OP_CHECKSIG; scriptPubKey.SetBitcoinAddress(address);
string strError = pwalletMain->SendMoney(scriptPubKey, nValue, wtx, true); string strError = pwalletMain->SendMoney(scriptPubKey, nValue, wtx, true);
if (strError == "") if (strError == "")
@ -2213,7 +2212,7 @@ void CSendDialog::OnButtonSend(wxCommandEvent& event)
} }
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
if (!pwalletMain->mapAddressBook.count(strAddress)) if (!pwalletMain->mapAddressBook.count(address))
pwalletMain->SetAddressBookName(strAddress, ""); pwalletMain->SetAddressBookName(strAddress, "");
EndModal(true); EndModal(true);
@ -2625,15 +2624,14 @@ CAddressBookDialog::CAddressBookDialog(wxWindow* parent, const wxString& strInit
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook) CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{ {
string strDefaultReceiving = (string)pframeMain->m_textCtrlAddress->GetValue(); string strDefaultReceiving = (string)pframeMain->m_textCtrlAddress->GetValue();
BOOST_FOREACH(const PAIRTYPE(string, string)& item, pwalletMain->mapAddressBook) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& item, pwalletMain->mapAddressBook)
{ {
string strAddress = item.first; const CBitcoinAddress& address = item.first;
string strName = item.second; string strName = item.second;
uint160 hash160; bool fMine = pwalletMain->HaveKey(address);
bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160));
wxListCtrl* plistCtrl = fMine ? m_listCtrlReceiving : m_listCtrlSending; wxListCtrl* plistCtrl = fMine ? m_listCtrlReceiving : m_listCtrlSending;
int nIndex = InsertLine(plistCtrl, strName, strAddress); int nIndex = InsertLine(plistCtrl, strName, address.ToString());
if (strAddress == (fMine ? strDefaultReceiving : string(strInitSelected))) if (address.ToString() == (fMine ? strDefaultReceiving : string(strInitSelected)))
plistCtrl->SetItemState(nIndex, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED); plistCtrl->SetItemState(nIndex, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED, wxLIST_STATE_SELECTED|wxLIST_STATE_FOCUSED);
} }
} }
@ -2740,8 +2738,8 @@ void CAddressBookDialog::OnButtonCopy(wxCommandEvent& event)
bool CAddressBookDialog::CheckIfMine(const string& strAddress, const string& strTitle) bool CAddressBookDialog::CheckIfMine(const string& strAddress, const string& strTitle)
{ {
uint160 hash160; CBitcoinAddress address(strAddress);
bool fMine = (AddressToHash160(strAddress, hash160) && mapPubKeys.count(hash160)); bool fMine = address.IsValid() && pwalletMain->HaveKey(address);
if (fMine) if (fMine)
wxMessageBox(_("This is one of your own addresses for receiving payments and cannot be entered in the address book. "), strTitle); wxMessageBox(_("This is one of your own addresses for receiving payments and cannot be entered in the address book. "), strTitle);
return fMine; return fMine;
@ -2827,7 +2825,7 @@ void CAddressBookDialog::OnButtonNew(wxCommandEvent& event)
return; return;
// Generate new key // Generate new key
strAddress = PubKeyToAddress(pwalletMain->GetOrReuseKeyFromPool()); strAddress = CBitcoinAddress(pwalletMain->GetOrReuseKeyFromPool()).ToString();
if (fWasLocked) if (fWasLocked)
pwalletMain->Lock(); pwalletMain->Lock();

View File

@ -124,7 +124,6 @@ public:
bool CWallet::EncryptWallet(const string& strWalletPassphrase) bool CWallet::EncryptWallet(const string& strWalletPassphrase)
{ {
CRITICAL_BLOCK(cs_mapPubKeys)
CRITICAL_BLOCK(cs_KeyStore) CRITICAL_BLOCK(cs_KeyStore)
CRITICAL_BLOCK(cs_vMasterKey) CRITICAL_BLOCK(cs_vMasterKey)
CRITICAL_BLOCK(cs_pwalletdbEncryption) CRITICAL_BLOCK(cs_pwalletdbEncryption)
@ -271,7 +270,7 @@ bool CWallet::AddToWallet(const CWalletTx& wtxIn)
if (txout.scriptPubKey == scriptDefaultKey) if (txout.scriptPubKey == scriptDefaultKey)
{ {
SetDefaultKey(GetOrReuseKeyFromPool()); SetDefaultKey(GetOrReuseKeyFromPool());
SetAddressBookName(PubKeyToAddress(vchDefaultKey), ""); SetAddressBookName(CBitcoinAddress(vchDefaultKey), "");
} }
} }
#endif #endif
@ -407,8 +406,8 @@ int CWalletTx::GetRequestCount() const
return nRequests; return nRequests;
} }
void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list<pair<string, int64> >& listReceived, void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, list<pair<CBitcoinAddress, int64> >& listReceived,
list<pair<string, int64> >& listSent, int64& nFee, string& strSentAccount) const list<pair<CBitcoinAddress, int64> >& listSent, int64& nFee, string& strSentAccount) const
{ {
nGeneratedImmature = nGeneratedMature = nFee = 0; nGeneratedImmature = nGeneratedMature = nFee = 0;
listReceived.clear(); listReceived.clear();
@ -436,14 +435,9 @@ void CWalletTx::GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, l
// but non-standard clients might (so return a list of address/amount pairs) // but non-standard clients might (so return a list of address/amount pairs)
BOOST_FOREACH(const CTxOut& txout, vout) BOOST_FOREACH(const CTxOut& txout, vout)
{ {
string address; CBitcoinAddress address;
uint160 hash160;
vector<unsigned char> vchPubKey; vector<unsigned char> vchPubKey;
if (ExtractHash160(txout.scriptPubKey, hash160)) if (!ExtractAddress(txout.scriptPubKey, pwallet, address))
address = Hash160ToAddress(hash160);
else if (ExtractPubKey(txout.scriptPubKey, NULL, vchPubKey))
address = PubKeyToAddress(vchPubKey);
else
{ {
printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n", printf("CWalletTx::GetAmounts: Unknown transaction type found, txid %s\n",
this->GetHash().ToString().c_str()); this->GetHash().ToString().c_str());
@ -471,25 +465,25 @@ void CWalletTx::GetAccountAmounts(const string& strAccount, int64& nGenerated, i
int64 allGeneratedImmature, allGeneratedMature, allFee; int64 allGeneratedImmature, allGeneratedMature, allFee;
allGeneratedImmature = allGeneratedMature = allFee = 0; allGeneratedImmature = allGeneratedMature = allFee = 0;
string strSentAccount; string strSentAccount;
list<pair<string, int64> > listReceived; list<pair<CBitcoinAddress, int64> > listReceived;
list<pair<string, int64> > listSent; list<pair<CBitcoinAddress, int64> > listSent;
GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount); GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount);
if (strAccount == "") if (strAccount == "")
nGenerated = allGeneratedMature; nGenerated = allGeneratedMature;
if (strAccount == strSentAccount) if (strAccount == strSentAccount)
{ {
BOOST_FOREACH(const PAIRTYPE(string,int64)& s, listSent) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& s, listSent)
nSent += s.second; nSent += s.second;
nFee = allFee; nFee = allFee;
} }
CRITICAL_BLOCK(pwallet->cs_mapAddressBook) CRITICAL_BLOCK(pwallet->cs_mapAddressBook)
{ {
BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listReceived) BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listReceived)
{ {
if (pwallet->mapAddressBook.count(r.first)) if (pwallet->mapAddressBook.count(r.first))
{ {
map<string, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first); map<CBitcoinAddress, string>::const_iterator mi = pwallet->mapAddressBook.find(r.first);
if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount) if (mi != pwallet->mapAddressBook.end() && (*mi).second == strAccount)
nReceived += r.second; nReceived += r.second;
} }
@ -956,9 +950,17 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend, CW
dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain(); dPriority += (double)nCredit * pcoin.first->GetDepthInMainChain();
} }
// Fill a vout back to self with any change int64 nChange = nValueIn - nValue - nFeeRet;
int64 nChange = nValueIn - nTotalValue; // if sub-cent change is required, the fee must be raised to at least MIN_TX_FEE
if (nChange >= CENT) // or until nChange becomes zero
if (nFeeRet < MIN_TX_FEE && nChange > 0 && nChange < CENT)
{
int64 nMoveToFee = min(nChange, MIN_TX_FEE - nFeeRet);
nChange -= nMoveToFee;
nFeeRet += nMoveToFee;
}
if (nChange > 0)
{ {
// Note: We use a new key here to keep it from being obvious which side is the change. // Note: We use a new key here to keep it from being obvious which side is the change.
// The drawback is that by not reusing a previous key, the change may be lost if a // The drawback is that by not reusing a previous key, the change may be lost if a
@ -973,7 +975,7 @@ bool CWallet::CreateTransaction(const vector<pair<CScript, int64> >& vecSend, CW
// Fill a vout to ourself, using same address type as the payment // Fill a vout to ourself, using same address type as the payment
CScript scriptChange; CScript scriptChange;
if (vecSend[0].first.GetBitcoinAddressHash160() != 0) if (vecSend[0].first.GetBitcoinAddress().IsValid())
scriptChange.SetBitcoinAddress(vchPubKey); scriptChange.SetBitcoinAddress(vchPubKey);
else else
scriptChange << vchPubKey << OP_CHECKSIG; scriptChange << vchPubKey << OP_CHECKSIG;
@ -1122,7 +1124,7 @@ string CWallet::SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew,
// requires cs_main lock // requires cs_main lock
string CWallet::SendMoneyToBitcoinAddress(string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee) string CWallet::SendMoneyToBitcoinAddress(const CBitcoinAddress& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee)
{ {
// Check amount // Check amount
if (nValue <= 0) if (nValue <= 0)
@ -1132,8 +1134,7 @@ string CWallet::SendMoneyToBitcoinAddress(string strAddress, int64 nValue, CWall
// Parse bitcoin address // Parse bitcoin address
CScript scriptPubKey; CScript scriptPubKey;
if (!scriptPubKey.SetBitcoinAddress(strAddress)) scriptPubKey.SetBitcoinAddress(address);
return _("Invalid bitcoin address");
return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee); return SendMoney(scriptPubKey, nValue, wtxNew, fAskFee);
} }
@ -1151,13 +1152,13 @@ int CWallet::LoadWallet(bool& fFirstRunRet)
return nLoadWalletRet; return nLoadWalletRet;
fFirstRunRet = vchDefaultKey.empty(); fFirstRunRet = vchDefaultKey.empty();
if (!HaveKey(vchDefaultKey)) if (!HaveKey(Hash160(vchDefaultKey)))
{ {
// Create new keyUser and set as default key // Create new keyUser and set as default key
RandAddSeedPerfmon(); RandAddSeedPerfmon();
SetDefaultKey(GetOrReuseKeyFromPool()); SetDefaultKey(GetOrReuseKeyFromPool());
if (!SetAddressBookName(PubKeyToAddress(vchDefaultKey), "")) if (!SetAddressBookName(CBitcoinAddress(vchDefaultKey), ""))
return DB_LOAD_FAIL; return DB_LOAD_FAIL;
} }
@ -1166,20 +1167,20 @@ int CWallet::LoadWallet(bool& fFirstRunRet)
} }
bool CWallet::SetAddressBookName(const string& strAddress, const string& strName) bool CWallet::SetAddressBookName(const CBitcoinAddress& address, const string& strName)
{ {
mapAddressBook[strAddress] = strName; mapAddressBook[address] = strName;
if (!fFileBacked) if (!fFileBacked)
return false; return false;
return CWalletDB(strWalletFile).WriteName(strAddress, strName); return CWalletDB(strWalletFile).WriteName(address.ToString(), strName);
} }
bool CWallet::DelAddressBookName(const string& strAddress) bool CWallet::DelAddressBookName(const CBitcoinAddress& address)
{ {
mapAddressBook.erase(strAddress); mapAddressBook.erase(address);
if (!fFileBacked) if (!fFileBacked)
return false; return false;
return CWalletDB(strWalletFile).EraseName(strAddress); return CWalletDB(strWalletFile).EraseName(address.ToString());
} }
@ -1278,7 +1279,7 @@ void CWallet::ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool)
setKeyPool.erase(setKeyPool.begin()); setKeyPool.erase(setKeyPool.begin());
if (!walletdb.ReadPool(nIndex, keypool)) if (!walletdb.ReadPool(nIndex, keypool))
throw runtime_error("ReserveKeyFromKeyPool() : read failed"); throw runtime_error("ReserveKeyFromKeyPool() : read failed");
if (!HaveKey(keypool.vchPubKey)) if (!HaveKey(Hash160(keypool.vchPubKey)))
throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool"); throw runtime_error("ReserveKeyFromKeyPool() : unknown key in key pool");
assert(!keypool.vchPubKey.empty()); assert(!keypool.vchPubKey.empty());
printf("keypool reserve %"PRI64d"\n", nIndex); printf("keypool reserve %"PRI64d"\n", nIndex);

View File

@ -53,7 +53,7 @@ public:
std::map<uint256, int> mapRequestCount; std::map<uint256, int> mapRequestCount;
mutable CCriticalSection cs_mapRequestCount; mutable CCriticalSection cs_mapRequestCount;
std::map<std::string, std::string> mapAddressBook; std::map<CBitcoinAddress, std::string> mapAddressBook;
mutable CCriticalSection cs_mapAddressBook; mutable CCriticalSection cs_mapAddressBook;
std::vector<unsigned char> vchDefaultKey; std::vector<unsigned char> vchDefaultKey;
@ -82,7 +82,7 @@ public:
bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey); bool CommitTransaction(CWalletTx& wtxNew, CReserveKey& reservekey);
bool BroadcastTransaction(CWalletTx& wtxNew); bool BroadcastTransaction(CWalletTx& wtxNew);
std::string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); std::string SendMoney(CScript scriptPubKey, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
std::string SendMoneyToBitcoinAddress(std::string strAddress, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false); std::string SendMoneyToBitcoinAddress(const CBitcoinAddress& address, int64 nValue, CWalletTx& wtxNew, bool fAskFee=false);
bool TopUpKeyPool(); bool TopUpKeyPool();
void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool); void ReserveKeyFromKeyPool(int64& nIndex, CKeyPool& keypool);
@ -105,10 +105,10 @@ public:
} }
bool IsChange(const CTxOut& txout) const bool IsChange(const CTxOut& txout) const
{ {
std::vector<unsigned char> vchPubKey; CBitcoinAddress address;
if (ExtractPubKey(txout.scriptPubKey, this, vchPubKey)) if (ExtractAddress(txout.scriptPubKey, this, address))
CRITICAL_BLOCK(cs_mapAddressBook) CRITICAL_BLOCK(cs_mapAddressBook)
if (!mapAddressBook.count(PubKeyToAddress(vchPubKey))) if (!mapAddressBook.count(address))
return true; return true;
return false; return false;
} }
@ -172,10 +172,10 @@ public:
// bool BackupWallet(const std::string& strDest); // bool BackupWallet(const std::string& strDest);
// requires cs_mapAddressBook lock // requires cs_mapAddressBook lock
bool SetAddressBookName(const std::string& strAddress, const std::string& strName); bool SetAddressBookName(const CBitcoinAddress& address, const std::string& strName);
// requires cs_mapAddressBook lock // requires cs_mapAddressBook lock
bool DelAddressBookName(const std::string& strAddress); bool DelAddressBookName(const CBitcoinAddress& address);
void UpdatedTransaction(const uint256 &hashTx) void UpdatedTransaction(const uint256 &hashTx)
{ {
@ -465,8 +465,8 @@ public:
return nChangeCached; return nChangeCached;
} }
void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list<std::pair<std::string /* address */, int64> >& listReceived, void GetAmounts(int64& nGeneratedImmature, int64& nGeneratedMature, std::list<std::pair<CBitcoinAddress, int64> >& listReceived,
std::list<std::pair<std::string /* address */, int64> >& listSent, int64& nFee, std::string& strSentAccount) const; std::list<std::pair<CBitcoinAddress, int64> >& listSent, int64& nFee, std::string& strSentAccount) const;
void GetAccountAmounts(const std::string& strAccount, int64& nGenerated, int64& nReceived, void GetAccountAmounts(const std::string& strAccount, int64& nGenerated, int64& nReceived,
int64& nSent, int64& nFee) const; int64& nSent, int64& nFee) const;