Skip to content

Commit

Permalink
rpc: add getsilentpaymentblockdata
Browse files Browse the repository at this point in the history
Co-Authored-By: w0xlt <woltx@protonmail.com>
  • Loading branch information
Sjors and w0xlt committed Sep 20, 2023
1 parent 8d9987d commit 91fd02f
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
59 changes: 59 additions & 0 deletions src/rpc/blockchain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include <hash.h>
#include <index/blockfilterindex.h>
#include <index/coinstatsindex.h>
#include <index/silentpaymentindex.h>
#include <kernel/coinstats.h>
#include <logging/timer.h>
#include <net.h>
Expand Down Expand Up @@ -640,6 +641,63 @@ const RPCResult getblock_vin{
}
};

static RPCHelpMan getsilentpaymentblockdata()
{
return RPCHelpMan{"getsilentpaymentblockdata",
"\nReturns an array of hex-encoded strings data for the tweaked public key sum of candidate silent transaction inputs in each transaction.\n",
{
{"block_hash", RPCArg::Type::STR_HEX, RPCArg::Optional::NO, "The block hash"},
},
{
RPCResult{
RPCResult::Type::OBJ, "", "",
{
{RPCResult::Type::STR_HEX, "hash", "the block hash (same as provided)"},
{RPCResult::Type::ARR, "tweaks", "The tweaked public key for each transaction that could be a silent payment",
{{RPCResult::Type::STR_HEX, "tweak", "Hex-encoded data for the public key sum of candidate silent transaction inputs in the transaction"}}
}
}},
},
RPCExamples{
HelpExampleCli("getsilentpaymentblockdata", "\"00000000000000000002cbdf64ae445b53b545ba1e960f9e83787130d1530484\"")
+ HelpExampleRpc("getsilentpaymentblockdata", "\"00000000000000000002cbdf64ae445b53b545ba1e960f9e83787130d1530484\"")
},
[&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
{
if (!g_silent_payment_index) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Requires silentpaymentindex");
}

uint256 block_hash(ParseHashV(request.params[0], "block_hash"));
{
ChainstateManager& chainman = EnsureAnyChainman(request.context);
const CBlockIndex* block_index;
LOCK(cs_main);
block_index = chainman.m_blockman.LookupBlockIndex(block_hash);
if (!block_index) {
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Block not found");
}
}

std::vector<CPubKey> tweaks;
if (!g_silent_payment_index->FindSilentPayment(block_hash, tweaks)) {
throw JSONRPCError(RPC_INVALID_PARAMETER, "Block has not been indexed yet");
}

UniValue ret(UniValue::VOBJ);
UniValue tweaks_res(UniValue::VARR);

for (CPubKey& tweak : tweaks) {
tweaks_res.push_back(HexStr(tweak));
}

ret.pushKV("block_hash", HexStr(block_hash));
ret.pushKV("tweaks", tweaks_res);
return ret;
},
};
}

static RPCHelpMan getblock()
{
return RPCHelpMan{"getblock",
Expand Down Expand Up @@ -2711,6 +2769,7 @@ void RegisterBlockchainRPCCommands(CRPCTable& t)
{"blockchain", &getblockfrompeer},
{"blockchain", &getblockhash},
{"blockchain", &getblockheader},
{"blockchain", &getsilentpaymentblockdata},
{"blockchain", &getchaintips},
{"blockchain", &getdifficulty},
{"blockchain", &getdeploymentinfo},
Expand Down
1 change: 1 addition & 0 deletions src/test/fuzz/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,7 @@ const std::vector<std::string> RPC_COMMANDS_SAFE_FOR_FUZZING{
"waitforblock",
"waitforblockheight",
"waitfornewblock",
"getsilentpaymentblockdata"
};

std::string ConsumeScalarRPCArgument(FuzzedDataProvider& fuzzed_data_provider)
Expand Down

0 comments on commit 91fd02f

Please sign in to comment.