Move GetSpendingKeyForPaymentAddress visitor into wallet.h

Also fixes it to not use the global pwalletMain.
This commit is contained in:
Jack Grigg 2018-08-04 12:06:05 +01:00
parent c53884d20a
commit e22c115e78
No known key found for this signature in database
GPG Key ID: 1B8D649257DB0829
3 changed files with 50 additions and 36 deletions

View File

@ -784,41 +784,6 @@ UniValue z_importviewingkey(const UniValue& params, bool fHelp)
return NullUniValue;
}
class GetSpendingKeyForPaymentAddress : public boost::static_visitor<libzcash::SpendingKey>
{
private:
CWallet *m_wallet;
public:
GetSpendingKeyForPaymentAddress(CWallet *wallet) : m_wallet(wallet) {}
libzcash::SpendingKey operator()(const libzcash::SproutPaymentAddress &zaddr) const
{
libzcash::SproutSpendingKey k;
if (!pwalletMain->GetSproutSpendingKey(zaddr, k)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr");
}
return k;
}
libzcash::SpendingKey operator()(const libzcash::SaplingPaymentAddress &zaddr) const
{
libzcash::SaplingIncomingViewingKey ivk;
libzcash::SaplingFullViewingKey fvk;
libzcash::SaplingSpendingKey sk;
if (!pwalletMain->GetSaplingIncomingViewingKey(zaddr, ivk) ||
!pwalletMain->GetSaplingFullViewingKey(ivk, fvk) ||
!pwalletMain->GetSaplingSpendingKey(fvk, sk)) {
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr");
}
return sk;
}
libzcash::SpendingKey operator()(const libzcash::InvalidEncoding& no) const {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid zaddr");
}
};
UniValue z_exportkey(const UniValue& params, bool fHelp)
{
if (!EnsureWalletIsAvailable(fHelp))
@ -852,7 +817,10 @@ UniValue z_exportkey(const UniValue& params, bool fHelp)
// Sapling support
auto sk = boost::apply_visitor(GetSpendingKeyForPaymentAddress(pwalletMain), address);
return EncodeSpendingKey(sk);
if (!sk) {
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet does not hold private zkey for this zaddr");
}
return EncodeSpendingKey(sk.get());
}
UniValue z_exportviewingkey(const UniValue& params, bool fHelp)

View File

@ -4373,3 +4373,37 @@ bool PaymentAddressBelongsToWallet::operator()(const libzcash::InvalidEncoding&
{
return false;
}
boost::optional<libzcash::SpendingKey> GetSpendingKeyForPaymentAddress::operator()(
const libzcash::SproutPaymentAddress &zaddr) const
{
libzcash::SproutSpendingKey k;
if (m_wallet->GetSproutSpendingKey(zaddr, k)) {
return libzcash::SpendingKey(k);
} else {
return boost::none;
}
}
boost::optional<libzcash::SpendingKey> GetSpendingKeyForPaymentAddress::operator()(
const libzcash::SaplingPaymentAddress &zaddr) const
{
libzcash::SaplingIncomingViewingKey ivk;
libzcash::SaplingFullViewingKey fvk;
libzcash::SaplingSpendingKey sk;
if (m_wallet->GetSaplingIncomingViewingKey(zaddr, ivk) &&
m_wallet->GetSaplingFullViewingKey(ivk, fvk) &&
m_wallet->GetSaplingSpendingKey(fvk, sk)) {
return libzcash::SpendingKey(sk);
} else {
return boost::none;
}
}
boost::optional<libzcash::SpendingKey> GetSpendingKeyForPaymentAddress::operator()(
const libzcash::InvalidEncoding& no) const
{
// Defaults to InvalidEncoding
return libzcash::SpendingKey();
}

View File

@ -1319,4 +1319,16 @@ public:
bool operator()(const libzcash::InvalidEncoding& no) const;
};
class GetSpendingKeyForPaymentAddress : public boost::static_visitor<boost::optional<libzcash::SpendingKey>>
{
private:
CWallet *m_wallet;
public:
GetSpendingKeyForPaymentAddress(CWallet *wallet) : m_wallet(wallet) {}
boost::optional<libzcash::SpendingKey> operator()(const libzcash::SproutPaymentAddress &zaddr) const;
boost::optional<libzcash::SpendingKey> operator()(const libzcash::SaplingPaymentAddress &zaddr) const;
boost::optional<libzcash::SpendingKey> operator()(const libzcash::InvalidEncoding& no) const;
};
#endif // BITCOIN_WALLET_WALLET_H