Browse Source
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
6 years ago
committed by
Rusty Russell
6 changed files with 40 additions and 21 deletions
@ -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; |
|||
} |
@ -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 */ |
Loading…
Reference in new issue