Skip to content

Commit 0b9dc9c

Browse files
committed
[move] move listunspent to wallet/rpcwallet.cpp
1 parent c8a1350 commit 0b9dc9c

File tree

2 files changed

+112
-111
lines changed

2 files changed

+112
-111
lines changed

src/rpcrawtransaction.cpp

Lines changed: 0 additions & 111 deletions
Original file line numberDiff line numberDiff line change
@@ -193,117 +193,6 @@ Value getrawtransaction(const Array& params, bool fHelp)
193193
return result;
194194
}
195195

196-
#ifdef ENABLE_WALLET
197-
Value listunspent(const Array& params, bool fHelp)
198-
{
199-
if (fHelp || params.size() > 3)
200-
throw runtime_error(
201-
"listunspent ( minconf maxconf [\"address\",...] )\n"
202-
"\nReturns array of unspent transaction outputs\n"
203-
"with between minconf and maxconf (inclusive) confirmations.\n"
204-
"Optionally filter to only include txouts paid to specified addresses.\n"
205-
"Results are an array of Objects, each of which has:\n"
206-
"{txid, vout, scriptPubKey, amount, confirmations}\n"
207-
"\nArguments:\n"
208-
"1. minconf (numeric, optional, default=1) The minimum confirmations to filter\n"
209-
"2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter\n"
210-
"3. \"addresses\" (string) A json array of bitcoin addresses to filter\n"
211-
" [\n"
212-
" \"address\" (string) bitcoin address\n"
213-
" ,...\n"
214-
" ]\n"
215-
"\nResult\n"
216-
"[ (array of json object)\n"
217-
" {\n"
218-
" \"txid\" : \"txid\", (string) the transaction id \n"
219-
" \"vout\" : n, (numeric) the vout value\n"
220-
" \"address\" : \"address\", (string) the bitcoin address\n"
221-
" \"account\" : \"account\", (string) DEPRECATED. The associated account, or \"\" for the default account\n"
222-
" \"scriptPubKey\" : \"key\", (string) the script key\n"
223-
" \"amount\" : x.xxx, (numeric) the transaction amount in btc\n"
224-
" \"confirmations\" : n (numeric) The number of confirmations\n"
225-
" }\n"
226-
" ,...\n"
227-
"]\n"
228-
229-
"\nExamples\n"
230-
+ HelpExampleCli("listunspent", "")
231-
+ HelpExampleCli("listunspent", "6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
232-
+ HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
233-
);
234-
235-
RPCTypeCheck(params, boost::assign::list_of(int_type)(int_type)(array_type));
236-
237-
int nMinDepth = 1;
238-
if (params.size() > 0)
239-
nMinDepth = params[0].get_int();
240-
241-
int nMaxDepth = 9999999;
242-
if (params.size() > 1)
243-
nMaxDepth = params[1].get_int();
244-
245-
set<CBitcoinAddress> setAddress;
246-
if (params.size() > 2) {
247-
Array inputs = params[2].get_array();
248-
BOOST_FOREACH(Value& input, inputs) {
249-
CBitcoinAddress address(input.get_str());
250-
if (!address.IsValid())
251-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+input.get_str());
252-
if (setAddress.count(address))
253-
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+input.get_str());
254-
setAddress.insert(address);
255-
}
256-
}
257-
258-
Array results;
259-
vector<COutput> vecOutputs;
260-
assert(pwalletMain != NULL);
261-
LOCK2(cs_main, pwalletMain->cs_wallet);
262-
pwalletMain->AvailableCoins(vecOutputs, false);
263-
BOOST_FOREACH(const COutput& out, vecOutputs) {
264-
if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth)
265-
continue;
266-
267-
if (setAddress.size()) {
268-
CTxDestination address;
269-
if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
270-
continue;
271-
272-
if (!setAddress.count(address))
273-
continue;
274-
}
275-
276-
CAmount nValue = out.tx->vout[out.i].nValue;
277-
const CScript& pk = out.tx->vout[out.i].scriptPubKey;
278-
Object entry;
279-
entry.push_back(Pair("txid", out.tx->GetHash().GetHex()));
280-
entry.push_back(Pair("vout", out.i));
281-
CTxDestination address;
282-
if (ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) {
283-
entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
284-
if (pwalletMain->mapAddressBook.count(address))
285-
entry.push_back(Pair("account", pwalletMain->mapAddressBook[address].name));
286-
}
287-
entry.push_back(Pair("scriptPubKey", HexStr(pk.begin(), pk.end())));
288-
if (pk.IsPayToScriptHash()) {
289-
CTxDestination address;
290-
if (ExtractDestination(pk, address)) {
291-
const CScriptID& hash = boost::get<const CScriptID&>(address);
292-
CScript redeemScript;
293-
if (pwalletMain->GetCScript(hash, redeemScript))
294-
entry.push_back(Pair("redeemScript", HexStr(redeemScript.begin(), redeemScript.end())));
295-
}
296-
}
297-
entry.push_back(Pair("amount",ValueFromAmount(nValue)));
298-
entry.push_back(Pair("confirmations",out.nDepth));
299-
entry.push_back(Pair("spendable", out.fSpendable));
300-
results.push_back(entry);
301-
}
302-
303-
return results;
304-
}
305-
#endif
306-
307196
Value createrawtransaction(const Array& params, bool fHelp)
308197
{
309198
if (fHelp || params.size() != 2)

src/wallet/rpcwallet.cpp

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2118,3 +2118,115 @@ Value resendwallettransactions(const Array& params, bool fHelp)
21182118
}
21192119
return result;
21202120
}
2121+
2122+
Value listunspent(const Array& params, bool fHelp)
2123+
{
2124+
if (!EnsureWalletIsAvailable(fHelp))
2125+
return Value::null;
2126+
2127+
if (fHelp || params.size() > 3)
2128+
throw runtime_error(
2129+
"listunspent ( minconf maxconf [\"address\",...] )\n"
2130+
"\nReturns array of unspent transaction outputs\n"
2131+
"with between minconf and maxconf (inclusive) confirmations.\n"
2132+
"Optionally filter to only include txouts paid to specified addresses.\n"
2133+
"Results are an array of Objects, each of which has:\n"
2134+
"{txid, vout, scriptPubKey, amount, confirmations}\n"
2135+
"\nArguments:\n"
2136+
"1. minconf (numeric, optional, default=1) The minimum confirmations to filter\n"
2137+
"2. maxconf (numeric, optional, default=9999999) The maximum confirmations to filter\n"
2138+
"3. \"addresses\" (string) A json array of bitcoin addresses to filter\n"
2139+
" [\n"
2140+
" \"address\" (string) bitcoin address\n"
2141+
" ,...\n"
2142+
" ]\n"
2143+
"\nResult\n"
2144+
"[ (array of json object)\n"
2145+
" {\n"
2146+
" \"txid\" : \"txid\", (string) the transaction id \n"
2147+
" \"vout\" : n, (numeric) the vout value\n"
2148+
" \"address\" : \"address\", (string) the bitcoin address\n"
2149+
" \"account\" : \"account\", (string) DEPRECATED. The associated account, or \"\" for the default account\n"
2150+
" \"scriptPubKey\" : \"key\", (string) the script key\n"
2151+
" \"amount\" : x.xxx, (numeric) the transaction amount in btc\n"
2152+
" \"confirmations\" : n (numeric) The number of confirmations\n"
2153+
" }\n"
2154+
" ,...\n"
2155+
"]\n"
2156+
2157+
"\nExamples\n"
2158+
+ HelpExampleCli("listunspent", "")
2159+
+ HelpExampleCli("listunspent", "6 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2160+
+ HelpExampleRpc("listunspent", "6, 9999999 \"[\\\"1PGFqEzfmQch1gKD3ra4k18PNj3tTUUSqg\\\",\\\"1LtvqCaApEdUGFkpKMM4MstjcaL4dKg8SP\\\"]\"")
2161+
);
2162+
2163+
RPCTypeCheck(params, boost::assign::list_of(int_type)(int_type)(array_type));
2164+
2165+
int nMinDepth = 1;
2166+
if (params.size() > 0)
2167+
nMinDepth = params[0].get_int();
2168+
2169+
int nMaxDepth = 9999999;
2170+
if (params.size() > 1)
2171+
nMaxDepth = params[1].get_int();
2172+
2173+
set<CBitcoinAddress> setAddress;
2174+
if (params.size() > 2) {
2175+
Array inputs = params[2].get_array();
2176+
BOOST_FOREACH(Value& input, inputs) {
2177+
CBitcoinAddress address(input.get_str());
2178+
if (!address.IsValid())
2179+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+input.get_str());
2180+
if (setAddress.count(address))
2181+
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+input.get_str());
2182+
setAddress.insert(address);
2183+
}
2184+
}
2185+
2186+
Array results;
2187+
vector<COutput> vecOutputs;
2188+
assert(pwalletMain != NULL);
2189+
LOCK2(cs_main, pwalletMain->cs_wallet);
2190+
pwalletMain->AvailableCoins(vecOutputs, false);
2191+
BOOST_FOREACH(const COutput& out, vecOutputs) {
2192+
if (out.nDepth < nMinDepth || out.nDepth > nMaxDepth)
2193+
continue;
2194+
2195+
if (setAddress.size()) {
2196+
CTxDestination address;
2197+
if (!ExtractDestination(out.tx->vout[out.i].scriptPubKey, address))
2198+
continue;
2199+
2200+
if (!setAddress.count(address))
2201+
continue;
2202+
}
2203+
2204+
CAmount nValue = out.tx->vout[out.i].nValue;
2205+
const CScript& pk = out.tx->vout[out.i].scriptPubKey;
2206+
Object entry;
2207+
entry.push_back(Pair("txid", out.tx->GetHash().GetHex()));
2208+
entry.push_back(Pair("vout", out.i));
2209+
CTxDestination address;
2210+
if (ExtractDestination(out.tx->vout[out.i].scriptPubKey, address)) {
2211+
entry.push_back(Pair("address", CBitcoinAddress(address).ToString()));
2212+
if (pwalletMain->mapAddressBook.count(address))
2213+
entry.push_back(Pair("account", pwalletMain->mapAddressBook[address].name));
2214+
}
2215+
entry.push_back(Pair("scriptPubKey", HexStr(pk.begin(), pk.end())));
2216+
if (pk.IsPayToScriptHash()) {
2217+
CTxDestination address;
2218+
if (ExtractDestination(pk, address)) {
2219+
const CScriptID& hash = boost::get<const CScriptID&>(address);
2220+
CScript redeemScript;
2221+
if (pwalletMain->GetCScript(hash, redeemScript))
2222+
entry.push_back(Pair("redeemScript", HexStr(redeemScript.begin(), redeemScript.end())));
2223+
}
2224+
}
2225+
entry.push_back(Pair("amount",ValueFromAmount(nValue)));
2226+
entry.push_back(Pair("confirmations",out.nDepth));
2227+
entry.push_back(Pair("spendable", out.fSpendable));
2228+
results.push_back(entry);
2229+
}
2230+
2231+
return results;
2232+
}

0 commit comments

Comments
 (0)