/ src / rpc / external_signer.cpp
external_signer.cpp
 1  // Copyright (c) 2018-present 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 <bitcoin-build-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  
20  static RPCMethod enumeratesigners()
21  {
22      return RPCMethod{"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          },
39          RPCExamples{
40              HelpExampleCli("enumeratesigners", "")
41              + HelpExampleRpc("enumeratesigners", "")
42          },
43          [](const RPCMethod& 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  
68  void RegisterSignerRPCCommands(CRPCTable& t)
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