Auto merge of #3228 - str4d:3058-taddr-encoding-refactor, r=str4d

Refactor t-address encoding

Includes code cherry-picked from the following upstream PRs:

- bitcoin/bitcoin#11117
- bitcoin/bitcoin#11259
  - Only the second commit (first is for QT code)
- bitcoin/bitcoin#11167
  - Only the first commit (the rest are not part of the t-address encoding refactor).

Part of #3058. Precursor to #3202.
This commit is contained in:
Homu
2018-05-03 18:12:22 -07:00
23 changed files with 347 additions and 372 deletions

View File

@@ -146,11 +146,11 @@ UniValue getnewaddress(const UniValue& params, bool fHelp)
pwalletMain->SetAddressBook(keyID, strAccount, "receive");
return CBitcoinAddress(keyID).ToString();
return EncodeDestination(keyID);
}
CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
CTxDestination GetAccountAddress(std::string strAccount, bool bForceNew=false)
{
CWalletDB walletdb(pwalletMain->strWalletFile);
@@ -184,7 +184,7 @@ CBitcoinAddress GetAccountAddress(string strAccount, bool bForceNew=false)
walletdb.WriteAccount(strAccount, account);
}
return CBitcoinAddress(account.vchPubKey.GetID());
return account.vchPubKey.GetID();
}
UniValue getaccountaddress(const UniValue& params, bool fHelp)
@@ -214,7 +214,7 @@ UniValue getaccountaddress(const UniValue& params, bool fHelp)
UniValue ret(UniValue::VSTR);
ret = GetAccountAddress(strAccount).ToString();
ret = EncodeDestination(GetAccountAddress(strAccount));
return ret;
}
@@ -250,7 +250,7 @@ UniValue getrawchangeaddress(const UniValue& params, bool fHelp)
CKeyID keyID = vchPubKey.GetID();
return CBitcoinAddress(keyID).ToString();
return EncodeDestination(keyID);
}
@@ -273,25 +273,25 @@ UniValue setaccount(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
CBitcoinAddress address(params[0].get_str());
if (!address.IsValid())
CTxDestination dest = DecodeDestination(params[0].get_str());
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Zcash address");
}
string strAccount;
if (params.size() > 1)
strAccount = AccountFromValue(params[1]);
// Only add the account if the address is yours.
if (IsMine(*pwalletMain, address.Get()))
{
if (IsMine(*pwalletMain, dest)) {
// Detect when changing the account of an address that is the 'unused current key' of another account:
if (pwalletMain->mapAddressBook.count(address.Get()))
{
string strOldAccount = pwalletMain->mapAddressBook[address.Get()].name;
if (address == GetAccountAddress(strOldAccount))
if (pwalletMain->mapAddressBook.count(dest)) {
std::string strOldAccount = pwalletMain->mapAddressBook[dest].name;
if (dest == GetAccountAddress(strOldAccount)) {
GetAccountAddress(strOldAccount, true);
}
}
pwalletMain->SetAddressBook(address.Get(), strAccount, "receive");
pwalletMain->SetAddressBook(dest, strAccount, "receive");
}
else
throw JSONRPCError(RPC_MISC_ERROR, "setaccount can only be used with own address");
@@ -320,14 +320,16 @@ UniValue getaccount(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
CBitcoinAddress address(params[0].get_str());
if (!address.IsValid())
CTxDestination dest = DecodeDestination(params[0].get_str());
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Zcash address");
}
string strAccount;
map<CTxDestination, CAddressBookData>::iterator mi = pwalletMain->mapAddressBook.find(address.Get());
if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.name.empty())
std::string strAccount;
std::map<CTxDestination, CAddressBookData>::iterator mi = pwalletMain->mapAddressBook.find(dest);
if (mi != pwalletMain->mapAddressBook.end() && !(*mi).second.name.empty()) {
strAccount = (*mi).second.name;
}
return strAccount;
}
@@ -359,12 +361,12 @@ UniValue getaddressesbyaccount(const UniValue& params, bool fHelp)
// Find all addresses that have the given account
UniValue ret(UniValue::VARR);
BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, CAddressBookData)& item, pwalletMain->mapAddressBook)
{
const CBitcoinAddress& address = item.first;
const string& strName = item.second.name;
if (strName == strAccount)
ret.push_back(address.ToString());
for (const std::pair<CTxDestination, CAddressBookData>& item : pwalletMain->mapAddressBook) {
const CTxDestination& dest = item.first;
const std::string& strName = item.second.name;
if (strName == strAccount) {
ret.push_back(EncodeDestination(dest));
}
}
return ret;
}
@@ -431,9 +433,10 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
CBitcoinAddress address(params[0].get_str());
if (!address.IsValid())
CTxDestination dest = DecodeDestination(params[0].get_str());
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Zcash address");
}
// Amount
CAmount nAmount = AmountFromValue(params[1]);
@@ -453,7 +456,7 @@ UniValue sendtoaddress(const UniValue& params, bool fHelp)
EnsureWalletIsUnlocked();
SendMoney(address.Get(), nAmount, fSubtractFeeFromAmount, wtx);
SendMoney(dest, nAmount, fSubtractFeeFromAmount, wtx);
return wtx.GetHash().GetHex();
}
@@ -489,18 +492,18 @@ UniValue listaddressgroupings(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
UniValue jsonGroupings(UniValue::VARR);
map<CTxDestination, CAmount> balances = pwalletMain->GetAddressBalances();
BOOST_FOREACH(set<CTxDestination> grouping, pwalletMain->GetAddressGroupings())
{
std::map<CTxDestination, CAmount> balances = pwalletMain->GetAddressBalances();
for (const std::set<CTxDestination>& grouping : pwalletMain->GetAddressGroupings()) {
UniValue jsonGrouping(UniValue::VARR);
BOOST_FOREACH(CTxDestination address, grouping)
for (const CTxDestination& address : grouping)
{
UniValue addressInfo(UniValue::VARR);
addressInfo.push_back(CBitcoinAddress(address).ToString());
addressInfo.push_back(EncodeDestination(address));
addressInfo.push_back(ValueFromAmount(balances[address]));
{
if (pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get()) != pwalletMain->mapAddressBook.end())
addressInfo.push_back(pwalletMain->mapAddressBook.find(CBitcoinAddress(address).Get())->second.name);
if (pwalletMain->mapAddressBook.find(address) != pwalletMain->mapAddressBook.end()) {
addressInfo.push_back(pwalletMain->mapAddressBook.find(address)->second.name);
}
}
jsonGrouping.push_back(addressInfo);
}
@@ -542,17 +545,20 @@ UniValue signmessage(const UniValue& params, bool fHelp)
string strAddress = params[0].get_str();
string strMessage = params[1].get_str();
CBitcoinAddress addr(strAddress);
if (!addr.IsValid())
CTxDestination dest = DecodeDestination(strAddress);
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid address");
}
CKeyID keyID;
if (!addr.GetKeyID(keyID))
const CKeyID *keyID = boost::get<CKeyID>(&dest);
if (!keyID) {
throw JSONRPCError(RPC_TYPE_ERROR, "Address does not refer to key");
}
CKey key;
if (!pwalletMain->GetKey(keyID, key))
if (!pwalletMain->GetKey(*keyID, key)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Private key not available");
}
CHashWriter ss(SER_GETHASH, 0);
ss << strMessageMagic;
@@ -593,12 +599,14 @@ UniValue getreceivedbyaddress(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
// Bitcoin address
CBitcoinAddress address = CBitcoinAddress(params[0].get_str());
if (!address.IsValid())
CTxDestination dest = DecodeDestination(params[0].get_str());
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Zcash address");
CScript scriptPubKey = GetScriptForDestination(address.Get());
if (!IsMine(*pwalletMain, scriptPubKey))
}
CScript scriptPubKey = GetScriptForDestination(dest);
if (!IsMine(*pwalletMain, scriptPubKey)) {
return ValueFromAmount(0);
}
// Minimum confirmations
int nMinDepth = 1;
@@ -907,10 +915,11 @@ UniValue sendfrom(const UniValue& params, bool fHelp)
LOCK2(cs_main, pwalletMain->cs_wallet);
string strAccount = AccountFromValue(params[0]);
CBitcoinAddress address(params[1].get_str());
if (!address.IsValid())
std::string strAccount = AccountFromValue(params[0]);
CTxDestination dest = DecodeDestination(params[1].get_str());
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid Zcash address");
}
CAmount nAmount = AmountFromValue(params[2]);
if (nAmount <= 0)
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
@@ -932,7 +941,7 @@ UniValue sendfrom(const UniValue& params, bool fHelp)
if (nAmount > nBalance)
throw JSONRPCError(RPC_WALLET_INSUFFICIENT_FUNDS, "Account has insufficient funds");
SendMoney(address.Get(), nAmount, false, wtx);
SendMoney(dest, nAmount, false, wtx);
return wtx.GetHash().GetHex();
}
@@ -996,22 +1005,23 @@ UniValue sendmany(const UniValue& params, bool fHelp)
if (params.size() > 4)
subtractFeeFromAmount = params[4].get_array();
set<CBitcoinAddress> setAddress;
vector<CRecipient> vecSend;
std::set<CTxDestination> destinations;
std::vector<CRecipient> vecSend;
CAmount totalAmount = 0;
vector<string> keys = sendTo.getKeys();
BOOST_FOREACH(const string& name_, keys)
{
CBitcoinAddress address(name_);
if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Zcash address: ")+name_);
std::vector<std::string> keys = sendTo.getKeys();
for (const std::string& name_ : keys) {
CTxDestination dest = DecodeDestination(name_);
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Zcash address: ") + name_);
}
if (setAddress.count(address))
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+name_);
setAddress.insert(address);
if (destinations.count(dest)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + name_);
}
destinations.insert(dest);
CScript scriptPubKey = GetScriptForDestination(address.Get());
CScript scriptPubKey = GetScriptForDestination(dest);
CAmount nAmount = AmountFromValue(sendTo[name_]);
if (nAmount <= 0)
throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send");
@@ -1097,7 +1107,7 @@ UniValue addmultisigaddress(const UniValue& params, bool fHelp)
pwalletMain->AddCScript(inner);
pwalletMain->SetAddressBook(innerID, strAccount, "send");
return CBitcoinAddress(innerID).ToString();
return EncodeDestination(innerID);
}
@@ -1133,10 +1143,9 @@ UniValue ListReceived(const UniValue& params, bool fByAccounts)
filter = filter | ISMINE_WATCH_ONLY;
// Tally
map<CBitcoinAddress, tallyitem> mapTally;
for (map<uint256, CWalletTx>::iterator it = pwalletMain->mapWallet.begin(); it != pwalletMain->mapWallet.end(); ++it)
{
const CWalletTx& wtx = (*it).second;
std::map<CTxDestination, tallyitem> mapTally;
for (const std::pair<uint256, CWalletTx>& pairWtx : pwalletMain->mapWallet) {
const CWalletTx& wtx = pairWtx.second;
if (wtx.IsCoinBase() || !CheckFinalTx(wtx))
continue;
@@ -1166,12 +1175,11 @@ UniValue ListReceived(const UniValue& params, bool fByAccounts)
// Reply
UniValue ret(UniValue::VARR);
map<string, tallyitem> mapAccountTally;
BOOST_FOREACH(const PAIRTYPE(CBitcoinAddress, CAddressBookData)& item, pwalletMain->mapAddressBook)
{
const CBitcoinAddress& address = item.first;
const string& strAccount = item.second.name;
map<CBitcoinAddress, tallyitem>::iterator it = mapTally.find(address);
std::map<std::string, tallyitem> mapAccountTally;
for (const std::pair<CTxDestination, CAddressBookData>& item : pwalletMain->mapAddressBook) {
const CTxDestination& dest = item.first;
const std::string& strAccount = item.second.name;
std::map<CTxDestination, tallyitem>::iterator it = mapTally.find(dest);
if (it == mapTally.end() && !fIncludeEmpty)
continue;
@@ -1197,7 +1205,7 @@ UniValue ListReceived(const UniValue& params, bool fByAccounts)
UniValue obj(UniValue::VOBJ);
if(fIsWatchonly)
obj.push_back(Pair("involvesWatchonly", true));
obj.push_back(Pair("address", address.ToString()));
obj.push_back(Pair("address", EncodeDestination(dest)));
obj.push_back(Pair("account", strAccount));
obj.push_back(Pair("amount", ValueFromAmount(nAmount)));
obj.push_back(Pair("confirmations", (nConf == std::numeric_limits<int>::max() ? 0 : nConf)));
@@ -1308,9 +1316,9 @@ UniValue listreceivedbyaccount(const UniValue& params, bool fHelp)
static void MaybePushAddress(UniValue & entry, const CTxDestination &dest)
{
CBitcoinAddress addr;
if (addr.Set(dest))
entry.push_back(Pair("address", addr.ToString()));
if (IsValidDestination(dest)) {
entry.push_back(Pair("address", EncodeDestination(dest)));
}
}
void ListTransactions(const CWalletTx& wtx, const string& strAccount, int nMinDepth, bool fLong, UniValue& ret, const isminefilter& filter)
@@ -2367,17 +2375,18 @@ UniValue listunspent(const UniValue& params, bool fHelp)
if (params.size() > 1)
nMaxDepth = params[1].get_int();
set<CBitcoinAddress> setAddress;
std::set<CTxDestination> destinations;
if (params.size() > 2) {
UniValue inputs = params[2].get_array();
for (size_t idx = 0; idx < inputs.size(); idx++) {
const UniValue& input = inputs[idx];
CBitcoinAddress address(input.get_str());
if (!address.IsValid())
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Zcash address: ")+input.get_str());
if (setAddress.count(address))
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+input.get_str());
setAddress.insert(address);
CTxDestination dest = DecodeDestination(input.get_str());
if (!IsValidDestination(dest)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, std::string("Invalid Zcash address: ") + input.get_str());
}
if (!destinations.insert(dest).second) {
throw JSONRPCError(RPC_INVALID_PARAMETER, std::string("Invalid parameter, duplicated address: ") + input.get_str());
}
}
}
@@ -2394,7 +2403,7 @@ UniValue listunspent(const UniValue& params, bool fHelp)
const CScript& scriptPubKey = out.tx->vout[out.i].scriptPubKey;
bool fValidAddress = ExtractDestination(scriptPubKey, address);
if (setAddress.size() && (!fValidAddress || !setAddress.count(address)))
if (destinations.size() && (!fValidAddress || !destinations.count(address)))
continue;
UniValue entry(UniValue::VOBJ);
@@ -2403,7 +2412,7 @@ UniValue listunspent(const UniValue& params, bool fHelp)
entry.push_back(Pair("generated", out.tx->IsCoinBase()));
if (fValidAddress) {
entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
entry.push_back(Pair("address", EncodeDestination(address)));
if (pwalletMain->mapAddressBook.count(address))
entry.push_back(Pair("account", pwalletMain->mapAddressBook[address].name));
@@ -3129,16 +3138,16 @@ UniValue z_listaddresses(const UniValue& params, bool fHelp)
}
CAmount getBalanceTaddr(std::string transparentAddress, int minDepth=1, bool ignoreUnspendable=true) {
set<CBitcoinAddress> setAddress;
std::set<CTxDestination> destinations;
vector<COutput> vecOutputs;
CAmount balance = 0;
if (transparentAddress.length() > 0) {
CBitcoinAddress taddr = CBitcoinAddress(transparentAddress);
if (!taddr.IsValid()) {
CTxDestination taddr = DecodeDestination(transparentAddress);
if (!IsValidDestination(taddr)) {
throw std::runtime_error("invalid transparent address");
}
setAddress.insert(taddr);
destinations.insert(taddr);
}
LOCK2(cs_main, pwalletMain->cs_wallet);
@@ -3154,13 +3163,13 @@ CAmount getBalanceTaddr(std::string transparentAddress, int minDepth=1, bool ign
continue;
}
if (setAddress.size()) {
if (destinations.size()) {
CTxDestination address;
if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) {
continue;
}
if (!setAddress.count(address)) {
if (!destinations.count(address)) {
continue;
}
}
@@ -3288,8 +3297,8 @@ UniValue z_getbalance(const UniValue& params, bool fHelp)
// Check that the from address is valid.
auto fromaddress = params[0].get_str();
bool fromTaddr = false;
CBitcoinAddress taddr(fromaddress);
fromTaddr = taddr.IsValid();
CTxDestination taddr = DecodeDestination(fromaddress);
fromTaddr = IsValidDestination(taddr);
libzcash::PaymentAddress zaddr;
if (!fromTaddr) {
CZCPaymentAddress address(fromaddress);
@@ -3522,8 +3531,8 @@ UniValue z_sendmany(const UniValue& params, bool fHelp)
// Check that the from address is valid.
auto fromaddress = params[0].get_str();
bool fromTaddr = false;
CBitcoinAddress taddr(fromaddress);
fromTaddr = taddr.IsValid();
CTxDestination taddr = DecodeDestination(fromaddress);
fromTaddr = IsValidDestination(taddr);
libzcash::PaymentAddress zaddr;
if (!fromTaddr) {
CZCPaymentAddress address(fromaddress);
@@ -3568,8 +3577,8 @@ UniValue z_sendmany(const UniValue& params, bool fHelp)
string address = find_value(o, "address").get_str();
bool isZaddr = false;
CBitcoinAddress taddr(address);
if (!taddr.IsValid()) {
CTxDestination taddr = DecodeDestination(address);
if (!IsValidDestination(taddr)) {
try {
CZCPaymentAddress zaddr(address);
zaddr.Get();
@@ -3743,10 +3752,10 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp)
// Validate the from address
auto fromaddress = params[0].get_str();
bool isFromWildcard = fromaddress == "*";
CBitcoinAddress taddr;
CTxDestination taddr;
if (!isFromWildcard) {
taddr = CBitcoinAddress(fromaddress);
if (!taddr.IsValid()) {
taddr = DecodeDestination(fromaddress);
if (!IsValidDestination(taddr)) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid from address, should be a taddr or \"*\".");
}
}
@@ -3791,9 +3800,9 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp)
size_t mempoolLimit = (nLimit != 0) ? nLimit : (overwinterActive ? 0 : (size_t)GetArg("-mempooltxinputlimit", 0));
// Set of addresses to filter utxos by
set<CBitcoinAddress> setAddress = {};
std::set<CTxDestination> destinations = {};
if (!isFromWildcard) {
setAddress.insert(taddr);
destinations.insert(taddr);
}
// Get available utxos
@@ -3811,7 +3820,7 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp)
continue;
}
// If taddr is not wildcard "*", filter utxos
if (setAddress.size()>0 && !setAddress.count(address)) {
if (destinations.size() > 0 && !destinations.count(address)) {
continue;
}
@@ -3823,8 +3832,7 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp)
CAmount nValue = out.tx->vout[out.i].nValue;
if (!maxedOutFlag) {
CBitcoinAddress ba(address);
size_t increase = (ba.IsScript()) ? CTXIN_SPEND_P2SH_SIZE : CTXIN_SPEND_DUST_SIZE;
size_t increase = (boost::get<CScriptID>(&address) != nullptr) ? CTXIN_SPEND_P2SH_SIZE : CTXIN_SPEND_DUST_SIZE;
if (estimatedTxSize + increase >= MAX_TX_SIZE ||
(mempoolLimit > 0 && utxoCounter > mempoolLimit))
{
@@ -3967,7 +3975,7 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp)
bool useAny = false;
bool useAnyUTXO = false;
bool useAnyNote = false;
std::set<CBitcoinAddress> taddrs = {};
std::set<CTxDestination> taddrs = {};
std::set<libzcash::PaymentAddress> zaddrs = {};
UniValue addresses = params[0].get_array();
@@ -3990,8 +3998,8 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp)
} else if (address == "ANY_ZADDR") {
useAnyNote = true;
} else {
CBitcoinAddress taddr(address);
if (taddr.IsValid()) {
CTxDestination taddr = DecodeDestination(address);
if (IsValidDestination(taddr)) {
// Ignore any listed t-addrs if we are using all of them
if (!(useAny || useAnyUTXO)) {
taddrs.insert(taddr);
@@ -4019,8 +4027,8 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp)
// Validate the destination address
auto destaddress = params[1].get_str();
bool isToZaddr = false;
CBitcoinAddress taddr(destaddress);
if (!taddr.IsValid()) {
CTxDestination taddr = DecodeDestination(destaddress);
if (!IsValidDestination(taddr)) {
try {
CZCPaymentAddress zaddr(destaddress);
zaddr.Get();
@@ -4116,8 +4124,7 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp)
CAmount nValue = out.tx->vout[out.i].nValue;
if (!maxedOutUTXOsFlag) {
CBitcoinAddress ba(address);
size_t increase = (ba.IsScript()) ? CTXIN_SPEND_P2SH_SIZE : CTXIN_SPEND_DUST_SIZE;
size_t increase = (boost::get<CScriptID>(&address) != nullptr) ? CTXIN_SPEND_P2SH_SIZE : CTXIN_SPEND_DUST_SIZE;
if (estimatedTxSize + increase >= MAX_TX_SIZE ||
(mempoolLimit > 0 && utxoCounter > mempoolLimit))
{