Skip to content

Commit

Permalink
fundpsbt: add option to filter out wrapped p2sh inputs
Browse files Browse the repository at this point in the history
We need to be able to only use non-wrapped inputs for v2/interactive tx
protocol.

Changelog-Added: JSONRPC: `fundpsbt` option `nonwrapped` filters out p2sh wrapped inputs
  • Loading branch information
niftynei authored and rustyrussell committed Feb 4, 2023
1 parent 4c46750 commit 679a473
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 2 deletions.
3 changes: 3 additions & 0 deletions doc/lightning-fundpsbt.7.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ the actual witness weight will be used.
*excess\_as\_change* is an optional boolean to flag to add a change output
for the excess sats.

*nonwrapped* is an optional boolean to signal to filter out any p2sh-wrapped
inputs from funding this PSBT.

EXAMPLE USAGE
-------------

Expand Down
5 changes: 4 additions & 1 deletion wallet/reservation.c
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ static struct command_result *json_fundpsbt(struct command *cmd,
u32 *feerate_per_kw;
u32 *minconf, *weight, *min_witness_weight;
struct amount_sat *amount, input, diff;
bool all, *excess_as_change;
bool all, *excess_as_change, *nonwrapped;
u32 *locktime, *reserve, maxheight;

if (!param(cmd, buffer, params,
Expand All @@ -458,6 +458,8 @@ static struct command_result *json_fundpsbt(struct command *cmd,
&min_witness_weight, 0),
p_opt_def("excess_as_change", param_bool,
&excess_as_change, false),
p_opt_def("nonwrapped", param_bool,
&nonwrapped, false),
NULL))
return command_param_failed();

Expand All @@ -479,6 +481,7 @@ static struct command_result *json_fundpsbt(struct command *cmd,
&diff,
*feerate_per_kw,
maxheight,
*nonwrapped,
cast_const2(const struct utxo **, utxos));
if (utxo) {
utxo_weight = utxo_spend_weight(utxo,
Expand Down
34 changes: 33 additions & 1 deletion wallet/test/run-wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -1050,13 +1050,14 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
/* Arbitrarily set scriptpubkey len to 20 */
u.scriptPubkey = tal_arr(w, u8, 20);
memset(u.scriptPubkey, 1, 20);
CHECK_MSG(wallet_add_utxo(w, &u, p2sh_wpkh),
CHECK_MSG(wallet_add_utxo(w, &u, our_change),
"wallet_add_utxo with close_info");

/* Now select them */
utxos = tal_arr(w, const struct utxo *, 0);
while ((one_utxo = wallet_find_utxo(w, w, 100, NULL, 253,
0 /* no confirmations required */,
false,
utxos)) != NULL) {
tal_arr_expand(&utxos, one_utxo);
}
Expand Down Expand Up @@ -1145,6 +1146,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
utxos = tal_arr(w, const struct utxo *, 0);
while ((one_utxo = wallet_find_utxo(w, w, 100, NULL, 253,
0 /* no confirmations required */,
false,
utxos)) != NULL) {
tal_arr_expand(&utxos, one_utxo);
}
Expand All @@ -1166,6 +1168,7 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
utxos = tal_arr(w, const struct utxo *, 0);
while ((one_utxo = wallet_find_utxo(w, w, 104, NULL, 253,
0 /* no confirmations required */,
false,
utxos)) != NULL) {
tal_arr_expand(&utxos, one_utxo);
}
Expand All @@ -1183,6 +1186,35 @@ static bool test_wallet_outputs(struct lightningd *ld, const tal_t *ctx)
/* Now un-reserve them */
tal_free(utxos);

/* Check that nonwrapped flag works */
utxos = tal_arr(w, const struct utxo *, 0);
while ((one_utxo = wallet_find_utxo(w, w, 100, NULL, 253,
0 /* no confirmations required */,
true,
utxos)) != NULL) {
tal_arr_expand(&utxos, one_utxo);
}
/* No nonwrapped outputs available */
CHECK(tal_count(utxos) == 0);
tal_free(utxos);

/* So we add one... */
memset(&u.outpoint, 4, sizeof(u.outpoint));
u.amount = AMOUNT_SAT(4);
u.close_info = tal_free(u.close_info);
CHECK_MSG(wallet_add_utxo(w, &u, p2wpkh),
"wallet_add_utxo failed, p2wpkh");

utxos = tal_arr(w, const struct utxo *, 0);
while ((one_utxo = wallet_find_utxo(w, w, 100, NULL, 253,
0 /* no confirmations required */,
true,
utxos)) != NULL) {
tal_arr_expand(&utxos, one_utxo);
}
/* And that's what comes back */
CHECK(tal_count(utxos) == 1);

db_commit_transaction(w->db);
return true;
}
Expand Down
2 changes: 2 additions & 0 deletions wallet/wallet.c
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ struct utxo *wallet_find_utxo(const tal_t *ctx, struct wallet *w,
struct amount_sat *amount_hint,
unsigned feerate_per_kw,
u32 maxheight,
bool nonwrapped,
const struct utxo **excludes)
{
struct db_stmt *stmt;
Expand Down Expand Up @@ -569,6 +570,7 @@ struct utxo *wallet_find_utxo(const tal_t *ctx, struct wallet *w,
while (!utxo && db_step(stmt)) {
utxo = wallet_stmt2output(ctx, stmt);
if (excluded(excludes, utxo)
|| (nonwrapped && utxo->is_p2sh)
|| !deep_enough(maxheight, utxo, current_blockheight))
utxo = tal_free(utxo);

Expand Down
2 changes: 2 additions & 0 deletions wallet/wallet.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,7 @@ struct utxo **wallet_get_unconfirmed_closeinfo_utxos(const tal_t *ctx,
* @amount_we_are_short: optional amount.
* @feerate_per_kw: feerate we are using.
* @maxheight: zero (if caller doesn't care) or maximum blockheight to accept.
* @nonwrapped: filter out p2sh-wrapped inputs
* @excludes: UTXOs not to consider.
*
* If @amount_we_are_short is not NULL, we try to get something very close
Expand All @@ -486,6 +487,7 @@ struct utxo *wallet_find_utxo(const tal_t *ctx, struct wallet *w,
struct amount_sat *amount_we_are_short,
unsigned feerate_per_kw,
u32 maxheight,
bool nonwrapped,
const struct utxo **excludes);

/**
Expand Down

0 comments on commit 679a473

Please sign in to comment.