Skip to content

Commit

Permalink
add p2tr fallback generation. builds but crashes when run
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisguida committed Jul 21, 2023
1 parent 13b5b8d commit f5604f5
Show file tree
Hide file tree
Showing 9 changed files with 121 additions and 45 deletions.
6 changes: 3 additions & 3 deletions common/test/run-json_filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,9 @@ int segwit_addr_decode(
const char* addr
)
{ fprintf(stderr, "segwit_addr_decode called!\n"); abort(); }
/* Generated stub for to_canonical_invstr */
const char *to_canonical_invstr(const tal_t *ctx UNNEEDED, const char *invstring UNNEEDED)
{ fprintf(stderr, "to_canonical_invstr called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
Expand All @@ -152,9 +155,6 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
/* Generated stub for towire_u8_array */
void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "towire_u8_array called!\n"); abort(); }
/* Generated stub for strip_lightning_prefix */
const char *to_canonical_invstr(const tal_t *ctx, const char *invstring UNNEEDED)
{ fprintf(stderr, "strip_lightning_prefix called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

bool deprecated_apis;
Expand Down
6 changes: 3 additions & 3 deletions common/test/run-json_remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ int segwit_addr_decode(
const char* addr
)
{ fprintf(stderr, "segwit_addr_decode called!\n"); abort(); }
/* Generated stub for to_canonical_invstr */
const char *to_canonical_invstr(const tal_t *ctx UNNEEDED, const char *invstring UNNEEDED)
{ fprintf(stderr, "to_canonical_invstr called!\n"); abort(); }
/* Generated stub for towire */
void towire(u8 **pptr UNNEEDED, const void *data UNNEEDED, size_t len UNNEEDED)
{ fprintf(stderr, "towire called!\n"); abort(); }
Expand All @@ -187,9 +190,6 @@ void towire_u8(u8 **pptr UNNEEDED, u8 v UNNEEDED)
/* Generated stub for towire_u8_array */
void towire_u8_array(u8 **pptr UNNEEDED, const u8 *arr UNNEEDED, size_t num UNNEEDED)
{ fprintf(stderr, "towire_u8_array called!\n"); abort(); }
/* Generated stub for strip_lightning_prefix */
const char *to_canonical_invstr(const tal_t *ctx, const char *invstring UNNEEDED)
{ fprintf(stderr, "strip_lightning_prefix called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

struct json {
Expand Down
6 changes: 3 additions & 3 deletions common/test/run-param.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,15 +40,15 @@ struct command_result *command_fail(struct command *cmd,
/* Generated stub for command_filter_ptr */
struct json_filter **command_filter_ptr(struct command *cmd UNNEEDED)
{ fprintf(stderr, "command_filter_ptr called!\n"); abort(); }
/* Generated stub for strip_lightning_prefix */
const char *to_canonical_invstr(const tal_t *ctx, const char *invstring UNNEEDED)
{ fprintf(stderr, "strip_lightning_prefix called!\n"); abort(); }
/* Generated stub for fromwire_tlv */
bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED,
void *record UNNEEDED, struct tlv_field **fields UNNEEDED,
const u64 *extra_types UNNEEDED, size_t *err_off UNNEEDED, u64 *err_type UNNEEDED)
{ fprintf(stderr, "fromwire_tlv called!\n"); abort(); }
/* Generated stub for to_canonical_invstr */
const char *to_canonical_invstr(const tal_t *ctx UNNEEDED, const char *invstring UNNEEDED)
{ fprintf(stderr, "to_canonical_invstr called!\n"); abort(); }
/* Generated stub for towire_tlv */
void towire_tlv(u8 **pptr UNNEEDED,
const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED,
Expand Down
37 changes: 37 additions & 0 deletions lightningd/invoice.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "config.h"
#include <bitcoin/script.h>
#include <ccan/array_size/array_size.h>
#include <ccan/asort/asort.h>
#include <ccan/cast/cast.h>
Expand Down Expand Up @@ -26,7 +27,9 @@
#include <lightningd/plugin_hook.h>
#include <lightningd/routehint.h>
#include <sodium/randombytes.h>
#include <stdio.h>
#include <wallet/invoices.h>
#include <wallet/walletrpc.h>
#include <wire/wire_sync.h>

const char *invoice_status_str(enum invoice_status state)
Expand Down Expand Up @@ -1158,7 +1161,41 @@ static struct command_result *json_invoice(struct command *cmd,
if (r)
return r;
}
} else if (cmd->ld->unified_invoices) {
struct pubkey pubkey;
// enum addrtype *addrtype;
// char *p2sh, *bech32, *p2tr;
// size_t i;
// const jsmntok_t *t;
u8 *p2tr_script;
// p_opt("fallbacks", param_array, &fallbacks),
// addrtype = tal(cmd, enum addrtype);
// *addrtype = ADDR_P2TR;
// newaddr_inner(cmd, &pubkey, addrtype);
enum addrtype addrtype;
addrtype = ADDR_P2TR;
newaddr_inner(cmd, &pubkey, &addrtype);
p2tr_script = scriptpubkey_p2tr(tmpctx, &pubkey);
// p2sh = encode_pubkey_to_addr(cmd, &pubkey, ADDR_P2SH_SEGWIT, NULL);
// bech32 = encode_pubkey_to_addr(cmd, &pubkey, ADDR_BECH32, NULL);
// p2tr = encode_pubkey_to_addr(cmd, &pubkey, ADDR_P2TR, NULL);
// if (!p2sh || !bech32 || !p2tr) {
// return command_fail(cmd, LIGHTNINGD,
// "p2wpkh address encoding failure.");
// }

fallback_scripts = tal_arr(cmd, const u8 *, 1);
fallback_scripts[0] = p2tr_script;
// log_info(cmd->ld->log, "fallback scripts = %s", type_to_string(tmpctx, const** u8, fallback_scripts));
// json_for_each_arr(i, t, fallbacks) {
// struct command_result *r;

// r = parse_fallback(cmd, buffer, t, &fallback_scripts[i]);
// if (r)
// return r;
// }
}
fprintf(stderr, "what's in here?? %s\n", tal_hex(tmpctx, fallback_scripts[0]));

if (preimage)
info->payment_preimage = *preimage;
Expand Down
14 changes: 9 additions & 5 deletions lightningd/test/run-invoice-select-inchan.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,6 @@ struct channel *any_channel_by_scid(struct lightningd *ld UNNEEDED,
const struct short_channel_id *scid UNNEEDED,
bool privacy_leak_ok UNNEEDED)
{ fprintf(stderr, "any_channel_by_scid called!\n"); abort(); }
/* Generated stub for param_invstring */
struct command_result *param_invstring(struct command *cmd, const char *name,
const char * buffer, const jsmntok_t *tok,
const char **str)
{ fprintf(stderr, "param_invstring called!\n"); abort(); }
/* Generated stub for bip32_pubkey */
void bip32_pubkey(struct lightningd *ld UNNEEDED, struct pubkey *pubkey UNNEEDED, u32 index UNNEEDED)
{ fprintf(stderr, "bip32_pubkey called!\n"); abort(); }
Expand Down Expand Up @@ -662,6 +657,10 @@ struct channel *new_unsaved_channel(struct peer *peer UNNEEDED,
u32 feerate_base UNNEEDED,
u32 feerate_ppm UNNEEDED)
{ fprintf(stderr, "new_unsaved_channel called!\n"); abort(); }
/* Generated stub for newaddr_inner */
bool newaddr_inner(struct command *cmd UNNEEDED, struct pubkey *pubkey UNNEEDED,
enum addrtype *addrtype UNNEEDED)
{ fprintf(stderr, "newaddr_inner called!\n"); abort(); }
/* Generated stub for node_id_cmp */
int node_id_cmp(const struct node_id *a UNNEEDED, const struct node_id *b UNNEEDED)
{ fprintf(stderr, "node_id_cmp called!\n"); abort(); }
Expand Down Expand Up @@ -715,6 +714,11 @@ struct command_result *param_escaped_string(struct command *cmd UNNEEDED,
const jsmntok_t *tok UNNEEDED,
const char **str UNNEEDED)
{ fprintf(stderr, "param_escaped_string called!\n"); abort(); }
/* Generated stub for param_invstring */
struct command_result *param_invstring(struct command *cmd UNNEEDED, const char *name UNNEEDED,
const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
const char **str UNNEEDED)
{ fprintf(stderr, "param_invstring called!\n"); abort(); }
/* Generated stub for param_label */
struct command_result *param_label(struct command *cmd UNNEEDED, const char *name UNNEEDED,
const char * buffer UNNEEDED, const jsmntok_t *tok UNNEEDED,
Expand Down
6 changes: 3 additions & 3 deletions wallet/test/run-db.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ void logv(struct log *log UNNEEDED, enum log_level level UNNEEDED, const struct
/* Generated stub for psbt_fixup */
const u8 *psbt_fixup(const tal_t *ctx UNNEEDED, const u8 *psbtblob UNNEEDED)
{ fprintf(stderr, "psbt_fixup called!\n"); abort(); }
/* Generated stub for to_canonical_invstr */
const char *to_canonical_invstr(const tal_t *ctx UNNEEDED, const char *invstring UNNEEDED)
{ fprintf(stderr, "to_canonical_invstr called!\n"); abort(); }
/* Generated stub for towire_hsmd_get_channel_basepoints */
u8 *towire_hsmd_get_channel_basepoints(const tal_t *ctx UNNEEDED, const struct node_id *peerid UNNEEDED, u64 dbid UNNEEDED)
{ fprintf(stderr, "towire_hsmd_get_channel_basepoints called!\n"); abort(); }
Expand All @@ -65,9 +68,6 @@ u8 *wire_sync_read(const tal_t *ctx UNNEEDED, int fd UNNEEDED)
/* Generated stub for wire_sync_write */
bool wire_sync_write(int fd UNNEEDED, const void *msg TAKES UNNEEDED)
{ fprintf(stderr, "wire_sync_write called!\n"); abort(); }
/* Generated stub for strip_lightning_prefix */
const char *to_canonical_invstr(const tal_t *ctx, const char *invstring UNNEEDED)
{ fprintf(stderr, "strip_lightning_prefix called!\n"); abort(); }
/* AUTOGENERATED MOCKS END */

void plugin_hook_db_sync(struct db *db UNNEEDED)
Expand Down
6 changes: 3 additions & 3 deletions wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,6 @@ static void test_error(struct lightningd *ld, bool fatal, const char *fmt, va_li
#include <stdio.h>

/* AUTOGENERATED MOCKS START */
/* Generated stub for strip_lightning_prefix */
const char *to_canonical_invstr(const tal_t *ctx, const char *invstring UNNEEDED)
{ fprintf(stderr, "strip_lightning_prefix called!\n"); abort(); }
/* Generated stub for bigsize_put */
size_t bigsize_put(u8 buf[BIGSIZE_MAX_LEN] UNNEEDED, bigsize_t v UNNEEDED)
{ fprintf(stderr, "bigsize_put called!\n"); abort(); }
Expand Down Expand Up @@ -705,6 +702,9 @@ void subkey_from_hmac(const char *prefix UNNEEDED,
const struct secret *base UNNEEDED,
struct secret *key UNNEEDED)
{ fprintf(stderr, "subkey_from_hmac called!\n"); abort(); }
/* Generated stub for to_canonical_invstr */
const char *to_canonical_invstr(const tal_t *ctx UNNEEDED, const char *invstring UNNEEDED)
{ fprintf(stderr, "to_canonical_invstr called!\n"); abort(); }
/* Generated stub for topology_add_sync_waiter_ */
void topology_add_sync_waiter_(const tal_t *ctx UNNEEDED,
struct chain_topology *topo UNNEEDED,
Expand Down
75 changes: 50 additions & 25 deletions wallet/walletrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,6 @@
#include <wally_psbt.h>
#include <wire/wire_sync.h>

enum addrtype {
/* Deprecated! */
ADDR_P2SH_SEGWIT = 1,
ADDR_BECH32 = 2,
ADDR_P2TR = 4,
ADDR_ALL = (ADDR_P2SH_SEGWIT + ADDR_BECH32 + ADDR_P2TR)
};

/* May return NULL if encoding error occurs. */
static char *
encode_pubkey_to_addr(const tal_t *ctx,
Expand Down Expand Up @@ -126,40 +118,73 @@ static struct command_result *param_newaddr(struct command *cmd,
return NULL;
}

static struct command_result *json_newaddr(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
bool newaddr_inner(struct command *cmd, struct pubkey *pubkey, enum addrtype *addrtype)
{
struct json_stream *response;
struct pubkey pubkey;
enum addrtype *addrtype;
// struct pubkey pubkey;
// enum addrtype *addrtype;
s64 keyidx;
char *p2sh, *bech32, *p2tr;
u8 *b32script;
u8 *p2tr_script;

if (!param(cmd, buffer, params,
p_opt_def("addresstype", param_newaddr, &addrtype, ADDR_BECH32),
NULL))
return command_param_failed();

keyidx = wallet_get_newindex(cmd->ld);
if (keyidx < 0) {
return command_fail(cmd, LIGHTNINGD, "Keys exhausted ");
// return command_fail(cmd, LIGHTNINGD, "Keys exhausted ");
return false;
}

bip32_pubkey(cmd->ld, &pubkey, keyidx);
bip32_pubkey(cmd->ld, pubkey, keyidx);

b32script = scriptpubkey_p2wpkh(tmpctx, &pubkey);
p2tr_script = scriptpubkey_p2tr(tmpctx, &pubkey);
b32script = scriptpubkey_p2wpkh(tmpctx, pubkey);
p2tr_script = scriptpubkey_p2tr(tmpctx, pubkey);
if (*addrtype & ADDR_BECH32)
txfilter_add_scriptpubkey(cmd->ld->owned_txfilter, b32script);
if (*addrtype & ADDR_P2TR)
txfilter_add_scriptpubkey(cmd->ld->owned_txfilter, p2tr_script);
if (cmd->ld->deprecated_apis && (*addrtype & ADDR_P2SH_SEGWIT))
txfilter_add_scriptpubkey(cmd->ld->owned_txfilter,
scriptpubkey_p2sh(tmpctx, b32script));
return true;
}


static struct command_result *json_newaddr(struct command *cmd,
const char *buffer,
const jsmntok_t *obj UNNEEDED,
const jsmntok_t *params)
{
struct json_stream *response;
struct pubkey pubkey;
enum addrtype *addrtype;
// s64 keyidx;
char *p2sh, *bech32, *p2tr;
// u8 *b32script;
// u8 *p2tr_script;

if (!param(cmd, buffer, params,
p_opt_def("addresstype", param_newaddr, &addrtype, ADDR_BECH32),
NULL))
return command_param_failed();

if (!newaddr_inner(cmd, &pubkey, addrtype)) {
return command_fail(cmd, LIGHTNINGD, "Keys exhausted ");
};

// keyidx = wallet_get_newindex(cmd->ld);
// if (keyidx < 0) {
// return command_fail(cmd, LIGHTNINGD, "Keys exhausted ");
// }

// bip32_pubkey(cmd->ld, &pubkey, keyidx);

// b32script = scriptpubkey_p2wpkh(tmpctx, &pubkey);
// p2tr_script = scriptpubkey_p2tr(tmpctx, &pubkey);
// if (*addrtype & ADDR_BECH32)
// txfilter_add_scriptpubkey(cmd->ld->owned_txfilter, b32script);
// if (*addrtype & ADDR_P2TR)
// txfilter_add_scriptpubkey(cmd->ld->owned_txfilter, p2tr_script);
// if (cmd->ld->deprecated_apis && (*addrtype & ADDR_P2SH_SEGWIT))
// txfilter_add_scriptpubkey(cmd->ld->owned_txfilter,
// scriptpubkey_p2sh(tmpctx, b32script));

p2sh = encode_pubkey_to_addr(cmd, &pubkey, ADDR_P2SH_SEGWIT, NULL);
bech32 = encode_pubkey_to_addr(cmd, &pubkey, ADDR_BECH32, NULL);
Expand Down
10 changes: 10 additions & 0 deletions wallet/walletrpc.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,19 @@
#define LIGHTNING_WALLET_WALLETRPC_H
#include "config.h"

enum addrtype {
/* Deprecated! */
ADDR_P2SH_SEGWIT = 1,
ADDR_BECH32 = 2,
ADDR_P2TR = 4,
ADDR_ALL = (ADDR_P2SH_SEGWIT + ADDR_BECH32 + ADDR_P2TR)
};

struct utxo;

/* We evaluate reserved timeouts lazily, so use this. */
bool is_reserved(const struct utxo *utxo, u32 current_height);

bool newaddr_inner(struct command *cmd, struct pubkey *pubkey,
enum addrtype *addrtype);
#endif /* LIGHTNING_WALLET_WALLETRPC_H */

0 comments on commit f5604f5

Please sign in to comment.