Skip to content

Commit 33f9750

Browse files
committed
Merge #16185: gettransaction: add an argument to decode the transaction
9965940 doc: Add release note for the new gettransaction argument (darosior) b8b3f04 tests: Add a new functional test for gettransaction (darosior) 7f3bb24 gettransaction: add an argument to decode the transaction (darosior) Pull request description: This PR adds a new parameter to the `gettransaction` call : `decode`. If set to `true`, it will add a new `decoded` field to the response. This mimics the behavior of `getrawtransaction`'s `verbose` argument to avoid using 2 calls if we want to decode a wallet transaction (`gettransaction` then `decoderawtransaction`). Fix #16181 . ACKs for top commit: meshcollider: re-utACK 9965940 Tree-SHA512: bcb6b4bd252b3488d6afc77659c499c2ad99fd58661eb24b6a2e17014c74f22e47fde70e00fedb4f4754915786622ad02483b2cf2c4dea0ab0eb4ac8276dbeee
2 parents 6519be6 + 9965940 commit 33f9750

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

doc/release-notes-16185.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RPC changes
2+
-----------
3+
The `gettransaction` RPC now accepts a third (boolean) argument `decode`. If set to `true`, a new `decoded` field will be added to the response containing the decoded transaction.

src/rpc/client.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ static const CRPCConvertParam vRPCConvertParams[] =
8585
{ "getblockheader", 1, "verbose" },
8686
{ "getchaintxstats", 0, "nblocks" },
8787
{ "gettransaction", 1, "include_watchonly" },
88+
{ "gettransaction", 2, "decode" },
8889
{ "getrawtransaction", 1, "verbose" },
8990
{ "createrawtransaction", 0, "inputs" },
9091
{ "createrawtransaction", 1, "outputs" },

src/wallet/rpcwallet.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1649,6 +1649,7 @@ static UniValue gettransaction(const JSONRPCRequest& request)
16491649
{
16501650
{"txid", RPCArg::Type::STR, RPCArg::Optional::NO, "The transaction id"},
16511651
{"include_watchonly", RPCArg::Type::BOOL, /* default */ "true for watch-only wallets, otherwise false", "Whether to include watch-only addresses in balance calculation and details[]"},
1652+
{"decode", RPCArg::Type::BOOL, /* default */ "false", "Whether to add a field with the decoded transaction"},
16521653
},
16531654
RPCResult{
16541655
"{\n"
@@ -1684,11 +1685,13 @@ static UniValue gettransaction(const JSONRPCRequest& request)
16841685
" ,...\n"
16851686
" ],\n"
16861687
" \"hex\" : \"data\" (string) Raw data for transaction\n"
1688+
" \"decoded\" : transaction (json object) Optional, the decoded transaction\n"
16871689
"}\n"
16881690
},
16891691
RPCExamples{
16901692
HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
16911693
+ HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" true")
1694+
+ HelpExampleCli("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\" false true")
16921695
+ HelpExampleRpc("gettransaction", "\"1075db55d416d3ca199f55b6084e2115b9345e16c5cf302fc80e9d5fbf5d48d\"")
16931696
},
16941697
}.Check(request);
@@ -1708,6 +1711,8 @@ static UniValue gettransaction(const JSONRPCRequest& request)
17081711
filter |= ISMINE_WATCH_ONLY;
17091712
}
17101713

1714+
bool decode_tx = request.params[2].isNull() ? false : request.params[2].get_bool();
1715+
17111716
UniValue entry(UniValue::VOBJ);
17121717
auto it = pwallet->mapWallet.find(hash);
17131718
if (it == pwallet->mapWallet.end()) {
@@ -1733,6 +1738,12 @@ static UniValue gettransaction(const JSONRPCRequest& request)
17331738
std::string strHex = EncodeHexTx(*wtx.tx, pwallet->chain().rpcSerializationFlags());
17341739
entry.pushKV("hex", strHex);
17351740

1741+
if (decode_tx) {
1742+
UniValue decoded(UniValue::VOBJ);
1743+
TxToUniv(*wtx.tx, uint256(), decoded, false);
1744+
entry.pushKV("decoded", decoded);
1745+
}
1746+
17361747
return entry;
17371748
}
17381749

@@ -4175,7 +4186,7 @@ static const CRPCCommand commands[] =
41754186
{ "wallet", "getrawchangeaddress", &getrawchangeaddress, {"address_type"} },
41764187
{ "wallet", "getreceivedbyaddress", &getreceivedbyaddress, {"address","minconf"} },
41774188
{ "wallet", "getreceivedbylabel", &getreceivedbylabel, {"label","minconf"} },
4178-
{ "wallet", "gettransaction", &gettransaction, {"txid","include_watchonly"} },
4189+
{ "wallet", "gettransaction", &gettransaction, {"txid","include_watchonly","decode"} },
41794190
{ "wallet", "getunconfirmedbalance", &getunconfirmedbalance, {} },
41804191
{ "wallet", "getbalances", &getbalances, {} },
41814192
{ "wallet", "getwalletinfo", &getwalletinfo, {} },

test/functional/wallet_basic.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -499,6 +499,11 @@ def run_test(self):
499499
self.nodes[0].setlabel(change, 'foobar')
500500
assert_equal(self.nodes[0].getaddressinfo(change)['ischange'], False)
501501

502+
# Test "decoded" field value in gettransaction response
503+
self.log.info("Testing gettransaction decoding...")
504+
tx = self.nodes[0].gettransaction(txid=txid, decode=True)
505+
assert_equal(tx["decoded"], self.nodes[0].decoderawtransaction(tx["hex"]))
506+
502507

503508
if __name__ == '__main__':
504509
WalletTest().main()

0 commit comments

Comments
 (0)