diff --git a/src/data.rs b/src/data.rs index 0a34827..1745646 100644 --- a/src/data.rs +++ b/src/data.rs @@ -245,6 +245,7 @@ pub enum Party { impl Party { pub fn is_ourself(&self) -> bool { matches!(self, Party::Wallet(_)) } pub fn is_external(&self) -> bool { !self.is_ourself() } + pub fn is_unknown(&self) -> bool { matches!(self, Party::Unknown(_)) } pub fn derived_addr(&self) -> Option { match self { Party::Wallet(addr) => Some(*addr), @@ -257,6 +258,14 @@ impl Party { terminal: wallet_addr.terminal, }) } + pub fn script_pubkey(&self) -> Option { + match self { + Party::Subsidy => None, + Party::Counterparty(addr) => Some(addr.script_pubkey()), + Party::Unknown(script) => Some(script.clone()), + Party::Wallet(_) => None, + } + } } impl Display for Party { diff --git a/src/indexers/esplora.rs b/src/indexers/esplora.rs index 37ce192..a67db50 100644 --- a/src/indexers/esplora.rs +++ b/src/indexers/esplora.rs @@ -143,10 +143,10 @@ impl Indexer for BlockingClient { for txid in txids { let mut tx = cache.tx.remove(txid).expect("broken logic"); for debit in &mut tx.outputs { - let Party::Unknown(ref s) = debit.beneficiary else { + let Some(s) = debit.beneficiary.script_pubkey() else { continue; }; - if s == script { + if &s == script { cache.utxo.insert(debit.outpoint); debit.beneficiary = Party::from_wallet_addr(wallet_addr); wallet_addr.used = wallet_addr.used.saturating_add(1); @@ -154,8 +154,8 @@ impl Indexer for BlockingClient { wallet_addr.balance = wallet_addr .balance .saturating_add(debit.value.sats().try_into().expect("sats overflow")); - } else { - Address::with(s, descriptor.chain()) + } else if debit.beneficiary.is_unknown() { + Address::with(&s, descriptor.chain()) .map(|addr| { debit.beneficiary = Party::Counterparty(addr); }) @@ -170,16 +170,16 @@ impl Indexer for BlockingClient { for txid in txids { let mut tx = cache.tx.remove(txid).expect("broken logic"); for credit in &mut tx.inputs { - let Party::Unknown(ref s) = credit.payer else { + let Some(s) = credit.payer.script_pubkey() else { continue; }; - if s == script { + if &s == script { credit.payer = Party::from_wallet_addr(wallet_addr); wallet_addr.balance = wallet_addr .balance .saturating_sub(credit.value.sats().try_into().expect("sats overflow")); - } else { - Address::with(s, descriptor.chain()) + } else if credit.payer.is_unknown() { + Address::with(&s, descriptor.chain()) .map(|addr| { credit.payer = Party::Counterparty(addr); })