Changeset View
Changeset View
Standalone View
Standalone View
src/wallet/rpcwallet.cpp
// Copyright (c) 2010 Satoshi Nakamoto | // Copyright (c) 2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2018 The Bitcoin Core developers | // Copyright (c) 2009-2018 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#include <amount.h> | #include <amount.h> | ||||
#include <chain.h> | #include <chain.h> | ||||
#include <chainparams.h> // for GetConsensus. | #include <chainparams.h> // for GetConsensus. | ||||
#include <config.h> | #include <config.h> | ||||
#include <consensus/validation.h> | #include <consensus/validation.h> | ||||
#include <core_io.h> | #include <core_io.h> | ||||
#include <init.h> | |||||
#include <key_io.h> | #include <key_io.h> | ||||
#include <net.h> | #include <net.h> | ||||
#include <outputtype.h> | #include <outputtype.h> | ||||
#include <policy/fees.h> | #include <policy/fees.h> | ||||
#include <policy/policy.h> | #include <policy/policy.h> | ||||
#include <rpc/mining.h> | #include <rpc/mining.h> | ||||
#include <rpc/misc.h> | #include <rpc/misc.h> | ||||
#include <rpc/rawtransaction.h> | #include <rpc/rawtransaction.h> | ||||
▲ Show 20 Lines • Show All 89 Lines • ▼ Show 20 Lines | |||||
void EnsureWalletIsUnlocked(CWallet *const pwallet) { | void EnsureWalletIsUnlocked(CWallet *const pwallet) { | ||||
if (pwallet->IsLocked()) { | if (pwallet->IsLocked()) { | ||||
throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, | throw JSONRPCError(RPC_WALLET_UNLOCK_NEEDED, | ||||
"Error: Please enter the wallet passphrase with " | "Error: Please enter the wallet passphrase with " | ||||
"walletpassphrase first."); | "walletpassphrase first."); | ||||
} | } | ||||
} | } | ||||
static void WalletTxToJSON(const CWalletTx &wtx, UniValue &entry) | static void WalletTxToJSON(interfaces::Chain &chain, const CWalletTx &wtx, | ||||
EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | UniValue &entry) EXCLUSIVE_LOCKS_REQUIRED(cs_main) { | ||||
int confirms = wtx.GetDepthInMainChain(); | int confirms = wtx.GetDepthInMainChain(); | ||||
entry.pushKV("confirmations", confirms); | entry.pushKV("confirmations", confirms); | ||||
if (wtx.IsCoinBase()) { | if (wtx.IsCoinBase()) { | ||||
entry.pushKV("generated", true); | entry.pushKV("generated", true); | ||||
} | } | ||||
if (confirms > 0) { | if (confirms > 0) { | ||||
entry.pushKV("blockhash", wtx.hashBlock.GetHex()); | entry.pushKV("blockhash", wtx.hashBlock.GetHex()); | ||||
entry.pushKV("blockindex", wtx.nIndex); | entry.pushKV("blockindex", wtx.nIndex); | ||||
▲ Show 20 Lines • Show All 1,960 Lines • ▼ Show 20 Lines | if ((!listSent.empty() || nFee != Amount::zero()) && | ||||
entry.pushKV("amount", ValueFromAmount(-s.amount)); | entry.pushKV("amount", ValueFromAmount(-s.amount)); | ||||
if (pwallet->mapAddressBook.count(s.destination)) { | if (pwallet->mapAddressBook.count(s.destination)) { | ||||
entry.pushKV("label", | entry.pushKV("label", | ||||
pwallet->mapAddressBook[s.destination].name); | pwallet->mapAddressBook[s.destination].name); | ||||
} | } | ||||
entry.pushKV("vout", s.vout); | entry.pushKV("vout", s.vout); | ||||
entry.pushKV("fee", ValueFromAmount(-1 * nFee)); | entry.pushKV("fee", ValueFromAmount(-1 * nFee)); | ||||
if (fLong) { | if (fLong) { | ||||
WalletTxToJSON(wtx, entry); | WalletTxToJSON(pwallet->chain(), wtx, entry); | ||||
} | } | ||||
entry.pushKV("abandoned", wtx.isAbandoned()); | entry.pushKV("abandoned", wtx.isAbandoned()); | ||||
ret.push_back(entry); | ret.push_back(entry); | ||||
} | } | ||||
} | } | ||||
// Received | // Received | ||||
if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth) { | if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth) { | ||||
Show All 24 Lines | if (listReceived.size() > 0 && wtx.GetDepthInMainChain() >= nMinDepth) { | ||||
entry.pushKV("category", "receive"); | entry.pushKV("category", "receive"); | ||||
} | } | ||||
entry.pushKV("amount", ValueFromAmount(r.amount)); | entry.pushKV("amount", ValueFromAmount(r.amount)); | ||||
if (pwallet->mapAddressBook.count(r.destination)) { | if (pwallet->mapAddressBook.count(r.destination)) { | ||||
entry.pushKV("label", account); | entry.pushKV("label", account); | ||||
} | } | ||||
entry.pushKV("vout", r.vout); | entry.pushKV("vout", r.vout); | ||||
if (fLong) { | if (fLong) { | ||||
WalletTxToJSON(wtx, entry); | WalletTxToJSON(pwallet->chain(), wtx, entry); | ||||
} | } | ||||
ret.push_back(entry); | ret.push_back(entry); | ||||
} | } | ||||
} | } | ||||
} | } | ||||
} | } | ||||
static void AcentryToJSON(const CAccountingEntry &acentry, | static void AcentryToJSON(const CAccountingEntry &acentry, | ||||
▲ Show 20 Lines • Show All 743 Lines • ▼ Show 20 Lines | static UniValue gettransaction(const Config &config, | ||||
Amount nFee = (wtx.IsFromMe(filter) ? wtx.tx->GetValueOut() - nDebit | Amount nFee = (wtx.IsFromMe(filter) ? wtx.tx->GetValueOut() - nDebit | ||||
: Amount::zero()); | : Amount::zero()); | ||||
entry.pushKV("amount", ValueFromAmount(nNet - nFee)); | entry.pushKV("amount", ValueFromAmount(nNet - nFee)); | ||||
if (wtx.IsFromMe(filter)) { | if (wtx.IsFromMe(filter)) { | ||||
entry.pushKV("fee", ValueFromAmount(nFee)); | entry.pushKV("fee", ValueFromAmount(nFee)); | ||||
} | } | ||||
WalletTxToJSON(wtx, entry); | WalletTxToJSON(pwallet->chain(), wtx, entry); | ||||
UniValue details(UniValue::VARR); | UniValue details(UniValue::VARR); | ||||
ListTransactions(pwallet, wtx, "*", 0, false, details, filter); | ListTransactions(pwallet, wtx, "*", 0, false, details, filter); | ||||
entry.pushKV("details", details); | entry.pushKV("details", details); | ||||
std::string strHex = EncodeHexTx(*wtx.tx, RPCSerializationFlags()); | std::string strHex = EncodeHexTx(*wtx.tx, RPCSerializationFlags()); | ||||
entry.pushKV("hex", strHex); | entry.pushKV("hex", strHex); | ||||
▲ Show 20 Lines • Show All 859 Lines • ▼ Show 20 Lines | if (!location.Exists()) { | ||||
if (fs::symlink_status(wallet_dat_file).type() == fs::file_not_found) { | if (fs::symlink_status(wallet_dat_file).type() == fs::file_not_found) { | ||||
throw JSONRPCError(RPC_WALLET_NOT_FOUND, | throw JSONRPCError(RPC_WALLET_NOT_FOUND, | ||||
"Directory " + location.GetName() + | "Directory " + location.GetName() + | ||||
" does not contain a wallet.dat file."); | " does not contain a wallet.dat file."); | ||||
} | } | ||||
} | } | ||||
std::string warning; | std::string warning; | ||||
if (!CWallet::Verify(chainParams, location, false, error, warning)) { | if (!CWallet::Verify(chainParams, *g_rpc_interfaces->chain, location, false, | ||||
error, warning)) { | |||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Wallet file verification failed: " + error); | "Wallet file verification failed: " + error); | ||||
} | } | ||||
std::shared_ptr<CWallet> const wallet = | std::shared_ptr<CWallet> const wallet = CWallet::CreateWalletFromFile( | ||||
CWallet::CreateWalletFromFile(chainParams, location); | chainParams, *g_rpc_interfaces->chain, location); | ||||
if (!wallet) { | if (!wallet) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet loading failed."); | throw JSONRPCError(RPC_WALLET_ERROR, "Wallet loading failed."); | ||||
} | } | ||||
AddWallet(wallet); | AddWallet(wallet); | ||||
wallet->postInitProcess(); | wallet->postInitProcess(); | ||||
UniValue obj(UniValue::VOBJ); | UniValue obj(UniValue::VOBJ); | ||||
▲ Show 20 Lines • Show All 50 Lines • ▼ Show 20 Lines | static UniValue createwallet(const Config &config, | ||||
WalletLocation location(request.params[0].get_str()); | WalletLocation location(request.params[0].get_str()); | ||||
if (location.Exists()) { | if (location.Exists()) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Wallet " + location.GetName() + " already exists."); | "Wallet " + location.GetName() + " already exists."); | ||||
} | } | ||||
// Wallet::Verify will check if we're trying to create a wallet with a | // Wallet::Verify will check if we're trying to create a wallet with a | ||||
// duplicate name. | // duplicate name. | ||||
if (!CWallet::Verify(chainParams, location, false, error, warning)) { | if (!CWallet::Verify(chainParams, *g_rpc_interfaces->chain, location, false, | ||||
error, warning)) { | |||||
throw JSONRPCError(RPC_WALLET_ERROR, | throw JSONRPCError(RPC_WALLET_ERROR, | ||||
"Wallet file verification failed: " + error); | "Wallet file verification failed: " + error); | ||||
} | } | ||||
std::shared_ptr<CWallet> const wallet = | std::shared_ptr<CWallet> const wallet = CWallet::CreateWalletFromFile( | ||||
CWallet::CreateWalletFromFile(chainParams, location, flags); | chainParams, *g_rpc_interfaces->chain, location, flags); | ||||
if (!wallet) { | if (!wallet) { | ||||
throw JSONRPCError(RPC_WALLET_ERROR, "Wallet creation failed."); | throw JSONRPCError(RPC_WALLET_ERROR, "Wallet creation failed."); | ||||
} | } | ||||
AddWallet(wallet); | AddWallet(wallet); | ||||
wallet->postInitProcess(); | wallet->postInitProcess(); | ||||
UniValue obj(UniValue::VOBJ); | UniValue obj(UniValue::VOBJ); | ||||
▲ Show 20 Lines • Show All 637 Lines • ▼ Show 20 Lines | UniValue signrawtransactionwithwallet(const Config &config, | ||||
CMutableTransaction mtx; | CMutableTransaction mtx; | ||||
if (!DecodeHexTx(mtx, request.params[0].get_str())) { | if (!DecodeHexTx(mtx, request.params[0].get_str())) { | ||||
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); | throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed"); | ||||
} | } | ||||
// Sign the transaction | // Sign the transaction | ||||
LOCK2(cs_main, pwallet->cs_wallet); | LOCK2(cs_main, pwallet->cs_wallet); | ||||
return SignTransaction(mtx, request.params[1], pwallet, false, | return SignTransaction(pwallet->chain(), mtx, request.params[1], pwallet, | ||||
request.params[2]); | false, request.params[2]); | ||||
} | } | ||||
UniValue generate(const Config &config, const JSONRPCRequest &request) { | UniValue generate(const Config &config, const JSONRPCRequest &request) { | ||||
std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request); | std::shared_ptr<CWallet> const wallet = GetWalletForJSONRPCRequest(request); | ||||
CWallet *const pwallet = wallet.get(); | CWallet *const pwallet = wallet.get(); | ||||
if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { | if (!EnsureWalletIsAvailable(pwallet, request.fHelp)) { | ||||
return NullUniValue; | return NullUniValue; | ||||
▲ Show 20 Lines • Show All 974 Lines • Show Last 20 Lines |