Auto merge of #3242 - str4d:3058-key-encoding-refactor, r=str4d
Key encoding refactor Includes code cherry-picked from the following upstream PRs: - bitcoin/bitcoin#11372 - Only the first three commits (the fourth commit depends on #2390) Part of #3058.
This commit is contained in:
@@ -8,6 +8,7 @@
|
||||
#include "asyncrpcqueue.h"
|
||||
#include "core_io.h"
|
||||
#include "init.h"
|
||||
#include "key_io.h"
|
||||
#include "main.h"
|
||||
#include "miner.h"
|
||||
#include "net.h"
|
||||
@@ -78,14 +79,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 +856,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
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include "amount.h"
|
||||
#include "asyncrpcoperation.h"
|
||||
#include "base58.h"
|
||||
#include "paymentdisclosure.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "wallet.h"
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
#include "consensus/upgrades.h"
|
||||
#include "core_io.h"
|
||||
#include "init.h"
|
||||
#include "key_io.h"
|
||||
#include "main.h"
|
||||
#include "net.h"
|
||||
#include "netbase.h"
|
||||
@@ -79,9 +80,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 +93,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 +468,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 +727,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 +1081,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
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include "asyncrpcoperation.h"
|
||||
#include "amount.h"
|
||||
#include "base58.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "zcash/JoinSplit.hpp"
|
||||
#include "zcash/Address.hpp"
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "consensus/upgrades.h"
|
||||
#include "core_io.h"
|
||||
#include "init.h"
|
||||
#include "key_io.h"
|
||||
#include "main.h"
|
||||
#include "net.h"
|
||||
#include "netbase.h"
|
||||
@@ -71,11 +72,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 +452,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
|
||||
|
||||
|
||||
@@ -7,7 +7,6 @@
|
||||
|
||||
#include "asyncrpcoperation.h"
|
||||
#include "amount.h"
|
||||
#include "base58.h"
|
||||
#include "primitives/transaction.h"
|
||||
#include "zcash/JoinSplit.hpp"
|
||||
#include "zcash/Address.hpp"
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
@@ -2,9 +2,9 @@
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "base58.h"
|
||||
#include "rpcserver.h"
|
||||
#include "init.h"
|
||||
#include "key_io.h"
|
||||
#include "main.h"
|
||||
#include "script/script.h"
|
||||
#include "script/standard.h"
|
||||
@@ -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
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
// Distributed under the MIT software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "base58.h"
|
||||
#include "key_io.h"
|
||||
#include "rpcserver.h"
|
||||
#include "init.h"
|
||||
#include "main.h"
|
||||
@@ -112,13 +112,8 @@ UniValue importprivkey(const UniValue& params, bool fHelp)
|
||||
if (params.size() > 2)
|
||||
fRescan = params[2].get_bool();
|
||||
|
||||
CBitcoinSecret vchSecret;
|
||||
bool fGood = vchSecret.SetString(strSecret);
|
||||
|
||||
if (!fGood) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
|
||||
|
||||
CKey key = vchSecret.GetKey();
|
||||
if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Private key outside allowed range");
|
||||
CKey key = DecodeSecret(strSecret);
|
||||
if (!key.IsValid()) throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key encoding");
|
||||
|
||||
CPubKey pubkey = key.GetPubKey();
|
||||
assert(key.VerifyPubKey(pubkey));
|
||||
@@ -300,16 +295,16 @@ UniValue importwallet_impl(const UniValue& params, bool fHelp, bool fImportZKeys
|
||||
|
||||
// Let's see if the address is a valid Zcash spending key
|
||||
if (fImportZKeys) {
|
||||
try {
|
||||
CZCSpendingKey spendingkey(vstr[0]);
|
||||
libzcash::SpendingKey key = spendingkey.Get();
|
||||
auto spendingkey = DecodeSpendingKey(vstr[0]);
|
||||
if (spendingkey) {
|
||||
libzcash::SpendingKey key = *spendingkey;
|
||||
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;
|
||||
@@ -318,17 +313,15 @@ UniValue importwallet_impl(const UniValue& params, bool fHelp, bool fImportZKeys
|
||||
// Successfully imported zaddr. Now import the metadata.
|
||||
pwalletMain->mapZKeyMetadata[addr].nCreateTime = nTime;
|
||||
continue;
|
||||
}
|
||||
catch (const std::runtime_error &e) {
|
||||
LogPrint("zrpc","Importing detected an error: %s\n", e.what());
|
||||
} else {
|
||||
LogPrint("zrpc", "Importing detected an error: invalid spending key. Trying as a transparent key...\n");
|
||||
// Not a valid spending key, so carry on and see if it's a Zcash style address.
|
||||
}
|
||||
}
|
||||
|
||||
CBitcoinSecret vchSecret;
|
||||
if (!vchSecret.SetString(vstr[0]))
|
||||
CKey key = DecodeSecret(vstr[0]);
|
||||
if (!key.IsValid())
|
||||
continue;
|
||||
CKey key = vchSecret.GetKey();
|
||||
CPubKey pubkey = key.GetPubKey();
|
||||
assert(key.VerifyPubKey(pubkey));
|
||||
CKeyID keyid = pubkey.GetID();
|
||||
@@ -418,7 +411,7 @@ UniValue dumpprivkey(const UniValue& params, bool fHelp)
|
||||
if (!pwalletMain->GetKey(*keyID, vchSecret)) {
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Private key for address " + strAddress + " is not known");
|
||||
}
|
||||
return CBitcoinSecret(vchSecret).ToString();
|
||||
return EncodeSecret(vchSecret);
|
||||
}
|
||||
|
||||
|
||||
@@ -522,11 +515,11 @@ UniValue dumpwallet_impl(const UniValue& params, bool fHelp, bool fDumpZKeys)
|
||||
CKey key;
|
||||
if (pwalletMain->GetKey(keyid, key)) {
|
||||
if (pwalletMain->mapAddressBook.count(keyid)) {
|
||||
file << strprintf("%s %s label=%s # addr=%s\n", CBitcoinSecret(key).ToString(), strTime, EncodeDumpString(pwalletMain->mapAddressBook[keyid].name), strAddr);
|
||||
file << strprintf("%s %s label=%s # addr=%s\n", EncodeSecret(key), strTime, EncodeDumpString(pwalletMain->mapAddressBook[keyid].name), strAddr);
|
||||
} else if (setKeyPool.count(keyid)) {
|
||||
file << strprintf("%s %s reserve=1 # addr=%s\n", CBitcoinSecret(key).ToString(), strTime, strAddr);
|
||||
file << strprintf("%s %s reserve=1 # addr=%s\n", EncodeSecret(key), strTime, strAddr);
|
||||
} else {
|
||||
file << strprintf("%s %s change=1 # addr=%s\n", CBitcoinSecret(key).ToString(), strTime, strAddr);
|
||||
file << strprintf("%s %s change=1 # addr=%s\n", EncodeSecret(key), strTime, strAddr);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -542,7 +535,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", EncodeSpendingKey(key), strTime, EncodePaymentAddress(addr));
|
||||
}
|
||||
}
|
||||
file << "\n";
|
||||
@@ -620,8 +613,11 @@ UniValue z_importkey(const UniValue& params, bool fHelp)
|
||||
}
|
||||
|
||||
string strSecret = params[0].get_str();
|
||||
CZCSpendingKey spendingkey(strSecret);
|
||||
auto key = spendingkey.Get();
|
||||
auto spendingkey = DecodeSpendingKey(strSecret);
|
||||
if (!spendingkey) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key");
|
||||
}
|
||||
auto key = *spendingkey;
|
||||
auto addr = key.address();
|
||||
|
||||
{
|
||||
@@ -709,8 +705,11 @@ UniValue z_importviewingkey(const UniValue& params, bool fHelp)
|
||||
}
|
||||
|
||||
string strVKey = params[0].get_str();
|
||||
CZCViewingKey viewingkey(strVKey);
|
||||
auto vkey = viewingkey.Get();
|
||||
auto viewingkey = DecodeViewingKey(strVKey);
|
||||
if (!viewingkey) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid viewing key");
|
||||
}
|
||||
auto vkey = *viewingkey;
|
||||
auto addr = vkey.address();
|
||||
|
||||
{
|
||||
@@ -766,15 +765,17 @@ 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))
|
||||
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr");
|
||||
|
||||
CZCSpendingKey spendingkey(k);
|
||||
return spendingkey.ToString();
|
||||
return EncodeSpendingKey(k);
|
||||
}
|
||||
|
||||
UniValue z_exportviewingkey(const UniValue& params, bool fHelp)
|
||||
@@ -802,8 +803,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)) {
|
||||
@@ -814,6 +818,5 @@ UniValue z_exportviewingkey(const UniValue& params, bool fHelp)
|
||||
vk = k.viewing_key();
|
||||
}
|
||||
|
||||
CZCViewingKey viewingkey(vk);
|
||||
return viewingkey.ToString();
|
||||
return EncodeViewingKey(vk);
|
||||
}
|
||||
|
||||
@@ -4,10 +4,10 @@
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include "amount.h"
|
||||
#include "base58.h"
|
||||
#include "consensus/upgrades.h"
|
||||
#include "core_io.h"
|
||||
#include "init.h"
|
||||
#include "key_io.h"
|
||||
#include "main.h"
|
||||
#include "net.h"
|
||||
#include "netbase.h"
|
||||
@@ -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)));
|
||||
@@ -2792,8 +2792,11 @@ UniValue zc_raw_receive(const UniValue& params, bool fHelp)
|
||||
|
||||
LOCK(cs_main);
|
||||
|
||||
CZCSpendingKey spendingkey(params[0].get_str());
|
||||
SpendingKey k = spendingkey.Get();
|
||||
auto spendingkey = DecodeSpendingKey(params[0].get_str());
|
||||
if (!spendingkey) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key");
|
||||
}
|
||||
SpendingKey k = *spendingkey;
|
||||
|
||||
uint256 epk;
|
||||
unsigned char nonce;
|
||||
@@ -2903,8 +2906,11 @@ UniValue zc_raw_joinsplit(const UniValue& params, bool fHelp)
|
||||
std::vector<uint256> commitments;
|
||||
|
||||
for (const string& name_ : inputs.getKeys()) {
|
||||
CZCSpendingKey spendingkey(inputs[name_].get_str());
|
||||
SpendingKey k = spendingkey.Get();
|
||||
auto spendingkey = DecodeSpendingKey(inputs[name_].get_str());
|
||||
if (!spendingkey) {
|
||||
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key");
|
||||
}
|
||||
SpendingKey k = *spendingkey;
|
||||
|
||||
keys.push_back(k);
|
||||
|
||||
@@ -2945,11 +2951,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,14 +3067,10 @@ 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("zcsecretkey", spendingkey.ToString()));
|
||||
result.push_back(Pair("zcviewingkey", viewingkey.ToString()));
|
||||
result.push_back(Pair("zcaddress", EncodePaymentAddress(addr)));
|
||||
result.push_back(Pair("zcsecretkey", EncodeSpendingKey(k)));
|
||||
result.push_back(Pair("zcviewingkey", EncodeViewingKey(viewing_key)));
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -3092,9 +3096,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 +3134,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 +3231,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 +3301,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 +3536,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 +3579,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 +3773,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 +4014,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 +4038,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 );
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,13 +5,13 @@
|
||||
|
||||
#include "wallet/wallet.h"
|
||||
|
||||
#include "base58.h"
|
||||
#include "checkpoints.h"
|
||||
#include "coincontrol.h"
|
||||
#include "consensus/upgrades.h"
|
||||
#include "consensus/validation.h"
|
||||
#include "consensus/consensus.h"
|
||||
#include "init.h"
|
||||
#include "key_io.h"
|
||||
#include "main.h"
|
||||
#include "net.h"
|
||||
#include "script/script.h"
|
||||
@@ -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
|
||||
@@ -3716,7 +3715,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);
|
||||
@@ -3779,7 +3778,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
|
||||
@@ -3796,10 +3795,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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3855,7 +3854,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
|
||||
@@ -3872,10 +3871,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()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -5,8 +5,8 @@
|
||||
|
||||
#include "wallet/walletdb.h"
|
||||
|
||||
#include "base58.h"
|
||||
#include "consensus/validation.h"
|
||||
#include "key_io.h"
|
||||
#include "main.h"
|
||||
#include "protocol.h"
|
||||
#include "serialize.h"
|
||||
|
||||
Reference in New Issue
Block a user