Skip to content

Commit 2f750e5

Browse files
random-zebrafurszy
authored andcommitted
[RPC] add 'createrawzerocoinpublicspend' method
1 parent b5e2526 commit 2f750e5

File tree

3 files changed

+66
-0
lines changed

3 files changed

+66
-0
lines changed

src/rpc/rawtransaction.cpp

+64
Original file line numberDiff line numberDiff line change
@@ -991,4 +991,68 @@ UniValue createrawzerocoinstake(const UniValue& params, bool fHelp)
991991
return ret;
992992

993993
}
994+
995+
UniValue createrawzerocoinpublicspend(const UniValue& params, bool fHelp)
996+
{
997+
if (fHelp || params.size() < 1 || params.size() > 2)
998+
throw runtime_error(
999+
"createrawzerocoinpublicspend mint_input \n"
1000+
"\nCreates raw zPIV public spend.\n" +
1001+
HelpRequiringPassphrase() + "\n"
1002+
1003+
"\nArguments:\n"
1004+
"1. mint_input (hex string, required) serial hash of the mint used as input\n"
1005+
"2. \"address\" (string, optional, default=change) Send to specified address or to a new change address.\n"
1006+
1007+
1008+
"\nResult:\n"
1009+
"{\n"
1010+
" \"hex\": \"xxx\", (hex string) raw public spend signed transaction\n"
1011+
"}\n"
1012+
"\nExamples\n" +
1013+
HelpExampleCli("createrawzerocoinpublicspend", "0d8c16eee7737e3cc1e4e70dc006634182b175e039700931283b202715a0818f") +
1014+
HelpExampleRpc("createrawzerocoinpublicspend", "0d8c16eee7737e3cc1e4e70dc006634182b175e039700931283b202715a0818f"));
1015+
1016+
1017+
assert(pwalletMain != NULL);
1018+
LOCK2(cs_main, pwalletMain->cs_wallet);
1019+
1020+
std::string serial_hash = params[0].get_str();
1021+
if (!IsHex(serial_hash))
1022+
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid parameter, expected hex serial hash");
1023+
1024+
std::string address_str = "";
1025+
CBitcoinAddress address;
1026+
CBitcoinAddress* addr_ptr = nullptr;
1027+
if (params.size() > 1) {
1028+
address_str = params[1].get_str();
1029+
address = CBitcoinAddress(address_str);
1030+
if(!address.IsValid())
1031+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid PIVX address");
1032+
addr_ptr = &address;
1033+
}
1034+
1035+
EnsureWalletIsUnlocked();
1036+
1037+
uint256 hashSerial(serial_hash);
1038+
CZerocoinMint input_mint;
1039+
if (!pwalletMain->GetMint(hashSerial, input_mint)) {
1040+
std::string strErr = "Failed to fetch mint associated with serial hash " + serial_hash;
1041+
throw JSONRPCError(RPC_WALLET_ERROR, strErr);
1042+
}
1043+
CAmount nAmount = input_mint.GetDenominationAsAmount();
1044+
vector<CZerocoinMint> vMintsSelected = vector<CZerocoinMint>(1,input_mint);
1045+
1046+
// create the spend
1047+
CWalletTx rawTx;
1048+
CZerocoinSpendReceipt receipt;
1049+
CReserveKey reserveKey(pwalletMain);
1050+
vector<CDeterministicMint> vNewMints;
1051+
if (!pwalletMain->CreateZerocoinSpendTransaction(nAmount, rawTx, reserveKey, receipt, vMintsSelected, vNewMints, false, true, addr_ptr, true))
1052+
throw JSONRPCError(RPC_WALLET_ERROR, receipt.GetStatusMessage());
1053+
1054+
// output the raw transaction
1055+
return EncodeHexTx(rawTx);
1056+
}
9941057
#endif
1058+

src/rpc/server.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -446,6 +446,7 @@ static const CRPCCommand vRPCCommands[] =
446446
{"wallet", "walletpassphrase", &walletpassphrase, true, false, true},
447447

448448
{"zerocoin", "createrawzerocoinstake", &createrawzerocoinstake, false, false, true},
449+
{"zerocoin", "createrawzerocoinpublicspend", &createrawzerocoinpublicspend, false, false, true},
449450
{"zerocoin", "getzerocoinbalance", &getzerocoinbalance, false, false, true},
450451
{"zerocoin", "listmintedzerocoins", &listmintedzerocoins, false, false, true},
451452
{"zerocoin", "listspentzerocoins", &listspentzerocoins, false, false, true},

src/rpc/server.h

+1
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ extern UniValue decodescript(const UniValue& params, bool fHelp);
284284
extern UniValue signrawtransaction(const UniValue& params, bool fHelp);
285285
extern UniValue sendrawtransaction(const UniValue& params, bool fHelp);
286286
extern UniValue createrawzerocoinstake(const UniValue& params, bool fHelp);
287+
extern UniValue createrawzerocoinpublicspend(const UniValue& params, bool fHelp);
287288

288289
extern UniValue findserial(const UniValue& params, bool fHelp); // in rpc/blockchain.cpp
289290
extern UniValue getblockcount(const UniValue& params, bool fHelp);

0 commit comments

Comments
 (0)