diff --git a/wallet/wallet.c b/wallet/wallet.c index f3deecabd45d..a6b573e4eb00 100644 --- a/wallet/wallet.c +++ b/wallet/wallet.c @@ -2201,3 +2201,20 @@ void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx, db_exec_prepared(w->db, stmt); } } + +u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid) +{ + u32 blockheight; + sqlite3_stmt *stmt = db_prepare( + w->db, "SELECT blockheight, txindex, rawtx FROM transactions WHERE id=?"); + sqlite3_bind_sha256(stmt, 1, &txid->shad.sha); + + if (sqlite3_step(stmt) != SQLITE_ROW) { + sqlite3_finalize(stmt); + return 0; + } + + blockheight = sqlite3_column_int(stmt, 0); + sqlite3_finalize(stmt); + return blockheight; +} diff --git a/wallet/wallet.h b/wallet/wallet.h index 2dc3f764a498..187844163da1 100644 --- a/wallet/wallet.h +++ b/wallet/wallet.h @@ -814,4 +814,10 @@ void wallet_utxoset_add(struct wallet *w, const struct bitcoin_tx *tx, void wallet_transaction_add(struct wallet *w, const struct bitcoin_tx *tx, const u32 blockheight, const u32 txindex); +/** + * Get the confirmation height of a transaction we are watching by its + * txid. Returns 0 if the transaction was not part of any block. + */ +u32 wallet_transaction_height(struct wallet *w, const struct bitcoin_txid *txid); + #endif /* LIGHTNING_WALLET_WALLET_H */