Browse Source

rpc cleanups

miguelfreitas
Miguel Freitas 11 years ago
parent
commit
460be48e63
  1. 13
      src/bitcoinrpc.cpp
  2. 5
      src/bitcoinrpc.h
  3. 185
      src/rpcrawtransaction.cpp
  4. 23
      src/rpcwallet.cpp
  5. 22
      src/twister.cpp

13
src/bitcoinrpc.cpp

@ -206,8 +206,8 @@ static const CRPCCommand vRPCCommands[] = @@ -206,8 +206,8 @@ static const CRPCCommand vRPCCommands[] =
{ "gethashespersec", &gethashespersec, true, false },
{ "getinfo", &getinfo, true, false },
{ "getmininginfo", &getmininginfo, true, false },
{ "createuserkey", &createuserkey, true, false },
{ "listusernames", &listusernames, true, false },
{ "createwalletuser", &createwalletuser, true, false },
{ "listwalletusers", &listwalletusers, true, false },
{ "backupwallet", &backupwallet, true, false },
{ "walletpassphrase", &walletpassphrase, true, false },
{ "walletpassphrasechange", &walletpassphrasechange, false, false },
@ -231,7 +231,6 @@ static const CRPCCommand vRPCCommands[] = @@ -231,7 +231,6 @@ static const CRPCCommand vRPCCommands[] =
{ "getrawtransaction", &getrawtransaction, false, false },
{ "createrawtransaction", &createrawtransaction, false, false },
{ "decoderawtransaction", &decoderawtransaction, false, false },
{ "signrawtransaction", &signrawtransaction, false, false },
{ "sendrawtransaction", &sendrawtransaction, false, false },
{ "sendnewusertransaction", &sendnewusertransaction, false, false },
{ "verifychain", &verifychain, true, false },
@ -1172,8 +1171,6 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri @@ -1172,8 +1171,6 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "listunspent" && n > 2) ConvertTo<Array>(params[2]);
if (strMethod == "getblock" && n > 1) ConvertTo<bool>(params[1]);
if (strMethod == "getrawtransaction" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "signrawtransaction" && n > 1) ConvertTo<Array>(params[1], true);
if (strMethod == "signrawtransaction" && n > 2) ConvertTo<Array>(params[2], true);
if (strMethod == "gettxout" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "gettxout" && n > 2) ConvertTo<bool>(params[2]);
if (strMethod == "lockunspent" && n > 0) ConvertTo<bool>(params[0]);
@ -1181,6 +1178,12 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri @@ -1181,6 +1178,12 @@ Array RPCConvertValues(const std::string &strMethod, const std::vector<std::stri
if (strMethod == "importprivkey" && n > 2) ConvertTo<bool>(params[2]);
if (strMethod == "verifychain" && n > 0) ConvertTo<boost::int64_t>(params[0]);
if (strMethod == "verifychain" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "dhtput" && n > 5) ConvertTo<boost::int64_t>(params[5]);
if (strMethod == "newpostmsg" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "newpostmsg" && n > 4) ConvertTo<boost::int64_t>(params[4]);
if (strMethod == "newdirectmsg" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "newrtmsg" && n > 1) ConvertTo<boost::int64_t>(params[1]);
if (strMethod == "newrtmsg" && n > 2) ConvertTo<Object>(params[2]);
return params;
}

5
src/bitcoinrpc.h

