Introduce wrappers around CZCPaymentAddress

This patch removes the need for the intermediary Base58 type
CZCPaymentAddress, by providing {Encode,Decode}PaymentAddress
functions that directly operate on the conversion between strings
and libzcash::PaymentAddress.
This commit is contained in:
Jack Grigg
2018-04-24 15:01:45 +01:00
parent f146029b0a
commit 80ed13d545
15 changed files with 135 additions and 140 deletions

View File

@@ -78,14 +78,12 @@ AsyncRPCOperation_mergetoaddress::AsyncRPCOperation_mergetoaddress(
isToZaddr_ = false;
if (!isToTaddr_) {
CZCPaymentAddress address(std::get<0>(recipient));
try {
PaymentAddress addr = address.Get();
auto address = DecodePaymentAddress(std::get<0>(recipient));
if (address) {
isToZaddr_ = true;
toPaymentAddress_ = addr;
} catch (const std::runtime_error& e) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("runtime error: ") + e.what());
toPaymentAddress_ = *address;
} else {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid recipient address");
}
}
@@ -857,8 +855,7 @@ UniValue AsyncRPCOperation_mergetoaddress::perform_joinsplit(
PaymentDisclosureInfo pdInfo = {PAYMENT_DISCLOSURE_VERSION_EXPERIMENTAL, esk, joinSplitPrivKey, zaddr};
paymentDisclosureData_.push_back(PaymentDisclosureKeyInfo(pdKey, pdInfo));
CZCPaymentAddress address(zaddr);
LogPrint("paymentdisclosure", "%s: Payment Disclosure: js=%d, n=%d, zaddr=%s\n", getId(), js_index, int(mapped_index), address.ToString());
LogPrint("paymentdisclosure", "%s: Payment Disclosure: js=%d, n=%d, zaddr=%s\n", getId(), js_index, int(mapped_index), EncodePaymentAddress(zaddr));
}
// !!! Payment disclosure END

View File

