| 2296 | } |
| 2297 | |
| 2298 | int wallet_extract_owned_outputs(struct wallet *w, const struct wally_tx *wtx, |
| 2299 | const u32 *blockheight, |
| 2300 | struct amount_sat *total) |
| 2301 | { |
| 2302 | int num_utxos = 0; |
| 2303 | |
| 2304 | *total = AMOUNT_SAT(0); |
| 2305 | for (size_t output = 0; output < wtx->num_outputs; output++) { |
| 2306 | struct utxo *utxo; |
| 2307 | u32 index; |
| 2308 | bool is_p2sh; |
| 2309 | const u8 *script; |
| 2310 | struct amount_asset asset = |
| 2311 | wally_tx_output_get_amount(&wtx->outputs[output]); |
| 2312 | struct chain_coin_mvt *mvt; |
| 2313 | |
| 2314 | if (!amount_asset_is_main(&asset)) |
| 2315 | continue; |
| 2316 | |
| 2317 | script = wally_tx_output_get_script(tmpctx, |
| 2318 | &wtx->outputs[output]); |
| 2319 | if (!script) |
| 2320 | continue; |
| 2321 | |
| 2322 | if (!wallet_can_spend(w, script, &index, &is_p2sh)) |
| 2323 | continue; |
| 2324 | |
| 2325 | utxo = tal(w, struct utxo); |
| 2326 | utxo->keyindex = index; |
| 2327 | utxo->is_p2sh = is_p2sh; |
| 2328 | utxo->amount = amount_asset_to_sat(&asset); |
| 2329 | utxo->status = OUTPUT_STATE_AVAILABLE; |
| 2330 | wally_txid(wtx, &utxo->outpoint.txid); |
| 2331 | utxo->outpoint.n = output; |
| 2332 | utxo->close_info = NULL; |
| 2333 | |
| 2334 | utxo->blockheight = blockheight ? blockheight : NULL; |
| 2335 | utxo->spendheight = NULL; |
| 2336 | utxo->scriptPubkey = tal_dup_talarr(utxo, u8, script); |
| 2337 | |
| 2338 | log_debug(w->log, "Owning output %zu %s (%s) txid %s%s", |
| 2339 | output, |
| 2340 | type_to_string(tmpctx, struct amount_sat, |
| 2341 | &utxo->amount), |
| 2342 | is_p2sh ? "P2SH" : "SEGWIT", |
| 2343 | type_to_string(tmpctx, struct bitcoin_txid, |
| 2344 | &utxo->outpoint.txid), |
| 2345 | blockheight ? " CONFIRMED" : ""); |
| 2346 | |
| 2347 | /* We only record final ledger movements */ |
| 2348 | if (blockheight) { |
| 2349 | mvt = new_coin_wallet_deposit(tmpctx, &utxo->outpoint, |
| 2350 | *blockheight, |
| 2351 | utxo->amount, |
| 2352 | DEPOSIT); |
| 2353 | notify_chain_mvt(w->ld, mvt); |
| 2354 | } |
| 2355 |
no test coverage detected