Skip to content

Commit 25b2513

Browse files
random-zebraFuzzbawls
authored andcommitted
[RPC] Redirect sendtoaddress to shieldedsendmany when shielded recipient
Github-Pull: #2050 Rebased-From: 46fe147
1 parent 8c2c328 commit 25b2513

File tree

1 file changed

+49
-9
lines changed

1 file changed

+49
-9
lines changed

src/wallet/rpcwallet.cpp

Lines changed: 49 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ void SendMoney(const CTxDestination& address, CAmount nValue, CWalletTx& wtxNew)
938938
throw JSONRPCError(RPC_WALLET_ERROR, res.ToString());
939939
}
940940

941+
static SaplingOperation CreateShieldedTransaction(const JSONRPCRequest& request);
942+
941943
UniValue sendtoaddress(const JSONRPCRequest& request)
942944
{
943945
if (request.fHelp || request.params.size() < 2 || request.params.size() > 4)
@@ -963,22 +965,60 @@ UniValue sendtoaddress(const JSONRPCRequest& request)
963965
HelpExampleCli("sendtoaddress", "\"DMJRSsuU9zfyrvxVaAEFQqK4MxZg6vgeS6\" 0.1 \"donation\" \"seans outpost\"") +
964966
HelpExampleRpc("sendtoaddress", "\"DMJRSsuU9zfyrvxVaAEFQqK4MxZg6vgeS6\", 0.1, \"donation\", \"seans outpost\""));
965967

966-
LOCK2(cs_main, pwalletMain->cs_wallet);
967-
968-
bool isStaking = false;
969-
CTxDestination address = DecodeDestination(request.params[0].get_str(), isStaking);
970-
if (!IsValidDestination(address) || isStaking)
968+
bool isStaking = false, isShielded = false;
969+
const std::string addrStr = request.params[0].get_str();
970+
const CWDestination& destination = Standard::DecodeDestination(addrStr, isStaking, isShielded);
971+
if (!Standard::IsValidDestination(destination) || isStaking)
971972
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address");
973+
const std::string commentStr = (request.params.size() > 2 && !request.params[2].isNull()) ?
974+
request.params[2].get_str() : "";
975+
const std::string toStr = (request.params.size() > 3 && !request.params[3].isNull()) ?
976+
request.params[3].get_str() : "";
977+
978+
if (isShielded) {
979+
// convert params to 'shieldedsendmany' format
980+
JSONRPCRequest req;
981+
req.params = UniValue(UniValue::VARR);
982+
req.params.push_back(UniValue("from_transparent"));
983+
UniValue recipients(UniValue::VARR);
984+
UniValue recipient(UniValue::VOBJ);
985+
recipient.pushKV("address", addrStr);
986+
recipient.pushKV("amount", request.params[1]);
987+
recipients.push_back(recipient);
988+
req.params.push_back(recipients);
989+
990+
// send
991+
SaplingOperation operation = CreateShieldedTransaction(req);
992+
std::string txid;
993+
auto res = operation.send(txid);
994+
if (!res)
995+
throw JSONRPCError(RPC_WALLET_ERROR, res.getError());
996+
997+
// add comment
998+
const uint256 txHash(txid);
999+
assert(pwalletMain->mapWallet.count(txHash));
1000+
if (!commentStr.empty()) {
1001+
pwalletMain->mapWallet[txHash].mapValue["comment"] = commentStr;
1002+
}
1003+
if (!toStr.empty()) {
1004+
pwalletMain->mapWallet[txHash].mapValue["to"] = toStr;
1005+
}
1006+
1007+
return txid;
1008+
}
1009+
1010+
const CTxDestination& address = *Standard::GetTransparentDestination(destination);
1011+
LOCK2(cs_main, pwalletMain->cs_wallet);
9721012

9731013
// Amount
9741014
CAmount nAmount = AmountFromValue(request.params[1]);
9751015

9761016
// Wallet comments
9771017
CWalletTx wtx;
978-
if (request.params.size() > 2 && !request.params[2].isNull() && !request.params[2].get_str().empty())
979-
wtx.mapValue["comment"] = request.params[2].get_str();
980-
if (request.params.size() > 3 && !request.params[3].isNull() && !request.params[3].get_str().empty())
981-
wtx.mapValue["to"] = request.params[3].get_str();
1018+
if (!commentStr.empty())
1019+
wtx.mapValue["comment"] = commentStr;
1020+
if (!toStr.empty())
1021+
wtx.mapValue["to"] = toStr;
9821022

9831023
EnsureWalletIsUnlocked();
9841024

0 commit comments

Comments
 (0)