@@ -10,7 +10,7 @@ use bdk_wallet::bitcoin::taproot::{Signature, TAPROOT_ANNEX_PREFIX, TaprootBuild
1010use bdk_wallet:: bitcoin:: transaction:: Version ;
1111use bdk_wallet:: bitcoin:: {
1212 Address , Amount , FeeRate , Network , OutPoint , Psbt , ScriptBuf , Sequence , TapNodeHash ,
13- TapSighash , TapSighashType , Transaction , TxIn , TxOut , Weight , Witness , XOnlyPublicKey ,
13+ TapSighash , TapSighashType , Transaction , TxIn , TxOut , Txid , Weight , Witness , XOnlyPublicKey ,
1414 absolute, relative, script, secp256k1,
1515} ;
1616use paste:: paste;
@@ -93,9 +93,12 @@ impl NetworkParams for Network {
9393
9494macro_rules! make_getter {
9595 ( $field_name: ident: $field_type: ident) => {
96+ make_getter!( $field_name: $field_type: $field_type) ;
97+ } ;
98+ ( $field_name: ident: $field_type: ident: $base_field_type: ident) => {
9699 paste! {
97100 pub fn $field_name( & self ) -> Result <& $field_type> {
98- self . $field_name. as_ref( ) . ok_or( TransactionErrorKind :: [ <Missing $field_type >] )
101+ self . $field_name. as_ref( ) . ok_or( TransactionErrorKind :: [ <Missing $base_field_type >] )
99102 }
100103 }
101104 } ;
@@ -253,9 +256,9 @@ impl DepositTxBuilder {
253256 make_getter_setter ! ( fee_rate: FeeRate ) ;
254257 make_getter_setter ! ( buyers_half_psbt: Psbt ) ;
255258 make_getter_setter ! ( sellers_half_psbt: Psbt ) ;
256- make_getter ! ( psbt: Psbt ) ;
257- make_getter ! ( buyer_payout: TxOutput ) ;
258- make_getter ! ( seller_payout: TxOutput ) ;
259+ make_getter ! ( psbt: Psbt : Transaction ) ;
260+ make_getter ! ( buyer_payout: TxOutput : Transaction ) ;
261+ make_getter ! ( seller_payout: TxOutput : Transaction ) ;
259262
260263 fn sellers_trade_deposit ( & self ) -> Result < Amount > {
261264 self . trade_amount ( ) ?. checked_add ( * self . sellers_security_deposit ( ) ?)
@@ -316,6 +319,8 @@ impl DepositTxBuilder {
316319 Ok ( self )
317320 }
318321
322+ pub fn txid ( & self ) -> Result < & Txid > { Ok ( & self . buyer_payout ( ) ?. outpoint . txid ) }
323+
319324 pub fn sign_buyer_inputs ( & mut self , trade_wallet : & ( impl TradeWallet + ?Sized ) ) -> Result < & mut Self > {
320325 self . sign_matching_inputs ( trade_wallet, & prevout_set ( self . buyers_half_psbt ( ) ?) )
321326 }
@@ -329,14 +334,14 @@ impl DepositTxBuilder {
329334 trade_wallet : & ( impl TradeWallet + ?Sized ) ,
330335 prevouts : & BTreeSet < OutPoint > ,
331336 ) -> Result < & mut Self > {
332- let psbt = self . psbt . as_mut ( ) . ok_or ( TransactionErrorKind :: MissingPsbt ) ?;
337+ let psbt = self . psbt . as_mut ( ) . ok_or ( TransactionErrorKind :: MissingTransaction ) ?;
333338 trade_wallet. sign_selected_inputs ( psbt, & |o| prevouts. contains ( o) ) ?;
334339 Ok ( self )
335340 }
336341
337342 pub fn combine_psbts ( & mut self , other : Psbt ) -> Result < & mut Self > {
338343 // TODO: We may need to do some validation of the provided PSBT.
339- self . psbt . as_mut ( ) . ok_or ( TransactionErrorKind :: MissingPsbt ) ?. combine ( other) ?;
344+ self . psbt . as_mut ( ) . ok_or ( TransactionErrorKind :: MissingTransaction ) ?. combine ( other) ?;
340345 Ok ( self )
341346 }
342347
@@ -357,6 +362,7 @@ pub struct WarningTxBuilder {
357362 // Derived fields:
358363 unsigned_tx : Option < Transaction > ,
359364 signed_tx : Option < Transaction > ,
365+ txid : Option < Txid > ,
360366}
361367
362368impl WarningTxBuilder {
@@ -370,19 +376,20 @@ impl WarningTxBuilder {
370376 make_getter_setter ! ( seller_input_signature: Signature ) ;
371377 make_getter ! ( unsigned_tx: Transaction ) ;
372378 make_getter ! ( signed_tx: Transaction ) ;
379+ make_getter ! ( txid: Txid : Transaction ) ;
373380
374- pub fn escrow_amount ( input_amounts : impl IntoIterator < Item =Amount > , fee_rate : FeeRate ) -> Option < Amount > {
375- input_amounts. into_iter ( ) . checked_sum ( ) ?
381+ pub fn escrow_amount ( input_amounts : impl IntoIterator < Item =Amount > , fee_rate : FeeRate ) -> Result < Amount > {
382+ ( || input_amounts. into_iter ( ) . checked_sum ( ) ?
376383 . checked_sub ( ANCHOR_AMOUNT ) ?
377384 . checked_sub ( fee_rate. checked_mul_by_weight ( SIGNED_WARNING_TX_WEIGHT ) ?)
385+ ) ( ) . ok_or ( TransactionErrorKind :: Overflow )
378386 }
379387
380388 pub fn compute_unsigned_tx ( & mut self ) -> Result < & mut Self > {
381389 let escrow_output = TxOut {
382390 value : Self :: escrow_amount (
383391 [ self . buyer_input ( ) ?. prevout . value , self . seller_input ( ) ?. prevout . value ] ,
384- * self . fee_rate ( ) ?,
385- ) . ok_or ( TransactionErrorKind :: Overflow ) ?,
392+ * self . fee_rate ( ) ?) ?,
386393 script_pubkey : self . escrow_address ( ) ?. script_pubkey ( ) ,
387394 } ;
388395 let anchor_output = TxOut {
@@ -395,14 +402,13 @@ impl WarningTxBuilder {
395402 input : self . tx_ins ( * self . lock_time ( ) ?) ?. to_vec ( ) ,
396403 output : vec ! [ escrow_output, anchor_output] ,
397404 } ;
398- self . unsigned_tx . get_or_insert ( tx) ;
405+ self . txid = Some ( self . unsigned_tx . get_or_insert ( tx) . compute_txid ( ) ) ;
399406 Ok ( self )
400407 }
401408
402409 pub fn escrow ( & self ) -> Result < TxOutput > {
403- let txid = self . unsigned_tx ( ) ?. compute_txid ( ) ;
404410 let output = self . unsigned_tx ( ) ?. output [ 0 ] . clone ( ) ;
405- Ok ( TxOutput :: new ( OutPoint :: new ( txid, 0 ) , output) )
411+ Ok ( TxOutput :: new ( OutPoint :: new ( * self . txid ( ) ? , 0 ) , output) )
406412 }
407413
408414 pub fn buyer_input_sighash ( & self ) -> Result < TapSighash > {
@@ -437,6 +443,7 @@ pub struct RedirectTxBuilder {
437443 // Derived fields:
438444 unsigned_tx : Option < Transaction > ,
439445 signed_tx : Option < Transaction > ,
446+ txid : Option < Txid > ,
440447}
441448
442449impl RedirectTxBuilder {
@@ -447,14 +454,17 @@ impl RedirectTxBuilder {
447454 make_getter_setter ! ( input_signature: Signature ) ;
448455 make_getter ! ( unsigned_tx: Transaction ) ;
449456 make_getter ! ( signed_tx: Transaction ) ;
457+ make_getter ! ( txid: Txid : Transaction ) ;
450458
451- pub fn available_amount_msat ( escrow_amount : Amount , fee_rate : FeeRate ) -> Option < u64 > {
452- let redirection_tx_base_fee = fee_rate. to_sat_per_kwu ( )
453- . checked_mul ( SIGNED_REDIRECT_TX_BASE_WEIGHT . to_wu ( ) ) ?;
454- escrow_amount
455- . checked_sub ( ANCHOR_AMOUNT ) ?
456- . to_sat ( ) . checked_mul ( 1000 ) ?
457- . checked_sub ( redirection_tx_base_fee)
459+ pub fn available_amount_msat ( escrow_amount : Amount , fee_rate : FeeRate ) -> Result < u64 > {
460+ ( || {
461+ let redirection_tx_base_fee = fee_rate. to_sat_per_kwu ( )
462+ . checked_mul ( SIGNED_REDIRECT_TX_BASE_WEIGHT . to_wu ( ) ) ?;
463+ escrow_amount
464+ . checked_sub ( ANCHOR_AMOUNT ) ?
465+ . to_sat ( ) . checked_mul ( 1000 ) ?
466+ . checked_sub ( redirection_tx_base_fee)
467+ } ) ( ) . ok_or ( TransactionErrorKind :: Overflow )
458468 }
459469
460470 pub fn compute_unsigned_tx ( & mut self ) -> Result < & mut Self > {
@@ -470,7 +480,7 @@ impl RedirectTxBuilder {
470480 input : self . tx_ins ( * self . lock_time ( ) ?) ?. to_vec ( ) ,
471481 output,
472482 } ;
473- self . unsigned_tx . get_or_insert ( tx) ;
483+ self . txid = Some ( self . unsigned_tx . get_or_insert ( tx) . compute_txid ( ) ) ;
474484 Ok ( self )
475485 }
476486
0 commit comments