@@ -134,80 +134,72 @@ impl PayjoinHandler {
134
134
) -> Result < Transaction , Error > {
135
135
let wallet = self . wallet . clone ( ) ;
136
136
wallet. sign_payjoin_proposal ( payjoin_proposal, original_psbt) ?;
137
+
137
138
let proposal_tx = payjoin_proposal. clone ( ) . extract_tx ( ) ;
138
- match self
139
- . transactions
140
- . read ( )
141
- . unwrap ( )
142
- . iter ( )
143
- . position ( |t| t. original_psbt ( ) == original_psbt)
144
- {
145
- Some ( pos) => {
146
- let pj_tx = self . transactions . write ( ) . unwrap ( ) . remove ( pos) ;
147
- pj_tx. waiting_first_confirmation ( proposal_tx. clone ( ) ) ?;
148
- let txid = proposal_tx. txid ( ) ;
149
- // watch proposal transaction
150
- self . chain_source . register_tx ( & txid, Script :: empty ( ) ) ;
151
- Ok ( proposal_tx)
152
- } ,
153
- None => {
154
- log_error ! (
155
- self . logger,
156
- "Failed to process Payjoin response: transaction not found"
157
- ) ;
158
- Err ( Error :: PayjoinResponseProcessingFailed )
159
- } ,
139
+ let mut transactions = self . transactions . write ( ) . unwrap ( ) ;
140
+ let pos = transactions. iter ( ) . position ( |t| t. original_psbt ( ) == original_psbt) ;
141
+ if let Some ( pos) = pos {
142
+ let pj_tx = transactions. remove ( pos) ;
143
+ pj_tx. waiting_first_confirmation ( proposal_tx. clone ( ) ) ?;
144
+ let txid = proposal_tx. txid ( ) ;
145
+ // watch proposal transaction
146
+ self . chain_source . register_tx ( & txid, Script :: empty ( ) ) ;
147
+ self . event_queue . add_event ( Event :: PayjoinPaymentBroadcasted {
148
+ txid,
149
+ amount_sats : pj_tx. amount ( ) . to_sat ( ) ,
150
+ recipient : pj_tx. receiver ( ) . clone ( ) . into ( ) ,
151
+ } ) ?;
152
+ Ok ( proposal_tx)
153
+ } else {
154
+ log_error ! ( self . logger, "Failed to process Payjoin response: transaction not found" ) ;
155
+ Err ( Error :: PayjoinResponseProcessingFailed )
160
156
}
161
157
}
162
158
163
159
pub ( crate ) fn handle_request_failure (
164
160
& self , original_psbt : & Psbt , reason : PayjoinPaymentFailureReason ,
165
161
) -> Result < ( ) , Error > {
166
- match self
167
- . transactions
168
- . read ( )
169
- . unwrap ( )
170
- . iter ( )
171
- . position ( |o| o. original_psbt ( ) == original_psbt)
172
- {
173
- Some ( pos) => {
174
- let mut transactions = self . transactions . write ( ) . unwrap ( ) ;
175
- let tx = transactions. remove ( pos) ;
176
- let payment_id: [ u8 ; 32 ] =
177
- tx. original_psbt ( ) . unsigned_tx . txid ( ) [ ..] . try_into ( ) . map_err ( |_| {
178
- log_error ! (
162
+ let mut transactions = self . transactions . write ( ) . unwrap ( ) ;
163
+ let pos = transactions. iter ( ) . position ( |t| t. original_psbt ( ) == original_psbt) ;
164
+ if let Some ( pos) = pos {
165
+ let tx = transactions. remove ( pos) ;
166
+ let payment_id: [ u8 ; 32 ] =
167
+ tx. original_psbt ( ) . unsigned_tx . txid ( ) [ ..] . try_into ( ) . map_err ( |_| {
168
+ log_error ! (
179
169
self . logger,
180
170
"Failed to handle request failure for Payjoin payment: invalid payment id"
181
171
) ;
182
- Error :: PayjoinRequestSendingFailed
183
- } ) ?;
184
- let mut update_details = PaymentDetailsUpdate :: new ( PaymentId ( payment_id) ) ;
185
- update_details. status = Some ( PaymentStatus :: Failed ) ;
186
- let _ = self . payment_store . update ( & update_details) ;
187
- self . event_queue . add_event ( Event :: PayjoinPaymentFailed {
188
- txid : original_psbt. unsigned_tx . txid ( ) ,
189
- recipient : tx. receiver ( ) . clone ( ) . into ( ) ,
190
- amount_sats : tx. amount ( ) . to_sat ( ) ,
191
- reason,
192
- } )
193
- } ,
194
- None => {
195
- log_error ! (
196
- self . logger,
197
- "Failed to handle request failure for Payjoin payment: transaction not found"
198
- ) ;
199
- Err ( Error :: PayjoinRequestSendingFailed )
200
- } ,
172
+ Error :: PayjoinRequestSendingFailed
173
+ } ) ?;
174
+ let mut update_details = PaymentDetailsUpdate :: new ( PaymentId ( payment_id) ) ;
175
+ update_details. status = Some ( PaymentStatus :: Failed ) ;
176
+ let _ = self . payment_store . update ( & update_details) ;
177
+ self . event_queue . add_event ( Event :: PayjoinPaymentFailed {
178
+ txid : original_psbt. unsigned_tx . txid ( ) ,
179
+ recipient : tx. receiver ( ) . clone ( ) . into ( ) ,
180
+ amount_sats : tx. amount ( ) . to_sat ( ) ,
181
+ reason,
182
+ } )
183
+ }
184
+ else {
185
+ log_error ! (
186
+ self . logger,
187
+ "Failed to handle request failure for Payjoin payment: transaction not found"
188
+ ) ;
189
+ Err ( Error :: PayjoinRequestSendingFailed )
201
190
}
202
191
}
203
192
204
193
fn internal_transactions_confirmed (
205
194
& self , header : & Header , txdata : & TransactionData , height : u32 ,
206
195
) {
196
+ dbg ! ( "internal transactions confirmed" ) ;
207
197
let ( _, tx) = txdata[ 0 ] ;
208
198
let confirmed_tx_txid = tx. txid ( ) ;
209
199
let mut transactions = self . transactions . write ( ) . unwrap ( ) ;
210
- if let Some ( pos) = transactions. iter ( ) . position ( |o| o. txid ( ) == confirmed_tx_txid) {
200
+ let pos = transactions. iter ( ) . position ( |t| t. txid ( ) == confirmed_tx_txid) ;
201
+ dbg ! ( & pos) ;
202
+ if let Some ( pos) = pos {
211
203
match transactions. remove ( pos) {
212
204
PayjoinTransaction :: PendingReceiverResponse { original_psbt, receiver, amount } => {
213
205
println ! ( "Payjoin receiver broadcasted original psbt transaction!" ) ;
@@ -300,6 +292,7 @@ impl PayjoinHandler {
300
292
}
301
293
}
302
294
295
+
303
296
impl Confirm for PayjoinHandler {
304
297
fn transactions_confirmed ( & self , header : & Header , txdata : & TransactionData , height : u32 ) {
305
298
self . internal_transactions_confirmed ( header, txdata, height) ;
0 commit comments