From 3c777fa0f3337ff94411ecf828c7c8a8092086ab Mon Sep 17 00:00:00 2001 From: Christian Decker Date: Tue, 28 May 2019 22:39:48 +0200 Subject: [PATCH] json-rpc: Add `listtransactions` RPC method Signed-off-by: Christian Decker --- wallet/walletrpc.c | 77 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index c76144710..64d9d7c5e 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #include @@ -782,3 +783,79 @@ static const struct json_command dev_rescan_output_command = { "For each output stored in the internal wallet ask `bitcoind` whether we are in sync with its state (spent vs. unspent)" }; AUTODATA(json_command, &dev_rescan_output_command); + +struct { + enum wallet_tx_type t; + const char *name; +} wallet_tx_type_display_names[] = { + {TX_THEIRS, "theirs"}, + {TX_WALLET_DEPOSIT, "deposit"}, + {TX_WALLET_WITHDRAWAL, "withdraw"}, + {TX_CHANNEL_FUNDING, "channel_funding"}, + {TX_CHANNEL_CLOSE, "channel_mutual_close"}, + {TX_CHANNEL_UNILATERAL, "channel_unilateral_close"}, + {TX_CHANNEL_SWEEP, "channel_sweep"}, + {TX_CHANNEL_HTLC_SUCCESS, "channel_htlc_success"}, + {TX_CHANNEL_HTLC_TIMEOUT, "channel_htlc_timeout"}, + {TX_CHANNEL_PENALTY, "channel_penalty"}, + {TX_CHANNEL_CHEAT, "channel_unilateral_cheat"}, + {0, NULL} +}; + +static void json_add_txtypes(struct json_stream *result, const char *fieldname, txtypes value) +{ + json_array_start(result, fieldname); + for (size_t i=0; wallet_tx_type_display_names[i].name != NULL; i++) { + if (value & wallet_tx_type_display_names[i].t) + json_add_string(result, NULL, wallet_tx_type_display_names[i].name); + } + json_array_end(result); +} + +static struct command_result *json_listtransactions(struct command *cmd, + const char *buffer, + const jsmntok_t *obj UNNEEDED, + const jsmntok_t *params) +{ + struct json_stream *response; + struct wallet_transaction *txs; + + if (!param(cmd, buffer, params, NULL)) + return command_param_failed(); + + response = json_stream_success(cmd); + txs = wallet_transactions_get(cmd->ld->wallet, response); + + json_object_start(response, NULL); + json_array_start(response, "transactions"); + for (size_t i=0; i