Browse Source

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)
pull/2938/head
lisa neigut 5 years ago
committed by Rusty Russell
parent
commit
a9d0550cf4
  1. 1
      common/Makefile
  2. 22
      common/addr.c
  3. 12
      common/addr.h
  4. 1
      lightningd/Makefile
  5. 1
      lightningd/opening_control.c
  6. 24
      wallet/walletrpc.c

1
common/Makefile

@ -1,4 +1,5 @@
COMMON_SRC_NOGEN := \
common/addr.c \
common/amount.c \
common/base32.c \
common/bech32.c \

22
common/addr.c

@ -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

@ -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 */

1
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 \

1
lightningd/opening_control.c

@ -2,6 +2,7 @@
#include <bitcoin/privkey.h>
#include <bitcoin/script.h>
#include <ccan/tal/str/str.h>
#include <common/addr.h>
#include <common/channel_config.h>
#include <common/funding_tx.h>
#include <common/json_command.h>

24
wallet/walletrpc.c

@ -2,6 +2,7 @@
#include <bitcoin/base58.h>
#include <bitcoin/script.h>
#include <ccan/tal/str/str.h>
#include <common/addr.h>
#include <common/bech32.h>
#include <common/json_command.h>
#include <common/json_helpers.h>
@ -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);
}

Loading…
Cancel
Save