Skip to content

Commit 1fecf85

Browse files
committed
f Clean up list_confirmed_utxos a bit
1 parent eb3e0fe commit 1fecf85

File tree

1 file changed

+47
-44
lines changed

1 file changed

+47
-44
lines changed

src/wallet.rs

+47-44
Original file line numberDiff line numberDiff line change
@@ -260,52 +260,55 @@ where
260260
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
261261
let locked_wallet = self.inner.lock().unwrap();
262262
let mut utxos = Vec::new();
263-
let txs = locked_wallet.list_transactions(true).map_err(|e| {
264-
log_error!(self.logger, "Failed to retrieve transactions from wallet: {}", e);
265-
})?;
266-
let unspent = locked_wallet.list_unspent().map_err(|e| {
267-
log_error!(self.logger, "Failed to retrieve unspent transactions from wallet: {}", e);
268-
})?;
269-
270-
for u in unspent {
271-
for t in &txs {
272-
if u.outpoint.txid == t.txid && t.confirmation_time.is_some() {
273-
let payload = Payload::from_script(&u.txout.script_pubkey).map_err(|e| {
274-
log_error!(self.logger, "Failed to retrieve script payload: {}", e);
275-
})?;
276-
277-
match payload {
278-
Payload::WitnessProgram(program) => {
279-
if program.version() == WitnessVersion::V0
280-
&& program.program().len() == 20
281-
{
282-
let wpkh = WPubkeyHash::from_slice(program.program().as_bytes())
283-
.map_err(|e| {
284-
log_error!(
285-
self.logger,
286-
"Failed to retrieve script payload: {}",
287-
e
288-
);
289-
})?;
290-
let utxo = Utxo::new_v0_p2wpkh(u.outpoint, u.txout.value, &wpkh);
291-
utxos.push(utxo);
292-
} else {
293-
log_error!(
294-
self.logger,
295-
"Unexpected program length: {}",
296-
program.program().len()
297-
);
298-
}
299-
}
300-
_ => {
301-
log_error!(
302-
self.logger,
303-
"Tried to use a non-witness script. This must never happen."
304-
);
305-
panic!("Tried to use a non-witness script. This must never happen.");
306-
}
263+
let confirmed_txs: Vec<bdk::TransactionDetails> = locked_wallet
264+
.list_transactions(false)
265+
.map_err(|e| {
266+
log_error!(self.logger, "Failed to retrieve transactions from wallet: {}", e);
267+
})?
268+
.into_iter()
269+
.filter(|t| t.confirmation_time.is_some())
270+
.collect();
271+
let unspent_confirmed_utxos = locked_wallet
272+
.list_unspent()
273+
.map_err(|e| {
274+
log_error!(
275+
self.logger,
276+
"Failed to retrieve unspent transactions from wallet: {}",
277+
e
278+
);
279+
})?
280+
.into_iter()
281+
.filter(|u| confirmed_txs.iter().find(|t| t.txid == u.outpoint.txid).is_some());
282+
283+
for u in unspent_confirmed_utxos {
284+
let payload = Payload::from_script(&u.txout.script_pubkey).map_err(|e| {
285+
log_error!(self.logger, "Failed to retrieve script payload: {}", e);
286+
})?;
287+
288+
match payload {
289+
Payload::WitnessProgram(program) => {
290+
if program.version() == WitnessVersion::V0 && program.program().len() == 20 {
291+
let wpkh =
292+
WPubkeyHash::from_slice(program.program().as_bytes()).map_err(|e| {
293+
log_error!(self.logger, "Failed to retrieve script payload: {}", e);
294+
})?;
295+
let utxo = Utxo::new_v0_p2wpkh(u.outpoint, u.txout.value, &wpkh);
296+
utxos.push(utxo);
297+
} else {
298+
log_error!(
299+
self.logger,
300+
"Unexpected program length: {}",
301+
program.program().len()
302+
);
307303
}
308304
}
305+
_ => {
306+
log_error!(
307+
self.logger,
308+
"Tried to use a non-witness script. This must never happen."
309+
);
310+
panic!("Tried to use a non-witness script. This must never happen.");
311+
}
309312
}
310313
}
311314

0 commit comments

Comments
 (0)