Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Display close_to address in listpeers #3223

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions common/addr.c
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
#include "addr.h"
#include <bitcoin/address.h>
#include <bitcoin/base58.h>
#include <bitcoin/script.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,
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;
Expand Down
5 changes: 3 additions & 2 deletions common/addr.h
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
#ifndef LIGHTNING_COMMON_ADDR_H
#define LIGHTNING_COMMON_ADDR_H
#include "config.h"
#include <bitcoin/chainparams.h>
#include <ccan/short_types/short_types.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,
const char *hrp,
const struct chainparams *chainparams,
const u8 *scriptPubkey);

#endif /* LIGHTNING_COMMON_ADDR_H */
4 changes: 3 additions & 1 deletion contrib/startup_regtest.sh
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ start_ln() {
"$LIGHTNINGD" --lightning-dir=/tmp/l2-regtest

# Give a hint.
echo "Commands: l1-cli, l2-cli, bt-cli, stop_ln, cleanup_ln"
echo "Commands: l1-cli, l2-cli, l[1|2]-log, bt-cli, stop_ln, cleanup_ln"
}

stop_ln() {
Expand All @@ -126,6 +126,8 @@ cleanup_ln() {
unalias l1-cli
unalias l2-cli
unalias bt-cli
unalias l1-log
unalias l2-log
unset -f start_ln
unset -f stop_ln
unset -f cleanup_ln
Expand Down
2 changes: 1 addition & 1 deletion lightningd/opening_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions lightningd/peer_control.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <ccan/take/take.h>
#include <ccan/tal/str/str.h>
#include <channeld/gen_channel_wire.h>
#include <common/addr.h>
#include <common/dev_disconnect.h>
#include <common/features.h>
#include <common/htlc_trim.h>
Expand Down Expand Up @@ -619,6 +620,17 @@ static void json_add_channel(struct lightningd *ld,
json_add_string(response, "channel_id",
type_to_string(tmpctx, struct channel_id, &cid));
json_add_txid(response, "funding_txid", &channel->funding_txid);

if (channel->shutdown_scriptpubkey[LOCAL]) {
char *addr = encode_scriptpubkey_to_addr(tmpctx,
get_chainparams(ld),
channel->shutdown_scriptpubkey[LOCAL]);
if (addr)
json_add_string(response, "close_to_addr", addr);
json_add_hex_talarr(response, "close_to",
channel->shutdown_scriptpubkey[LOCAL]);
}

json_add_bool(
response, "private",
!(channel->channel_flags & CHANNEL_FLAGS_ANNOUNCE_CHANNEL));
Expand Down
5 changes: 5 additions & 0 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
1 change: 1 addition & 0 deletions plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
3 changes: 1 addition & 2 deletions plugins/fundchannel.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
4 changes: 4 additions & 0 deletions tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1133,12 +1133,16 @@ def _fundchannel(l1, l2, close_to):
# check that you can provide a closing address upfront
addr = l1.rpc.newaddr()['bech32']
_fundchannel(l1, l2, addr)
# confirm that it appears in listpeers
assert addr == only_one(l1.rpc.listpeers()['peers'])['channels'][1]['close_to_addr']
resp = l1.rpc.close(l2.info['id'])
assert resp['type'] == 'mutual'
assert only_one(only_one(bitcoind.rpc.decoderawtransaction(resp['tx'])['vout'])['scriptPubKey']['addresses']) == addr

# check that passing in the same addr to close works
addr = bitcoind.rpc.getnewaddress()
_fundchannel(l1, l2, addr)
assert addr == only_one(l1.rpc.listpeers()['peers'])['channels'][2]['close_to_addr']
resp = l1.rpc.close(l2.info['id'], destination=addr)
assert resp['type'] == 'mutual'
assert only_one(only_one(bitcoind.rpc.decoderawtransaction(resp['tx'])['vout'])['scriptPubKey']['addresses']) == addr
Expand Down
5 changes: 5 additions & 0 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(); }
Expand Down
2 changes: 1 addition & 1 deletion wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down