/tmp/bitcoin/src/rpc/external_signer.cpp
Line | Count | Source |
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 <external_signer.h> |
8 | | #include <rpc/register.h> // IWYU pragma: associated |
9 | | |
10 | | #include <common/args.h> |
11 | | #include <rpc/protocol.h> |
12 | | #include <rpc/request.h> |
13 | | #include <rpc/server.h> |
14 | | #include <rpc/util.h> |
15 | | #include <univalue.h> |
16 | | |
17 | | #include <exception> |
18 | | #include <string> |
19 | | #include <utility> |
20 | | #include <vector> |
21 | | |
22 | | #ifdef ENABLE_EXTERNAL_SIGNER |
23 | | |
24 | | static RPCMethod enumeratesigners() |
25 | 2.47k | { |
26 | 2.47k | return RPCMethod{"enumeratesigners", |
27 | 2.47k | "Returns a list of external signers from -signer. Signers with duplicate master key fingerprints are skipped.", |
28 | 2.47k | {}, |
29 | 2.47k | RPCResult{ |
30 | 2.47k | RPCResult::Type::OBJ, "", "", |
31 | 2.47k | { |
32 | 2.47k | {RPCResult::Type::ARR, "signers", /*optional=*/false, "", |
33 | 2.47k | { |
34 | 2.47k | {RPCResult::Type::OBJ, "", "", |
35 | 2.47k | { |
36 | 2.47k | {RPCResult::Type::STR_HEX, "fingerprint", "Master key fingerprint"}, |
37 | 2.47k | {RPCResult::Type::STR, "name", "Device name, the model returned by the signer"}, |
38 | 2.47k | }}, |
39 | 2.47k | }, |
40 | 2.47k | } |
41 | 2.47k | } |
42 | 2.47k | }, |
43 | 2.47k | RPCExamples{ |
44 | 2.47k | HelpExampleCli("enumeratesigners", "") |
45 | 2.47k | + HelpExampleRpc("enumeratesigners", "") |
46 | 2.47k | }, |
47 | 2.47k | [](const RPCMethod& self, const JSONRPCRequest& request) -> UniValue |
48 | 2.47k | { |
49 | 11 | const std::string command = gArgs.GetArg("-signer", ""); |
50 | 11 | if (command == "") throw JSONRPCError(RPC_MISC_ERROR, "Error: restart bitcoind with -signer=<cmd>"); |
51 | 10 | const std::string chain = gArgs.GetChainTypeString(); |
52 | 10 | UniValue signers_res = UniValue::VARR; |
53 | 10 | try { |
54 | 10 | std::vector<ExternalSigner> signers; |
55 | 10 | ExternalSigner::Enumerate(command, signers, chain); |
56 | 10 | for (const ExternalSigner& signer : signers) { |
57 | 3 | UniValue signer_res = UniValue::VOBJ; |
58 | 3 | signer_res.pushKV("fingerprint", signer.m_fingerprint); |
59 | 3 | signer_res.pushKV("name", signer.m_name); |
60 | 3 | signers_res.push_back(std::move(signer_res)); |
61 | 3 | } |
62 | 10 | } catch (const std::exception& e) { |
63 | 8 | throw JSONRPCError(RPC_MISC_ERROR, e.what()); |
64 | 8 | } |
65 | 2 | UniValue result(UniValue::VOBJ); |
66 | 2 | result.pushKV("signers", std::move(signers_res)); |
67 | 2 | return result; |
68 | 10 | } |
69 | 2.47k | }; |
70 | 2.47k | } |
71 | | |
72 | | void RegisterSignerRPCCommands(CRPCTable& t) |
73 | 1.36k | { |
74 | 1.36k | static const CRPCCommand commands[]{ |
75 | 1.36k | {"signer", &enumeratesigners}, |
76 | 1.36k | }; |
77 | 1.36k | for (const auto& c : commands) { |
78 | 1.36k | t.appendCommand(c.name, &c); |
79 | 1.36k | } |
80 | 1.36k | } |
81 | | |
82 | | #endif // ENABLE_EXTERNAL_SIGNER |