#include <boost/iostreams/stream.hpp>
#include <boost/algorithm/string.hpp>
#ifdef USE_SSL
-#include <boost/asio/ssl.hpp>
+#include <boost/asio/ssl.hpp>
#include <boost/filesystem.hpp>
#include <boost/filesystem/fstream.hpp>
typedef boost::asio::ssl::stream<boost::asio::ip::tcp::socket> SSLStream;
strAccount = AccountFromValue(params[0]);
// 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.
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
// requires cs_main, cs_mapWallet, cs_mapAddressBook locks
-string GetAccountAddress(string strAccount, bool bForceNew=false)
+CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
{
string strAddress;
else
{
account.vchPubKey = pwalletMain->GetOrReuseKeyFromPool();
- string strAddress = PubKeyToAddress(account.vchPubKey);
+ string strAddress = CBitcoinAddress(account.vchPubKey).ToString();
pwalletMain->SetAddressBookName(strAddress, strAccount);
walletdb.WriteAccount(strAccount, account);
}
}
}
- strAddress = PubKeyToAddress(account.vchPubKey);
-
- return strAddress;
+ return CBitcoinAddress(account.vchPubKey);
}
Value getaccountaddress(const Array& params, bool fHelp)
CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{
- ret = GetAccountAddress(strAccount);
+ ret = GetAccountAddress(strAccount).ToString();
}
return ret;
"Sets the account associated with the given address.");
string strAddress = params[0].get_str();
- uint160 hash160;
- bool isValid = AddressToHash160(strAddress, hash160);
- if (!isValid)
+ CBitcoinAddress address(strAddress);
+ if (!address.IsValid())
throw JSONRPCError(-5, "Invalid bitcoin address");
CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{
- if (pwalletMain->mapAddressBook.count(strAddress))
+ if (pwalletMain->mapAddressBook.count(address))
{
- string strOldAccount = pwalletMain->mapAddressBook[strAddress];
- if (strAddress == GetAccountAddress(strOldAccount))
+ string strOldAccount = pwalletMain->mapAddressBook[address];
+ if (address == GetAccountAddress(strOldAccount))
GetAccountAddress(strOldAccount, true);
}
"Returns the account associated with the given address.");
string strAddress = params[0].get_str();
+ CBitcoinAddress address(strAddress);
string strAccount;
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())
strAccount = (*mi).second;
}
Array ret;
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;
if (strName == strAccount)
- {
- // We're only adding valid bitcoin addresses and not ip addresses
- CScript scriptPubKey;
- if (scriptPubKey.SetBitcoinAddress(strAddress))
- ret.push_back(strAddress);
- }
+ ret.push_back(address.ToString());
}
}
return ret;
"Returns the total amount received by <bitcoinaddress> in transactions with at least [minconf] confirmations.");
// Bitcoin address
- string strAddress = params[0].get_str();
+ CBitcoinAddress address = CBitcoinAddress(params[0].get_str());
CScript scriptPubKey;
- if (!scriptPubKey.SetBitcoinAddress(strAddress))
+ if (!address.IsValid())
throw JSONRPCError(-5, "Invalid bitcoin address");
+ scriptPubKey.SetBitcoinAddress(address);
if (!IsMine(*pwalletMain,scriptPubKey))
return (double)0.0;
}
-void GetAccountPubKeys(string strAccount, set<CScript>& setPubKey)
+void GetAccountAddresses(string strAccount, set<CBitcoinAddress>& setAddress)
{
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;
if (strName == strAccount)
- {
- // 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);
- }
+ setAddress.insert(address);
}
}
}
// Get the set of pub keys that have the label
string strAccount = AccountFromValue(params[0]);
- set<CScript> setPubKey;
- GetAccountPubKeys(strAccount, setPubKey);
+ set<CBitcoinAddress> setAddress;
+ GetAccountAddresses(strAccount, setAddress);
// Tally
int64 nAmount = 0;
continue;
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)
nAmount += txout.nValue;
+ }
}
}
int64 allGeneratedImmature, allGeneratedMature, allFee;
allGeneratedImmature = allGeneratedMature = allFee = 0;
string strSentAccount;
- list<pair<string, int64> > listReceived;
- list<pair<string, int64> > listSent;
+ list<pair<CBitcoinAddress, int64> > listReceived;
+ list<pair<CBitcoinAddress, int64> > listSent;
wtx.GetAmounts(allGeneratedImmature, allGeneratedMature, listReceived, listSent, allFee, strSentAccount);
if (wtx.GetDepthInMainChain() >= nMinDepth)
- BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listReceived)
+ BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listReceived)
nBalance += r.second;
- BOOST_FOREACH(const PAIRTYPE(string,int64)& r, listSent)
+ BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress,int64)& r, listSent)
nBalance -= r.second;
nBalance -= allFee;
nBalance += allGeneratedMature;
if (params.size() > 3 && params[3].type() != null_type && !params[3].get_str().empty())
wtx.mapValue["comment"] = params[3].get_str();
- set<string> setAddress;
+ set<CBitcoinAddress> setAddress;
vector<pair<CScript, int64> > vecSend;
int64 totalAmount = 0;
BOOST_FOREACH(const Pair& s, sendTo)
{
- uint160 hash160;
- string strAddress = s.name_;
+ CBitcoinAddress address(s.name_);
+ if (!address.IsValid())
+ throw JSONRPCError(-5, string("Invalid bitcoin address:")+s.name_);
- if (setAddress.count(strAddress))
- throw JSONRPCError(-8, string("Invalid parameter, duplicated address: ")+strAddress);
- setAddress.insert(strAddress);
+ if (setAddress.count(address))
+ throw JSONRPCError(-8, string("Invalid parameter, duplicated address: ")+s.name_);
+ setAddress.insert(address);
CScript scriptPubKey;
- if (!scriptPubKey.SetBitcoinAddress(strAddress))
- throw JSONRPCError(-5, string("Invalid bitcoin address:")+strAddress);
- int64 nAmount = AmountFromValue(s.value_);
+ scriptPubKey.SetBitcoinAddress(address);
+ int64 nAmount = AmountFromValue(s.value_);
totalAmount += nAmount;
vecSend.push_back(make_pair(scriptPubKey, nAmount));
fIncludeEmpty = params[1].get_bool();
// Tally
- map<uint160, tallyitem> mapTally;
+ map<CBitcoinAddress, tallyitem> mapTally;
CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
{
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
BOOST_FOREACH(const CTxOut& txout, wtx.vout)
{
- // Only counting our own bitcoin addresses and not ip addresses
- uint160 hash160 = txout.scriptPubKey.GetBitcoinAddressHash160();
- if (hash160 == 0 || !mapPubKeys.count(hash160)) // IsMine
+ CBitcoinAddress address;
+ if (!ExtractAddress(txout.scriptPubKey, pwalletMain, address) || !address.IsValid())
continue;
- tallyitem& item = mapTally[hash160];
+ tallyitem& item = mapTally[address];
item.nAmount += txout.nValue;
item.nConf = min(item.nConf, nDepth);
}
map<string, tallyitem> mapAccountTally;
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;
- uint160 hash160;
- if (!AddressToHash160(strAddress, hash160))
- continue;
- map<uint160, tallyitem>::iterator it = mapTally.find(hash160);
+ map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
if (it == mapTally.end() && !fIncludeEmpty)
continue;
else
{
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("label", strAccount)); // deprecated
obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
{
int64 nGeneratedImmature, nGeneratedMature, nFee;
string strSentAccount;
- list<pair<string, int64> > listReceived;
- list<pair<string, int64> > listSent;
+ list<pair<CBitcoinAddress, int64> > listReceived;
+ list<pair<CBitcoinAddress, int64> > listSent;
wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount);
bool fAllAccounts = (strAccount == string("*"));
// Sent
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;
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("amount", ValueFromAmount(-s.second)));
entry.push_back(Pair("fee", ValueFromAmount(-nFee)));
if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{
- BOOST_FOREACH(const PAIRTYPE(string, int64)& r, listReceived)
+ BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& r, listReceived)
{
string account;
if (pwalletMain->mapAddressBook.count(r.first))
{
Object entry;
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("amount", ValueFromAmount(r.second)));
if (fLong)
}
// ret is now newest to oldest
}
-
+
// Make sure we return only last nCount items (sends-to-self might give us an extra):
if (ret.size() > nCount)
{
CRITICAL_BLOCK(pwalletMain->cs_mapWallet)
CRITICAL_BLOCK(pwalletMain->cs_mapAddressBook)
{
- BOOST_FOREACH(const PAIRTYPE(string, string)& entry, pwalletMain->mapAddressBook) {
- uint160 hash160;
- if(AddressToHash160(entry.first, hash160) && mapPubKeys.count(hash160)) // This address belongs to me
+ BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, string)& entry, pwalletMain->mapAddressBook) {
+ if (pwalletMain->HaveKey(entry.first)) // This address belongs to me
mapAccountBalances[entry.second] = 0;
}
const CWalletTx& wtx = (*it).second;
int64 nGeneratedImmature, nGeneratedMature, nFee;
string strSentAccount;
- list<pair<string, int64> > listReceived;
- list<pair<string, int64> > listSent;
+ list<pair<CBitcoinAddress, int64> > listReceived;
+ list<pair<CBitcoinAddress, int64> > listSent;
wtx.GetAmounts(nGeneratedImmature, nGeneratedMature, listReceived, listSent, nFee, strSentAccount);
mapAccountBalances[strSentAccount] -= nFee;
- BOOST_FOREACH(const PAIRTYPE(string, int64)& s, listSent)
+ BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& s, listSent)
mapAccountBalances[strSentAccount] -= s.second;
if (wtx.GetDepthInMainChain() >= nMinDepth)
{
mapAccountBalances[""] += nGeneratedMature;
- BOOST_FOREACH(const PAIRTYPE(string, int64)& r, listReceived)
+ BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, int64)& r, listReceived)
if (pwalletMain->mapAddressBook.count(r.first))
mapAccountBalances[pwalletMain->mapAddressBook[r.first]] += r.second;
else
"Return information about <bitcoinaddress>.");
string strAddress = params[0].get_str();
- uint160 hash160;
- bool isValid = AddressToHash160(strAddress, hash160);
+ CBitcoinAddress address(strAddress);
+ bool isValid = address.IsValid();
Object ret;
ret.push_back(Pair("isvalid", isValid));
{
// Call Hash160ToAddress() so we always return current ADDRESSVERSION
// version of the address:
- string currentAddress = Hash160ToAddress(hash160);
+ string currentAddress = address.ToString();
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)
{
- if (pwalletMain->mapAddressBook.count(currentAddress))
- ret.push_back(Pair("account", pwalletMain->mapAddressBook[currentAddress]));
+ if (pwalletMain->mapAddressBook.count(address))
+ ret.push_back(Pair("account", pwalletMain->mapAddressBook[address]));
}
}
return ret;