@ -158,8 +158,8 @@ extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp); @@ -158,8 +158,8 @@ extern json_spirit::Value getwork(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value getblocktemplate(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value submitblock(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value createuserkey(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
extern json_spirit::Value listusernames(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value createwalletuser(const json_spirit::Array& params, bool fHelp); // in rpcwallet.cpp
extern json_spirit::Value listwalletusers(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value signmessage(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value verifymessage(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value listtransactions(const json_spirit::Array& params, bool fHelp);
@ -179,7 +179,6 @@ extern json_spirit::Value lockunspent(const json_spirit::Array& params, bool fHe @@ -179,7 +179,6 @@ extern json_spirit::Value lockunspent(const json_spirit::Array& params, bool fHe
extern json_spirit::Value listlockunspent(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value createrawtransaction(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value decoderawtransaction(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value signrawtransaction(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value sendrawtransaction(const json_spirit::Array& params, bool fHelp);
extern json_spirit::Value sendnewusertransaction(const json_spirit::Array& params, bool fHelp);

185
src/rpcrawtransaction.cpp

@ -196,189 +196,6 @@ Value decoderawtransaction(const Array& params, bool fHelp) @@ -196,189 +196,6 @@ Value decoderawtransaction(const Array& params, bool fHelp)
return result;
}
Value signrawtransaction(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 4)
throw runtime_error(
"signrawtransaction <hex string> [{\"txid\":txid,\"vout\":n,\"scriptPubKey\":hex,\"redeemScript\":hex},...] [<privatekey1>,...] [sighashtype=\"ALL\"]\n"
"Sign inputs for raw transaction (serialized, hex-encoded).\n"
"Second optional argument (may be null) is an array of previous transaction outputs that\n"
"this transaction depends on but may not yet be in the block chain.\n"
"Third optional argument (may be null) is an array of base58-encoded private\n"
"keys that, if given, will be the only keys used to sign the transaction.\n"
"Fourth optional argument is a string that is one of six values; ALL, NONE, SINGLE or\n"
"ALL|ANYONECANPAY, NONE|ANYONECANPAY, SINGLE|ANYONECANPAY.\n"
"Returns json object with keys:\n"
" hex : raw transaction with signature(s) (hex-encoded string)\n"
" complete : 1 if transaction has a complete set of signature (0 if not)"
+ HelpRequiringPassphrase());
RPCTypeCheck(params, list_of(str_type)(array_type)(array_type)(str_type), true);
vector<unsigned char> txData(ParseHexV(params[0], "argument 1"));
CDataStream ssData(txData, SER_NETWORK, PROTOCOL_VERSION);
vector<CTransaction> txVariants;
while (!ssData.empty())
{
try {
CTransaction tx;
ssData >> tx;
txVariants.push_back(tx);
}
catch (std::exception &e) {
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "TX decode failed");
}
}
if (txVariants.empty())
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "Missing transaction");
// mergedTx will end up with all the signatures; it
// starts as a clone of the rawtx:
CTransaction mergedTx(txVariants[0]);
bool fComplete = true;
// Fetch previous transactions (inputs):
CCoinsView viewDummy;
CCoinsViewCache view(viewDummy);
{
LOCK(mempool.cs);
CCoinsViewCache &viewChain = *pcoinsTip;
CCoinsViewMemPool viewMempool(viewChain, mempool);
view.SetBackend(viewMempool); // temporarily switch cache backend to db+mempool view
/*
BOOST_FOREACH(const CTxIn& txin, mergedTx.vin) {
const uint256& prevHash = txin.prevout.hash;
CCoins coins;
view.GetCoins(prevHash, coins); // this is certainly allowed to fail
}
*/
view.SetBackend(viewDummy); // switch back to avoid locking mempool for too long
}
bool fGivenKeys = false;
CBasicKeyStore tempKeystore;
if (params.size() > 2 && params[2].type() != null_type)
{
fGivenKeys = true;
Array keys = params[2].get_array();
BOOST_FOREACH(Value k, keys)
{
CBitcoinSecret vchSecret;
bool fGood = vchSecret.SetString(k.get_str());
if (!fGood)
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, "Invalid private key");
CKey key = vchSecret.GetKey();
tempKeystore.AddKey(key);
}
}
else
EnsureWalletIsUnlocked();
// Add previous txouts given in the RPC call:
if (params.size() > 1 && params[1].type() != null_type)
{
Array prevTxs = params[1].get_array();
BOOST_FOREACH(Value& p, prevTxs)
{
if (p.type() != obj_type)
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "expected object with {\"txid'\",\"vout\",\"scriptPubKey\"}");
Object prevOut = p.get_obj();
RPCTypeCheck(prevOut, map_list_of("txid", str_type)("vout", int_type)("scriptPubKey", str_type));
uint256 txid = ParseHashO(prevOut, "txid");
int nOut = find_value(prevOut, "vout").get_int();
if (nOut < 0)
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, "vout must be positive");
vector<unsigned char> pkData(ParseHexO(prevOut, "scriptPubKey"));
CScript scriptPubKey(pkData.begin(), pkData.end());
CCoins coins;
/*
if (view.GetCoins(txid, coins)) {
if (coins.IsAvailable(nOut) && coins.vout[nOut].scriptPubKey != scriptPubKey) {
string err("Previous output scriptPubKey mismatch:\n");
err = err + coins.vout[nOut].scriptPubKey.ToString() + "\nvs:\n"+
scriptPubKey.ToString();
throw JSONRPCError(RPC_DESERIALIZATION_ERROR, err);
}
// what todo if txid is known, but the actual output isn't?
}
if ((unsigned int)nOut >= coins.vout.size())
coins.vout.resize(nOut+1);
coins.vout[nOut].scriptPubKey = scriptPubKey;
coins.vout[nOut].nValue = 0; // we don't know the actual output value
view.SetCoins(txid, coins);
*/
// if redeemScript given and not using the local wallet (private keys
// given), add redeemScript to the tempKeystore so it can be signed:
}
}
const CKeyStore& keystore = (fGivenKeys ? tempKeystore : *pwalletMain);
int nHashType = SIGHASH_ALL;
if (params.size() > 3 && params[3].type() != null_type)
{
static map<string, int> mapSigHashValues =
boost::assign::map_list_of
(string("ALL"), int(SIGHASH_ALL))
(string("ALL|ANYONECANPAY"), int(SIGHASH_ALL|SIGHASH_ANYONECANPAY))
(string("NONE"), int(SIGHASH_NONE))
(string("NONE|ANYONECANPAY"), int(SIGHASH_NONE|SIGHASH_ANYONECANPAY))
(string("SINGLE"), int(SIGHASH_SINGLE))
(string("SINGLE|ANYONECANPAY"), int(SIGHASH_SINGLE|SIGHASH_ANYONECANPAY))
;
string strHashType = params[3].get_str();
if (mapSigHashValues.count(strHashType))
nHashType = mapSigHashValues[strHashType];
else
throw JSONRPCError(RPC_INVALID_PARAMETER, "Invalid sighash param");
}
// Sign what we can:
/* [MF]
for (unsigned int i = 0; i < mergedTx.vin.size(); i++)
{
CTxIn& txin = mergedTx.vin[i];
CCoins coins;
if (!view.GetCoins(txin.prevout.hash, coins) || !coins.IsAvailable(txin.prevout.n))
{
fComplete = false;
continue;
}
const CScript& prevPubKey = coins.vout[txin.prevout.n].scriptPubKey;
txin.scriptSig.clear();
// Only sign SIGHASH_SINGLE if there's a corresponding output:
if (!fHashSingle || (i < mergedTx.vout.size()))
SignSignature(keystore, prevPubKey, mergedTx, i, nHashType);
// ... and merge in other signatures:
BOOST_FOREACH(const CTransaction& txv, txVariants)
{
txin.scriptSig = CombineSignatures(prevPubKey, mergedTx, i, txin.scriptSig, txv.vin[i].scriptSig);
}
if (!VerifyScript(txin.scriptSig, prevPubKey, mergedTx, i, SCRIPT_VERIFY_P2SH | SCRIPT_VERIFY_STRICTENC, 0))
fComplete = false;
}
*/
Object result;
CDataStream ssTx(SER_NETWORK, PROTOCOL_VERSION);
ssTx << mergedTx;
result.push_back(Pair("hex", HexStr(ssTx.begin(), ssTx.end())));
result.push_back(Pair("complete", fComplete));
return result;
}
Value sendrawtransaction(const Array& params, bool fHelp)
{
if (fHelp || params.size() < 1 || params.size() > 1)
@ -431,7 +248,7 @@ Value sendnewusertransaction(const Array& params, bool fHelp) @@ -431,7 +248,7 @@ Value sendnewusertransaction(const Array& params, bool fHelp)
throw runtime_error(
"sendnewusertransaction <username>\n"
"Send a transaction registering a previously created new user\n"
"using createuserkey or imported to the wallet\n"
"using createwalletuser or imported to the wallet\n"
"Submits raw transaction (serialized, hex-encoded) to local node and network.");
if (params[0].type() != str_type)

23
src/rpcwallet.cpp

@ -88,14 +88,14 @@ Value getinfo(const Array& params, bool fHelp) @@ -88,14 +88,14 @@ Value getinfo(const Array& params, bool fHelp)
Value createuserkey(const Array& params, bool fHelp)
Value createwalletuser(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 1)
throw runtime_error(
"createuserkey <username>\n"
"Returns a new Bitcoin address for receiving payments. "
"If [account] is specified (recommended), it is added to the address book "
"so payments received with the address will be credited to [account].");
"createwalletuser <username>\n"
"Create a new key pair for user and add it to wallet\n"
"Use sendnewusertransaction to publish it to the network.\n"
"Returns key secret (keep it safe)");
EnsureWalletIsUnlocked();
@ -106,7 +106,7 @@ Value createuserkey(const Array& params, bool fHelp) @@ -106,7 +106,7 @@ Value createuserkey(const Array& params, bool fHelp)
throw JSONRPCError(RPC_WALLET_INVALID_ACCOUNT_NAME, "Error: this username exists in wallet");
uint256 userhash = SerializeHash(strUsername);
printf("createuserkey: usernamehash(%s) = %s\n", strUsername.c_str(), userhash.GetHex().c_str());
printf("createwalletuser: usernamehash(%s) = %s\n", strUsername.c_str(), userhash.GetHex().c_str());
CTransaction txOut;
uint256 hashBlock;
@ -117,16 +117,19 @@ Value createuserkey(const Array& params, bool fHelp) @@ -117,16 +117,19 @@ Value createuserkey(const Array& params, bool fHelp)
CPubKey newKey = pwalletMain->GenerateNewKey(strUsername);
keyID = newKey.GetID();
return CBitcoinAddress(keyID).ToString();
CKey vchSecret;
if (!pwalletMain->GetKey(keyID, vchSecret))
throw JSONRPCError(RPC_WALLET_ERROR, "Error: could not obtain just created privkey?!");
return CBitcoinSecret(vchSecret).ToString();
}
Value listusernames(const Array& params, bool fHelp)
Value listwalletusers(const Array& params, bool fHelp)
{
if (fHelp || params.size() != 0)
throw runtime_error(
"listusernames\n"
"Returns the list of usernames.");
"listwalletusers\n"
"Returns the list of wallet usernames.");
// Find all addresses that have the given account
Array ret;

22
src/twister.cpp

@ -9,6 +9,7 @@ using namespace std; @@ -9,6 +9,7 @@ using namespace std;
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <time.h>
@ -857,8 +858,7 @@ Value dhtput(const Array& params, bool fHelp) @@ -857,8 +858,7 @@ Value dhtput(const Array& params, bool fHelp)
if( !multi && params.size() != 6 )
throw JSONRPCError(RPC_WALLET_ERROR, "Seq parameter required for single");
int seq = -1;
if( params.size() == 6 ) seq = atoi( params[5].get_str().c_str() );
int seq = params[5].get_int();
if( !multi && strUsername != strSigUser )
throw JSONRPCError(RPC_WALLET_ERROR, "Username must be the same as sig_user for single");
@ -925,15 +925,16 @@ Value newpostmsg(const Array& params, bool fHelp) @@ -925,15 +925,16 @@ Value newpostmsg(const Array& params, bool fHelp)
EnsureWalletIsUnlocked();
string strUsername = params[0].get_str();
string strK = params[1].get_str();
int k = params[1].get_int();
string strK = boost::lexical_cast<std::string>(k);
string strMsg = params[2].get_str();
int k = atoi( strK.c_str() );
string strReplyN, strReplyK;
int replyK = 0;
if( params.size() > 3 ) {
if( params.size() == 5 ) {
strReplyN = params[3].get_str();
strReplyK = params[4].get_str();
replyK = atoi( strReplyK.c_str() );
replyK = params[4].get_int();
strReplyK = boost::lexical_cast<std::string>(replyK);
}
entry v;
@ -999,10 +1000,9 @@ Value newdirectmsg(const Array& params, bool fHelp) @@ -999,10 +1000,9 @@ Value newdirectmsg(const Array& params, bool fHelp)
EnsureWalletIsUnlocked();
string strFrom = params[0].get_str();
string strK = params[1].get_str();
int k = params[1].get_int();
string strTo = params[2].get_str();
string strMsg = params[3].get_str();
int k = atoi( strK.c_str() );
entry dm;
if( !createDirectMessage(dm, strTo, strMsg) )
@ -1038,9 +1038,9 @@ Value newrtmsg(const Array& params, bool fHelp) @@ -1038,9 +1038,9 @@ Value newrtmsg(const Array& params, bool fHelp)
EnsureWalletIsUnlocked();
string strUsername = params[0].get_str();
string strK = params[1].get_str();
int k = params[1].get_int();
string strK = boost::lexical_cast<std::string>(k);
entry vrt = jsonToEntry(params[2].get_obj());
int k = atoi( strK.c_str() );
entry v;
if( !createSignedUserpost(v, strUsername, k, "",

Loading…
Cancel
Save