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