diff --git a/common/Makefile b/common/Makefile index 4ad5602b7..b783b2f4d 100644 --- a/common/Makefile +++ b/common/Makefile @@ -1,4 +1,5 @@ COMMON_SRC_NOGEN := \ + common/addr.c \ common/amount.c \ common/base32.c \ common/bech32.c \ diff --git a/common/addr.c b/common/addr.c new file mode 100644 index 000000000..980409e2a --- /dev/null +++ b/common/addr.c @@ -0,0 +1,22 @@ +#include "addr.h" +#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) +{ + char *out; + size_t scriptLen = tal_bytelen(scriptPubkey); + + /* Check that scriptPubkey is P2WSH or P2WPKH */ + if (!is_p2wsh(scriptPubkey, NULL) && !is_p2wpkh(scriptPubkey, NULL)) + return NULL; + + out = tal_arr(ctx, char, 73 + strlen(hrp)); + if (!segwit_addr_encode(out, hrp, 0, scriptPubkey + 2, scriptLen - 2)) + return tal_free(out); + + return out; +} diff --git a/common/addr.h b/common/addr.h new file mode 100644 index 000000000..67604659a --- /dev/null +++ b/common/addr.h @@ -0,0 +1,12 @@ +#ifndef LIGHTNING_COMMON_ADDR_H +#define LIGHTNING_COMMON_ADDR_H +#include "config.h" +#include +#include + +/* Given a P2WSH or P2WPKH scriptPubkey, return a bech32 encoded address */ +char *encode_scriptpubkey_to_addr(const tal_t *ctx, + const char *hrp, + const u8 *scriptPubkey); + +#endif /* LIGHTNING_COMMON_ADDR_H */ diff --git a/lightningd/Makefile b/lightningd/Makefile index a7957b69a..bc7ceff78 100644 --- a/lightningd/Makefile +++ b/lightningd/Makefile @@ -14,6 +14,7 @@ default: lightningd-all # Common source we use. LIGHTNINGD_COMMON_OBJS := \ + common/addr.o \ common/amount.o \ common/base32.o \ common/bech32.o \ diff --git a/lightningd/opening_control.c b/lightningd/opening_control.c index fd4aa6a51..caa396ab8 100644 --- a/lightningd/opening_control.c +++ b/lightningd/opening_control.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include diff --git a/wallet/walletrpc.c b/wallet/walletrpc.c index 4e53e0f5d..b1dac9786 100644 --- a/wallet/walletrpc.c +++ b/wallet/walletrpc.c @@ -2,6 +2,7 @@ #include #include #include +#include #include #include #include @@ -419,26 +420,6 @@ encode_pubkey_to_addr(const tal_t *ctx, return out; } -/* Returns NULL if the script is not a P2WPKH */ -static char * -encode_scriptpubkey_to_addr(const tal_t *ctx, - const struct lightningd *ld, - const u8 *scriptPubkey) -{ - char *out; - const char *hrp; - size_t scriptLen = tal_bytelen(scriptPubkey); - bool ok; - if (scriptLen != 22 || scriptPubkey[0] != 0x00 || scriptPubkey[1] != 0x14) - return NULL; - hrp = get_chainparams(ld)->bip173_name; - out = tal_arr(ctx, char, 73 + strlen(hrp)); - ok = segwit_addr_encode(out, hrp, 0, scriptPubkey + 2, scriptLen - 2); - if (!ok) - return tal_free(out); - return out; -} - enum addrtype { ADDR_P2SH_SEGWIT = 1, ADDR_BECH32 = 2, @@ -638,7 +619,8 @@ 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, cmd->ld, utxos[i]->scriptPubkey); + cmd, get_chainparams(cmd->ld)->bip173_name, + utxos[i]->scriptPubkey); if (out) json_add_string(response, "address", out); }