@@ -79,9 +79,9 @@ AsyncRPCOperation_sendmany::AsyncRPCOperation_sendmany(
isfromzaddr_ = false;
if (!isfromtaddr_) {
CZCPaymentAddress address(fromAddress);
try {
PaymentAddress addr = address.Get();
auto address = DecodePaymentAddress(fromAddress);
if (address) {
PaymentAddress addr = *address;
// We don't need to lock on the wallet as spending key related methods are thread-safe
SpendingKey key;
@@ -92,8 +92,8 @@ AsyncRPCOperation_sendmany::AsyncRPCOperation_sendmany(
isfromzaddr_ = true;
frompaymentaddress_ = addr;
spendingkey_ = key;
} catch (const std::runtime_error& e) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("runtime error: ") + e.what());
} else {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid from address");
}
}
@@ -467,7 +467,7 @@ bool AsyncRPCOperation_sendmany::main_impl() {
std::string hexMemo = std::get<2>(smr);
zOutputsDeque.pop_front();
PaymentAddress pa = CZCPaymentAddress(address).Get();
PaymentAddress pa = *DecodePaymentAddress(address);
JSOutput jso = JSOutput(pa, value);
if (hexMemo.size() > 0) {
jso.memo = get_memo_from_hex_string(hexMemo);
@@ -726,7 +726,7 @@ bool AsyncRPCOperation_sendmany::main_impl() {
assert(value==0);
info.vjsout.push_back(JSOutput()); // dummy output while we accumulate funds into a change note for vpub_new
} else {
PaymentAddress pa = CZCPaymentAddress(address).Get();
PaymentAddress pa = *DecodePaymentAddress(address);
JSOutput jso = JSOutput(pa, value);
if (hexMemo.size() > 0) {
jso.memo = get_memo_from_hex_string(hexMemo);
@@ -1080,8 +1080,7 @@ UniValue AsyncRPCOperation_sendmany::perform_joinsplit(
PaymentDisclosureInfo pdInfo = {PAYMENT_DISCLOSURE_VERSION_EXPERIMENTAL, esk, joinSplitPrivKey, zaddr};
paymentDisclosureData_.push_back(PaymentDisclosureKeyInfo(pdKey, pdInfo));
CZCPaymentAddress address(zaddr);
LogPrint("paymentdisclosure", "%s: Payment Disclosure: js=%d, n=%d, zaddr=%s\n", getId(), js_index, int(mapped_index), address.ToString());
LogPrint("paymentdisclosure", "%s: Payment Disclosure: js=%d, n=%d, zaddr=%s\n", getId(), js_index, int(mapped_index), EncodePaymentAddress(zaddr));
}
// !!! Payment disclosure END

View File

@@ -71,11 +71,11 @@ AsyncRPCOperation_shieldcoinbase::AsyncRPCOperation_shieldcoinbase(
}
// Check the destination address is valid for this network i.e. not testnet being used on mainnet
CZCPaymentAddress address(toAddress);
try {
tozaddr_ = address.Get();
} catch (const std::runtime_error& e) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("runtime error: ") + e.what());
auto address = DecodePaymentAddress(toAddress);
if (address) {
tozaddr_ = *address;
} else {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid to address");
}
// Log the context info
@@ -451,8 +451,7 @@ UniValue AsyncRPCOperation_shieldcoinbase::perform_joinsplit(ShieldCoinbaseJSInf
PaymentDisclosureInfo pdInfo = {PAYMENT_DISCLOSURE_VERSION_EXPERIMENTAL, esk, joinSplitPrivKey, zaddr};
paymentDisclosureData_.push_back(PaymentDisclosureKeyInfo(pdKey, pdInfo));
CZCPaymentAddress address(zaddr);
LogPrint("paymentdisclosure", "%s: Payment Disclosure: js=%d, n=%d, zaddr=%s\n", getId(), js_index, int(mapped_index), address.ToString());
LogPrint("paymentdisclosure", "%s: Payment Disclosure: js=%d, n=%d, zaddr=%s\n", getId(), js_index, int(mapped_index), EncodePaymentAddress(zaddr));
}
// !!! Payment disclosure END

View File

@@ -25,12 +25,11 @@ TEST(wallet_zkeys_tests, store_and_load_zkeys) {
ASSERT_EQ(0, addrs.size());
// wallet should have one key
CZCPaymentAddress paymentAddress = wallet.GenerateNewZKey();
auto addr = wallet.GenerateNewZKey();
wallet.GetPaymentAddresses(addrs);
ASSERT_EQ(1, addrs.size());
// verify wallet has spending key for the address
auto addr = paymentAddress.Get();
ASSERT_TRUE(wallet.HaveSpendingKey(addr));
// manually add new spending key to wallet
@@ -289,22 +288,22 @@ TEST(wallet_zkeys_tests, write_cryptedzkey_direct_to_db) {
ASSERT_EQ(2, addrs.size());
// check we have entries for our payment addresses
ASSERT_TRUE(addrs.count(paymentAddress.Get()));
ASSERT_TRUE(addrs.count(paymentAddress2.Get()));
ASSERT_TRUE(addrs.count(paymentAddress));
ASSERT_TRUE(addrs.count(paymentAddress2));
// spending key is crypted, so we can't extract valid payment address
libzcash::SpendingKey keyOut;
wallet2.GetSpendingKey(paymentAddress.Get(), keyOut);
ASSERT_FALSE(paymentAddress.Get() == keyOut.address());
wallet2.GetSpendingKey(paymentAddress, keyOut);
ASSERT_FALSE(paymentAddress == keyOut.address());
// unlock wallet to get spending keys and verify payment addresses
wallet2.Unlock(strWalletPass);
wallet2.GetSpendingKey(paymentAddress.Get(), keyOut);
ASSERT_EQ(paymentAddress.Get(), keyOut.address());
wallet2.GetSpendingKey(paymentAddress, keyOut);
ASSERT_EQ(paymentAddress, keyOut.address());
wallet2.GetSpendingKey(paymentAddress2.Get(), keyOut);
ASSERT_EQ(paymentAddress2.Get(), keyOut.address());
wallet2.GetSpendingKey(paymentAddress2, keyOut);
ASSERT_EQ(paymentAddress2, keyOut.address());
ECC_Stop();
}

View File

@@ -254,11 +254,8 @@ UniValue z_validatepaymentdisclosure(const UniValue& params, bool fHelp)
// Check the payment address is valid
PaymentAddress zaddr = pd.payload.zaddr;
CZCPaymentAddress address;
if (!address.Set(zaddr)) {
errs.push_back("Payment disclosure refers to an invalid payment address");
} else {
o.push_back(Pair("paymentAddress", address.ToString()));
{
o.push_back(Pair("paymentAddress", EncodePaymentAddress(zaddr)));
try {
// Decrypt the note to get value and memo field

View File

@@ -300,11 +300,11 @@ UniValue importwallet_impl(const UniValue& params, bool fHelp, bool fImportZKeys
libzcash::SpendingKey key = spendingkey.Get();
libzcash::PaymentAddress addr = key.address();
if (pwalletMain->HaveSpendingKey(addr)) {
LogPrint("zrpc", "Skipping import of zaddr %s (key already present)\n", CZCPaymentAddress(addr).ToString());
LogPrint("zrpc", "Skipping import of zaddr %s (key already present)\n", EncodePaymentAddress(addr));
continue;
}
int64_t nTime = DecodeDumpTime(vstr[1]);
LogPrint("zrpc", "Importing zaddr %s...\n", CZCPaymentAddress(addr).ToString());
LogPrint("zrpc", "Importing zaddr %s...\n", EncodePaymentAddress(addr));
if (!pwalletMain->AddZKey(key)) {
// Something went wrong
fGood = false;
@@ -536,7 +536,7 @@ UniValue dumpwallet_impl(const UniValue& params, bool fHelp, bool fDumpZKeys)
libzcash::SpendingKey key;
if (pwalletMain->GetSpendingKey(addr, key)) {
std::string strTime = EncodeDumpTime(pwalletMain->mapZKeyMetadata[addr].nCreateTime);
file << strprintf("%s %s # zaddr=%s\n", CZCSpendingKey(key).ToString(), strTime, CZCPaymentAddress(addr).ToString());
file << strprintf("%s %s # zaddr=%s\n", CZCSpendingKey(key).ToString(), strTime, EncodePaymentAddress(addr));
}
}
file << "\n";
@@ -760,8 +760,11 @@ UniValue z_exportkey(const UniValue& params, bool fHelp)
string strAddress = params[0].get_str();
CZCPaymentAddress address(strAddress);
auto addr = address.Get();
auto address = DecodePaymentAddress(strAddress);
if (!address) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr");
}
auto addr = *address;
libzcash::SpendingKey k;
if (!pwalletMain->GetSpendingKey(addr, k))
@@ -796,8 +799,11 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp)
string strAddress = params[0].get_str();
CZCPaymentAddress address(strAddress);
auto addr = address.Get();
auto address = DecodePaymentAddress(strAddress);
if (!address) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr");
}
auto addr = *address;
libzcash::ViewingKey vk;
if (!pwalletMain->GetViewingKey(addr, vk)) {

View File

@@ -2521,14 +2521,14 @@ UniValue z_listunspent(const UniValue& params, bool fHelp)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected string");
}
string address = o.get_str();
try {
CZCPaymentAddress zaddr(address);
libzcash::PaymentAddress addr = zaddr.Get();
auto zaddr = DecodePaymentAddress(address);
if (zaddr) {
libzcash::PaymentAddress addr = *zaddr;
if (!fIncludeWatchonly && !pwalletMain->HaveSpendingKey(addr)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, spending key for address does not belong to wallet: ") + address);
}
zaddrs.insert(addr);
} catch (const std::runtime_error&) {
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, address is not a valid zaddr: ") + address);
}
@@ -2555,7 +2555,7 @@ UniValue z_listunspent(const UniValue& params, bool fHelp)
obj.push_back(Pair("jsoutindex", (int)entry.jsop.n));
obj.push_back(Pair("confirmations", entry.nHeight));
obj.push_back(Pair("spendable", pwalletMain->HaveSpendingKey(entry.address)));
obj.push_back(Pair("address", CZCPaymentAddress(entry.address).ToString()));
obj.push_back(Pair("address", EncodePaymentAddress(entry.address)));
obj.push_back(Pair("amount", ValueFromAmount(CAmount(entry.plaintext.value()))));
std::string data(entry.plaintext.memo().begin(), entry.plaintext.memo().end());
obj.push_back(Pair("memo", HexStr(data)));
@@ -2945,11 +2945,13 @@ UniValue zc_raw_joinsplit(const UniValue& params, bool fHelp)
}
for (const string& name_ : outputs.getKeys()) {
CZCPaymentAddress pubaddr(name_);
PaymentAddress addrTo = pubaddr.Get();
auto addrTo = DecodePaymentAddress(name_);
if (!addrTo) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid recipient address.");
}
CAmount nAmount = AmountFromValue(outputs[name_]);
vjsout.push_back(JSOutput(addrTo, nAmount));
vjsout.push_back(JSOutput(*addrTo, nAmount));
}
while (vjsout.size() < ZC_NUM_JS_OUTPUTS) {
@@ -3059,12 +3061,11 @@ UniValue zc_raw_keygen(const UniValue& params, bool fHelp)
auto addr = k.address();
auto viewing_key = k.viewing_key();
CZCPaymentAddress pubaddr(addr);
CZCSpendingKey spendingkey(k);
CZCViewingKey viewingkey(viewing_key);
UniValue result(UniValue::VOBJ);
result.push_back(Pair("zcaddress", pubaddr.ToString()));
result.push_back(Pair("zcaddress", EncodePaymentAddress(addr)));
result.push_back(Pair("zcsecretkey", spendingkey.ToString()));
result.push_back(Pair("zcviewingkey", viewingkey.ToString()));
return result;
@@ -3092,9 +3093,8 @@ UniValue z_getnewaddress(const UniValue& params, bool fHelp)
EnsureWalletIsUnlocked();
CZCPaymentAddress pubaddr = pwalletMain->GenerateNewZKey();
std::string result = pubaddr.ToString();
return result;
auto zaddr = pwalletMain->GenerateNewZKey();
return EncodePaymentAddress(zaddr);
}
@@ -3131,7 +3131,7 @@ UniValue z_listaddresses(const UniValue& params, bool fHelp)
pwalletMain->GetPaymentAddresses(addresses);
for (auto addr : addresses ) {
if (fIncludeWatchonly || pwalletMain->HaveSpendingKey(addr)) {
ret.push_back(CZCPaymentAddress(addr).ToString());
ret.push_back(EncodePaymentAddress(addr));
}
}
return ret;
@@ -3228,15 +3228,12 @@ UniValue z_listreceivedbyaddress(const UniValue& params, bool fHelp)
// Check that the from address is valid.
auto fromaddress = params[0].get_str();
libzcash::PaymentAddress zaddr;
CZCPaymentAddress address(fromaddress);
try {
zaddr = address.Get();
} catch (const std::runtime_error&) {
auto zaddr = DecodePaymentAddress(fromaddress);
if (!zaddr) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr.");
}
if (!(pwalletMain->HaveSpendingKey(zaddr) || pwalletMain->HaveViewingKey(zaddr))) {
if (!(pwalletMain->HaveSpendingKey(*zaddr) || pwalletMain->HaveViewingKey(*zaddr))) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "From address does not belong to this node, zaddr spending key or viewing key not found.");
}
@@ -3301,12 +3298,11 @@ UniValue z_getbalance(const UniValue& params, bool fHelp)
fromTaddr = IsValidDestination(taddr);
libzcash::PaymentAddress zaddr;
if (!fromTaddr) {
CZCPaymentAddress address(fromaddress);
try {
zaddr = address.Get();
} catch (const std::runtime_error&) {
auto res = DecodePaymentAddress(fromaddress);
if (!res) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid from address, should be a taddr or zaddr.");
}
zaddr = *res;
if (!(pwalletMain->HaveSpendingKey(zaddr) || pwalletMain->HaveViewingKey(zaddr))) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "From address does not belong to this node, zaddr spending key or viewing key not found.");
}
@@ -3537,13 +3533,12 @@ UniValue z_sendmany(const UniValue& params, bool fHelp)
fromTaddr = IsValidDestination(taddr);
libzcash::PaymentAddress zaddr;
if (!fromTaddr) {
CZCPaymentAddress address(fromaddress);
try {
zaddr = address.Get();
} catch (const std::runtime_error&) {
auto res = DecodePaymentAddress(fromaddress);
if (!res) {
// invalid
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid from address, should be a taddr or zaddr.");
}
zaddr = *res;
}
// Check that we have the spending key
@@ -3581,11 +3576,9 @@ UniValue z_sendmany(const UniValue& params, bool fHelp)
bool isZaddr = false;
CTxDestination taddr = DecodeDestination(address);
if (!IsValidDestination(taddr)) {
try {
CZCPaymentAddress zaddr(address);
zaddr.Get();
if (DecodePaymentAddress(address)) {
isZaddr = true;
} catch (const std::runtime_error&) {
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, unknown address format: ")+address );
}
}
@@ -3777,10 +3770,7 @@ UniValue z_shieldcoinbase(const UniValue& params, bool fHelp)
// Validate the destination address
auto destaddress = params[1].get_str();
try {
CZCPaymentAddress pa(destaddress);
libzcash::PaymentAddress zaddr = pa.Get();
} catch (const std::runtime_error&) {
if (!DecodePaymentAddress(destaddress)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, unknown address format: ") + destaddress );
}
@@ -4021,13 +4011,13 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp)
taddrs.insert(taddr);
}
} else {
try {
CZCPaymentAddress zaddr(address);
auto zaddr = DecodePaymentAddress(address);
if (zaddr) {
// Ignore listed z-addrs if we are using all of them
if (!(useAny || useAnyNote)) {
zaddrs.insert(zaddr.Get());
zaddrs.insert(*zaddr);
}
} catch (const std::runtime_error&) {
} else {
throw JSONRPCError(
RPC_INVALID_PARAMETER,
string("Invalid parameter, unknown address format: ") + address);
@@ -4045,11 +4035,9 @@ UniValue z_mergetoaddress(const UniValue& params, bool fHelp)
bool isToZaddr = false;
CTxDestination taddr = DecodeDestination(destaddress);
if (!IsValidDestination(taddr)) {
try {
CZCPaymentAddress zaddr(destaddress);
zaddr.Get();
if (DecodePaymentAddress(destaddress)) {
isToZaddr = true;
} catch (const std::runtime_error&) {
} else {
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, unknown address format: ") + destaddress );
}
}

View File

@@ -80,7 +80,7 @@ const CWalletTx* CWallet::GetWalletTx(const uint256& hash) const
}
// Generate a new spending key and return its public payment address
CZCPaymentAddress CWallet::GenerateNewZKey()
libzcash::PaymentAddress CWallet::GenerateNewZKey()
{
AssertLockHeld(cs_wallet); // mapZKeyMetadata
auto k = SpendingKey::random();
@@ -94,10 +94,9 @@ CZCPaymentAddress CWallet::GenerateNewZKey()
int64_t nCreationTime = GetTime();
mapZKeyMetadata[addr] = CKeyMetadata(nCreationTime);
CZCPaymentAddress pubaddr(addr);
if (!AddZKey(k))
throw std::runtime_error("CWallet::GenerateNewZKey(): AddZKey failed");
return pubaddr;
return addr;
}
// Add spending key to keystore and persist to disk
@@ -3718,7 +3717,7 @@ void CWallet::GetFilteredNotes(std::vector<CSproutNotePlaintextEntry> & outEntri
std::set<PaymentAddress> filterAddresses;
if (address.length() > 0) {
filterAddresses.insert(CZCPaymentAddress(address).Get());
filterAddresses.insert(*DecodePaymentAddress(address));
}
GetFilteredNotes(outEntries, filterAddresses, minDepth, ignoreSpent, ignoreUnspendable);
@@ -3781,7 +3780,7 @@ void CWallet::GetFilteredNotes(
ZCNoteDecryption decryptor;
if (!GetNoteDecryptor(pa, decryptor)) {
// Note decryptors are created when the wallet is loaded, so it should always exist
throw std::runtime_error(strprintf("Could not find note decryptor for payment address %s", CZCPaymentAddress(pa).ToString()));
throw std::runtime_error(strprintf("Could not find note decryptor for payment address %s", EncodePaymentAddress(pa)));
}
// determine amount of funds in the note
@@ -3798,10 +3797,10 @@ void CWallet::GetFilteredNotes(
} catch (const note_decryption_failed &err) {
// Couldn't decrypt with this spending key
throw std::runtime_error(strprintf("Could not decrypt note for payment address %s", CZCPaymentAddress(pa).ToString()));
throw std::runtime_error(strprintf("Could not decrypt note for payment address %s", EncodePaymentAddress(pa)));
} catch (const std::exception &exc) {
// Unexpected failure
throw std::runtime_error(strprintf("Error while decrypting note for payment address %s: %s", CZCPaymentAddress(pa).ToString(), exc.what()));
throw std::runtime_error(strprintf("Error while decrypting note for payment address %s: %s", EncodePaymentAddress(pa), exc.what()));
}
}
}
@@ -3857,7 +3856,7 @@ void CWallet::GetUnspentFilteredNotes(
ZCNoteDecryption decryptor;
if (!GetNoteDecryptor(pa, decryptor)) {
// Note decryptors are created when the wallet is loaded, so it should always exist
throw std::runtime_error(strprintf("Could not find note decryptor for payment address %s", CZCPaymentAddress(pa).ToString()));
throw std::runtime_error(strprintf("Could not find note decryptor for payment address %s", EncodePaymentAddress(pa)));
}
// determine amount of funds in the note
@@ -3874,10 +3873,10 @@ void CWallet::GetUnspentFilteredNotes(
} catch (const note_decryption_failed &err) {
// Couldn't decrypt with this spending key
throw std::runtime_error(strprintf("Could not decrypt note for payment address %s", CZCPaymentAddress(pa).ToString()));
throw std::runtime_error(strprintf("Could not decrypt note for payment address %s", EncodePaymentAddress(pa)));
} catch (const std::exception &exc) {
// Unexpected failure
throw std::runtime_error(strprintf("Error while decrypting note for payment address %s: %s", CZCPaymentAddress(pa).ToString(), exc.what()));
throw std::runtime_error(strprintf("Error while decrypting note for payment address %s: %s", EncodePaymentAddress(pa), exc.what()));
}
}
}

View File

@@ -960,7 +960,7 @@ public:
* ZKeys
*/
//! Generates a new zaddr
CZCPaymentAddress GenerateNewZKey();
libzcash::PaymentAddress GenerateNewZKey();
//! Adds spending key to the store, and saves it to disk
bool AddZKey(const libzcash::SpendingKey &key);
//! Adds spending key to the store, without saving it to disk (used by LoadWallet)