Skip to content

Commit 6bb2805

Browse files
committed
Merge pull request #6346
627468d Add support for data-based outputs (OP_RETURN) to bitcoin-tx. (Pavel Janík) d707853 Add OP_RETURN support in createrawtransaction RPC call, add tests. (Pavel Janík)
2 parents 0201a79 + 627468d commit 6bb2805

File tree

7 files changed

+111
-16
lines changed

7 files changed

+111
-16
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/rpcrawtransaction.cpp

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,9 @@ UniValue createrawtransaction(const UniValue& params, bool fHelp)
318318
{
319319
if (fHelp || params.size() != 2)
320320
throw runtime_error(
321-
"createrawtransaction [{\"txid\":\"id\",\"vout\":n},...] {\"address\":amount,...}\n"
322-
"\nCreate a transaction spending the given inputs and sending to the given addresses.\n"
321+
"createrawtransaction [{\"txid\":\"id\",\"vout\":n},...] {\"address\":amount,\"data\":\"hex\",...}\n"
322+
"\nCreate a transaction spending the given inputs and creating new outputs.\n"
323+
"Outputs can be addresses or data.\n"
323324
"Returns hex-encoded raw transaction.\n"
324325
"Note that the transaction's inputs are not signed, and\n"
325326
"it is not stored in the wallet or transmitted to the network.\n"
@@ -328,23 +329,25 @@ UniValue createrawtransaction(const UniValue& params, bool fHelp)
328329
"1. \"transactions\" (string, required) A json array of json objects\n"
329330
" [\n"
330331
" {\n"
331-
" \"txid\":\"id\", (string, required) The transaction id\n"
332+
" \"txid\":\"id\", (string, required) The transaction id\n"
332333
" \"vout\":n (numeric, required) The output number\n"
333334
" }\n"
334335
" ,...\n"
335336
" ]\n"
336-
"2. \"addresses\" (string, required) a json object with addresses as keys and amounts as values\n"
337+
"2. \"outputs\" (string, required) a json object with outputs\n"
337338
" {\n"
338339
" \"address\": x.xxx (numeric, required) The key is the bitcoin address, the value is the " + CURRENCY_UNIT + " amount\n"
339-
" ,...\n"
340+
" \"data\": \"hex\", (string, required) The key is \"data\", the value is hex encoded data\n"
341+
" ...\n"
340342
" }\n"
341-
342343
"\nResult:\n"
343344
"\"transaction\" (string) hex string of the transaction\n"
344345

345346
"\nExamples\n"
346347
+ HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"{\\\"address\\\":0.01}\"")
348+
+ HelpExampleCli("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\" \"{\\\"data\\\":\\\"00010203\\\"}\"")
347349
+ HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"address\\\":0.01}\"")
350+
+ HelpExampleRpc("createrawtransaction", "\"[{\\\"txid\\\":\\\"myid\\\",\\\"vout\\\":0}]\", \"{\\\"data\\\":\\\"00010203\\\"}\"")
348351
);
349352

350353
LOCK(cs_main);
@@ -375,19 +378,27 @@ UniValue createrawtransaction(const UniValue& params, bool fHelp)
375378
set<CBitcoinAddress> setAddress;
376379
vector<string> addrList = sendTo.getKeys();
377380
BOOST_FOREACH(const string& name_, addrList) {
378-
CBitcoinAddress address(name_);
379-
if (!address.IsValid())
380-
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+name_);
381381

382-
if (setAddress.count(address))
383-
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+name_);
384-
setAddress.insert(address);
382+
if (name_ == "data") {
383+
std::vector<unsigned char> data = ParseHexV(sendTo[name_].getValStr(),"Data");
384+
385+
CTxOut out(0, CScript() << OP_RETURN << data);
386+
rawTx.vout.push_back(out);
387+
} else {
388+
CBitcoinAddress address(name_);
389+
if (!address.IsValid())
390+
throw JSONRPCError(RPC_INVALID_ADDRESS_OR_KEY, string("Invalid Bitcoin address: ")+name_);
385391

386-
CScript scriptPubKey = GetScriptForDestination(address.Get());
387-
CAmount nAmount = AmountFromValue(sendTo[name_]);
392+
if (setAddress.count(address))
393+
throw JSONRPCError(RPC_INVALID_PARAMETER, string("Invalid parameter, duplicated address: ")+name_);
394+
setAddress.insert(address);
388395

389-
CTxOut out(nAmount, scriptPubKey);
390-
rawTx.vout.push_back(out);
396+
CScript scriptPubKey = GetScriptForDestination(address.Get());
397+
CAmount nAmount = AmountFromValue(sendTo[name_]);
398+
399+
CTxOut out(nAmount, scriptPubKey);
400+
rawTx.vout.push_back(out);
401+
}
391402
}
392403

393404
return EncodeHexTx(rawTx);

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

src/test/rpc_tests.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,24 @@ BOOST_AUTO_TEST_CASE(rpc_rawsign)
110110
BOOST_CHECK(find_value(r.get_obj(), "complete").get_bool() == true);
111111
}
112112

113+
BOOST_AUTO_TEST_CASE(rpc_createraw_op_return)
114+
{
115+
BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"68656c6c6f776f726c64\"}"));
116+
117+
// Allow more than one data transaction output
118+
BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"68656c6c6f776f726c64\",\"data\":\"68656c6c6f776f726c64\"}"));
119+
120+
// Key not "data" (bad address)
121+
BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"somedata\":\"68656c6c6f776f726c64\"}"), runtime_error);
122+
123+
// Bad hex encoding of data output
124+
BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"12345\"}"), runtime_error);
125+
BOOST_CHECK_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"12345g\"}"), runtime_error);
126+
127+
// Data 81 bytes long
128+
BOOST_CHECK_NO_THROW(CallRPC("createrawtransaction [{\"txid\":\"a3b807410df0b60fcb9736768df5823938b2f838694939ba45f3c0a1bff150ed\",\"vout\":0}] {\"data\":\"010203040506070809101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081\"}"));
129+
}
130+
113131
BOOST_AUTO_TEST_CASE(rpc_format_monetary_values)
114132
{
115133
BOOST_CHECK(ValueFromAmount(0LL).write() == "0.00000000");

0 commit comments

Comments
 (0)