From 75e946d256e0b3a7ecff51e40ce160a5441ec8c0 Mon Sep 17 00:00:00 2001 From: trueptolemy Date: Sun, 29 Sep 2019 21:19:42 +0800 Subject: [PATCH] json: Move `param_bitcoin_address` from wallet/walletrpc.c to lightningd/json.c It's a useful helper, and it will be used to prase address in `close` command. --- lightningd/json.c | 26 +++++++++++++++++++++ lightningd/json.h | 6 +++++ lightningd/test/run-invoice-select-inchan.c | 7 ++++++ lightningd/test/run-jsonrpc.c | 3 +++ wallet/test/run-wallet.c | 7 ++++++ wallet/walletrpc.c | 26 --------------------- 6 files changed, 49 insertions(+), 26 deletions(-) diff --git a/lightningd/json.c b/lightningd/json.c index a860b5028..fde8bec7f 100644 --- a/lightningd/json.c +++ b/lightningd/json.c @@ -572,3 +572,29 @@ json_to_address_scriptpubkey(const tal_t *ctx, return ADDRESS_PARSE_UNRECOGNIZED; } + +struct command_result *param_bitcoin_address(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + const u8 **scriptpubkey) +{ + /* Parse address. */ + switch (json_to_address_scriptpubkey(cmd, + get_chainparams(cmd->ld), + buffer, tok, + scriptpubkey)) { + case ADDRESS_PARSE_UNRECOGNIZED: + return command_fail(cmd, LIGHTNINGD, + "Could not parse destination address, " + "%s should be a valid address", + name ? name : "address field"); + case ADDRESS_PARSE_WRONG_NETWORK: + return command_fail(cmd, LIGHTNINGD, + "Destination address is not on network %s", + get_chainparams(cmd->ld)->network_name); + case ADDRESS_PARSE_SUCCESS: + return NULL; + } + abort(); +} diff --git a/lightningd/json.h b/lightningd/json.h index e6e613a1b..24458618b 100644 --- a/lightningd/json.h +++ b/lightningd/json.h @@ -211,4 +211,10 @@ void json_add_time(struct json_stream *result, const char *fieldname, void json_add_sha256(struct json_stream *result, const char *fieldname, const struct sha256 *hash); +struct command_result *param_bitcoin_address(struct command *cmd, + const char *name, + const char *buffer, + const jsmntok_t *tok, + const u8 **scriptpubkey); + #endif /* LIGHTNING_LIGHTNINGD_JSON_H */ diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 5eaf69390..e3b5fe43b 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -609,6 +609,13 @@ struct txowatch *watch_txo(const tal_t *ctx UNNEEDED, size_t input_num UNNEEDED, const struct block *block)) { fprintf(stderr, "watch_txo called!\n"); abort(); } +/* Generated stub for param_bitcoin_address */ +struct command_result *param_bitcoin_address(struct command *cmd UNNEEDED, + const char *name UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const u8 **scriptpubkey UNNEEDED) +{ fprintf(stderr, "param_bitcoin_address called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ #if DEVELOPER diff --git a/lightningd/test/run-jsonrpc.c b/lightningd/test/run-jsonrpc.c index d78959a69..28b2e7818 100644 --- a/lightningd/test/run-jsonrpc.c +++ b/lightningd/test/run-jsonrpc.c @@ -99,6 +99,9 @@ struct command_result *param_tok(struct command *cmd UNNEEDED, const char *name const char *buffer UNNEEDED, const jsmntok_t * tok UNNEEDED, const jsmntok_t **out UNNEEDED) { fprintf(stderr, "param_tok called!\n"); abort(); } +/* Generated stub for get_chainparams */ +const struct chainparams *get_chainparams(const struct lightningd *ld UNNEEDED) +{ fprintf(stderr, "get_chainparams called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ bool deprecated_apis; diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 6400f37a6..8dfb510b9 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -608,6 +608,13 @@ struct txowatch *watch_txo(const tal_t *ctx UNNEEDED, size_t input_num UNNEEDED, const struct block *block)) { fprintf(stderr, "watch_txo called!\n"); abort(); } +/* Generated stub for param_bitcoin_address */ +struct command_result *param_bitcoin_address(struct command *cmd UNNEEDED, + const char *name UNNEEDED, + const char *buffer UNNEEDED, + const jsmntok_t *tok UNNEEDED, + const u8 **scriptpubkey UNNEEDED) +{ fprintf(stderr, "param_bitcoin_address called!\n"); abort(); } /* AUTOGENERATED MOCKS END */ #if DEVELOPER diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 5d3f67341..c8a2a8ae7 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -73,32 +73,6 @@ static void wallet_withdrawal_broadcast(struct bitcoind *bitcoind UNUSED, } } -static struct command_result *param_bitcoin_address(struct command *cmd, - const char *name, - const char *buffer, - const jsmntok_t *tok, - const u8 **scriptpubkey) -{ - /* Parse address. */ - switch (json_to_address_scriptpubkey(cmd, - get_chainparams(cmd->ld), - buffer, tok, - scriptpubkey)) { - case ADDRESS_PARSE_UNRECOGNIZED: - return command_fail(cmd, LIGHTNINGD, - "Could not parse destination address, " - "%s should be a valid address", - name ? name : "address field"); - case ADDRESS_PARSE_WRONG_NETWORK: - return command_fail(cmd, LIGHTNINGD, - "Destination address is not on network %s", - get_chainparams(cmd->ld)->network_name); - case ADDRESS_PARSE_SUCCESS: - return NULL; - } - abort(); -} - /* Signs the tx, broadcasts it: broadcast calls wallet_withdrawal_broadcast */ static struct command_result *broadcast_and_wait(struct command *cmd, struct unreleased_tx *utx)