From 4d6cfeb0ae2807f888128c445b64e55130a13747 Mon Sep 17 00:00:00 2001 From: Ruslan Kasheparov Date: Mon, 22 Jun 2026 13:54:45 +0200 Subject: [PATCH] rpc: use null for optional parameters --- src/rpc/blockchain.cpp | 2 +- src/rpc/misc.cpp | 6 +++--- src/rpc/server.cpp | 2 +- src/wallet/rpc/coins.cpp | 4 ++-- src/wallet/rpc/elements.cpp | 22 +++++++++++----------- src/wallet/rpc/spend.cpp | 2 +- src/wallet/rpc/transactions.cpp | 2 +- 7 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index 1001a9b5b9f..b7f8eb0eb7f 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2776,7 +2776,7 @@ static RPCHelpMan scantxoutset() throw JSONRPCError(RPC_INVALID_PARAMETER, "Scan already in progress, use action \"abort\" or \"status\""); } - if (request.params.size() < 2) { + if (request.params[1].isNull()) { throw JSONRPCError(RPC_MISC_ERROR, "scanobjects argument is required for the start action"); } diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index 6cdb80a3364..771fdecc5bb 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -299,7 +299,7 @@ static RPCHelpMan deriveaddresses() int64_t range_begin = 0; int64_t range_end = 0; - if (request.params.size() >= 2 && !request.params[1].isNull()) { + if (!request.params[1].isNull()) { std::tie(range_begin, range_end) = ParseDescriptorRange(request.params[1]); } @@ -310,11 +310,11 @@ static RPCHelpMan deriveaddresses() throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, error); } - if (!desc->IsRange() && request.params.size() > 1) { + if (!desc->IsRange() && !request.params[1].isNull()) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Range should not be specified for an un-ranged descriptor"); } - if (desc->IsRange() && request.params.size() == 1) { + if (desc->IsRange() && request.params[1].isNull()) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Range must be specified for a ranged descriptor"); } diff --git a/src/rpc/server.cpp b/src/rpc/server.cpp index c70236cc1c3..7a96ad0f248 100644 --- a/src/rpc/server.cpp +++ b/src/rpc/server.cpp @@ -145,7 +145,7 @@ static RPCHelpMan help() [&](const RPCHelpMan& self, const JSONRPCRequest& jsonRequest) -> UniValue { std::string strCommand; - if (jsonRequest.params.size() > 0) { + if (!jsonRequest.params[0].isNull()) { strCommand = jsonRequest.params[0].get_str(); } if (strCommand == "dump_all_command_conversions") { diff --git a/src/wallet/rpc/coins.cpp b/src/wallet/rpc/coins.cpp index ad1fe5a4b2b..d8bee1a643b 100644 --- a/src/wallet/rpc/coins.cpp +++ b/src/wallet/rpc/coins.cpp @@ -135,7 +135,7 @@ RPCHelpMan getreceivedbyaddress() LOCK(pwallet->cs_wallet); std::string asset = ""; - if (request.params.size() > 2 && request.params[2].isStr()) { + if (!request.params[2].isNull() && request.params[2].isStr()) { asset = request.params[2].get_str(); } @@ -193,7 +193,7 @@ RPCHelpMan getreceivedbylabel() LOCK(pwallet->cs_wallet); std::string asset = ""; - if (request.params.size() > 2 && request.params[2].isStr()) { + if (!request.params[2].isNull() && request.params[2].isStr()) { asset = request.params[2].get_str(); } diff --git a/src/wallet/rpc/elements.cpp b/src/wallet/rpc/elements.cpp index ae5f76a846c..50e18d5bb3a 100644 --- a/src/wallet/rpc/elements.cpp +++ b/src/wallet/rpc/elements.cpp @@ -330,7 +330,7 @@ RPCHelpMan initpegoutwallet() // Generate a new key that is added to wallet or set from argument CPubKey online_pubkey; - if (request.params.size() < 3) { + if (request.params[2].isNull()) { std::string error; if (!pwallet->GetOnlinePakKey(online_pubkey, error)) { throw JSONRPCError(RPC_WALLET_KEYPOOL_RAN_OUT, error); @@ -347,7 +347,7 @@ RPCHelpMan initpegoutwallet() // Parse offline counter int counter = 0; - if (request.params.size() > 1) { + if (!request.params[1].isNull()) { counter = request.params[1].get_int(); if (counter < 0 || counter > 1000000000) { throw JSONRPCError(RPC_INVALID_PARAMETER, "bip32_counter must be between 0 and 1,000,000,000, inclusive."); @@ -502,7 +502,7 @@ RPCHelpMan sendtomainchain_base() throw JSONRPCError(RPC_TYPE_ERROR, "Invalid amount for send"); bool subtract_fee = false; - if (request.params.size() > 2) { + if (!request.params[2].isNull()) { subtract_fee = request.params[2].get_bool(); } @@ -615,7 +615,7 @@ RPCHelpMan sendtomainchain_pak() throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid amount for send, must send more than 0.00100000 BTC"); bool subtract_fee = false; - if (request.params.size() > 2) { + if (!request.params[2].isNull()) { subtract_fee = request.params[2].get_bool(); } @@ -827,7 +827,7 @@ static UniValue createrawpegin(const JSONRPCRequest& request, T_tx_ref& txBTCRef std::vector txOutProofData = ParseHex(request.params[1].get_str()); std::set claim_scripts; - if (request.params.size() > 2) { + if (!request.params[2].isNull()) { const std::string claim_script = request.params[2].get_str(); if (!IsHex(claim_script)) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Given claim_script is not hex."); @@ -1273,12 +1273,12 @@ RPCHelpMan blindrawtransaction() } bool ignore_blind_fail = true; - if (request.params.size() > 1) { + if (!request.params[1].isNull()) { ignore_blind_fail = request.params[1].get_bool(); } std::vector > auxiliary_generators; - if (request.params.size() > 2) { + if (!request.params[2].isNull()) { UniValue assetCommitments = request.params[2].get_array(); if (assetCommitments.size() != 0 && assetCommitments.size() < tx.vin.size()) { throw JSONRPCError(RPC_INVALID_PARAMETER, "Asset commitment array must have at least as many entries as transaction inputs."); @@ -1539,11 +1539,11 @@ RPCHelpMan issueasset() throw JSONRPCError(RPC_TYPE_ERROR, "Issuance must have one non-zero component"); } - bool blind_issuances = request.params.size() < 3 || request.params[2].get_bool(); + bool blind_issuances = request.params[2].isNull() || request.params[2].get_bool(); // Check for optional contract to hash into definition uint256 contract_hash; - if (request.params.size() >= 4) { + if (!request.params[3].isNull()) { contract_hash = ParseHashV(request.params[3], "contract_hash"); } @@ -1730,7 +1730,7 @@ RPCHelpMan listissuances() std::string assetstr; CAsset asset_filter; - if (request.params.size() > 0) { + if (!request.params[0].isNull()) { assetstr = request.params[0].get_str(); asset_filter = GetAssetFromString(assetstr); } @@ -1828,7 +1828,7 @@ RPCHelpMan destroyamount() } mapValue_t mapValue; - if (request.params.size() > 2 && !request.params[2].isNull() && !request.params[2].get_str().empty()) { + if (!request.params[2].isNull() && !request.params[2].get_str().empty()) { mapValue["comment"] = request.params[2].get_str(); } diff --git a/src/wallet/rpc/spend.cpp b/src/wallet/rpc/spend.cpp index bc2d16d0165..0e6903c4a8d 100644 --- a/src/wallet/rpc/spend.cpp +++ b/src/wallet/rpc/spend.cpp @@ -222,7 +222,7 @@ RPCHelpMan sendtoaddress() coin_control.m_avoid_partial_spends |= coin_control.m_avoid_address_reuse; std::string strasset = Params().GetConsensus().pegged_asset.GetHex(); - if (request.params.size() > 9 && request.params[9].isStr() && !request.params[9].get_str().empty()) { + if (!request.params[9].isNull() && request.params[9].isStr() && !request.params[9].get_str().empty()) { strasset = request.params[9].get_str(); } CAsset asset = GetAssetFromString(strasset); diff --git a/src/wallet/rpc/transactions.cpp b/src/wallet/rpc/transactions.cpp index 8ea03668b74..77060b43d02 100644 --- a/src/wallet/rpc/transactions.cpp +++ b/src/wallet/rpc/transactions.cpp @@ -101,7 +101,7 @@ static UniValue ListReceived(const CWallet& wallet, const UniValue& params, cons } std::string strasset = ""; - if (params.size() > 4 && params[4].isStr()) { + if (!params[4].isNull() && params[4].isStr()) { strasset = params[4].get_str(); } CAsset asset;