mirror of
https://github.com/aljazceru/lightning.git
synced 2025-12-19 15:14:23 +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;
|
||||
|
||||
Reference in New Issue
Block a user