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  {},
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 // clang-format off
68 static 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
RPC_MISC_ERROR
@ RPC_MISC_ERROR
General application defined errors.
Definition: protocol.h:39
HelpExampleCli
std::string HelpExampleCli(const std::string &methodname, const std::string &args)
Definition: util.cpp:155
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:21
RPCHelpMan
Definition: util.h:345
chainparamsbase.h
ArgsManager::GetChainName
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:988
protocol.h
ExternalSigner::Enumerate
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, const std::string chain)
Obtain a list of signers.
Definition: external_signer.cpp:23
UniValue::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
UniValue
Definition: univalue.h:19
strencodings.h
external_signer.h
RPCResult::Type::OBJ
@ OBJ
CRPCCommand
Definition: server.h:90
RPCResult::Type::STR_HEX
@ STR_HEX
Special string with only hex chars.
ArgsManager::GetArg
std::string GetArg(const std::string &strArg, const std::string &strDefault) const
Return string argument or default value.
Definition: system.cpp:588
RPCExamples
Definition: util.h:335
RPCResult::Type::STR
@ STR
RPCResult::Type::ARR
@ ARR
HelpExampleRpc
std::string HelpExampleRpc(const std::string &methodname, const std::string &args)
Definition: util.cpp:173
gArgs
ArgsManager gArgs
Definition: system.cpp:84
CRPCTable
RPC command dispatcher.
Definition: server.h:126
ExternalSigner
Enables interaction with an external signing device or service, such as a hardware wallet.
Definition: external_signer.h:18
enumeratesigners
static RPCHelpMan enumeratesigners()
Definition: external_signer.cpp:17
CRPCTable::appendCommand
void appendCommand(const std::string &name, const CRPCCommand *pcmd)
Appends a CRPCCommand to the dispatch table.
Definition: server.cpp:270
JSONRPCError
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:51
RegisterSignerRPCCommands
void RegisterSignerRPCCommands(CRPCTable &t)
Register raw transaction RPC commands.
Definition: external_signer.cpp:65
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
JSONRPCRequest
Definition: request.h:28
util.h
RPCResult
Definition: util.h:231
UniValue::VARR
@ VARR
Definition: univalue.h:21
server.h