Skip to content

Commit

Permalink
f Clean up list_confirmed_utxos a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Feb 5, 2024
1 parent eb3e0fe commit 1fecf85
Showing 1 changed file with 47 additions and 44 deletions.
91 changes: 47 additions & 44 deletions src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -260,52 +260,55 @@ where
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
let locked_wallet = self.inner.lock().unwrap();
let mut utxos = Vec::new();
let txs = locked_wallet.list_transactions(true).map_err(|e| {
log_error!(self.logger, "Failed to retrieve transactions from wallet: {}", e);
})?;
let unspent = locked_wallet.list_unspent().map_err(|e| {
log_error!(self.logger, "Failed to retrieve unspent transactions from wallet: {}", e);
})?;

for u in unspent {
for t in &txs {
if u.outpoint.txid == t.txid && t.confirmation_time.is_some() {
let payload = Payload::from_script(&u.txout.script_pubkey).map_err(|e| {
log_error!(self.logger, "Failed to retrieve script payload: {}", e);
})?;

match payload {
Payload::WitnessProgram(program) => {
if program.version() == WitnessVersion::V0
&& program.program().len() == 20
{
let wpkh = WPubkeyHash::from_slice(program.program().as_bytes())
.map_err(|e| {
log_error!(
self.logger,
"Failed to retrieve script payload: {}",
e
);
})?;
let utxo = Utxo::new_v0_p2wpkh(u.outpoint, u.txout.value, &wpkh);
utxos.push(utxo);
} else {
log_error!(
self.logger,
"Unexpected program length: {}",
program.program().len()
);
}
}
_ => {
log_error!(
self.logger,
"Tried to use a non-witness script. This must never happen."
);
panic!("Tried to use a non-witness script. This must never happen.");
}
let confirmed_txs: Vec<bdk::TransactionDetails> = locked_wallet
.list_transactions(false)
.map_err(|e| {
log_error!(self.logger, "Failed to retrieve transactions from wallet: {}", e);
})?
.into_iter()
.filter(|t| t.confirmation_time.is_some())
.collect();
let unspent_confirmed_utxos = locked_wallet
.list_unspent()
.map_err(|e| {
log_error!(
self.logger,
"Failed to retrieve unspent transactions from wallet: {}",
e
);
})?
.into_iter()
.filter(|u| confirmed_txs.iter().find(|t| t.txid == u.outpoint.txid).is_some());

for u in unspent_confirmed_utxos {
let payload = Payload::from_script(&u.txout.script_pubkey).map_err(|e| {
log_error!(self.logger, "Failed to retrieve script payload: {}", e);
})?;

match payload {
Payload::WitnessProgram(program) => {
if program.version() == WitnessVersion::V0 && program.program().len() == 20 {
let wpkh =
WPubkeyHash::from_slice(program.program().as_bytes()).map_err(|e| {
log_error!(self.logger, "Failed to retrieve script payload: {}", e);
})?;
let utxo = Utxo::new_v0_p2wpkh(u.outpoint, u.txout.value, &wpkh);
utxos.push(utxo);
} else {
log_error!(
self.logger,
"Unexpected program length: {}",
program.program().len()
);
}
}
_ => {
log_error!(
self.logger,
"Tried to use a non-witness script. This must never happen."
);
panic!("Tried to use a non-witness script. This must never happen.");
}
}
}

Expand Down

0 comments on commit 1fecf85

Please sign in to comment.