@@ -2912,6 +2912,68 @@ void wallet_confirm_tx(struct wallet *w,
29122912 db_exec_prepared_v2 (take (stmt ));
29132913}
29142914
2915+ static void got_utxo (struct wallet * w ,
2916+ u64 keyindex ,
2917+ const struct wally_tx * wtx ,
2918+ size_t outnum ,
2919+ bool is_coinbase ,
2920+ const u32 * blockheight )
2921+ {
2922+ struct utxo * utxo = tal (tmpctx , struct utxo );
2923+ const struct wally_tx_output * txout = & wtx -> outputs [outnum ];
2924+ struct amount_asset asset = wally_tx_output_get_amount (txout );
2925+
2926+ utxo -> keyindex = keyindex ;
2927+ utxo -> is_p2sh = is_p2sh (txout -> script , txout -> script_len , NULL );
2928+ utxo -> amount = amount_asset_to_sat (& asset );
2929+ utxo -> status = OUTPUT_STATE_AVAILABLE ;
2930+ wally_txid (wtx , & utxo -> outpoint .txid );
2931+ utxo -> outpoint .n = outnum ;
2932+ utxo -> close_info = NULL ;
2933+ utxo -> is_in_coinbase = is_coinbase ;
2934+
2935+ utxo -> blockheight = blockheight ;
2936+ utxo -> spendheight = NULL ;
2937+ utxo -> scriptPubkey = tal_dup_arr (utxo , u8 , txout -> script , txout -> script_len , 0 );
2938+ log_debug (w -> log , "Owning output %zu %s (%s) txid %s%s%s" ,
2939+ outnum ,
2940+ fmt_amount_sat (tmpctx , utxo -> amount ),
2941+ utxo -> is_p2sh ? "P2SH" : "SEGWIT" ,
2942+ fmt_bitcoin_txid (tmpctx , & utxo -> outpoint .txid ),
2943+ blockheight ? " CONFIRMED" : "" ,
2944+ is_coinbase ? " COINBASE" : "" );
2945+
2946+ /* We only record final ledger movements */
2947+ if (blockheight ) {
2948+ struct chain_coin_mvt * mvt ;
2949+
2950+ mvt = new_coin_wallet_deposit (tmpctx , & utxo -> outpoint ,
2951+ * blockheight ,
2952+ utxo -> amount ,
2953+ DEPOSIT );
2954+ notify_chain_mvt (w -> ld , mvt );
2955+ }
2956+
2957+ if (!wallet_add_utxo (w , utxo , utxo -> is_p2sh ? p2sh_wpkh : our_change )) {
2958+ /* In case we already know the output, make
2959+ * sure we actually track its
2960+ * blockheight. This can happen when we grab
2961+ * the output from a transaction we created
2962+ * ourselves. */
2963+ if (blockheight )
2964+ wallet_confirm_tx (w , & utxo -> outpoint .txid , * blockheight );
2965+ return ;
2966+ }
2967+
2968+ /* This is an unconfirmed change output, we should track it */
2969+ if (!utxo -> is_p2sh && !blockheight )
2970+ txfilter_add_scriptpubkey (w -> ld -> owned_txfilter , txout -> script );
2971+
2972+ outpointfilter_add (w -> owned_outpoints , & utxo -> outpoint );
2973+
2974+ wallet_annotate_txout (w , & utxo -> outpoint , TX_WALLET_DEPOSIT , 0 );
2975+ }
2976+
29152977int wallet_extract_owned_outputs (struct wallet * w , const struct wally_tx * wtx ,
29162978 bool is_coinbase ,
29172979 const u32 * blockheight )
@@ -2920,68 +2982,16 @@ int wallet_extract_owned_outputs(struct wallet *w, const struct wally_tx *wtx,
29202982
29212983 for (size_t i = 0 ; i < wtx -> num_outputs ; i ++ ) {
29222984 const struct wally_tx_output * txout = & wtx -> outputs [i ];
2923- struct utxo * utxo ;
2924- u32 index ;
2985+ u32 keyindex ;
29252986 struct amount_asset asset = wally_tx_output_get_amount (txout );
2926- struct chain_coin_mvt * mvt ;
29272987
29282988 if (!amount_asset_is_main (& asset ))
29292989 continue ;
29302990
2931- if (!wallet_can_spend (w , txout -> script , txout -> script_len , & index ))
2991+ if (!wallet_can_spend (w , txout -> script , txout -> script_len , & keyindex ))
29322992 continue ;
29332993
2934- utxo = tal (w , struct utxo );
2935- utxo -> keyindex = index ;
2936- utxo -> is_p2sh = is_p2sh (txout -> script , txout -> script_len , NULL );
2937- utxo -> amount = amount_asset_to_sat (& asset );
2938- utxo -> status = OUTPUT_STATE_AVAILABLE ;
2939- wally_txid (wtx , & utxo -> outpoint .txid );
2940- utxo -> outpoint .n = i ;
2941- utxo -> close_info = NULL ;
2942- utxo -> is_in_coinbase = is_coinbase ;
2943-
2944- utxo -> blockheight = blockheight ? blockheight : NULL ;
2945- utxo -> spendheight = NULL ;
2946- utxo -> scriptPubkey = tal_dup_arr (utxo , u8 , txout -> script , txout -> script_len , 0 );
2947- log_debug (w -> log , "Owning output %zu %s (%s) txid %s%s%s" ,
2948- i ,
2949- fmt_amount_sat (tmpctx , utxo -> amount ),
2950- utxo -> is_p2sh ? "P2SH" : "SEGWIT" ,
2951- fmt_bitcoin_txid (tmpctx , & utxo -> outpoint .txid ),
2952- blockheight ? " CONFIRMED" : "" ,
2953- is_coinbase ? " COINBASE" : "" );
2954-
2955- /* We only record final ledger movements */
2956- if (blockheight ) {
2957- mvt = new_coin_wallet_deposit (tmpctx , & utxo -> outpoint ,
2958- * blockheight ,
2959- utxo -> amount ,
2960- DEPOSIT );
2961- notify_chain_mvt (w -> ld , mvt );
2962- }
2963-
2964- if (!wallet_add_utxo (w , utxo , utxo -> is_p2sh ? p2sh_wpkh : our_change )) {
2965- /* In case we already know the output, make
2966- * sure we actually track its
2967- * blockheight. This can happen when we grab
2968- * the output from a transaction we created
2969- * ourselves. */
2970- if (blockheight )
2971- wallet_confirm_tx (w , & utxo -> outpoint .txid ,
2972- * blockheight );
2973- tal_free (utxo );
2974- continue ;
2975- }
2976-
2977- /* This is an unconfirmed change output, we should track it */
2978- if (!utxo -> is_p2sh && !blockheight )
2979- txfilter_add_scriptpubkey (w -> ld -> owned_txfilter , txout -> script );
2980-
2981- outpointfilter_add (w -> owned_outpoints , & utxo -> outpoint );
2982-
2983- wallet_annotate_txout (w , & utxo -> outpoint , TX_WALLET_DEPOSIT , 0 );
2984- tal_free (utxo );
2994+ got_utxo (w , keyindex , wtx , i , is_coinbase , blockheight );
29852995 num_utxos ++ ;
29862996 }
29872997 return num_utxos ;
0 commit comments