/ src / rpc / external_signer.cpp
external_signer.cpp
 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)
 6  #include <config/bitcoin-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  
22  static RPCHelpMan enumeratesigners()
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          },
41          RPCExamples{
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  
70  void RegisterSignerRPCCommands(CRPCTable& t)
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