Removed using namespace std from bitcoin-cli/-tx and added std:: in appropriate places.

This commit is contained in:
Karl-Johan Alm 2016-11-25 17:17:57 +09:00
parent bc121b0eb1
commit 2f2625a0bb
2 changed files with 108 additions and 112 deletions

View File

@ -24,15 +24,13 @@
#include <univalue.h> #include <univalue.h>
using namespace std;
static const char DEFAULT_RPCCONNECT[] = "127.0.0.1"; static const char DEFAULT_RPCCONNECT[] = "127.0.0.1";
static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900; static const int DEFAULT_HTTP_CLIENT_TIMEOUT=900;
static const int CONTINUE_EXECUTION=-1; static const int CONTINUE_EXECUTION=-1;
std::string HelpMessageCli() std::string HelpMessageCli()
{ {
string strUsage; std::string strUsage;
strUsage += HelpMessageGroup(_("Options:")); strUsage += HelpMessageGroup(_("Options:"));
strUsage += HelpMessageOpt("-?", _("This help message")); strUsage += HelpMessageOpt("-?", _("This help message"));
strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME)); strUsage += HelpMessageOpt("-conf=<file>", strprintf(_("Specify configuration file (default: %s)"), BITCOIN_CONF_FILENAME));
@ -187,7 +185,7 @@ static void http_error_cb(enum evhttp_request_error err, void *ctx)
} }
#endif #endif
UniValue CallRPC(const string& strMethod, const UniValue& params) UniValue CallRPC(const std::string& strMethod, const UniValue& params)
{ {
std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT); std::string host = GetArg("-rpcconnect", DEFAULT_RPCCONNECT);
int port = GetArg("-rpcport", BaseParams().RPCPort()); int port = GetArg("-rpcport", BaseParams().RPCPort());
@ -195,18 +193,18 @@ UniValue CallRPC(const string& strMethod, const UniValue& params)
// Create event base // Create event base
struct event_base *base = event_base_new(); // TODO RAII struct event_base *base = event_base_new(); // TODO RAII
if (!base) if (!base)
throw runtime_error("cannot create event_base"); throw std::runtime_error("cannot create event_base");
// Synchronously look up hostname // Synchronously look up hostname
struct evhttp_connection *evcon = evhttp_connection_base_new(base, NULL, host.c_str(), port); // TODO RAII struct evhttp_connection *evcon = evhttp_connection_base_new(base, NULL, host.c_str(), port); // TODO RAII
if (evcon == NULL) if (evcon == NULL)
throw runtime_error("create connection failed"); throw std::runtime_error("create connection failed");
evhttp_connection_set_timeout(evcon, GetArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT)); evhttp_connection_set_timeout(evcon, GetArg("-rpcclienttimeout", DEFAULT_HTTP_CLIENT_TIMEOUT));
HTTPReply response; HTTPReply response;
struct evhttp_request *req = evhttp_request_new(http_request_done, (void*)&response); // TODO RAII struct evhttp_request *req = evhttp_request_new(http_request_done, (void*)&response); // TODO RAII
if (req == NULL) if (req == NULL)
throw runtime_error("create http request failed"); throw std::runtime_error("create http request failed");
#if LIBEVENT_VERSION_NUMBER >= 0x02010300 #if LIBEVENT_VERSION_NUMBER >= 0x02010300
evhttp_request_set_error_cb(req, http_error_cb); evhttp_request_set_error_cb(req, http_error_cb);
#endif #endif
@ -216,7 +214,7 @@ UniValue CallRPC(const string& strMethod, const UniValue& params)
if (mapArgs["-rpcpassword"] == "") { if (mapArgs["-rpcpassword"] == "") {
// Try fall back to cookie-based authentication if no password is provided // Try fall back to cookie-based authentication if no password is provided
if (!GetAuthCookie(&strRPCUserColonPass)) { if (!GetAuthCookie(&strRPCUserColonPass)) {
throw runtime_error(strprintf( throw std::runtime_error(strprintf(
_("Could not locate RPC credentials. No authentication cookie could be found, and no rpcpassword is set in the configuration file (%s)"), _("Could not locate RPC credentials. No authentication cookie could be found, and no rpcpassword is set in the configuration file (%s)"),
GetConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME)).string().c_str())); GetConfigFile(GetArg("-conf", BITCOIN_CONF_FILENAME)).string().c_str()));
@ -251,26 +249,26 @@ UniValue CallRPC(const string& strMethod, const UniValue& params)
if (response.status == 0) if (response.status == 0)
throw CConnectionFailed(strprintf("couldn't connect to server\n(make sure server is running and you are connecting to the correct RPC port: %d %s)", response.error, http_errorstring(response.error))); throw CConnectionFailed(strprintf("couldn't connect to server\n(make sure server is running and you are connecting to the correct RPC port: %d %s)", response.error, http_errorstring(response.error)));
else if (response.status == HTTP_UNAUTHORIZED) else if (response.status == HTTP_UNAUTHORIZED)
throw runtime_error("incorrect rpcuser or rpcpassword (authorization failed)"); throw std::runtime_error("incorrect rpcuser or rpcpassword (authorization failed)");
else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR) else if (response.status >= 400 && response.status != HTTP_BAD_REQUEST && response.status != HTTP_NOT_FOUND && response.status != HTTP_INTERNAL_SERVER_ERROR)
throw runtime_error(strprintf("server returned HTTP error %d", response.status)); throw std::runtime_error(strprintf("server returned HTTP error %d", response.status));
else if (response.body.empty()) else if (response.body.empty())
throw runtime_error("no response from server"); throw std::runtime_error("no response from server");
// Parse reply // Parse reply
UniValue valReply(UniValue::VSTR); UniValue valReply(UniValue::VSTR);
if (!valReply.read(response.body)) if (!valReply.read(response.body))
throw runtime_error("couldn't parse reply from server"); throw std::runtime_error("couldn't parse reply from server");
const UniValue& reply = valReply.get_obj(); const UniValue& reply = valReply.get_obj();
if (reply.empty()) if (reply.empty())
throw runtime_error("expected reply to have result, error and id properties"); throw std::runtime_error("expected reply to have result, error and id properties");
return reply; return reply;
} }
int CommandLineRPC(int argc, char *argv[]) int CommandLineRPC(int argc, char *argv[])
{ {
string strPrint; std::string strPrint;
int nRet = 0; int nRet = 0;
try { try {
// Skip switches // Skip switches
@ -286,7 +284,7 @@ int CommandLineRPC(int argc, char *argv[])
args.push_back(line); args.push_back(line);
} }
if (args.size() < 1) if (args.size() < 1)
throw runtime_error("too few parameters (need at least command)"); throw std::runtime_error("too few parameters (need at least command)");
std::string strMethod = args[0]; std::string strMethod = args[0];
UniValue params = RPCConvertValues(strMethod, std::vector<std::string>(args.begin()+1, args.end())); UniValue params = RPCConvertValues(strMethod, std::vector<std::string>(args.begin()+1, args.end()));
@ -340,7 +338,7 @@ int CommandLineRPC(int argc, char *argv[])
throw; throw;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
strPrint = string("error: ") + e.what(); strPrint = std::string("error: ") + e.what();
nRet = EXIT_FAILURE; nRet = EXIT_FAILURE;
} }
catch (...) { catch (...) {

View File

@ -26,10 +26,8 @@
#include <boost/algorithm/string.hpp> #include <boost/algorithm/string.hpp>
#include <boost/assign/list_of.hpp> #include <boost/assign/list_of.hpp>
using namespace std;
static bool fCreateBlank; static bool fCreateBlank;
static map<string,UniValue> registers; static std::map<std::string,UniValue> registers;
static const int CONTINUE_EXECUTION=-1; static const int CONTINUE_EXECUTION=-1;
// //
@ -103,52 +101,52 @@ static int AppInitRawTx(int argc, char* argv[])
return CONTINUE_EXECUTION; return CONTINUE_EXECUTION;
} }
static void RegisterSetJson(const string& key, const string& rawJson) static void RegisterSetJson(const std::string& key, const std::string& rawJson)
{ {
UniValue val; UniValue val;
if (!val.read(rawJson)) { if (!val.read(rawJson)) {
string strErr = "Cannot parse JSON for key " + key; std::string strErr = "Cannot parse JSON for key " + key;
throw runtime_error(strErr); throw std::runtime_error(strErr);
} }
registers[key] = val; registers[key] = val;
} }
static void RegisterSet(const string& strInput) static void RegisterSet(const std::string& strInput)
{ {
// separate NAME:VALUE in string // separate NAME:VALUE in string
size_t pos = strInput.find(':'); size_t pos = strInput.find(':');
if ((pos == string::npos) || if ((pos == std::string::npos) ||
(pos == 0) || (pos == 0) ||
(pos == (strInput.size() - 1))) (pos == (strInput.size() - 1)))
throw runtime_error("Register input requires NAME:VALUE"); throw std::runtime_error("Register input requires NAME:VALUE");
string key = strInput.substr(0, pos); std::string key = strInput.substr(0, pos);
string valStr = strInput.substr(pos + 1, string::npos); std::string valStr = strInput.substr(pos + 1, std::string::npos);
RegisterSetJson(key, valStr); RegisterSetJson(key, valStr);
} }
static void RegisterLoad(const string& strInput) static void RegisterLoad(const std::string& strInput)
{ {
// separate NAME:FILENAME in string // separate NAME:FILENAME in string
size_t pos = strInput.find(':'); size_t pos = strInput.find(':');
if ((pos == string::npos) || if ((pos == std::string::npos) ||
(pos == 0) || (pos == 0) ||
(pos == (strInput.size() - 1))) (pos == (strInput.size() - 1)))
throw runtime_error("Register load requires NAME:FILENAME"); throw std::runtime_error("Register load requires NAME:FILENAME");
string key = strInput.substr(0, pos); std::string key = strInput.substr(0, pos);
string filename = strInput.substr(pos + 1, string::npos); std::string filename = strInput.substr(pos + 1, std::string::npos);
FILE *f = fopen(filename.c_str(), "r"); FILE *f = fopen(filename.c_str(), "r");
if (!f) { if (!f) {
string strErr = "Cannot open file " + filename; std::string strErr = "Cannot open file " + filename;
throw runtime_error(strErr); throw std::runtime_error(strErr);
} }
// load file chunks into one big buffer // load file chunks into one big buffer
string valStr; std::string valStr;
while ((!feof(f)) && (!ferror(f))) { while ((!feof(f)) && (!ferror(f))) {
char buf[4096]; char buf[4096];
int bread = fread(buf, 1, sizeof(buf), f); int bread = fread(buf, 1, sizeof(buf), f);
@ -162,55 +160,55 @@ static void RegisterLoad(const string& strInput)
fclose(f); fclose(f);
if (error) { if (error) {
string strErr = "Error reading file " + filename; std::string strErr = "Error reading file " + filename;
throw runtime_error(strErr); throw std::runtime_error(strErr);
} }
// evaluate as JSON buffer register // evaluate as JSON buffer register
RegisterSetJson(key, valStr); RegisterSetJson(key, valStr);
} }
static void MutateTxVersion(CMutableTransaction& tx, const string& cmdVal) static void MutateTxVersion(CMutableTransaction& tx, const std::string& cmdVal)
{ {
int64_t newVersion = atoi64(cmdVal); int64_t newVersion = atoi64(cmdVal);
if (newVersion < 1 || newVersion > CTransaction::MAX_STANDARD_VERSION) if (newVersion < 1 || newVersion > CTransaction::MAX_STANDARD_VERSION)
throw runtime_error("Invalid TX version requested"); throw std::runtime_error("Invalid TX version requested");
tx.nVersion = (int) newVersion; tx.nVersion = (int) newVersion;
} }
static void MutateTxLocktime(CMutableTransaction& tx, const string& cmdVal) static void MutateTxLocktime(CMutableTransaction& tx, const std::string& cmdVal)
{ {
int64_t newLocktime = atoi64(cmdVal); int64_t newLocktime = atoi64(cmdVal);
if (newLocktime < 0LL || newLocktime > 0xffffffffLL) if (newLocktime < 0LL || newLocktime > 0xffffffffLL)
throw runtime_error("Invalid TX locktime requested"); throw std::runtime_error("Invalid TX locktime requested");
tx.nLockTime = (unsigned int) newLocktime; tx.nLockTime = (unsigned int) newLocktime;
} }
static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput) static void MutateTxAddInput(CMutableTransaction& tx, const std::string& strInput)
{ {
std::vector<std::string> vStrInputParts; std::vector<std::string> vStrInputParts;
boost::split(vStrInputParts, strInput, boost::is_any_of(":")); boost::split(vStrInputParts, strInput, boost::is_any_of(":"));
// separate TXID:VOUT in string // separate TXID:VOUT in string
if (vStrInputParts.size()<2) if (vStrInputParts.size()<2)
throw runtime_error("TX input missing separator"); throw std::runtime_error("TX input missing separator");
// extract and validate TXID // extract and validate TXID
string strTxid = vStrInputParts[0]; std::string strTxid = vStrInputParts[0];
if ((strTxid.size() != 64) || !IsHex(strTxid)) if ((strTxid.size() != 64) || !IsHex(strTxid))
throw runtime_error("invalid TX input txid"); throw std::runtime_error("invalid TX input txid");
uint256 txid(uint256S(strTxid)); uint256 txid(uint256S(strTxid));
static const unsigned int minTxOutSz = 9; static const unsigned int minTxOutSz = 9;
static const unsigned int maxVout = MAX_BLOCK_BASE_SIZE / minTxOutSz; static const unsigned int maxVout = MAX_BLOCK_BASE_SIZE / minTxOutSz;
// extract and validate vout // extract and validate vout
string strVout = vStrInputParts[1]; std::string strVout = vStrInputParts[1];
int vout = atoi(strVout); int vout = atoi(strVout);
if ((vout < 0) || (vout > (int)maxVout)) if ((vout < 0) || (vout > (int)maxVout))
throw runtime_error("invalid TX input vout"); throw std::runtime_error("invalid TX input vout");
// extract the optional sequence number // extract the optional sequence number
uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max(); uint32_t nSequenceIn=std::numeric_limits<unsigned int>::max();
@ -222,26 +220,26 @@ static void MutateTxAddInput(CMutableTransaction& tx, const string& strInput)
tx.vin.push_back(txin); tx.vin.push_back(txin);
} }
static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput) static void MutateTxAddOutAddr(CMutableTransaction& tx, const std::string& strInput)
{ {
// separate VALUE:ADDRESS in string // separate VALUE:ADDRESS in string
size_t pos = strInput.find(':'); size_t pos = strInput.find(':');
if ((pos == string::npos) || if ((pos == std::string::npos) ||
(pos == 0) || (pos == 0) ||
(pos == (strInput.size() - 1))) (pos == (strInput.size() - 1)))
throw runtime_error("TX output missing separator"); throw std::runtime_error("TX output missing separator");
// extract and validate VALUE // extract and validate VALUE
string strValue = strInput.substr(0, pos); std::string strValue = strInput.substr(0, pos);
CAmount value; CAmount value;
if (!ParseMoney(strValue, value)) if (!ParseMoney(strValue, value))
throw runtime_error("invalid TX output value"); throw std::runtime_error("invalid TX output value");
// extract and validate ADDRESS // extract and validate ADDRESS
string strAddr = strInput.substr(pos + 1, string::npos); std::string strAddr = strInput.substr(pos + 1, std::string::npos);
CBitcoinAddress addr(strAddr); CBitcoinAddress addr(strAddr);
if (!addr.IsValid()) if (!addr.IsValid())
throw runtime_error("invalid TX output address"); throw std::runtime_error("invalid TX output address");
// build standard output script via GetScriptForDestination() // build standard output script via GetScriptForDestination()
CScript scriptPubKey = GetScriptForDestination(addr.Get()); CScript scriptPubKey = GetScriptForDestination(addr.Get());
@ -251,7 +249,7 @@ static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
tx.vout.push_back(txout); tx.vout.push_back(txout);
} }
static void MutateTxAddOutData(CMutableTransaction& tx, const string& strInput) static void MutateTxAddOutData(CMutableTransaction& tx, const std::string& strInput)
{ {
CAmount value = 0; CAmount value = 0;
@ -259,20 +257,20 @@ static void MutateTxAddOutData(CMutableTransaction& tx, const string& strInput)
size_t pos = strInput.find(':'); size_t pos = strInput.find(':');
if (pos==0) if (pos==0)
throw runtime_error("TX output value not specified"); throw std::runtime_error("TX output value not specified");
if (pos != string::npos) { if (pos != std::string::npos) {
// extract and validate VALUE // extract and validate VALUE
string strValue = strInput.substr(0, pos); std::string strValue = strInput.substr(0, pos);
if (!ParseMoney(strValue, value)) if (!ParseMoney(strValue, value))
throw runtime_error("invalid TX output value"); throw std::runtime_error("invalid TX output value");
} }
// extract and validate DATA // extract and validate DATA
string strData = strInput.substr(pos + 1, string::npos); std::string strData = strInput.substr(pos + 1, std::string::npos);
if (!IsHex(strData)) if (!IsHex(strData))
throw runtime_error("invalid TX output data"); throw std::runtime_error("invalid TX output data");
std::vector<unsigned char> data = ParseHex(strData); std::vector<unsigned char> data = ParseHex(strData);
@ -280,22 +278,22 @@ static void MutateTxAddOutData(CMutableTransaction& tx, const string& strInput)
tx.vout.push_back(txout); tx.vout.push_back(txout);
} }
static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput) static void MutateTxAddOutScript(CMutableTransaction& tx, const std::string& strInput)
{ {
// separate VALUE:SCRIPT in string // separate VALUE:SCRIPT in string
size_t pos = strInput.find(':'); size_t pos = strInput.find(':');
if ((pos == string::npos) || if ((pos == std::string::npos) ||
(pos == 0)) (pos == 0))
throw runtime_error("TX output missing separator"); throw std::runtime_error("TX output missing separator");
// extract and validate VALUE // extract and validate VALUE
string strValue = strInput.substr(0, pos); std::string strValue = strInput.substr(0, pos);
CAmount value; CAmount value;
if (!ParseMoney(strValue, value)) if (!ParseMoney(strValue, value))
throw runtime_error("invalid TX output value"); throw std::runtime_error("invalid TX output value");
// extract and validate script // extract and validate script
string strScript = strInput.substr(pos + 1, string::npos); std::string strScript = strInput.substr(pos + 1, std::string::npos);
CScript scriptPubKey = ParseScript(strScript); // throws on err CScript scriptPubKey = ParseScript(strScript); // throws on err
// construct TxOut, append to transaction output list // construct TxOut, append to transaction output list
@ -303,26 +301,26 @@ static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput
tx.vout.push_back(txout); tx.vout.push_back(txout);
} }
static void MutateTxDelInput(CMutableTransaction& tx, const string& strInIdx) static void MutateTxDelInput(CMutableTransaction& tx, const std::string& strInIdx)
{ {
// parse requested deletion index // parse requested deletion index
int inIdx = atoi(strInIdx); int inIdx = atoi(strInIdx);
if (inIdx < 0 || inIdx >= (int)tx.vin.size()) { if (inIdx < 0 || inIdx >= (int)tx.vin.size()) {
string strErr = "Invalid TX input index '" + strInIdx + "'"; std::string strErr = "Invalid TX input index '" + strInIdx + "'";
throw runtime_error(strErr.c_str()); throw std::runtime_error(strErr.c_str());
} }
// delete input from transaction // delete input from transaction
tx.vin.erase(tx.vin.begin() + inIdx); tx.vin.erase(tx.vin.begin() + inIdx);
} }
static void MutateTxDelOutput(CMutableTransaction& tx, const string& strOutIdx) static void MutateTxDelOutput(CMutableTransaction& tx, const std::string& strOutIdx)
{ {
// parse requested deletion index // parse requested deletion index
int outIdx = atoi(strOutIdx); int outIdx = atoi(strOutIdx);
if (outIdx < 0 || outIdx >= (int)tx.vout.size()) { if (outIdx < 0 || outIdx >= (int)tx.vout.size()) {
string strErr = "Invalid TX output index '" + strOutIdx + "'"; std::string strErr = "Invalid TX output index '" + strOutIdx + "'";
throw runtime_error(strErr.c_str()); throw std::runtime_error(strErr.c_str());
} }
// delete output from transaction // delete output from transaction
@ -342,7 +340,7 @@ static const struct {
{"SINGLE|ANYONECANPAY", SIGHASH_SINGLE|SIGHASH_ANYONECANPAY}, {"SINGLE|ANYONECANPAY", SIGHASH_SINGLE|SIGHASH_ANYONECANPAY},
}; };
static bool findSighashFlags(int& flags, const string& flagStr) static bool findSighashFlags(int& flags, const std::string& flagStr)
{ {
flags = 0; flags = 0;
@ -356,17 +354,17 @@ static bool findSighashFlags(int& flags, const string& flagStr)
return false; return false;
} }
uint256 ParseHashUO(map<string,UniValue>& o, string strKey) uint256 ParseHashUO(std::map<std::string,UniValue>& o, std::string strKey)
{ {
if (!o.count(strKey)) if (!o.count(strKey))
return uint256(); return uint256();
return ParseHashUV(o[strKey], strKey); return ParseHashUV(o[strKey], strKey);
} }
vector<unsigned char> ParseHexUO(map<string,UniValue>& o, string strKey) std::vector<unsigned char> ParseHexUO(std::map<std::string,UniValue>& o, std::string strKey)
{ {
if (!o.count(strKey)) { if (!o.count(strKey)) {
vector<unsigned char> emptyVec; std::vector<unsigned char> emptyVec;
return emptyVec; return emptyVec;
} }
return ParseHexUV(o[strKey], strKey); return ParseHexUV(o[strKey], strKey);
@ -375,24 +373,24 @@ vector<unsigned char> ParseHexUO(map<string,UniValue>& o, string strKey)
static CAmount AmountFromValue(const UniValue& value) static CAmount AmountFromValue(const UniValue& value)
{ {
if (!value.isNum() && !value.isStr()) if (!value.isNum() && !value.isStr())
throw runtime_error("Amount is not a number or string"); throw std::runtime_error("Amount is not a number or string");
CAmount amount; CAmount amount;
if (!ParseFixedPoint(value.getValStr(), 8, &amount)) if (!ParseFixedPoint(value.getValStr(), 8, &amount))
throw runtime_error("Invalid amount"); throw std::runtime_error("Invalid amount");
if (!MoneyRange(amount)) if (!MoneyRange(amount))
throw runtime_error("Amount out of range"); throw std::runtime_error("Amount out of range");
return amount; return amount;
} }
static void MutateTxSign(CMutableTransaction& tx, const string& flagStr) static void MutateTxSign(CMutableTransaction& tx, const std::string& flagStr)
{ {
int nHashType = SIGHASH_ALL; int nHashType = SIGHASH_ALL;
if (flagStr.size() > 0) if (flagStr.size() > 0)
if (!findSighashFlags(nHashType, flagStr)) if (!findSighashFlags(nHashType, flagStr))
throw runtime_error("unknown sighash flag/sign option"); throw std::runtime_error("unknown sighash flag/sign option");
vector<CTransaction> txVariants; std::vector<CTransaction> txVariants;
txVariants.push_back(tx); txVariants.push_back(tx);
// mergedTx will end up with all the signatures; it // mergedTx will end up with all the signatures; it
@ -403,17 +401,17 @@ static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
CCoinsViewCache view(&viewDummy); CCoinsViewCache view(&viewDummy);
if (!registers.count("privatekeys")) if (!registers.count("privatekeys"))
throw runtime_error("privatekeys register variable must be set."); throw std::runtime_error("privatekeys register variable must be set.");
CBasicKeyStore tempKeystore; CBasicKeyStore tempKeystore;
UniValue keysObj = registers["privatekeys"]; UniValue keysObj = registers["privatekeys"];
for (unsigned int kidx = 0; kidx < keysObj.size(); kidx++) { for (unsigned int kidx = 0; kidx < keysObj.size(); kidx++) {
if (!keysObj[kidx].isStr()) if (!keysObj[kidx].isStr())
throw runtime_error("privatekey not a string"); throw std::runtime_error("privatekey not a std::string");
CBitcoinSecret vchSecret; CBitcoinSecret vchSecret;
bool fGood = vchSecret.SetString(keysObj[kidx].getValStr()); bool fGood = vchSecret.SetString(keysObj[kidx].getValStr());
if (!fGood) if (!fGood)
throw runtime_error("privatekey not valid"); throw std::runtime_error("privatekey not valid");
CKey key = vchSecret.GetKey(); CKey key = vchSecret.GetKey();
tempKeystore.AddKey(key); tempKeystore.AddKey(key);
@ -421,34 +419,34 @@ static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
// Add previous txouts given in the RPC call: // Add previous txouts given in the RPC call:
if (!registers.count("prevtxs")) if (!registers.count("prevtxs"))
throw runtime_error("prevtxs register variable must be set."); throw std::runtime_error("prevtxs register variable must be set.");
UniValue prevtxsObj = registers["prevtxs"]; UniValue prevtxsObj = registers["prevtxs"];
{ {
for (unsigned int previdx = 0; previdx < prevtxsObj.size(); previdx++) { for (unsigned int previdx = 0; previdx < prevtxsObj.size(); previdx++) {
UniValue prevOut = prevtxsObj[previdx]; UniValue prevOut = prevtxsObj[previdx];
if (!prevOut.isObject()) if (!prevOut.isObject())
throw runtime_error("expected prevtxs internal object"); throw std::runtime_error("expected prevtxs internal object");
map<string,UniValue::VType> types = boost::assign::map_list_of("txid", UniValue::VSTR)("vout",UniValue::VNUM)("scriptPubKey",UniValue::VSTR); std::map<std::string,UniValue::VType> types = boost::assign::map_list_of("txid", UniValue::VSTR)("vout",UniValue::VNUM)("scriptPubKey",UniValue::VSTR);
if (!prevOut.checkObject(types)) if (!prevOut.checkObject(types))
throw runtime_error("prevtxs internal object typecheck fail"); throw std::runtime_error("prevtxs internal object typecheck fail");
uint256 txid = ParseHashUV(prevOut["txid"], "txid"); uint256 txid = ParseHashUV(prevOut["txid"], "txid");
int nOut = atoi(prevOut["vout"].getValStr()); int nOut = atoi(prevOut["vout"].getValStr());
if (nOut < 0) if (nOut < 0)
throw runtime_error("vout must be positive"); throw std::runtime_error("vout must be positive");
vector<unsigned char> pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey")); std::vector<unsigned char> pkData(ParseHexUV(prevOut["scriptPubKey"], "scriptPubKey"));
CScript scriptPubKey(pkData.begin(), pkData.end()); CScript scriptPubKey(pkData.begin(), pkData.end());
{ {
CCoinsModifier coins = view.ModifyCoins(txid); CCoinsModifier coins = view.ModifyCoins(txid);
if (coins->IsAvailable(nOut) && coins->vout[nOut].scriptPubKey != scriptPubKey) { if (coins->IsAvailable(nOut) && coins->vout[nOut].scriptPubKey != scriptPubKey) {
string err("Previous output scriptPubKey mismatch:\n"); std::string err("Previous output scriptPubKey mismatch:\n");
err = err + ScriptToAsmStr(coins->vout[nOut].scriptPubKey) + "\nvs:\n"+ err = err + ScriptToAsmStr(coins->vout[nOut].scriptPubKey) + "\nvs:\n"+
ScriptToAsmStr(scriptPubKey); ScriptToAsmStr(scriptPubKey);
throw runtime_error(err); throw std::runtime_error(err);
} }
if ((unsigned int)nOut >= coins->vout.size()) if ((unsigned int)nOut >= coins->vout.size())
coins->vout.resize(nOut+1); coins->vout.resize(nOut+1);
@ -464,7 +462,7 @@ static void MutateTxSign(CMutableTransaction& tx, const string& flagStr)
if ((scriptPubKey.IsPayToScriptHash() || scriptPubKey.IsPayToWitnessScriptHash()) && if ((scriptPubKey.IsPayToScriptHash() || scriptPubKey.IsPayToWitnessScriptHash()) &&
prevOut.exists("redeemScript")) { prevOut.exists("redeemScript")) {
UniValue v = prevOut["redeemScript"]; UniValue v = prevOut["redeemScript"];
vector<unsigned char> rsData(ParseHexUV(v, "redeemScript")); std::vector<unsigned char> rsData(ParseHexUV(v, "redeemScript"));
CScript redeemScript(rsData.begin(), rsData.end()); CScript redeemScript(rsData.begin(), rsData.end());
tempKeystore.AddCScript(redeemScript); tempKeystore.AddCScript(redeemScript);
} }
@ -521,8 +519,8 @@ public:
} }
}; };
static void MutateTx(CMutableTransaction& tx, const string& command, static void MutateTx(CMutableTransaction& tx, const std::string& command,
const string& commandVal) const std::string& commandVal)
{ {
std::unique_ptr<Secp256k1Init> ecc; std::unique_ptr<Secp256k1Init> ecc;
@ -557,7 +555,7 @@ static void MutateTx(CMutableTransaction& tx, const string& command,
RegisterSet(commandVal); RegisterSet(commandVal);
else else
throw runtime_error("unknown command"); throw std::runtime_error("unknown command");
} }
static void OutputTxJSON(const CTransaction& tx) static void OutputTxJSON(const CTransaction& tx)
@ -565,20 +563,20 @@ static void OutputTxJSON(const CTransaction& tx)
UniValue entry(UniValue::VOBJ); UniValue entry(UniValue::VOBJ);
TxToUniv(tx, uint256(), entry); TxToUniv(tx, uint256(), entry);
string jsonOutput = entry.write(4); std::string jsonOutput = entry.write(4);
fprintf(stdout, "%s\n", jsonOutput.c_str()); fprintf(stdout, "%s\n", jsonOutput.c_str());
} }
static void OutputTxHash(const CTransaction& tx) static void OutputTxHash(const CTransaction& tx)
{ {
string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id) std::string strHexHash = tx.GetHash().GetHex(); // the hex-encoded transaction hash (aka the transaction id)
fprintf(stdout, "%s\n", strHexHash.c_str()); fprintf(stdout, "%s\n", strHexHash.c_str());
} }
static void OutputTxHex(const CTransaction& tx) static void OutputTxHex(const CTransaction& tx)
{ {
string strHex = EncodeHexTx(tx); std::string strHex = EncodeHexTx(tx);
fprintf(stdout, "%s\n", strHex.c_str()); fprintf(stdout, "%s\n", strHex.c_str());
} }
@ -593,10 +591,10 @@ static void OutputTx(const CTransaction& tx)
OutputTxHex(tx); OutputTxHex(tx);
} }
static string readStdin() static std::string readStdin()
{ {
char buf[4096]; char buf[4096];
string ret; std::string ret;
while (!feof(stdin)) { while (!feof(stdin)) {
size_t bread = fread(buf, 1, sizeof(buf), stdin); size_t bread = fread(buf, 1, sizeof(buf), stdin);
@ -606,7 +604,7 @@ static string readStdin()
} }
if (ferror(stdin)) if (ferror(stdin))
throw runtime_error("error reading stdin"); throw std::runtime_error("error reading stdin");
boost::algorithm::trim_right(ret); boost::algorithm::trim_right(ret);
@ -615,7 +613,7 @@ static string readStdin()
static int CommandLineRawTx(int argc, char* argv[]) static int CommandLineRawTx(int argc, char* argv[])
{ {
string strPrint; std::string strPrint;
int nRet = 0; int nRet = 0;
try { try {
// Skip switches; Permit common stdin convention "-" // Skip switches; Permit common stdin convention "-"
@ -631,15 +629,15 @@ static int CommandLineRawTx(int argc, char* argv[])
if (!fCreateBlank) { if (!fCreateBlank) {
// require at least one param // require at least one param
if (argc < 2) if (argc < 2)
throw runtime_error("too few parameters"); throw std::runtime_error("too few parameters");
// param: hex-encoded bitcoin transaction // param: hex-encoded bitcoin transaction
string strHexTx(argv[1]); std::string strHexTx(argv[1]);
if (strHexTx == "-") // "-" implies standard input if (strHexTx == "-") // "-" implies standard input
strHexTx = readStdin(); strHexTx = readStdin();
if (!DecodeHexTx(txDecodeTmp, strHexTx, true)) if (!DecodeHexTx(txDecodeTmp, strHexTx, true))
throw runtime_error("invalid transaction encoding"); throw std::runtime_error("invalid transaction encoding");
startArg = 2; startArg = 2;
} else } else
@ -648,10 +646,10 @@ static int CommandLineRawTx(int argc, char* argv[])
CMutableTransaction tx(txDecodeTmp); CMutableTransaction tx(txDecodeTmp);
for (int i = startArg; i < argc; i++) { for (int i = startArg; i < argc; i++) {
string arg = argv[i]; std::string arg = argv[i];
string key, value; std::string key, value;
size_t eqpos = arg.find('='); size_t eqpos = arg.find('=');
if (eqpos == string::npos) if (eqpos == std::string::npos)
key = arg; key = arg;
else { else {
key = arg.substr(0, eqpos); key = arg.substr(0, eqpos);
@ -668,7 +666,7 @@ static int CommandLineRawTx(int argc, char* argv[])
throw; throw;
} }
catch (const std::exception& e) { catch (const std::exception& e) {
strPrint = string("error: ") + e.what(); strPrint = std::string("error: ") + e.what();
nRet = EXIT_FAILURE; nRet = EXIT_FAILURE;
} }
catch (...) { catch (...) {