Browse Source

addr: handle P2SH/P2PKH in scriptpubkey encoding

Previously, returned null if a scriptpubkey was not Segwit; now
handles encoding to Base58 for other types.
travis-debug
lisa neigut 5 years ago
committed by Rusty Russell
parent
commit
963a1da958
  1. 20
      common/addr.c
  2. 5
      common/addr.h
  3. 2
      lightningd/opening_control.c
  4. 5
      lightningd/test/run-invoice-select-inchan.c
  5. 1
      plugins/Makefile
  6. 3
      plugins/fundchannel.c
  7. 5
      wallet/test/run-wallet.c
  8. 2
      wallet/walletrpc.c

20
common/addr.c

@ -1,21 +1,27 @@
#include "addr.h" #include "addr.h"
#include <bitcoin/address.h>
#include <bitcoin/base58.h>
#include <bitcoin/script.h> #include <bitcoin/script.h>
#include <common/bech32.h> #include <common/bech32.h>
/* Returns NULL if the script is not a P2WPKH or P2WSH */
char *encode_scriptpubkey_to_addr(const tal_t *ctx, char *encode_scriptpubkey_to_addr(const tal_t *ctx,
const char *hrp, const struct chainparams *chainparams,
const u8 *scriptPubkey) const u8 *scriptPubkey)
{ {
char *out; char *out;
size_t scriptLen = tal_bytelen(scriptPubkey); size_t scriptLen = tal_bytelen(scriptPubkey);
struct bitcoin_address pkh;
struct ripemd160 sh;
/* Check that scriptPubkey is P2WSH or P2WPKH */ if (is_p2pkh(scriptPubkey, &pkh))
if (!is_p2wsh(scriptPubkey, NULL) && !is_p2wpkh(scriptPubkey, NULL)) return bitcoin_to_base58(ctx, chainparams, &pkh);
return NULL;
out = tal_arr(ctx, char, 73 + strlen(hrp)); if (is_p2sh(scriptPubkey, &sh))
if (!segwit_addr_encode(out, hrp, 0, scriptPubkey + 2, scriptLen - 2)) 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 tal_free(out);
return out; return out;

5
common/addr.h

@ -1,12 +1,13 @@
#ifndef LIGHTNING_COMMON_ADDR_H #ifndef LIGHTNING_COMMON_ADDR_H
#define LIGHTNING_COMMON_ADDR_H #define LIGHTNING_COMMON_ADDR_H
#include "config.h" #include "config.h"
#include <bitcoin/chainparams.h>
#include <ccan/short_types/short_types.h> #include <ccan/short_types/short_types.h>
#include <ccan/tal/tal.h> #include <ccan/tal/tal.h>
/* 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, char *encode_scriptpubkey_to_addr(const tal_t *ctx,
const char *hrp, const struct chainparams *chainparams,
const u8 *scriptPubkey); const u8 *scriptPubkey);
#endif /* LIGHTNING_COMMON_ADDR_H */ #endif /* LIGHTNING_COMMON_ADDR_H */

2
lightningd/opening_control.c

@ -301,7 +301,7 @@ static void funding_started_success(struct funding_channel *fc,
response = json_stream_success(cmd); response = json_stream_success(cmd);
out = encode_scriptpubkey_to_addr(cmd, out = encode_scriptpubkey_to_addr(cmd,
get_chainparams(cmd->ld)->bip173_name, get_chainparams(cmd->ld),
scriptPubkey); scriptPubkey);
if (out) { if (out) {
json_add_string(response, "funding_address", out); json_add_string(response, "funding_address", out);

5
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, void delay_then_reconnect(struct channel *channel UNNEEDED, u32 seconds_delay UNNEEDED,
const struct wireaddr_internal *addrhint TAKES UNNEEDED) const struct wireaddr_internal *addrhint TAKES UNNEEDED)
{ fprintf(stderr, "delay_then_reconnect called!\n"); abort(); } { 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 */ /* Generated stub for fail_htlc */
void fail_htlc(struct htlc_in *hin UNNEEDED, enum onion_type failcode UNNEEDED) void fail_htlc(struct htlc_in *hin UNNEEDED, enum onion_type failcode UNNEEDED)
{ fprintf(stderr, "fail_htlc called!\n"); abort(); } { fprintf(stderr, "fail_htlc called!\n"); abort(); }

1
plugins/Makefile

@ -12,6 +12,7 @@ PLUGIN_LIB_HEADER := plugins/libplugin.h
PLUGIN_LIB_OBJS := $(PLUGIN_LIB_SRC:.c=.o) PLUGIN_LIB_OBJS := $(PLUGIN_LIB_SRC:.c=.o)
PLUGIN_COMMON_OBJS := \ PLUGIN_COMMON_OBJS := \
bitcoin/base58.o \
bitcoin/pubkey.o \ bitcoin/pubkey.o \
bitcoin/pullpush.o \ bitcoin/pullpush.o \
bitcoin/script.o \ bitcoin/script.o \

3
plugins/fundchannel.c

@ -470,8 +470,7 @@ static void init(struct plugin_conn *rpc,
"network")), "network")),
rpc, ".network"); rpc, ".network");
chainparams = chainparams_for_network(network_name); chainparams = chainparams_for_network(network_name);
placeholder_funding_addr = encode_scriptpubkey_to_addr(NULL, placeholder_funding_addr = encode_scriptpubkey_to_addr(NULL, chainparams,
chainparams->bip173_name,
placeholder); placeholder);
} }

5
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, void delay_then_reconnect(struct channel *channel UNNEEDED, u32 seconds_delay UNNEEDED,
const struct wireaddr_internal *addrhint TAKES UNNEEDED) const struct wireaddr_internal *addrhint TAKES UNNEEDED)
{ fprintf(stderr, "delay_then_reconnect called!\n"); abort(); } { 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 */ /* Generated stub for fatal */
void fatal(const char *fmt UNNEEDED, ...) void fatal(const char *fmt UNNEEDED, ...)
{ fprintf(stderr, "fatal called!\n"); abort(); } { fprintf(stderr, "fatal called!\n"); abort(); }

2
wallet/walletrpc.c

@ -814,7 +814,7 @@ static struct command_result *json_listfunds(struct command *cmd,
json_add_string(response, "address", out); json_add_string(response, "address", out);
} else if (utxos[i]->scriptPubkey != NULL) { } else if (utxos[i]->scriptPubkey != NULL) {
out = encode_scriptpubkey_to_addr( out = encode_scriptpubkey_to_addr(
cmd, get_chainparams(cmd->ld)->bip173_name, cmd, get_chainparams(cmd->ld),
utxos[i]->scriptPubkey); utxos[i]->scriptPubkey);
if (out) if (out)
json_add_string(response, "address", out); json_add_string(response, "address", out);

Loading…
Cancel
Save