diff --git a/common/addr.c b/common/addr.c index 980409e2a..c1ff6ce10 100644 --- a/common/addr.c +++ b/common/addr.c @@ -1,21 +1,27 @@ #include "addr.h" +#include +#include #include #include -/* Returns NULL if the script is not a P2WPKH or P2WSH */ char *encode_scriptpubkey_to_addr(const tal_t *ctx, - const char *hrp, - const u8 *scriptPubkey) + const struct chainparams *chainparams, + const u8 *scriptPubkey) { char *out; size_t scriptLen = tal_bytelen(scriptPubkey); + struct bitcoin_address pkh; + struct ripemd160 sh; - /* Check that scriptPubkey is P2WSH or P2WPKH */ - if (!is_p2wsh(scriptPubkey, NULL) && !is_p2wpkh(scriptPubkey, NULL)) - return NULL; + if (is_p2pkh(scriptPubkey, &pkh)) + return bitcoin_to_base58(ctx, chainparams, &pkh); - out = tal_arr(ctx, char, 73 + strlen(hrp)); - if (!segwit_addr_encode(out, hrp, 0, scriptPubkey + 2, scriptLen - 2)) + if (is_p2sh(scriptPubkey, &sh)) + return p2sh_to_base58(ctx, chainparams, &sh); + + out = tal_arr(ctx, char, 73 + strlen(chainparams->bip173_name)); + if (!segwit_addr_encode(out, chainparams->bip173_name, 0, + scriptPubkey + 2, scriptLen - 2)) return tal_free(out); return out; diff --git a/common/addr.h b/common/addr.h index 67604659a..1890e53a5 100644 --- a/common/addr.h +++ b/common/addr.h @@ -1,12 +1,13 @@ #ifndef LIGHTNING_COMMON_ADDR_H #define LIGHTNING_COMMON_ADDR_H #include "config.h" +#include #include #include -/* Given a P2WSH or P2WPKH scriptPubkey, return a bech32 encoded address */ +/* Given a scriptPubkey, return an encoded address */ char *encode_scriptpubkey_to_addr(const tal_t *ctx, - const char *hrp, + const struct chainparams *chainparams, const u8 *scriptPubkey); #endif /* LIGHTNING_COMMON_ADDR_H */ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index de033d800..18319611c 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -301,7 +301,7 @@ static void funding_started_success(struct funding_channel *fc, response = json_stream_success(cmd); out = encode_scriptpubkey_to_addr(cmd, - get_chainparams(cmd->ld)->bip173_name, + get_chainparams(cmd->ld), scriptPubkey); if (out) { json_add_string(response, "funding_address", out); diff --git a/lightningd/test/run-invoice-select-inchan.c b/lightningd/test/run-invoice-select-inchan.c index 238b3f673..59c5f0d40 100644 --- a/lightningd/test/run-invoice-select-inchan.c +++ b/lightningd/test/run-invoice-select-inchan.c @@ -82,6 +82,11 @@ void connect_succeeded(struct lightningd *ld UNNEEDED, const struct node_id *id void delay_then_reconnect(struct channel *channel UNNEEDED, u32 seconds_delay UNNEEDED, const struct wireaddr_internal *addrhint TAKES UNNEEDED) { fprintf(stderr, "delay_then_reconnect called!\n"); abort(); } +/* Generated stub for encode_scriptpubkey_to_addr */ +char *encode_scriptpubkey_to_addr(const tal_t *ctx UNNEEDED, + const struct chainparams *chainparams UNNEEDED, + const u8 *scriptPubkey UNNEEDED) +{ fprintf(stderr, "encode_scriptpubkey_to_addr called!\n"); abort(); } /* Generated stub for fail_htlc */ void fail_htlc(struct htlc_in *hin UNNEEDED, enum onion_type failcode UNNEEDED) { fprintf(stderr, "fail_htlc called!\n"); abort(); } diff --git a/plugins/Makefile b/plugins/Makefile index ca1222bbd..cd4b03636 100644 --- a/plugins/Makefile +++ b/plugins/Makefile @@ -12,6 +12,7 @@ PLUGIN_LIB_HEADER := plugins/libplugin.h PLUGIN_LIB_OBJS := $(PLUGIN_LIB_SRC:.c=.o) PLUGIN_COMMON_OBJS := \ + bitcoin/base58.o \ bitcoin/pubkey.o \ bitcoin/pullpush.o \ bitcoin/script.o \ diff --git a/plugins/fundchannel.c b/plugins/fundchannel.c index eacbc4086..2de8948cd 100644 --- a/plugins/fundchannel.c +++ b/plugins/fundchannel.c @@ -470,8 +470,7 @@ static void init(struct plugin_conn *rpc, "network")), rpc, ".network"); chainparams = chainparams_for_network(network_name); - placeholder_funding_addr = encode_scriptpubkey_to_addr(NULL, - chainparams->bip173_name, + placeholder_funding_addr = encode_scriptpubkey_to_addr(NULL, chainparams, placeholder); } diff --git a/wallet/test/run-wallet.c b/wallet/test/run-wallet.c index 953e157af..f11d87792 100644 --- a/wallet/test/run-wallet.c +++ b/wallet/test/run-wallet.c @@ -90,6 +90,11 @@ void connect_succeeded(struct lightningd *ld UNNEEDED, const struct node_id *id void delay_then_reconnect(struct channel *channel UNNEEDED, u32 seconds_delay UNNEEDED, const struct wireaddr_internal *addrhint TAKES UNNEEDED) { fprintf(stderr, "delay_then_reconnect called!\n"); abort(); } +/* Generated stub for encode_scriptpubkey_to_addr */ +char *encode_scriptpubkey_to_addr(const tal_t *ctx UNNEEDED, + const struct chainparams *chainparams UNNEEDED, + const u8 *scriptPubkey UNNEEDED) +{ fprintf(stderr, "encode_scriptpubkey_to_addr called!\n"); abort(); } /* Generated stub for fatal */ void fatal(const char *fmt UNNEEDED, ...) { fprintf(stderr, "fatal called!\n"); abort(); } diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 8a9cbba95..ba37918d0 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -814,7 +814,7 @@ static struct command_result *json_listfunds(struct command *cmd, json_add_string(response, "address", out); } else if (utxos[i]->scriptPubkey != NULL) { out = encode_scriptpubkey_to_addr( - cmd, get_chainparams(cmd->ld)->bip173_name, + cmd, get_chainparams(cmd->ld), utxos[i]->scriptPubkey); if (out) json_add_string(response, "address", out);