Skip to content

Commit 1814b08

Browse files
czzarrjnewbery
authored andcommitted
add p2sh and segwit options to bitcoin-tx outscript command
1 parent dce853e commit 1814b08

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/bitcoin-tx.cpp

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,9 @@ static int AppInitRawTx(int argc, char* argv[])
7979
strUsage += HelpMessageOpt("nversion=N", _("Set TX version to N"));
8080
strUsage += HelpMessageOpt("outaddr=VALUE:ADDRESS", _("Add address-based output to TX"));
8181
strUsage += HelpMessageOpt("outdata=[VALUE:]DATA", _("Add data-based output to TX"));
82-
strUsage += HelpMessageOpt("outscript=VALUE:SCRIPT", _("Add raw script output to TX"));
82+
strUsage += HelpMessageOpt("outscript=VALUE:SCRIPT(:\"SEGWIT\")(:\"P2SH\")", _("Add raw script output to TX") + ". " +
83+
_("Optionally add the \"SEGWIT\" flag to produce a segwit output") + ". " +
84+
_("Optionally add the \"P2SH\" flag to wrap the script in a P2SH output."));
8385
strUsage += HelpMessageOpt("sign=SIGHASH-FLAGS", _("Add zero or more signatures to transaction") + ". " +
8486
_("This command requires JSON registers:") +
8587
_("prevtxs=JSON object") + ", " +
@@ -280,22 +282,30 @@ static void MutateTxAddOutData(CMutableTransaction& tx, const std::string& strIn
280282

281283
static void MutateTxAddOutScript(CMutableTransaction& tx, const std::string& strInput)
282284
{
283-
// separate VALUE:SCRIPT in string
284-
size_t pos = strInput.find(':');
285-
if ((pos == std::string::npos) ||
286-
(pos == 0))
287-
throw std::runtime_error("TX output missing separator");
285+
// separate VALUE:SCRIPT(:SEGWIT)(:P2SH)
286+
std::vector<std::string> vStrInput;
287+
boost::split(vStrInput, strInput, boost::is_any_of(":"));
288+
if (vStrInput.size() < 2)
289+
throw srd::runtime_error("TX output missing separator");
288290

289291
// extract and validate VALUE
290-
std::string strValue = strInput.substr(0, pos);
292+
std::string strValue = vStrInput[0];
291293
CAmount value;
292294
if (!ParseMoney(strValue, value))
293295
throw std::runtime_error("invalid TX output value");
294296

295297
// extract and validate script
296-
std::string strScript = strInput.substr(pos + 1, std::string::npos);
298+
std::string strScript = vStrInput[1];
297299
CScript scriptPubKey = ParseScript(strScript); // throws on err
298300

301+
if (std::find(vStrInput.begin(), vStrInput.end(), "SEGWIT") != vStrInput.end()) {
302+
scriptPubKey = GetScriptForWitness(scriptPubKey);
303+
}
304+
if (std::find(vStrInput.begin(), vStrInput.end(), "P2SH") != vStrInput.end()) {
305+
CBitcoinAddress addr(scriptPubKey);
306+
scriptPubKey = GetScriptForDestination(addr.Get());
307+
}
308+
299309
// construct TxOut, append to transaction output list
300310
CTxOut txout(value, scriptPubKey);
301311
tx.vout.push_back(txout);

0 commit comments

Comments
 (0)