Skip to content

Commit

Permalink
fallback: json add fallback cleanup
Browse files Browse the repository at this point in the history
Expand to handle P2TR addresses, use the common script encoding utility
function instead of reimplementing it.
  • Loading branch information
niftynei committed Jul 24, 2023
1 parent ff7088e commit 590c882
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 20 deletions.
31 changes: 12 additions & 19 deletions common/bolt11_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <bitcoin/base58.h>
#include <bitcoin/script.h>
#include <ccan/tal/str/str.h>
#include <common/addr.h>
#include <common/bech32.h>
#include <common/bolt11.h>
#include <common/bolt11_json.h>
Expand All @@ -13,32 +14,24 @@ static void json_add_fallback(struct json_stream *response,
const u8 *fallback,
const struct chainparams *chain)
{
struct bitcoin_address pkh;
struct ripemd160 sh;
struct sha256 wsh;
char *addr;

json_object_start(response, fieldname);
if (is_p2pkh(fallback, &pkh)) {
if (is_p2pkh(fallback, NULL)) {
json_add_string(response, "type", "P2PKH");
json_add_string(response, "addr",
bitcoin_to_base58(tmpctx, chain, &pkh));
} else if (is_p2sh(fallback, &sh)) {
} else if (is_p2sh(fallback, NULL)) {
json_add_string(response, "type", "P2SH");
json_add_string(response, "addr",
p2sh_to_base58(tmpctx, chain, &sh));
} else if (is_p2wpkh(fallback, &pkh)) {
char out[73 + strlen(chain->onchain_hrp)];
} else if (is_p2wpkh(fallback, NULL)) {
json_add_string(response, "type", "P2WPKH");
if (segwit_addr_encode(out, chain->onchain_hrp, 0,
(const u8 *)&pkh, sizeof(pkh)))
json_add_string(response, "addr", out);
} else if (is_p2wsh(fallback, &wsh)) {
char out[73 + strlen(chain->onchain_hrp)];
} else if (is_p2wsh(fallback, NULL)) {
json_add_string(response, "type", "P2WSH");
if (segwit_addr_encode(out, chain->onchain_hrp, 0,
(const u8 *)&wsh, sizeof(wsh)))
json_add_string(response, "addr", out);
} else if (is_p2tr(fallback, NULL)) {
json_add_string(response, "type", "P2TR");
}

addr = encode_scriptpubkey_to_addr(tmpctx, chain, fallback);
if (addr)
json_add_string(response, "addr", addr);
json_add_hex_talarr(response, "hex", fallback);
json_object_end(response);
}
Expand Down
2 changes: 1 addition & 1 deletion plugins/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ $(PLUGIN_KEYSEND_OBJS): $(PLUGIN_PAY_LIB_HEADER)

plugins/spenderp: bitcoin/block.o bitcoin/preimage.o bitcoin/psbt.o common/psbt_open.o wire/peer${EXP}_wiregen.o $(PLUGIN_SPENDER_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) $(JSMN_OBJS)

plugins/offers: $(PLUGIN_OFFERS_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) common/bolt12.o common/bolt12_merkle.o common/bolt11_json.o common/iso4217.o $(WIRE_OBJS) $(WIRE_BOLT12_OBJS) bitcoin/block.o common/channel_id.o bitcoin/preimage.o common/blindedpath.o common/invoice_path_id.o common/blinding.o common/hmac.o $(JSMN_OBJS)
plugins/offers: $(PLUGIN_OFFERS_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) common/addr.o common/bolt12.o common/bolt12_merkle.o common/bolt11_json.o common/iso4217.o $(WIRE_OBJS) $(WIRE_BOLT12_OBJS) bitcoin/block.o common/channel_id.o bitcoin/preimage.o common/blindedpath.o common/invoice_path_id.o common/blinding.o common/hmac.o $(JSMN_OBJS)

plugins/fetchinvoice: $(PLUGIN_FETCHINVOICE_OBJS) $(PLUGIN_LIB_OBJS) $(PLUGIN_COMMON_OBJS) common/bolt12.o common/bolt12_merkle.o common/iso4217.o $(WIRE_OBJS) $(WIRE_BOLT12_OBJS) bitcoin/block.o common/channel_id.o bitcoin/preimage.o $(JSMN_OBJS) common/gossmap.o common/fp16.o common/dijkstra.o common/route.o common/blindedpath.o common/hmac.o common/blinding.o

Expand Down

0 comments on commit 590c882

Please sign in to comment.