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

Function encode_pubkey_to_addr

wallet/walletrpc.c:21–86  ·  view source on GitHub ↗

May return NULL if encoding error occurs. */

Source from the content-addressed store, hash-verified

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