diff options
author | Gregory Maxwell <greg@xiph.org> | 2013-09-12 20:50:32 -0700 |
---|---|---|
committer | Gregory Maxwell <greg@xiph.org> | 2013-09-12 20:50:32 -0700 |
commit | 4c5969b367d2b8582abc3b0018e18456a7377497 (patch) | |
tree | 6093f15e6df1d939a1ee746d3f9f9bfb54ee1490 | |
parent | 11a79680b174ec39d462925ea565a23aa8c147ad (diff) | |
parent | 22536422878d05a7229f74726148dd50051c883f (diff) |
Merge pull request #2830 from petertodd/p2sh-rpc
P2SH related RPC improvements
-rw-r--r-- | src/bitcoinrpc.cpp | 29 | ||||
-rw-r--r-- | src/bitcoinrpc.h | 10 | ||||
-rw-r--r-- | src/rpcblockchain.cpp | 4 | ||||
-rw-r--r-- | src/rpcrawtransaction.cpp | 63 | ||||
-rw-r--r-- | src/rpcwallet.cpp | 1 |
5 files changed, 69 insertions, 38 deletions
diff --git a/src/bitcoinrpc.cpp b/src/bitcoinrpc.cpp index 47c7383564..d1082c7ecf 100644 --- a/src/bitcoinrpc.cpp +++ b/src/bitcoinrpc.cpp @@ -113,6 +113,34 @@ std::string HexBits(unsigned int nBits) return HexStr(BEGIN(uBits.cBits), END(uBits.cBits)); } +uint256 ParseHashV(const Value& v, string strName) +{ + string strHex; + if (v.type() == str_type) + strHex = v.get_str(); + if (!IsHex(strHex)) // Note: IsHex("") is false + throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')"); + uint256 result; + result.SetHex(strHex); + return result; +} +uint256 ParseHashO(const Object& o, string strKey) +{ + return ParseHashV(find_value(o, strKey), strKey); +} +vector<unsigned char> ParseHexV(const Value& v, string strName) +{ + string strHex; + if (v.type() == str_type) + strHex = v.get_str(); + if (!IsHex(strHex)) + throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')"); + return ParseHex(strHex); +} +vector<unsigned char> ParseHexO(const Object& o, string strKey) +{ + return ParseHexV(find_value(o, strKey), strKey); +} /// @@ -252,6 +280,7 @@ static const CRPCCommand vRPCCommands[] = { "getrawtransaction", &getrawtransaction, false, false }, { "createrawtransaction", &createrawtransaction, false, false }, { "decoderawtransaction", &decoderawtransaction, false, false }, + { "decodescript", &decodescript, false, false }, { "signrawtransaction", &signrawtransaction, false, false }, { "sendrawtransaction", &sendrawtransaction, false, false }, { "gettxoutsetinfo", &gettxoutsetinfo, true, false }, diff --git a/src/bitcoinrpc.h b/src/bitcoinrpc.h index 1aa2e70d26..ab0a0bc968 100644 --- a/src/bitcoinrpc.h +++ b/src/bitcoinrpc.h @@ -130,6 +130,15 @@ public: extern const CRPCTable tableRPC; +// +// Utilities: convert hex-encoded Values +// (throws error if not hex). +// +extern uint256 ParseHashV(const json_spirit::Value& v, std::string strName); +extern uint256 ParseHashO(const json_spirit::Object& o, std::string strKey); +extern std::vector<unsigned char> ParseHexV(const json_spirit::Value& v, std::string strName); +extern std::vector<unsigned char> ParseHexO(const json_spirit::Object& o, std::string strKey); + extern void InitRPCMining(); extern void ShutdownRPCMining(); @@ -198,6 +207,7 @@ 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 decodescript(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); diff --git a/src/rpcblockchain.cpp b/src/rpcblockchain.cpp index adff708a06..398f33605a 100644 --- a/src/rpcblockchain.cpp +++ b/src/rpcblockchain.cpp @@ -9,7 +9,7 @@ using namespace json_spirit; using namespace std; -void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out); +void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex); double GetDifficulty(const CBlockIndex* blockindex) { @@ -245,7 +245,7 @@ Value gettxout(const Array& params, bool fHelp) ret.push_back(Pair("confirmations", pcoinsTip->GetBestBlock()->nHeight - coins.nHeight + 1)); ret.push_back(Pair("value", ValueFromAmount(coins.vout[n].nValue))); Object o; - ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o); + ScriptPubKeyToJSON(coins.vout[n].scriptPubKey, o, true); ret.push_back(Pair("scriptPubKey", o)); ret.push_back(Pair("version", coins.nVersion)); ret.push_back(Pair("coinbase", coins.fCoinBase)); diff --git a/src/rpcrawtransaction.cpp b/src/rpcrawtransaction.cpp index 580120f2a2..5f33849f77 100644 --- a/src/rpcrawtransaction.cpp +++ b/src/rpcrawtransaction.cpp @@ -17,47 +17,15 @@ using namespace boost; using namespace boost::assign; using namespace json_spirit; -// -// Utilities: convert hex-encoded Values -// (throws error if not hex). -// -uint256 ParseHashV(const Value& v, string strName) -{ - string strHex; - if (v.type() == str_type) - strHex = v.get_str(); - if (!IsHex(strHex)) // Note: IsHex("") is false - throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')"); - uint256 result; - result.SetHex(strHex); - return result; -} -uint256 ParseHashO(const Object& o, string strKey) -{ - return ParseHashV(find_value(o, strKey), strKey); -} -vector<unsigned char> ParseHexV(const Value& v, string strName) -{ - string strHex; - if (v.type() == str_type) - strHex = v.get_str(); - if (!IsHex(strHex)) - throw JSONRPCError(RPC_INVALID_PARAMETER, strName+" must be hexadecimal string (not '"+strHex+"')"); - return ParseHex(strHex); -} -vector<unsigned char> ParseHexO(const Object& o, string strKey) -{ - return ParseHexV(find_value(o, strKey), strKey); -} - -void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out) +void ScriptPubKeyToJSON(const CScript& scriptPubKey, Object& out, bool fIncludeHex) { txnouttype type; vector<CTxDestination> addresses; int nRequired; out.push_back(Pair("asm", scriptPubKey.ToString())); - out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()))); + if (fIncludeHex) + out.push_back(Pair("hex", HexStr(scriptPubKey.begin(), scriptPubKey.end()))); if (!ExtractDestinations(scriptPubKey, type, addresses, nRequired)) { @@ -106,7 +74,7 @@ void TxToJSON(const CTransaction& tx, const uint256 hashBlock, Object& entry) out.push_back(Pair("value", ValueFromAmount(txout.nValue))); out.push_back(Pair("n", (boost::int64_t)i)); Object o; - ScriptPubKeyToJSON(txout.scriptPubKey, o); + ScriptPubKeyToJSON(txout.scriptPubKey, o, false); out.push_back(Pair("scriptPubKey", o)); vout.push_back(out); } @@ -334,6 +302,29 @@ Value decoderawtransaction(const Array& params, bool fHelp) return result; } +Value decodescript(const Array& params, bool fHelp) +{ + if (fHelp || params.size() != 1) + throw runtime_error( + "decodescript <hex string>\n" + "Decode a hex-encoded script."); + + RPCTypeCheck(params, list_of(str_type)); + + Object r; + CScript script; + if (params[0].get_str().size() > 0){ + vector<unsigned char> scriptData(ParseHexV(params[0], "argument")); + script = CScript(scriptData.begin(), scriptData.end()); + } else { + // Empty scripts are valid + } + ScriptPubKeyToJSON(script, r, false); + + r.push_back(Pair("p2sh", CBitcoinAddress(script.GetID()).ToString())); + return r; +} + Value signrawtransaction(const Array& params, bool fHelp) { if (fHelp || params.size() < 1 || params.size() > 4) diff --git a/src/rpcwallet.cpp b/src/rpcwallet.cpp index 14b4956a15..46338bdf26 100644 --- a/src/rpcwallet.cpp +++ b/src/rpcwallet.cpp @@ -1447,6 +1447,7 @@ public: int nRequired; ExtractDestinations(subscript, whichType, addresses, nRequired); obj.push_back(Pair("script", GetTxnOutputType(whichType))); + obj.push_back(Pair("hex", HexStr(subscript.begin(), subscript.end()))); Array a; BOOST_FOREACH(const CTxDestination& addr, addresses) a.push_back(CBitcoinAddress(addr).ToString()); |