core-lightning/common/addr.c
Rusty Russell 4ffda340d3 check: make sure all files outside contrib/ include "config.h" first.
And turn "" includes into full-path (which makes it easier to put
config.h first, and finds some cases check-includes.sh missed
previously).

config.h sets _GNU_SOURCE which really needs to be done before any
'#includes': we mainly got away with it with glibc, but other platforms
like Alpine may have stricter requirements.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
2021-12-06 10:05:39 +10:30

30 lines
771 B
C

#include "config.h"
#include <bitcoin/address.h>
#include <bitcoin/base58.h>
#include <bitcoin/script.h>
#include <common/addr.h>
#include <common/bech32.h>
char *encode_scriptpubkey_to_addr(const tal_t *ctx,
const struct chainparams *chainparams,
const u8 *scriptPubkey)
{
char *out;
size_t scriptLen = tal_bytelen(scriptPubkey);
struct bitcoin_address pkh;
struct ripemd160 sh;
if (is_p2pkh(scriptPubkey, &pkh))
return bitcoin_to_base58(ctx, chainparams, &pkh);
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;
}