Bitcoin Core 22.99.0
P2P Digital Currency
external_signer.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-2021 The Bitcoin Core developers
2// Distributed under the MIT software license, see the accompanying
3// file COPYING or http://www.opensource.org/licenses/mit-license.php.
4
5#include <chainparamsbase.h>
6#include <external_signer.h>
7#include <rpc/server.h>
8#include <rpc/util.h>
9#include <util/strencodings.h>
10#include <rpc/protocol.h>
11
12#include <string>
13#include <vector>
14
15#ifdef ENABLE_EXTERNAL_SIGNER
16
18{
19 return RPCHelpMan{"enumeratesigners",
20 "Returns a list of external signers from -signer.",
21 {},
24 {
25 {RPCResult::Type::ARR, "signers", /* optional */ false, "",
26 {
27 {RPCResult::Type::OBJ, "", "",
28 {
29 {RPCResult::Type::STR_HEX, "fingerprint", "Master key fingerprint"},
30 {RPCResult::Type::STR, "name", "Device name"},
31 }},
32 },
33 }
34 }
35 },
37 HelpExampleCli("enumeratesigners", "")
38 + HelpExampleRpc("enumeratesigners", "")
39 },
40 [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
41 {
42 const std::string command = gArgs.GetArg("-signer", "");
43 if (command == "") throw JSONRPCError(RPC_MISC_ERROR, "Error: restart bitcoind with -signer=<cmd>");
44 const std::string chain = gArgs.GetChainName();
45 UniValue signers_res = UniValue::VARR;
46 try {
47 std::vector<ExternalSigner> signers;
48 ExternalSigner::Enumerate(command, signers, chain);
49 for (const ExternalSigner& signer : signers) {
50 UniValue signer_res = UniValue::VOBJ;
51 signer_res.pushKV("fingerprint", signer.m_fingerprint);
52 signer_res.pushKV("name", signer.m_name);
53 signers_res.push_back(signer_res);
54 }
55 } catch (const std::exception& e) {
56 throw JSONRPCError(RPC_MISC_ERROR, e.what());
57 }
59 result.pushKV("signers", signers_res);
60 return result;
61 }
62 };
63}
64
66{
67// clang-format off
68static const CRPCCommand commands[] =
69{ // category actor (function)
70 // --------------------- ------------------------
71 { "signer", &enumeratesigners, },
72};
73// clang-format on
74 for (const auto& c : commands) {
75 t.appendCommand(c.name, &c);
76 }
77}
78
79#endif // ENABLE_EXTERNAL_SIGNER
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:590
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:989
RPC command dispatcher.
Definition: server.h:126
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:270
Enables interaction with an external signing device or service, such as a hardware wallet.
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, const std::string chain)
Obtain a list of signers.
@ VOBJ
Definition: univalue.h:19
@ VARR
Definition: univalue.h:19
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:51
void RegisterSignerRPCCommands(CRPCTable &t)
Register raw transaction RPC commands.
static RPCHelpMan enumeratesigners()
@ RPC_MISC_ERROR
General application defined errors.
Definition: protocol.h:39
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:156
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:174
@ STR_HEX
Special string with only hex chars.
ArgsManager gArgs
Definition: system.cpp:85