diff --git a/src/wallet/rpcwallet.cpp b/src/wallet/rpcwallet.cpp index 12f9aca5b..22c9e9860 100644 --- a/src/wallet/rpcwallet.cpp +++ b/src/wallet/rpcwallet.cpp @@ -6380,7 +6380,7 @@ void RegisterWalletRPCCommands(CRPCTable &tableRPC) UniValue opreturn_burn(const UniValue& params, bool fHelp, const CPubKey& mypk) { - std::vector vHexStr; CScript opret; int32_t txfee = 10000;CPubKey myPubkey; + std::vector vHexStr; CScript opret; CAmount txfee = 10000;CPubKey myPubkey; if (fHelp || (params.size() < 2) || (params.size() > 4) ) { throw runtime_error( @@ -6413,6 +6413,9 @@ UniValue opreturn_burn(const UniValue& params, bool fHelp, const CPubKey& mypk) if ( params.size() > 2 ) txfee = AmountFromValue(params[2]); + if ( !MoneyRange(nAmount) || !MoneyRange(txfee) || !MoneyRange(nAmount + txfee) ) + throw JSONRPCError(RPC_TYPE_ERROR, "burn_amount + txfee out of range."); + if (!EnsureWalletIsAvailable(fHelp)) throw JSONRPCError(RPC_TYPE_ERROR, "wallet is locked or unavailable."); EnsureWalletIsUnlocked(); @@ -6425,12 +6428,17 @@ UniValue opreturn_burn(const UniValue& params, bool fHelp, const CPubKey& mypk) CMutableTransaction mtx = CreateNewContextualCMutableTransaction(Params().GetConsensus(), hush_nextheight()); int64_t normalInputs = AddNormalinputs(mtx, myPubkey, nAmount+txfee, 60); - if (normalInputs < nAmount) + if (normalInputs < nAmount+txfee) throw runtime_error("insufficient funds\n"); opret << OP_RETURN << E_MARSHAL(ss << vHexStr); mtx.vout.push_back(CTxOut(nAmount,opret)); + // Return the unspent surplus (selected inputs - burn amount - txfee) as change to a + // wallet-owned address; without this the entire surplus is silently paid as miner fee. + CAmount change = normalInputs - nAmount - txfee; + if ( change > 0 ) + mtx.vout.push_back(CTxOut(change, GetScriptForDestination(myPubkey.GetID()))); ret.push_back(Pair("hex", EncodeHexTx(mtx))); return(ret); }