|
1 | 1 | //! A implementation of `TrustedWalletInterface` using the Spark SDK. |
2 | 2 | use crate::bitcoin::consensus::encode::deserialize_hex; |
| 3 | +use crate::bitcoin::hex::FromHex; |
3 | 4 | use crate::bitcoin::{Txid, io}; |
4 | 5 | use crate::logging::Logger; |
5 | 6 | use crate::store::{PaymentId, StoreTransaction, TxStatus}; |
@@ -131,28 +132,32 @@ impl TrustedWalletInterface for Spark { |
131 | 132 | log_info!(l, "Payments synced to storage"); |
132 | 133 | } |
133 | 134 | }, |
134 | | - WalletEvent::TransferClaimed(transfer_id) => { |
135 | | - if let Ok(transfers) = w.list_transfers(None).await { |
136 | | - if let Err(e) = Self::sync_payments_to_storage(w.as_ref(), &s, l.as_ref()).await { |
137 | | - log_error!(l, "Failed to sync payments to storage: {e:?}"); |
138 | | - } else { |
139 | | - log_info!(l, "Payments synced to storage"); |
140 | | - } |
| 135 | + WalletEvent::TransferClaimed(transfer) => { |
| 136 | + if let Err(e) = Self::sync_payments_to_storage(w.as_ref(), &s, l.as_ref()).await { |
| 137 | + log_error!(l, "Failed to sync payments to storage: {e:?}"); |
| 138 | + } else { |
| 139 | + log_info!(l, "Payments synced to storage"); |
| 140 | + } |
141 | 141 |
|
142 | | - if let Some(transfer) = |
143 | | - transfers.into_iter().find(|t| t.id == transfer_id) |
144 | | - { |
145 | | - event_queue |
146 | | - .add_event(Event::PaymentReceived { |
147 | | - payment_id: PaymentId::Trusted( |
148 | | - convert_from_transfer_id(transfer.id.to_bytes()), |
149 | | - ), |
150 | | - payment_hash: PaymentHash([0; 32]), // fixme, spark does not give us the payment hash |
151 | | - amount_msat: transfer.total_value_sat * 1000, |
152 | | - custom_records: vec![], |
153 | | - lsp_fee_msats: None, |
154 | | - }) |
155 | | - .unwrap(); |
| 142 | + match transfer.user_request { |
| 143 | + None => { |
| 144 | + log_debug!(l, "Transfer claimed without user request: {transfer:?}"); |
| 145 | + }, |
| 146 | + Some(SspUserRequest::LightningReceiveRequest(req)) => { |
| 147 | + if let Ok(hash) = FromHex::from_hex(&req.invoice.payment_hash) { |
| 148 | + event_queue |
| 149 | + .add_event(Event::PaymentReceived { |
| 150 | + payment_id: PaymentId::Trusted(convert_from_transfer_id(transfer.id.to_bytes())), |
| 151 | + payment_hash: PaymentHash(hash), |
| 152 | + amount_msat: transfer.total_value_sat * 1_000, // convert to msats |
| 153 | + custom_records: vec![], |
| 154 | + lsp_fee_msats: None, |
| 155 | + }) |
| 156 | + .unwrap(); |
| 157 | + } |
| 158 | + }, |
| 159 | + Some(req) => { |
| 160 | + log_debug!(l, "Transfer claimed with user request: {req:?}"); |
156 | 161 | } |
157 | 162 | } |
158 | 163 | }, |
@@ -296,10 +301,19 @@ impl TrustedWalletInterface for Spark { |
296 | 301 |
|
297 | 302 | match res { |
298 | 303 | PayLightningInvoiceResult::LightningPayment(pay) => { |
299 | | - let uuid = Uuid::from_str(pay.id.as_str()).map_err(|_| { |
300 | | - Error::Generic(format!("Failed to parse payment id: {}", pay.id)) |
301 | | - })?; |
302 | | - Ok(convert_from_transfer_id(uuid.into_bytes())) |
| 304 | + // Spark uses UUIDs for payment IDs, so we need to convert them |
| 305 | + // to our format. Spark uses a UUID in the format `SparkLightningSendRequest:<uuid>` |
| 306 | + // We only need the UUID part, so we split by ':' and take the last part. |
| 307 | + // If the format is invalid, we return an error. |
| 308 | + if let Some(id) = pay.id.split(':').next_back() { |
| 309 | + let uuid = Uuid::from_str(id).map_err(|_| { |
| 310 | + Error::Generic(format!("Failed to parse payment id: {id}")) |
| 311 | + })?; |
| 312 | + Ok(convert_from_transfer_id(uuid.into_bytes())) |
| 313 | + } else { |
| 314 | + log_error!(self.logger, "Invalid payment id format: {}", pay.id); |
| 315 | + Err(Error::Generic(format!("Invalid payment id format: {}", pay.id))) |
| 316 | + } |
303 | 317 | }, |
304 | 318 | PayLightningInvoiceResult::Transfer(transfer) => { |
305 | 319 | Ok(convert_from_transfer_id(transfer.id.to_bytes())) |
|
0 commit comments