MCPcopy Create free account
hub / github.com/ElementsProject/lightning / encode_pubkey_to_addr

Function encode_pubkey_to_addr

wallet/walletrpc.c:22–87  ·  view source on GitHub ↗

May return NULL if encoding error occurs. */

Source from the content-addressed store, hash-verified

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

Callers 3

json_newaddrFunction · 0.85
json_listaddressesFunction · 0.85
json_listaddrsFunction · 0.85

Calls 5

pubkey_to_hash160Function · 0.85
segwit_addr_encodeFunction · 0.85
scriptpubkey_p2trFunction · 0.85
abortFunction · 0.85
tal_freeFunction · 0.85

Tested by

no test coverage detected