mirror of
https://github.com/aljazceru/lightning.git
synced 2026-01-05 07:14:36 +01:00
addr: handle P2SH/P2PKH in scriptpubkey encoding
Previously, returned null if a scriptpubkey was not Segwit; now handles encoding to Base58 for other types.
This commit is contained in:
committed by
Rusty Russell
parent
6f215a70e5
commit
963a1da958
@@ -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;
|
||||
|
||||
@@ -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 */
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -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(); }
|
||||
|
||||
@@ -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 \
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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(); }
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user