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 #if defined(HAVE_CONFIG_H)
7 #endif
8 
9 #include <common/args.h>
10 #include <common/system.h>
11 #include <external_signer.h>
12 #include <rpc/protocol.h>
13 #include <rpc/server.h>
14 #include <rpc/util.h>
15 #include <util/strencodings.h>
16 
17 #include <string>
18 #include <vector>
19 
20 #ifdef ENABLE_EXTERNAL_SIGNER
21 
23 {
24  return RPCHelpMan{"enumeratesigners",
25  "Returns a list of external signers from -signer.",
26  {},
27  RPCResult{
28  RPCResult::Type::OBJ, "", "",
29  {
30  {RPCResult::Type::ARR, "signers", /*optional=*/false, "",
31  {
32  {RPCResult::Type::OBJ, "", "",
33  {
34  {RPCResult::Type::STR_HEX, "fingerprint", "Master key fingerprint"},
35  {RPCResult::Type::STR, "name", "Device name"},
36  }},
37  },
38  }
39  }
40  },
42  HelpExampleCli("enumeratesigners", "")
43  + HelpExampleRpc("enumeratesigners", "")
44  },
45  [&](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue
46  {
47  const std::string command = gArgs.GetArg("-signer", "");
48  if (command == "") throw JSONRPCError(RPC_MISC_ERROR, "Error: restart bitcoind with -signer=<cmd>");
49  const std::string chain = gArgs.GetChainTypeString();
50  UniValue signers_res = UniValue::VARR;
51  try {
52  std::vector<ExternalSigner> signers;
53  ExternalSigner::Enumerate(command, signers, chain);
54  for (const ExternalSigner& signer : signers) {
55  UniValue signer_res = UniValue::VOBJ;
56  signer_res.pushKV("fingerprint", signer.m_fingerprint);
57  signer_res.pushKV("name", signer.m_name);
58  signers_res.push_back(signer_res);
59  }
60  } catch (const std::exception& e) {
61  throw JSONRPCError(RPC_MISC_ERROR, e.what());
62  }
63  UniValue result(UniValue::VOBJ);
64  result.pushKV("signers", signers_res);
65  return result;
66  }
67  };
68 }
69 
71 {
72  static const CRPCCommand commands[]{
73  {"signer", &enumeratesigners},
74  };
75  for (const auto& c : commands) {
76  t.appendCommand(c.name, &c);
77  }
78 }
79 
80 #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:748
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:23
@ VARR
Definition: univalue.h:23
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:126
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:58
void RegisterSignerRPCCommands(CRPCTable &t)
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:155
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:173
@ STR_HEX
Special string with only hex chars.