Bitcoin Core  24.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 <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  {},
22  RPCResult{
23  RPCResult::Type::OBJ, "", "",
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  }
58  UniValue result(UniValue::VOBJ);
59  result.pushKV("signers", signers_res);
60  return result;
61  }
62  };
63 }
64 
66 {
67  static const CRPCCommand commands[]{
68  {"signer", &enumeratesigners},
69  };
70  for (const auto& c : commands) {
71  t.appendCommand(c.name, &c);
72  }
73 }
74 
75 #endif // ENABLE_EXTERNAL_SIGNER
const auto command
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:591
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:1060
RPC command dispatcher.
Definition: server.h:126
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:20
@ VARR
Definition: univalue.h:20
void pushKV(std::string key, UniValue val)
Definition: univalue.cpp:126
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:56
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:139
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:157
@ STR_HEX
Special string with only hex chars.
ArgsManager gArgs
Definition: system.cpp:73