|
| 1 | +use bitcoin::blockdata::transaction::{OutPoint, Transaction}; |
| 2 | +use bitcoin::secp256k1::Secp256k1; |
| 3 | +use bitcoin::{Address, XOnlyPublicKey}; |
| 4 | + |
| 5 | +use crate::blockchain::Network; |
| 6 | +use crate::transaction::{Error, Fundable, Linkable}; |
| 7 | + |
| 8 | +use crate::bitcoin::taproot::Taproot; |
| 9 | +use crate::bitcoin::transaction::MetadataOutput; |
| 10 | +use crate::bitcoin::Bitcoin; |
| 11 | +use bitcoin::util::taproot::TaprootSpendInfo; |
| 12 | + |
| 13 | +#[derive(Debug, Clone)] |
| 14 | +pub struct Funding { |
| 15 | + untweaked_public_key: Option<XOnlyPublicKey>, |
| 16 | + network: Option<bitcoin::Network>, |
| 17 | + seen_tx: Option<Transaction>, |
| 18 | +} |
| 19 | + |
| 20 | +impl Linkable<MetadataOutput> for Funding { |
| 21 | + fn get_consumable_output(&self) -> Result<MetadataOutput, Error> { |
| 22 | + let secp = Secp256k1::new(); |
| 23 | + |
| 24 | + let address = Address::p2tr( |
| 25 | + &secp, |
| 26 | + self.untweaked_public_key.ok_or(Error::MissingPublicKey)?, |
| 27 | + None, |
| 28 | + self.network.ok_or(Error::MissingNetwork)?, |
| 29 | + ); |
| 30 | + let script_pubkey = address.script_pubkey(); |
| 31 | + |
| 32 | + match &self.seen_tx { |
| 33 | + Some(t) => t |
| 34 | + .output |
| 35 | + .iter() |
| 36 | + .enumerate() |
| 37 | + .find(|(_, tx_out)| tx_out.script_pubkey == script_pubkey) |
| 38 | + .map(|(ix, tx_out)| MetadataOutput { |
| 39 | + out_point: OutPoint::new(t.txid(), ix as u32), |
| 40 | + tx_out: tx_out.clone(), |
| 41 | + script_pubkey: Some(script_pubkey), |
| 42 | + }) |
| 43 | + .ok_or(Error::MissingUTXO), |
| 44 | + // The transaction has not been see yet, cannot infer the UTXO |
| 45 | + None => Err(Error::MissingOnchainTransaction), |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +impl Fundable<Bitcoin<Taproot>, MetadataOutput> for Funding { |
| 51 | + fn initialize(pubkey: XOnlyPublicKey, network: Network) -> Result<Self, Error> { |
| 52 | + let network = match network { |
| 53 | + Network::Mainnet => bitcoin::Network::Bitcoin, |
| 54 | + Network::Testnet => bitcoin::Network::Testnet, |
| 55 | + Network::Local => bitcoin::Network::Regtest, |
| 56 | + }; |
| 57 | + Ok(Funding { |
| 58 | + untweaked_public_key: Some(pubkey), |
| 59 | + network: Some(network), |
| 60 | + seen_tx: None, |
| 61 | + }) |
| 62 | + } |
| 63 | + |
| 64 | + fn get_address(&self) -> Result<Address, Error> { |
| 65 | + let secp = Secp256k1::new(); |
| 66 | + let taproot_info = TaprootSpendInfo::new_key_spend( |
| 67 | + &secp, |
| 68 | + self.untweaked_public_key.ok_or(Error::MissingPublicKey)?, |
| 69 | + None, |
| 70 | + ); |
| 71 | + |
| 72 | + Ok(Address::p2tr( |
| 73 | + &secp, |
| 74 | + taproot_info.internal_key(), |
| 75 | + taproot_info.merkle_root(), |
| 76 | + self.network.ok_or(Error::MissingNetwork)?, |
| 77 | + )) |
| 78 | + } |
| 79 | + |
| 80 | + fn update(&mut self, tx: Transaction) -> Result<(), Error> { |
| 81 | + self.seen_tx = Some(tx); |
| 82 | + Ok(()) |
| 83 | + } |
| 84 | + |
| 85 | + fn raw(tx: Transaction) -> Result<Self, Error> { |
| 86 | + Ok(Self { |
| 87 | + untweaked_public_key: None, |
| 88 | + network: None, |
| 89 | + seen_tx: Some(tx), |
| 90 | + }) |
| 91 | + } |
| 92 | + |
| 93 | + fn was_seen(&self) -> bool { |
| 94 | + self.seen_tx.is_some() |
| 95 | + } |
| 96 | +} |
0 commit comments