mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 14:53:43 +01:00
Merge pull request #1861 from jgarzik/coinlock
Add new RPC "lockunspent", to prevent spending of selected outputs
This commit is contained in:
@@ -3,14 +3,18 @@
|
||||
// Distributed under the MIT/X11 software license, see the accompanying
|
||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
||||
|
||||
#include <boost/assign/list_of.hpp>
|
||||
|
||||
#include "wallet.h"
|
||||
#include "walletdb.h"
|
||||
#include "bitcoinrpc.h"
|
||||
#include "init.h"
|
||||
#include "base58.h"
|
||||
|
||||
using namespace json_spirit;
|
||||
using namespace std;
|
||||
using namespace boost;
|
||||
using namespace boost::assign;
|
||||
using namespace json_spirit;
|
||||
|
||||
int64 nWalletUnlockTime;
|
||||
static CCriticalSection cs_nWalletUnlockTime;
|
||||
@@ -1497,3 +1501,74 @@ Value validateaddress(const Array& params, bool fHelp)
|
||||
return ret;
|
||||
}
|
||||
|
||||
Value lockunspent(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() < 1 || params.size() > 2)
|
||||
throw runtime_error(
|
||||
"lockunspent unlock? [array-of-Objects]\n"
|
||||
"Updates list of temporarily unspendable outputs.");
|
||||
|
||||
if (params.size() == 1)
|
||||
RPCTypeCheck(params, list_of(bool_type));
|
||||
else
|
||||
RPCTypeCheck(params, list_of(bool_type)(array_type));
|
||||
|
||||
bool fUnlock = params[0].get_bool();
|
||||
|
||||
if (params.size() == 1) {
|
||||
if (fUnlock)
|
||||
pwalletMain->UnlockAllCoins();
|
||||
return true;
|
||||
}
|
||||
|
||||
Array outputs = params[1].get_array();
|
||||
BOOST_FOREACH(Value& output, outputs)
|
||||
{
|
||||
if (output.type() != obj_type)
|
||||
throw JSONRPCError(-8, "Invalid parameter, expected object");
|
||||
const Object& o = output.get_obj();
|
||||
|
||||
RPCTypeCheck(o, map_list_of("txid", str_type)("vout", int_type));
|
||||
|
||||
string txid = find_value(o, "txid").get_str();
|
||||
if (!IsHex(txid))
|
||||
throw JSONRPCError(-8, "Invalid parameter, expected hex txid");
|
||||
|
||||
int nOutput = find_value(o, "vout").get_int();
|
||||
if (nOutput < 0)
|
||||
throw JSONRPCError(-8, "Invalid parameter, vout must be positive");
|
||||
|
||||
COutPoint outpt(uint256(txid), nOutput);
|
||||
|
||||
if (fUnlock)
|
||||
pwalletMain->UnlockCoin(outpt);
|
||||
else
|
||||
pwalletMain->LockCoin(outpt);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
Value listlockunspent(const Array& params, bool fHelp)
|
||||
{
|
||||
if (fHelp || params.size() > 0)
|
||||
throw runtime_error(
|
||||
"listlockunspent\n"
|
||||
"Returns list of temporarily unspendable outputs.");
|
||||
|
||||
vector<COutPoint> vOutpts;
|
||||
pwalletMain->ListLockedCoins(vOutpts);
|
||||
|
||||
Array ret;
|
||||
|
||||
BOOST_FOREACH(COutPoint &outpt, vOutpts) {
|
||||
Object o;
|
||||
|
||||
o.push_back(Pair("txid", outpt.hash.GetHex()));
|
||||
o.push_back(Pair("vout", (int)outpt.n));
|
||||
ret.push_back(o);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user