diff --git a/lib/core/src/model.rs b/lib/core/src/model.rs index b1a9cd1..66d661c 100644 --- a/lib/core/src/model.rs +++ b/lib/core/src/model.rs @@ -224,6 +224,7 @@ impl Swap { pub(crate) struct SendSwap { pub(crate) id: String, pub(crate) invoice: String, + pub(crate) preimage: Option, pub(crate) payer_amount_sat: u64, pub(crate) receiver_amount_sat: u64, /// JSON representation of [crate::persist::send::InternalCreateSubmarineResponse] diff --git a/lib/core/src/persist/send.rs b/lib/core/src/persist/send.rs index 35015e5..8a48153 100644 --- a/lib/core/src/persist/send.rs +++ b/lib/core/src/persist/send.rs @@ -81,6 +81,7 @@ impl Persister { SELECT id, invoice, + preimage, payer_amount_sat, receiver_amount_sat, create_response_json, @@ -116,14 +117,15 @@ impl Persister { Ok(SendSwap { id: row.get(0)?, invoice: row.get(1)?, - payer_amount_sat: row.get(2)?, - receiver_amount_sat: row.get(3)?, - create_response_json: row.get(4)?, - refund_private_key: row.get(5)?, - lockup_tx_id: row.get(6)?, - refund_tx_id: row.get(7)?, - created_at: row.get(8)?, - state: row.get(9)?, + preimage: row.get(2)?, + payer_amount_sat: row.get(3)?, + receiver_amount_sat: row.get(4)?, + create_response_json: row.get(5)?, + refund_private_key: row.get(6)?, + lockup_tx_id: row.get(7)?, + refund_tx_id: row.get(8)?, + created_at: row.get(9)?, + state: row.get(10)?, }) } diff --git a/lib/core/src/sdk.rs b/lib/core/src/sdk.rs index cbd8911..3bfc519 100644 --- a/lib/core/src/sdk.rs +++ b/lib/core/src/sdk.rs @@ -612,14 +612,27 @@ impl LiquidSdk { Ok(()) } + // Boltz announced they successfully broadcast the (cooperative or non-cooperative) claim tx Ok(SubSwapStates::TransactionClaimed) => { debug!("Send Swap {id} has been claimed"); - let preimage = - self.get_preimage_from_script_path_claim_spend(&ongoing_send_swap)?; - self.validate_send_swap_preimage(id, &ongoing_send_swap.invoice, &preimage) - .await?; - self.try_handle_send_swap_update(id, Complete, Some(&preimage), None, None) - .await?; + + match ongoing_send_swap.preimage { + Some(_) => { + debug!("The claim tx was a key path spend (cooperative claim)"); + // Preimage was already validated and stored, PaymentSucceeded event emitted, + // when the cooperative claim was handled. + } + None => { + debug!("The claim tx was a script path spend (non-cooperative claim)"); + let preimage = + self.get_preimage_from_script_path_claim_spend(&ongoing_send_swap)?; + self.validate_send_swap_preimage(id, &ongoing_send_swap.invoice, &preimage) + .await?; + self.try_handle_send_swap_update(id, Complete, Some(&preimage), None, None) + .await?; + } + } + Ok(()) } @@ -1000,6 +1013,7 @@ impl LiquidSdk { let swap = SendSwap { id: swap_id.clone(), invoice: req.invoice.clone(), + preimage: None, payer_amount_sat, receiver_amount_sat, create_response_json,