Skip to content

Commit 452955f

Browse files
committed
Move validateaddress from rpcwallet to rpcmisc
Enables it in --disable-wallet compiles. Delimit wallet-using part using #ifdef ENABLE_WALLET.
1 parent cd7fa8b commit 452955f

File tree

3 files changed

+86
-82
lines changed

3 files changed

+86
-82
lines changed

src/rpcmisc.cpp

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,4 +86,89 @@ Value getinfo(const Array& params, bool fHelp)
8686
return obj;
8787
}
8888

89+
#ifdef ENABLE_WALLET
90+
class DescribeAddressVisitor : public boost::static_visitor<Object>
91+
{
92+
public:
93+
Object operator()(const CNoDestination &dest) const { return Object(); }
94+
95+
Object operator()(const CKeyID &keyID) const {
96+
Object obj;
97+
CPubKey vchPubKey;
98+
pwalletMain->GetPubKey(keyID, vchPubKey);
99+
obj.push_back(Pair("isscript", false));
100+
obj.push_back(Pair("pubkey", HexStr(vchPubKey)));
101+
obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed()));
102+
return obj;
103+
}
104+
105+
Object operator()(const CScriptID &scriptID) const {
106+
Object obj;
107+
obj.push_back(Pair("isscript", true));
108+
CScript subscript;
109+
pwalletMain->GetCScript(scriptID, subscript);
110+
std::vector<CTxDestination> addresses;
111+
txnouttype whichType;
112+
int nRequired;
113+
ExtractDestinations(subscript, whichType, addresses, nRequired);
114+
obj.push_back(Pair("script", GetTxnOutputType(whichType)));
115+
obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end())));
116+
Array a;
117+
BOOST_FOREACH(const CTxDestination& addr, addresses)
118+
a.push_back(CBitcoinAddress(addr).ToString());
119+
obj.push_back(Pair("addresses", a));
120+
if (whichType == TX_MULTISIG)
121+
obj.push_back(Pair("sigsrequired", nRequired));
122+
return obj;
123+
}
124+
};
125+
#endif
126+
127+
Value validateaddress(const Array& params, bool fHelp)
128+
{
129+
if (fHelp || params.size() != 1)
130+
throw runtime_error(
131+
"validateaddress \"bitcoinaddress\"\n"
132+
"\nReturn information about the given bitcoin address.\n"
133+
"\nArguments:\n"
134+
"1. \"bitcoinaddress\" (string, required) The bitcoin address to validate\n"
135+
"\nResult:\n"
136+
"{\n"
137+
" \"isvalid\" : true|false, (boolean) If the address is valid or not. If not, this is the only property returned.\n"
138+
" \"address\" : \"bitcoinaddress\", (string) The bitcoin address validated\n"
139+
" \"ismine\" : true|false, (boolean) If the address is yours or not\n"
140+
" \"isscript\" : true|false, (boolean) If the key is a script\n"
141+
" \"pubkey\" : \"publickeyhex\", (string) The hex value of the raw public key\n"
142+
" \"iscompressed\" : true|false, (boolean) If the address is compressed\n"
143+
" \"account\" : \"account\" (string) The account associated with the address, \"\" is the default account\n"
144+
"}\n"
145+
"\nExamples:\n"
146+
+ HelpExampleCli("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
147+
+ HelpExampleRpc("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
148+
);
149+
150+
CBitcoinAddress address(params[0].get_str());
151+
bool isValid = address.IsValid();
152+
153+
Object ret;
154+
ret.push_back(Pair("isvalid", isValid));
155+
if (isValid)
156+
{
157+
CTxDestination dest = address.Get();
158+
string currentAddress = address.ToString();
159+
ret.push_back(Pair("address", currentAddress));
160+
#ifdef ENABLE_WALLET
161+
bool fMine = pwalletMain ? IsMine(*pwalletMain, dest) : false;
162+
ret.push_back(Pair("ismine", fMine));
163+
if (fMine) {
164+
Object detail = boost::apply_visitor(DescribeAddressVisitor(), dest);
165+
ret.insert(ret.end(), detail.begin(), detail.end());
166+
}
167+
if (pwalletMain && pwalletMain->mapAddressBook.count(dest))
168+
ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name));
169+
#endif
170+
}
171+
return ret;
172+
}
173+
89174

src/rpcserver.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ static const CRPCCommand vRPCCommands[] =
252252
{ "getmininginfo", &getmininginfo, true, false, false },
253253
{ "getblocktemplate", &getblocktemplate, true, false, false },
254254
{ "submitblock", &submitblock, false, false, false },
255+
{ "validateaddress", &validateaddress, true, false, false },
255256

256257
#ifdef ENABLE_WALLET
257258
/* Wallet */
@@ -272,7 +273,6 @@ static const CRPCCommand vRPCCommands[] =
272273
{ "walletpassphrasechange", &walletpassphrasechange, false, false, true },
273274
{ "walletlock", &walletlock, true, false, true },
274275
{ "encryptwallet", &encryptwallet, false, false, true },
275-
{ "validateaddress", &validateaddress, true, false, false },
276276
{ "getbalance", &getbalance, false, false, true },
277277
{ "move", &movecmd, false, false, true },
278278
{ "sendfrom", &sendfrom, false, false, true },

src/rpcwallet.cpp

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,87 +1862,6 @@ Value encryptwallet(const Array& params, bool fHelp)
18621862
return "wallet encrypted; Bitcoin server stopping, restart to run with encrypted wallet. The keypool has been flushed, you need to make a new backup.";
18631863
}
18641864

1865-
class DescribeAddressVisitor : public boost::static_visitor<Object>
1866-
{
1867-
public:
1868-
Object operator()(const CNoDestination &dest) const { return Object(); }
1869-
1870-
Object operator()(const CKeyID &keyID) const {
1871-
Object obj;
1872-
CPubKey vchPubKey;
1873-
pwalletMain->GetPubKey(keyID, vchPubKey);
1874-
obj.push_back(Pair("isscript", false));
1875-
obj.push_back(Pair("pubkey", HexStr(vchPubKey)));
1876-
obj.push_back(Pair("iscompressed", vchPubKey.IsCompressed()));
1877-
return obj;
1878-
}
1879-
1880-
Object operator()(const CScriptID &scriptID) const {
1881-
Object obj;
1882-
obj.push_back(Pair("isscript", true));
1883-
CScript subscript;
1884-
pwalletMain->GetCScript(scriptID, subscript);
1885-
std::vector<CTxDestination> addresses;
1886-
txnouttype whichType;
1887-
int nRequired;
1888-
ExtractDestinations(subscript, whichType, addresses, nRequired);
1889-
obj.push_back(Pair("script", GetTxnOutputType(whichType)));
1890-
obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end())));
1891-
Array a;
1892-
BOOST_FOREACH(const CTxDestination& addr, addresses)
1893-
a.push_back(CBitcoinAddress(addr).ToString());
1894-
obj.push_back(Pair("addresses", a));
1895-
if (whichType == TX_MULTISIG)
1896-
obj.push_back(Pair("sigsrequired", nRequired));
1897-
return obj;
1898-
}
1899-
};
1900-
1901-
Value validateaddress(const Array& params, bool fHelp)
1902-
{
1903-
if (fHelp || params.size() != 1)
1904-
throw runtime_error(
1905-
"validateaddress \"bitcoinaddress\"\n"
1906-
"\nReturn information about the given bitcoin address.\n"
1907-
"\nArguments:\n"
1908-
"1. \"bitcoinaddress\" (string, required) The bitcoin address to validate\n"
1909-
"\nResult:\n"
1910-
"{\n"
1911-
" \"isvalid\" : true|false, (boolean) If the address is valid or not. If not, this is the only property returned.\n"
1912-
" \"address\" : \"bitcoinaddress\", (string) The bitcoin address validated\n"
1913-
" \"ismine\" : true|false, (boolean) If the address is yours or not\n"
1914-
" \"isscript\" : true|false, (boolean) If the key is a script\n"
1915-
" \"pubkey\" : \"publickeyhex\", (string) The hex value of the raw public key\n"
1916-
" \"iscompressed\" : true|false, (boolean) If the address is compressed\n"
1917-
" \"account\" : \"account\" (string) The account associated with the address, \"\" is the default account\n"
1918-
"}\n"
1919-
"\nExamples:\n"
1920-
+ HelpExampleCli("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
1921-
+ HelpExampleRpc("validateaddress", "\"1PSSGeFHDnKNxiEyFrD1wcEaHr9hrQDDWc\"")
1922-
);
1923-
1924-
CBitcoinAddress address(params[0].get_str());
1925-
bool isValid = address.IsValid();
1926-
1927-
Object ret;
1928-
ret.push_back(Pair("isvalid", isValid));
1929-
if (isValid)
1930-
{
1931-
CTxDestination dest = address.Get();
1932-
string currentAddress = address.ToString();
1933-
ret.push_back(Pair("address", currentAddress));
1934-
bool fMine = pwalletMain ? IsMine(*pwalletMain, dest) : false;
1935-
ret.push_back(Pair("ismine", fMine));
1936-
if (fMine) {
1937-
Object detail = boost::apply_visitor(DescribeAddressVisitor(), dest);
1938-
ret.insert(ret.end(), detail.begin(), detail.end());
1939-
}
1940-
if (pwalletMain && pwalletMain->mapAddressBook.count(dest))
1941-
ret.push_back(Pair("account", pwalletMain->mapAddressBook[dest].name));
1942-
}
1943-
return ret;
1944-
}
1945-
19461865
Value lockunspent(const Array& params, bool fHelp)
19471866
{
19481867
if (fHelp || params.size() < 1 || params.size() > 2)

0 commit comments

Comments
 (0)