Bitcoin Core 32.99.0
P2P Digital Currency
external_signer.cpp
Go to the documentation of this file.
1// Copyright (c) 2018-present 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 <bitcoin-build-config.h> // IWYU pragma: keep
6
7#include <external_signer.h>
8#include <rpc/register.h> // IWYU pragma: associated
9
10#include <common/args.h>
11#include <rpc/protocol.h>
12#include <rpc/request.h>
13#include <rpc/server.h>
14#include <rpc/util.h>
15#include <univalue.h>
16
17#include <exception>
18#include <string>
19#include <utility>
20#include <vector>
21
22#ifdef ENABLE_EXTERNAL_SIGNER
23
25{
26 return RPCMethod{"enumeratesigners",
27 "Returns a list of external signers from -signer. Signers with duplicate master key fingerprints are skipped.",
28 {},
31 {
32 {RPCResult::Type::ARR, "signers", /*optional=*/false, "",
33 {
34 {RPCResult::Type::OBJ, "", "",
35 {
36 {RPCResult::Type::STR_HEX, "fingerprint", "Master key fingerprint"},
37 {RPCResult::Type::STR, "name", "Device name, the model returned by the signer"},
38 }},
39 },
40 }
41 }
42 },
44 HelpExampleCli("enumeratesigners", "")
45 + HelpExampleRpc("enumeratesigners", "")
46 },
47 [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue
48 {
49 const std::string command = gArgs.GetArg("-signer", "");
50 if (command == "") throw JSONRPCError(RPC_MISC_ERROR, "Error: restart bitcoind with -signer=<cmd>");
51 const std::string chain = gArgs.GetChainTypeString();
52 UniValue signers_res = UniValue::VARR;
53 try {
54 std::vector<ExternalSigner> signers;
55 ExternalSigner::Enumerate(command, signers, chain);
56 for (const ExternalSigner& signer : signers) {
57 UniValue signer_res = UniValue::VOBJ;
58 signer_res.pushKV("fingerprint", signer.m_fingerprint);
59 signer_res.pushKV("name", signer.m_name);
60 signers_res.push_back(std::move(signer_res));
61 }
62 } catch (const std::exception& e) {
63 throw JSONRPCError(RPC_MISC_ERROR, e.what());
64 }
66 result.pushKV("signers", std::move(signers_res));
67 return result;
68 }
69 };
70}
71
73{
74 static const CRPCCommand commands[]{
75 {"signer", &enumeratesigners},
76 };
77 for (const auto& c : commands) {
78 t.appendCommand(c.name, &c);
79 }
80}
81
82#endif // ENABLE_EXTERNAL_SIGNER
ArgsManager gArgs
Definition: args.cpp:38
const auto command
std::string GetArg(const std::string &strArg, const std::string &strDefault) const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Return string argument or default value.
Definition: args.cpp:517
std::string GetChainTypeString() const EXCLUSIVE_LOCKS_REQUIRED(!cs_args)
Returns the appropriate chain type string from the program arguments.
Definition: args.cpp:917
RPC command dispatcher.
Definition: server.h:89
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.
void push_back(UniValue val)
Definition: univalue.cpp:103
@ VOBJ
Definition: univalue.h:24
@ VARR
Definition: univalue.h:24
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:125
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:75
static RPCMethod enumeratesigners()
void RegisterSignerRPCCommands(CRPCTable &t)
@ RPC_MISC_ERROR
General application defined errors.
Definition: protocol.h:65
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:188
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:206
@ STR_HEX
Special string with only hex chars.
Definition: musig.c:31