Skip to content

Commit 627468d

Browse files
committed
Add support for data-based outputs (OP_RETURN) to bitcoin-tx.
1 parent d707853 commit 627468d

File tree

5 files changed

+66
-0
lines changed

5 files changed

+66
-0
lines changed

src/Makefile.test.include

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ EXTRA_DIST += \
1515
test/data/tx394b54bb.hex \
1616
test/data/txcreate1.hex \
1717
test/data/txcreate2.hex \
18+
test/data/txcreatedata1.hex \
19+
test/data/txcreatedata2.hex \
1820
test/data/txcreatesign.hex
1921

2022
JSON_TEST_FILES = \

src/bitcoin-tx.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ static bool AppInitRawTx(int argc, char* argv[])
7070
strUsage += HelpMessageOpt("locktime=N", _("Set TX lock time to N"));
7171
strUsage += HelpMessageOpt("nversion=N", _("Set TX version to N"));
7272
strUsage += HelpMessageOpt("outaddr=VALUE:ADDRESS", _("Add address-based output to TX"));
73+
strUsage += HelpMessageOpt("outdata=[VALUE:]DATA", _("Add data-based output to TX"));
7374
strUsage += HelpMessageOpt("outscript=VALUE:SCRIPT", _("Add raw script output to TX"));
7475
strUsage += HelpMessageOpt("sign=SIGHASH-FLAGS", _("Add zero or more signatures to transaction") + ". " +
7576
_("This command requires JSON registers:") +
@@ -231,6 +232,35 @@ static void MutateTxAddOutAddr(CMutableTransaction& tx, const string& strInput)
231232
tx.vout.push_back(txout);
232233
}
233234

235+
static void MutateTxAddOutData(CMutableTransaction& tx, const string& strInput)
236+
{
237+
CAmount value = 0;
238+
239+
// separate [VALUE:]DATA in string
240+
size_t pos = strInput.find(':');
241+
242+
if (pos==0)
243+
throw runtime_error("TX output value not specified");
244+
245+
if (pos != string::npos) {
246+
// extract and validate VALUE
247+
string strValue = strInput.substr(0, pos);
248+
if (!ParseMoney(strValue, value))
249+
throw runtime_error("invalid TX output value");
250+
}
251+
252+
// extract and validate DATA
253+
string strData = strInput.substr(pos + 1, string::npos);
254+
255+
if (!IsHex(strData))
256+
throw runtime_error("invalid TX output data");
257+
258+
std::vector<unsigned char> data = ParseHex(strData);
259+
260+
CTxOut txout(value, CScript() << OP_RETURN << data);
261+
tx.vout.push_back(txout);
262+
}
263+
234264
static void MutateTxAddOutScript(CMutableTransaction& tx, const string& strInput)
235265
{
236266
// separate VALUE:SCRIPT in string
@@ -470,6 +500,8 @@ static void MutateTx(CMutableTransaction& tx, const string& command,
470500
MutateTxDelOutput(tx, commandVal);
471501
else if (command == "outaddr")
472502
MutateTxAddOutAddr(tx, commandVal);
503+
else if (command == "outdata")
504+
MutateTxAddOutData(tx, commandVal);
473505
else if (command == "outscript")
474506
MutateTxAddOutScript(tx, commandVal);
475507

src/test/data/bitcoin-util-test.json

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,5 +56,35 @@
5656
"sign=ALL",
5757
"outaddr=0.001:193P6LtvS4nCnkDvM9uXn1gsSRqh4aDAz7"],
5858
"output_cmp": "txcreatesign.hex"
59+
},
60+
{ "exec": "./bitcoin-tx",
61+
"args":
62+
["-create",
63+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0",
64+
"outdata=4:badhexdata"],
65+
"return_code": 1
66+
},
67+
{ "exec": "./bitcoin-tx",
68+
"args":
69+
["-create",
70+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0",
71+
"outdata=badhexdata"],
72+
"return_code": 1
73+
},
74+
{ "exec": "./bitcoin-tx",
75+
"args":
76+
["-create",
77+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0",
78+
"outaddr=0.18:13tuJJDR2RgArmgfv6JScSdreahzgc4T6o",
79+
"outdata=4:54686973204f505f52455455524e207472616e73616374696f6e206f7574707574207761732063726561746564206279206d6f646966696564206372656174657261777472616e73616374696f6e2e"],
80+
"output_cmp": "txcreatedata1.hex"
81+
},
82+
{ "exec": "./bitcoin-tx",
83+
"args":
84+
["-create",
85+
"in=5897de6bd6027a475eadd57019d4e6872c396d0716c4875a5f1a6fcfdf385c1f:0",
86+
"outaddr=0.18:13tuJJDR2RgArmgfv6JScSdreahzgc4T6o",
87+
"outdata=54686973204f505f52455455524e207472616e73616374696f6e206f7574707574207761732063726561746564206279206d6f646966696564206372656174657261777472616e73616374696f6e2e"],
88+
"output_cmp": "txcreatedata2.hex"
5989
}
6090
]

src/test/data/txcreatedata1.hex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
01000000011f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000ffffffff0280a81201000000001976a9141fc11f39be1729bf973a7ab6a615ca4729d6457488ac0084d71700000000526a4c4f54686973204f505f52455455524e207472616e73616374696f6e206f7574707574207761732063726561746564206279206d6f646966696564206372656174657261777472616e73616374696f6e2e00000000

src/test/data/txcreatedata2.hex

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
01000000011f5c38dfcf6f1a5f5a87c416076d392c87e6d41970d5ad5e477a02d66bde97580000000000ffffffff0280a81201000000001976a9141fc11f39be1729bf973a7ab6a615ca4729d6457488ac0000000000000000526a4c4f54686973204f505f52455455524e207472616e73616374696f6e206f7574707574207761732063726561746564206279206d6f646966696564206372656174657261777472616e73616374696f6e2e00000000

0 commit comments

Comments
 (0)