| 6 | #include <common/bech32.h> |
| 7 | |
| 8 | char *encode_scriptpubkey_to_addr(const tal_t *ctx, |
| 9 | const struct chainparams *chainparams, |
| 10 | const u8 *scriptpubkey, |
| 11 | size_t scriptpubkey_len) |
| 12 | { |
| 13 | char *out; |
| 14 | struct bitcoin_address pkh; |
| 15 | struct ripemd160 sh; |
| 16 | int witver; |
| 17 | |
| 18 | if (is_p2pkh(scriptpubkey, scriptpubkey_len, &pkh)) |
| 19 | return bitcoin_to_base58(ctx, chainparams, &pkh); |
| 20 | |
| 21 | if (is_p2sh(scriptpubkey, scriptpubkey_len, &sh)) |
| 22 | return p2sh_to_base58(ctx, chainparams, &sh); |
| 23 | |
| 24 | if (is_p2tr(scriptpubkey, scriptpubkey_len, NULL)) |
| 25 | witver = 1; |
| 26 | else if (is_p2wpkh(scriptpubkey, scriptpubkey_len, NULL) |
| 27 | || is_p2wsh(scriptpubkey, scriptpubkey_len, NULL)) |
| 28 | witver = 0; |
| 29 | else { |
| 30 | return NULL; |
| 31 | } |
| 32 | out = tal_arr(ctx, char, 73 + strlen(chainparams->onchain_hrp)); |
| 33 | if (!segwit_addr_encode(out, chainparams->onchain_hrp, witver, |
| 34 | scriptpubkey + 2, scriptpubkey_len - 2)) |
| 35 | return tal_free(out); |
| 36 | |
| 37 | return out; |
| 38 | } |
| 39 | |
| 40 | static const char *segwit_addr_net_decode(int *witness_version, |
| 41 | uint8_t *witness_program, |
no test coverage detected