mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +01:00
common: pull out scriptPubkey address method
We're going to need this for P2WSH scripts. pull it out into a common file plus adopt the sanity checks so that it will allow for either P2WSH or P2WPKH (previously only encoded P2WPKH scripts)
This commit is contained in:
committed by
Rusty Russell
parent
5a520f4a07
commit
a9d0550cf4
@@ -1,4 +1,5 @@
|
|||||||
COMMON_SRC_NOGEN := \
|
COMMON_SRC_NOGEN := \
|
||||||
|
common/addr.c \
|
||||||
common/amount.c \
|
common/amount.c \
|
||||||
common/base32.c \
|
common/base32.c \
|
||||||
common/bech32.c \
|
common/bech32.c \
|
||||||
|
|||||||
22
common/addr.c
Normal file
22
common/addr.c
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
#include "addr.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)
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
12
common/addr.h
Normal file
12
common/addr.h
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
#ifndef LIGHTNING_COMMON_ADDR_H
|
||||||
|
#define LIGHTNING_COMMON_ADDR_H
|
||||||
|
#include "config.h"
|
||||||
|
#include <ccan/short_types/short_types.h>
|
||||||
|
#include <ccan/tal/tal.h>
|
||||||
|
|
||||||
|
/* 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 */
|
||||||
@@ -14,6 +14,7 @@ default: lightningd-all
|
|||||||
|
|
||||||
# Common source we use.
|
# Common source we use.
|
||||||
LIGHTNINGD_COMMON_OBJS := \
|
LIGHTNINGD_COMMON_OBJS := \
|
||||||
|
common/addr.o \
|
||||||
common/amount.o \
|
common/amount.o \
|
||||||
common/base32.o \
|
common/base32.o \
|
||||||
common/bech32.o \
|
common/bech32.o \
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <bitcoin/privkey.h>
|
#include <bitcoin/privkey.h>
|
||||||
#include <bitcoin/script.h>
|
#include <bitcoin/script.h>
|
||||||
#include <ccan/tal/str/str.h>
|
#include <ccan/tal/str/str.h>
|
||||||
|
#include <common/addr.h>
|
||||||
#include <common/channel_config.h>
|
#include <common/channel_config.h>
|
||||||
#include <common/funding_tx.h>
|
#include <common/funding_tx.h>
|
||||||
#include <common/json_command.h>
|
#include <common/json_command.h>
|
||||||
|
|||||||
@@ -2,6 +2,7 @@
|
|||||||
#include <bitcoin/base58.h>
|
#include <bitcoin/base58.h>
|
||||||
#include <bitcoin/script.h>
|
#include <bitcoin/script.h>
|
||||||
#include <ccan/tal/str/str.h>
|
#include <ccan/tal/str/str.h>
|
||||||
|
#include <common/addr.h>
|
||||||
#include <common/bech32.h>
|
#include <common/bech32.h>
|
||||||
#include <common/json_command.h>
|
#include <common/json_command.h>
|
||||||
#include <common/json_helpers.h>
|
#include <common/json_helpers.h>
|
||||||
@@ -419,26 +420,6 @@ encode_pubkey_to_addr(const tal_t *ctx,
|
|||||||
return out;
|
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 {
|
enum addrtype {
|
||||||
ADDR_P2SH_SEGWIT = 1,
|
ADDR_P2SH_SEGWIT = 1,
|
||||||
ADDR_BECH32 = 2,
|
ADDR_BECH32 = 2,
|
||||||
@@ -638,7 +619,8 @@ 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, cmd->ld, utxos[i]->scriptPubkey);
|
cmd, get_chainparams(cmd->ld)->bip173_name,
|
||||||
|
utxos[i]->scriptPubkey);
|
||||||
if (out)
|
if (out)
|
||||||
json_add_string(response, "address", out);
|
json_add_string(response, "address", out);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user