Bitcoin Core  21.99.0
P2P Digital Currency
rpcsigner.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 <key_io.h>
7 #include <rpc/server.h>
8 #include <rpc/util.h>
9 #include <util/strencodings.h>
10 #include <wallet/rpcsigner.h>
11 #include <wallet/rpcwallet.h>
12 #include <wallet/wallet.h>
13 
14 #ifdef ENABLE_EXTERNAL_SIGNER
15 
17 {
18  return RPCHelpMan{
19  "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::STR_HEX, "masterkeyfingerprint", "Master key fingerprint"},
28  {RPCResult::Type::STR, "name", "Device name"},
29  },
30  }
31  }
32  },
33  RPCExamples{""},
34  [](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
35  const std::string command = gArgs.GetArg("-signer", "");
36  if (command == "") throw JSONRPCError(RPC_WALLET_ERROR, "Error: restart bitcoind with -signer=<cmd>");
37  std::string chain = gArgs.GetChainName();
38  UniValue signers_res = UniValue::VARR;
39  try {
40  std::vector<ExternalSigner> signers;
41  ExternalSigner::Enumerate(command, signers, chain);
42  for (ExternalSigner signer : signers) {
43  UniValue signer_res = UniValue::VOBJ;
44  signer_res.pushKV("fingerprint", signer.m_fingerprint);
45  signer_res.pushKV("name", signer.m_name);
46  signers_res.push_back(signer_res);
47  }
48  } catch (const ExternalSignerException& e) {
49  throw JSONRPCError(RPC_WALLET_ERROR, e.what());
50  }
51  UniValue result(UniValue::VOBJ);
52  result.pushKV("signers", signers_res);
53  return result;
54  }
55  };
56 }
57 
59 {
60  return RPCHelpMan{
61  "signerdisplayaddress",
62  "Display address on an external signer for verification.\n",
63  {
64  {"address", RPCArg::Type::STR, RPCArg::Optional::NO, /* default_val */ "", "bitcoin address to display"},
65  },
67  RPCExamples{""},
68  [](const RPCHelpMan& self, const JSONRPCRequest& request) -> UniValue {
69  std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request);
70  if (!wallet) return NullUniValue;
71  CWallet* const pwallet = wallet.get();
72 
73  LOCK(pwallet->cs_wallet);
74 
75  CTxDestination dest = DecodeDestination(request.params[0].get_str());
76 
77  // Make sure the destination is valid
78  if (!IsValidDestination(dest)) {
79  throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid address");
80  }
81 
82  if (!pwallet->DisplayAddress(dest)) {
83  throw JSONRPCError(RPC_WALLET_ERROR, "Failed to display address");
84  }
85 
86  UniValue result(UniValue::VOBJ);
87  result.pushKV("address", request.params[0].get_str());
88  return result;
89  }
90  };
91 }
92 
94 {
95 
96 // clang-format off
97 static const CRPCCommand commands[] =
98 { // category actor (function)
99  // --------------------- ------------------------
100  { "signer", &enumeratesigners, },
101  { "signer", &signerdisplayaddress, },
102 };
103 // clang-format on
104  return MakeSpan(commands);
105 }
106 
107 
108 #endif // ENABLE_EXTERNAL_SIGNER
UniValue::VOBJ
@ VOBJ
Definition: univalue.h:21
wallet.h
key_io.h
RPCHelpMan
Definition: util.h:336
NullUniValue
const UniValue NullUniValue
Definition: univalue.cpp:13
chainparamsbase.h
ExternalSignerException
Definition: external_signer.h:15
ArgsManager::GetChainName
std::string GetChainName() const
Returns the appropriate chain name from the program arguments.
Definition: system.cpp:982
RPCArg::Optional::NO
@ NO
Required arg.
RPCArg::Type::STR
@ STR
rpcwallet.h
wallet
Definition: interfaces.cpp:48
signerdisplayaddress
static RPCHelpMan signerdisplayaddress()
Definition: rpcsigner.cpp:58
UniValue::pushKV
bool pushKV(const std::string &key, const UniValue &val)
Definition: univalue.cpp:133
UniValue
Definition: univalue.h:19
GetSignerRPCCommands
Span< const CRPCCommand > GetSignerRPCCommands()
Definition: rpcsigner.cpp:93
Span
A Span is an object that can refer to a contiguous sequence of objects.
Definition: span.h:92
strencodings.h
IsValidDestination
bool IsValidDestination(const CTxDestination &dest)
Check whether a CTxDestination is a CNoDestination.
Definition: standard.cpp:328
GetWalletForJSONRPCRequest
std::shared_ptr< CWallet > GetWalletForJSONRPCRequest(const JSONRPCRequest &request)
Figures out what wallet, if any, to use for a JSONRPCRequest.
Definition: rpcwallet.cpp:96
RPCResult::Type::OBJ
@ OBJ
CRPCCommand
Definition: server.h:90
RPCResult::Type::NONE
@ NONE
enumeratesigners
static RPCHelpMan enumeratesigners()
Definition: rpcsigner.cpp:16
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:503
RPCExamples
Definition: util.h:326
RPC_WALLET_ERROR
@ RPC_WALLET_ERROR
Wallet errors.
Definition: protocol.h:71
RPCResult::Type::STR
@ STR
RPCResult::Type::ARR
@ ARR
DecodeDestination
CTxDestination DecodeDestination(const std::string &str, std::string &error_msg)
Definition: key_io.cpp:246
ExternalSigner
Enables interaction with an external signing device or service, such as a hardware wallet.
Definition: external_signer.h:22
RPC_INVALID_ADDRESS_OR_KEY
@ RPC_INVALID_ADDRESS_OR_KEY
Invalid address or key.
Definition: protocol.h:41
ExternalSigner::Enumerate
static bool Enumerate(const std::string &command, std::vector< ExternalSigner > &signers, std::string chain, bool ignore_errors=false)
Obtain a list of signers.
Definition: external_signer.cpp:21
JSONRPCError
UniValue JSONRPCError(int code, const std::string &message)
Definition: request.cpp:51
LOCK
#define LOCK(cs)
Definition: sync.h:232
gArgs
ArgsManager gArgs
Definition: system.cpp:79
CWallet
A CWallet maintains a set of transactions and balances, and provides the ability to create new transa...
Definition: wallet.h:638
UniValue::push_back
bool push_back(const UniValue &val)
Definition: univalue.cpp:108
CTxDestination
std::variant< CNoDestination, PKHash, ScriptHash, WitnessV0ScriptHash, WitnessV0KeyHash, WitnessUnknown > CTxDestination
A txout script template with a specific destination.
Definition: standard.h:212
CWallet::cs_wallet
RecursiveMutex cs_wallet
Definition: wallet.h:746
rpcsigner.h
JSONRPCRequest
Definition: request.h:28
util.h
RPCResult
Definition: util.h:222
UniValue::VARR
@ VARR
Definition: univalue.h:21
server.h
MakeSpan
constexpr Span< A > MakeSpan(A(&a)[N])
MakeSpan for arrays:
Definition: span.h:222
CWallet::DisplayAddress
bool DisplayAddress(const CTxDestination &dest) EXCLUSIVE_LOCKS_REQUIRED(cs_wallet)
Display address on an external signer.
Definition: wallet.cpp:3610