Introduce wrappers around CZCSpendingKey
This commit is contained in:
parent
80ed13d545
commit
472f75bc2d
|
@ -378,3 +378,18 @@ boost::optional<libzcash::PaymentAddress> DecodePaymentAddress(const std::string
|
||||||
return boost::none;
|
return boost::none;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::string EncodeSpendingKey(const libzcash::SpendingKey& zkey)
|
||||||
|
{
|
||||||
|
return CZCSpendingKey(zkey).ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
boost::optional<libzcash::SpendingKey> DecodeSpendingKey(const std::string& str)
|
||||||
|
{
|
||||||
|
CZCSpendingKey key(str);
|
||||||
|
try {
|
||||||
|
return key.Get();
|
||||||
|
} catch (const std::runtime_error&) {
|
||||||
|
return boost::none;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -182,4 +182,7 @@ bool IsValidDestinationString(const std::string& str, const CChainParams& params
|
||||||
std::string EncodePaymentAddress(const libzcash::PaymentAddress& zaddr);
|
std::string EncodePaymentAddress(const libzcash::PaymentAddress& zaddr);
|
||||||
boost::optional<libzcash::PaymentAddress> DecodePaymentAddress(const std::string& str);
|
boost::optional<libzcash::PaymentAddress> DecodePaymentAddress(const std::string& str);
|
||||||
|
|
||||||
|
std::string EncodeSpendingKey(const libzcash::SpendingKey& zkey);
|
||||||
|
boost::optional<libzcash::SpendingKey> DecodeSpendingKey(const std::string& str);
|
||||||
|
|
||||||
#endif // BITCOIN_BASE58_H
|
#endif // BITCOIN_BASE58_H
|
||||||
|
|
|
@ -190,14 +190,14 @@ BOOST_AUTO_TEST_CASE(zc_address_test)
|
||||||
for (size_t i = 0; i < 1000; i++) {
|
for (size_t i = 0; i < 1000; i++) {
|
||||||
auto sk = SpendingKey::random();
|
auto sk = SpendingKey::random();
|
||||||
{
|
{
|
||||||
CZCSpendingKey spendingkey(sk);
|
string sk_string = EncodeSpendingKey(sk);
|
||||||
string sk_string = spendingkey.ToString();
|
|
||||||
|
|
||||||
BOOST_CHECK(sk_string[0] == 'S');
|
BOOST_CHECK(sk_string[0] == 'S');
|
||||||
BOOST_CHECK(sk_string[1] == 'K');
|
BOOST_CHECK(sk_string[1] == 'K');
|
||||||
|
|
||||||
CZCSpendingKey spendingkey2(sk_string);
|
auto spendingkey2 = DecodeSpendingKey(sk_string);
|
||||||
SpendingKey sk2 = spendingkey2.Get();
|
BOOST_ASSERT(static_cast<bool>(spendingkey2));
|
||||||
|
SpendingKey sk2 = *spendingkey2;
|
||||||
BOOST_CHECK(sk.inner() == sk2.inner());
|
BOOST_CHECK(sk.inner() == sk2.inner());
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
|
|
|
@ -415,7 +415,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_exportwallet)
|
||||||
BOOST_CHECK(pwalletMain->GetSpendingKey(addr, key));
|
BOOST_CHECK(pwalletMain->GetSpendingKey(addr, key));
|
||||||
|
|
||||||
std::string s1 = EncodePaymentAddress(addr);
|
std::string s1 = EncodePaymentAddress(addr);
|
||||||
std::string s2 = CZCSpendingKey(key).ToString();
|
std::string s2 = EncodeSpendingKey(key);
|
||||||
|
|
||||||
// There's no way to really delete a private key so we will read in the
|
// There's no way to really delete a private key so we will read in the
|
||||||
// exported wallet file and search for the spending key and payment address.
|
// exported wallet file and search for the spending key and payment address.
|
||||||
|
@ -459,7 +459,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
|
||||||
auto testSpendingKey = libzcash::SpendingKey::random();
|
auto testSpendingKey = libzcash::SpendingKey::random();
|
||||||
auto testPaymentAddress = testSpendingKey.address();
|
auto testPaymentAddress = testSpendingKey.address();
|
||||||
std::string testAddr = EncodePaymentAddress(testPaymentAddress);
|
std::string testAddr = EncodePaymentAddress(testPaymentAddress);
|
||||||
std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
|
std::string testKey = EncodeSpendingKey(testSpendingKey);
|
||||||
|
|
||||||
// create test data using the random key
|
// create test data using the random key
|
||||||
std::string format_str = "# Wallet dump created by Zcash v0.11.2.0.z8-9155cc6-dirty (2016-08-11 11:37:00 -0700)\n"
|
std::string format_str = "# Wallet dump created by Zcash v0.11.2.0.z8-9155cc6-dirty (2016-08-11 11:37:00 -0700)\n"
|
||||||
|
@ -503,8 +503,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importwallet)
|
||||||
// Verify the spending key is the same as the test data
|
// Verify the spending key is the same as the test data
|
||||||
libzcash::SpendingKey k;
|
libzcash::SpendingKey k;
|
||||||
BOOST_CHECK(pwalletMain->GetSpendingKey(addr, k));
|
BOOST_CHECK(pwalletMain->GetSpendingKey(addr, k));
|
||||||
CZCSpendingKey spendingkey(k);
|
BOOST_CHECK_EQUAL(testKey, EncodeSpendingKey(k));
|
||||||
BOOST_CHECK_EQUAL(testKey, spendingkey.ToString());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -528,7 +527,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
|
||||||
|
|
||||||
// error if invalid args
|
// error if invalid args
|
||||||
auto sk = libzcash::SpendingKey::random();
|
auto sk = libzcash::SpendingKey::random();
|
||||||
std::string prefix = std::string("z_importkey ") + CZCSpendingKey(sk).ToString() + " yes ";
|
std::string prefix = std::string("z_importkey ") + EncodeSpendingKey(sk) + " yes ";
|
||||||
BOOST_CHECK_THROW(CallRPC(prefix + "-1"), runtime_error);
|
BOOST_CHECK_THROW(CallRPC(prefix + "-1"), runtime_error);
|
||||||
BOOST_CHECK_THROW(CallRPC(prefix + "2147483647"), runtime_error); // allowed, but > height of active chain tip
|
BOOST_CHECK_THROW(CallRPC(prefix + "2147483647"), runtime_error); // allowed, but > height of active chain tip
|
||||||
BOOST_CHECK_THROW(CallRPC(prefix + "2147483648"), runtime_error); // not allowed, > int32 used for nHeight
|
BOOST_CHECK_THROW(CallRPC(prefix + "2147483648"), runtime_error); // not allowed, > int32 used for nHeight
|
||||||
|
@ -545,7 +544,7 @@ BOOST_AUTO_TEST_CASE(rpc_wallet_z_importexport)
|
||||||
auto testSpendingKey = libzcash::SpendingKey::random();
|
auto testSpendingKey = libzcash::SpendingKey::random();
|
||||||
auto testPaymentAddress = testSpendingKey.address();
|
auto testPaymentAddress = testSpendingKey.address();
|
||||||
std::string testAddr = EncodePaymentAddress(testPaymentAddress);
|
std::string testAddr = EncodePaymentAddress(testPaymentAddress);
|
||||||
std::string testKey = CZCSpendingKey(testSpendingKey).ToString();
|
std::string testKey = EncodeSpendingKey(testSpendingKey);
|
||||||
BOOST_CHECK_NO_THROW(CallRPC(string("z_importkey ") + testKey));
|
BOOST_CHECK_NO_THROW(CallRPC(string("z_importkey ") + testKey));
|
||||||
BOOST_CHECK_NO_THROW(retValue = CallRPC(string("z_exportkey ") + testAddr));
|
BOOST_CHECK_NO_THROW(retValue = CallRPC(string("z_exportkey ") + testAddr));
|
||||||
BOOST_CHECK_EQUAL(retValue.get_str(), testKey);
|
BOOST_CHECK_EQUAL(retValue.get_str(), testKey);
|
||||||
|
|
|
@ -295,9 +295,9 @@ UniValue importwallet_impl(const UniValue& params, bool fHelp, bool fImportZKeys
|
||||||
|
|
||||||
// Let's see if the address is a valid Zcash spending key
|
// Let's see if the address is a valid Zcash spending key
|
||||||
if (fImportZKeys) {
|
if (fImportZKeys) {
|
||||||
try {
|
auto spendingkey = DecodeSpendingKey(vstr[0]);
|
||||||
CZCSpendingKey spendingkey(vstr[0]);
|
if (spendingkey) {
|
||||||
libzcash::SpendingKey key = spendingkey.Get();
|
libzcash::SpendingKey key = *spendingkey;
|
||||||
libzcash::PaymentAddress addr = key.address();
|
libzcash::PaymentAddress addr = key.address();
|
||||||
if (pwalletMain->HaveSpendingKey(addr)) {
|
if (pwalletMain->HaveSpendingKey(addr)) {
|
||||||
LogPrint("zrpc", "Skipping import of zaddr %s (key already present)\n", EncodePaymentAddress(addr));
|
LogPrint("zrpc", "Skipping import of zaddr %s (key already present)\n", EncodePaymentAddress(addr));
|
||||||
|
@ -313,9 +313,8 @@ UniValue importwallet_impl(const UniValue& params, bool fHelp, bool fImportZKeys
|
||||||
// Successfully imported zaddr. Now import the metadata.
|
// Successfully imported zaddr. Now import the metadata.
|
||||||
pwalletMain->mapZKeyMetadata[addr].nCreateTime = nTime;
|
pwalletMain->mapZKeyMetadata[addr].nCreateTime = nTime;
|
||||||
continue;
|
continue;
|
||||||
}
|
} else {
|
||||||
catch (const std::runtime_error &e) {
|
LogPrint("zrpc", "Importing detected an error: invalid spending key. Trying as a transparent key...\n");
|
||||||
LogPrint("zrpc","Importing detected an error: %s\n", e.what());
|
|
||||||
// Not a valid spending key, so carry on and see if it's a Zcash style address.
|
// Not a valid spending key, so carry on and see if it's a Zcash style address.
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -536,7 +535,7 @@ UniValue dumpwallet_impl(const UniValue& params, bool fHelp, bool fDumpZKeys)
|
||||||
libzcash::SpendingKey key;
|
libzcash::SpendingKey key;
|
||||||
if (pwalletMain->GetSpendingKey(addr, key)) {
|
if (pwalletMain->GetSpendingKey(addr, key)) {
|
||||||
std::string strTime = EncodeDumpTime(pwalletMain->mapZKeyMetadata[addr].nCreateTime);
|
std::string strTime = EncodeDumpTime(pwalletMain->mapZKeyMetadata[addr].nCreateTime);
|
||||||
file << strprintf("%s %s # zaddr=%s\n", CZCSpendingKey(key).ToString(), strTime, EncodePaymentAddress(addr));
|
file << strprintf("%s %s # zaddr=%s\n", EncodeSpendingKey(key), strTime, EncodePaymentAddress(addr));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
file << "\n";
|
file << "\n";
|
||||||
|
@ -614,8 +613,11 @@ UniValue z_importkey(const UniValue& params, bool fHelp)
|
||||||
}
|
}
|
||||||
|
|
||||||
string strSecret = params[0].get_str();
|
string strSecret = params[0].get_str();
|
||||||
CZCSpendingKey spendingkey(strSecret);
|
auto spendingkey = DecodeSpendingKey(strSecret);
|
||||||
auto key = spendingkey.Get();
|
if (!spendingkey) {
|
||||||
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key");
|
||||||
|
}
|
||||||
|
auto key = *spendingkey;
|
||||||
auto addr = key.address();
|
auto addr = key.address();
|
||||||
|
|
||||||
{
|
{
|
||||||
|
@ -770,8 +772,7 @@ UniValue z_exportkey(const UniValue& params, bool fHelp)
|
||||||
if (!pwalletMain->GetSpendingKey(addr, k))
|
if (!pwalletMain->GetSpendingKey(addr, k))
|
||||||
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr");
|
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr");
|
||||||
|
|
||||||
CZCSpendingKey spendingkey(k);
|
return EncodeSpendingKey(k);
|
||||||
return spendingkey.ToString();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
UniValue z_exportviewingkey(const UniValue& params, bool fHelp)
|
UniValue z_exportviewingkey(const UniValue& params, bool fHelp)
|
||||||
|
|
|
@ -2792,8 +2792,11 @@ UniValue zc_raw_receive(const UniValue& params, bool fHelp)
|
||||||
|
|
||||||
LOCK(cs_main);
|
LOCK(cs_main);
|
||||||
|
|
||||||
CZCSpendingKey spendingkey(params[0].get_str());
|
auto spendingkey = DecodeSpendingKey(params[0].get_str());
|
||||||
SpendingKey k = spendingkey.Get();
|
if (!spendingkey) {
|
||||||
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key");
|
||||||
|
}
|
||||||
|
SpendingKey k = *spendingkey;
|
||||||
|
|
||||||
uint256 epk;
|
uint256 epk;
|
||||||
unsigned char nonce;
|
unsigned char nonce;
|
||||||
|
@ -2903,8 +2906,11 @@ UniValue zc_raw_joinsplit(const UniValue& params, bool fHelp)
|
||||||
std::vector<uint256> commitments;
|
std::vector<uint256> commitments;
|
||||||
|
|
||||||
for (const string& name_ : inputs.getKeys()) {
|
for (const string& name_ : inputs.getKeys()) {
|
||||||
CZCSpendingKey spendingkey(inputs[name_].get_str());
|
auto spendingkey = DecodeSpendingKey(inputs[name_].get_str());
|
||||||
SpendingKey k = spendingkey.Get();
|
if (!spendingkey) {
|
||||||
|
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid spending key");
|
||||||
|
}
|
||||||
|
SpendingKey k = *spendingkey;
|
||||||
|
|
||||||
keys.push_back(k);
|
keys.push_back(k);
|
||||||
|
|
||||||
|
@ -3061,12 +3067,11 @@ UniValue zc_raw_keygen(const UniValue& params, bool fHelp)
|
||||||
auto addr = k.address();
|
auto addr = k.address();
|
||||||
auto viewing_key = k.viewing_key();
|
auto viewing_key = k.viewing_key();
|
||||||
|
|
||||||
CZCSpendingKey spendingkey(k);
|
|
||||||
CZCViewingKey viewingkey(viewing_key);
|
CZCViewingKey viewingkey(viewing_key);
|
||||||
|
|
||||||
UniValue result(UniValue::VOBJ);
|
UniValue result(UniValue::VOBJ);
|
||||||
result.push_back(Pair("zcaddress", EncodePaymentAddress(addr)));
|
result.push_back(Pair("zcaddress", EncodePaymentAddress(addr)));
|
||||||
result.push_back(Pair("zcsecretkey", spendingkey.ToString()));
|
result.push_back(Pair("zcsecretkey", EncodeSpendingKey(k)));
|
||||||
result.push_back(Pair("zcviewingkey", viewingkey.ToString()));
|
result.push_back(Pair("zcviewingkey", viewingkey.ToString()));
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue