From f7f3ebae32a2cb9d8f5fad7fe0d0d9db5013d376 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 13 Nov 2024 13:24:27 +1030 Subject: [PATCH] wallet: new routine to simply get the funding spend tx, if known. Signed-off-by: Rusty Russell --- wallet/wallet.c | 30 ++++++++++++++++++++++++++++++ wallet/wallet.h | 8 ++++++++ 2 files changed, 38 insertions(+) diff --git a/wallet/wallet.c b/wallet/wallet.c index 149778f68..900837c4a 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -4738,6 +4738,36 @@ struct channeltx *wallet_channeltxs_get(const tal_t *ctx, struct wallet *w, return res; } +struct bitcoin_tx *wallet_get_funding_spend(const tal_t *ctx, + struct wallet *w, + u64 channel_id, + u32 *blockheight) +{ + struct db_stmt *stmt; + struct bitcoin_tx *tx; + + stmt = db_prepare_v2(w->db, + SQL("SELECT" + " t.blockheight" + ", t.rawtx" + " FROM channeltxs c" + " JOIN transactions t ON t.id = c.transaction_id" + " WHERE c.channel_id = ? AND t.blockheight IS NOT NULL AND c.type = ?" + " ORDER BY c.id ASC;")); + db_bind_int(stmt, channel_id); + db_bind_int(stmt, WIRE_ONCHAIND_INIT); + db_query_prepared(stmt); + + if (db_step(stmt)) { + tx = db_col_tx(ctx, stmt, "t.rawtx"); + *blockheight = db_col_int(stmt, "t.blockheight"); + } else + tx = NULL; + tal_free(stmt); + + return tx; +} + static bool wallet_forwarded_payment_update(struct wallet *w, const struct htlc_in *in, const struct htlc_out *out, diff --git a/wallet/wallet.h b/wallet/wallet.h index 9329e5a37..0d1b0f76f 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -1198,6 +1198,14 @@ u32 *wallet_onchaind_channels(const tal_t *ctx, struct wallet *w); struct channeltx *wallet_channeltxs_get(const tal_t *ctx, struct wallet *w, u32 channel_id); +/** + * Get the transaction which spend funding for this channel, if any. + */ +struct bitcoin_tx *wallet_get_funding_spend(const tal_t *ctx, + struct wallet *w, + u64 channel_id, + u32 *blockheight); + /** * Add of update a forwarded_payment */