May return NULL if encoding error occurs. */
| 27 | |
| 28 | /* May return NULL if encoding error occurs. */ |
| 29 | static char * |
| 30 | encode_pubkey_to_addr(const tal_t *ctx, |
| 31 | const struct pubkey *pubkey, |
| 32 | bool is_p2sh_p2wpkh, |
| 33 | /* Output: redeemscript to use to redeem outputs |
| 34 | * paying to the address. |
| 35 | * May be NULL if redeemscript is do not care. */ |
| 36 | u8 **out_redeemscript) |
| 37 | { |
| 38 | char *out; |
| 39 | const char *hrp; |
| 40 | struct sha256 h; |
| 41 | struct ripemd160 h160; |
| 42 | u8 *redeemscript; |
| 43 | bool ok; |
| 44 | |
| 45 | if (is_p2sh_p2wpkh) { |
| 46 | redeemscript = bitcoin_redeem_p2sh_p2wpkh(ctx, pubkey); |
| 47 | sha256(&h, redeemscript, tal_count(redeemscript)); |
| 48 | ripemd160(&h160, h.u.u8, sizeof(h)); |
| 49 | out = p2sh_to_base58(ctx, |
| 50 | chainparams, |
| 51 | &h160); |
| 52 | } else { |
| 53 | hrp = chainparams->onchain_hrp; |
| 54 | |
| 55 | /* out buffer is 73 + strlen(human readable part), |
| 56 | * see common/bech32.h*/ |
| 57 | out = tal_arr(ctx, char, 73 + strlen(hrp)); |
| 58 | pubkey_to_hash160(pubkey, &h160); |
| 59 | /* I am uncertain why this is so for direct SegWit |
| 60 | * outputs, but this is how listaddrs worked prior to |
| 61 | * this code being refactored. */ |
| 62 | redeemscript = tal_dup_arr(ctx, u8, |
| 63 | (u8 *) &h160, sizeof(h160), |
| 64 | 0); |
| 65 | |
| 66 | ok = segwit_addr_encode(out, hrp, 0, h160.u.u8, sizeof(h160)); |
| 67 | if (!ok) |
| 68 | out = tal_free(out); |
| 69 | } |
| 70 | |
| 71 | if (out_redeemscript) |
| 72 | *out_redeemscript = redeemscript; |
| 73 | else |
| 74 | tal_free(redeemscript); |
| 75 | |
| 76 | return out; |
| 77 | } |
| 78 | |
| 79 | enum addrtype { |
| 80 | ADDR_P2SH_SEGWIT = 1, |
no test coverage detected