2021-12-04 12:23:56 +01:00
|
|
|
#include "config.h"
|
2019-10-29 18:00:18 +01:00
|
|
|
#include <bitcoin/address.h>
|
|
|
|
#include <bitcoin/base58.h>
|
2019-05-22 23:20:14 +02:00
|
|
|
#include <bitcoin/script.h>
|
2021-12-04 12:23:56 +01:00
|
|
|
#include <common/addr.h>
|
2019-05-22 23:20:14 +02:00
|
|
|
#include <common/bech32.h>
|
|
|
|
|
|
|
|
char *encode_scriptpubkey_to_addr(const tal_t *ctx,
|
2019-10-29 18:00:18 +01:00
|
|
|
const struct chainparams *chainparams,
|
|
|
|
const u8 *scriptPubkey)
|
2019-05-22 23:20:14 +02:00
|
|
|
{
|
|
|
|
char *out;
|
|
|
|
size_t scriptLen = tal_bytelen(scriptPubkey);
|
2019-10-29 18:00:18 +01:00
|
|
|
struct bitcoin_address pkh;
|
|
|
|
struct ripemd160 sh;
|
2023-07-10 18:25:43 +02:00
|
|
|
int witver;
|
2019-05-22 23:20:14 +02:00
|
|
|
|
2019-10-29 18:00:18 +01:00
|
|
|
if (is_p2pkh(scriptPubkey, &pkh))
|
|
|
|
return bitcoin_to_base58(ctx, chainparams, &pkh);
|
2019-05-22 23:20:14 +02:00
|
|
|
|
2019-10-29 18:00:18 +01:00
|
|
|
if (is_p2sh(scriptPubkey, &sh))
|
|
|
|
return p2sh_to_base58(ctx, chainparams, &sh);
|
|
|
|
|
2021-11-29 08:10:06 +01:00
|
|
|
out = tal_arr(ctx, char, 73 + strlen(chainparams->onchain_hrp));
|
2023-07-10 18:25:43 +02:00
|
|
|
if (is_p2tr(scriptPubkey, NULL))
|
|
|
|
witver = 1;
|
|
|
|
else if (is_p2wpkh(scriptPubkey, NULL) || is_p2wsh(scriptPubkey, NULL))
|
|
|
|
witver = 0;
|
|
|
|
else {
|
|
|
|
return tal_free(out);
|
|
|
|
}
|
|
|
|
if (!segwit_addr_encode(out, chainparams->onchain_hrp, witver,
|
2019-10-29 18:00:18 +01:00
|
|
|
scriptPubkey + 2, scriptLen - 2))
|
2019-05-22 23:20:14 +02:00
|
|
|
return tal_free(out);
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|