From f2a559689950d9d122308aac8e69e04b4c213dcd Mon Sep 17 00:00:00 2001 From: Andrew Poelstra Date: Sun, 11 Dec 2022 18:30:31 +0000 Subject: [PATCH] examples: clean up taproot PSBT example locktime handling This still has the line let lock_time = absolute::LockTime::from_height(psbt.unsigned_tx.lock_time.to_consensus_u32() + lock_time_delta).unwrap(); I'm unsure whether this "adding height to a locktime" concept is a meaningful thing or just the sort of thing that shows up in example code. Maybe we should have first-class support for it. Note that the line, as written, depends on the fact that the original locktime was a small blockheight. A proper function for this would handle the exceptional case gracefully. --- bitcoin/examples/taproot-psbt.rs | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/bitcoin/examples/taproot-psbt.rs b/bitcoin/examples/taproot-psbt.rs index 1387d8ca..77d32e16 100644 --- a/bitcoin/examples/taproot-psbt.rs +++ b/bitcoin/examples/taproot-psbt.rs @@ -144,7 +144,7 @@ fn main() -> Result<(), Box> { ExtendedPrivKey::from_str(BENEFACTOR_XPRIV_STR)?, beneficiary.master_xpub(), )?; - let (tx, psbt) = benefactor.create_inheritance_funding_tx(1000, UTXO_2)?; + let (tx, psbt) = benefactor.create_inheritance_funding_tx(absolute::LockTime::from_height(1000).unwrap(), UTXO_2)?; let tx_hex = encode::serialize_hex(&tx); println!("Inheritance funding tx hex:\n\n{}", tx_hex); @@ -154,7 +154,7 @@ fn main() -> Result<(), Box> { // And mine a block to confirm the transaction: // bt generatetoaddress 1 $(bt-benefactor getnewaddress '' 'bech32m') - let spending_tx = beneficiary.spend_inheritance(psbt, 1000, to_address)?; + let spending_tx = beneficiary.spend_inheritance(psbt, absolute::LockTime::from_height(1000).unwrap(), to_address)?; let spending_tx_hex = encode::serialize_hex(&spending_tx); println!("\nInheritance spending tx hex:\n\n{}", spending_tx_hex); // If you try to broadcast now, the transaction will be rejected as it is timelocked. @@ -176,7 +176,7 @@ fn main() -> Result<(), Box> { ExtendedPrivKey::from_str(BENEFACTOR_XPRIV_STR)?, beneficiary.master_xpub(), )?; - let (tx, _) = benefactor.create_inheritance_funding_tx(2000, UTXO_3)?; + let (tx, _) = benefactor.create_inheritance_funding_tx(absolute::LockTime::from_height(2000).unwrap(), UTXO_3)?; let tx_hex = encode::serialize_hex(&tx); println!("Inheritance funding tx hex:\n\n{}", tx_hex); @@ -357,9 +357,9 @@ impl BenefactorWallet { }) } - fn time_lock_script(locktime: u32, beneficiary_key: XOnlyPublicKey) -> Script { + fn time_lock_script(locktime: absolute::LockTime, beneficiary_key: XOnlyPublicKey) -> Script { script::Builder::new() - .push_int(locktime as i64) + .push_int(locktime.to_consensus_u32() as i64) .push_opcode(OP_CLTV) .push_opcode(OP_DROP) .push_x_only_key(&beneficiary_key) @@ -369,7 +369,7 @@ impl BenefactorWallet { fn create_inheritance_funding_tx( &mut self, - lock_time: u32, + lock_time: absolute::LockTime, input_utxo: P2trUtxo, ) -> Result<(Transaction, Psbt), Box> { if let ChildNumber::Normal { index } = self.next { @@ -412,7 +412,7 @@ impl BenefactorWallet { // CREATOR + UPDATER let next_tx = Transaction { version: 2, - lock_time: absolute::LockTime::from_consensus(lock_time), + lock_time, input: vec![TxIn { previous_output: OutPoint { txid: tx.txid(), vout: 0 }, script_sig: Script::new(), @@ -482,7 +482,7 @@ impl BenefactorWallet { self.beneficiary_xpub.derive_pub(&self.secp, &new_derivation_path)?.to_x_only_pub(); // Build up the leaf script and combine with internal key into a taproot commitment - let lock_time = psbt.unsigned_tx.lock_time.to_consensus_u32() + lock_time_delta; + let lock_time = absolute::LockTime::from_height(psbt.unsigned_tx.lock_time.to_consensus_u32() + lock_time_delta).unwrap(); let script = Self::time_lock_script(lock_time, beneficiary_key); let leaf_hash = TapLeafHash::from_script(&script, LeafVersion::TapScript); @@ -557,7 +557,7 @@ impl BenefactorWallet { let next_tx = Transaction { version: 2, - lock_time: absolute::LockTime::from_consensus(lock_time), + lock_time, input: vec![TxIn { previous_output: OutPoint { txid: tx.txid(), vout: 0 }, script_sig: Script::new(), @@ -626,13 +626,13 @@ impl BeneficiaryWallet { fn spend_inheritance( &self, mut psbt: Psbt, - lock_time: u32, + lock_time: absolute::LockTime, to_address: Address, ) -> Result> { let input_value = psbt.inputs[0].witness_utxo.as_ref().unwrap().value; let input_script_pubkey = psbt.inputs[0].witness_utxo.as_ref().unwrap().script_pubkey.clone(); - psbt.unsigned_tx.lock_time = absolute::LockTime::from_consensus(lock_time); + psbt.unsigned_tx.lock_time = lock_time; psbt.unsigned_tx.output = vec![TxOut { script_pubkey: to_address.script_pubkey(), value: input_value - ABSOLUTE_FEES_IN_SATS,