From daaac7a278e0033a9d5b11268bda0839b8b5a14e Mon Sep 17 00:00:00 2001 From: coreyphillips Date: Wed, 2 Dec 2026 21:02:17 -0500 Subject: [PATCH] feat: return each swap's payment hash from pubky_swaps_advance PubkySwapRouteStatus gains payment_hash_hex, derived from the seed, so a swap pubky_swaps_restore rebuilt also names its hold payment. Defaults to empty in the Swift and Kotlin initializers. --- CHANGELOG.md | 1 + bindings/ios/bitkitcore.swift | 20 ++++++++++++++-- src/modules/pubky_swaps/README.md | 4 +++- src/modules/pubky_swaps/client.rs | 39 +++++++++++++++++++++++-------- src/modules/pubky_swaps/types.rs | 4 ++++ 5 files changed, 55 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d38077b1..26b3ba62 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,7 @@ ## Unreleased +- Pubky Swaps: `PubkySwapRouteStatus` gains `payment_hash_hex`, the hold invoice's hash, also for a swap `pubky_swaps_restore` rebuilt, so the app can stop advancing a swap whose payment failed. It defaults to empty in the Swift and Kotlin initializers. - Pubky Swaps: a reverse quote asks for the claimable hint from a provider that sends it. `PubkySwapRoutePayment` gains `provider_takes_wake`, and `pubky_swaps_send_wake_capability` hands the provider a `swap.claimable` wake grant, so it wakes the device when the swap can be claimed. - `decode` sends input to Lightning only when it starts with `lightning:` or is a bare `lnbc`/`lntb` invoice. A `bitcoin:` URI with `lightning:` inside a parameter value such as `label` or `message` now decodes as that URI instead of failing. The `lightning:` scheme is matched in any case, so `LIGHTNING:` followed by an LNURL, a node id (`pubkey@host:port`) or a Lightning address now decodes instead of returning `InvalidFormat`. - Decode BIP 321 `bitcoin:` URIs without an address. `bitcoin:?lightning=` (served over NFC by Numo point-of-sale terminals, and planned by Square, which serves `lightning:` today) returns `Scanner::Lightning`, and a segwit address in the `bc`, `tb` or `bcrt` parameter returns `Scanner::OnChain` with that address, keeping other instructions such as `lightning` in `params`. An address in the wrong parameter (`bc=tb1…`) returns `InvalidAddress`. `lno`, `sp` or `pay` alone returns `UnsupportedType`, and no payment instruction returns `InvalidAddress` as before. Any `req-` parameter, `req-pop` included, now returns `InvalidFormat`, because Bitkit handles none of them and BIP 321 forbids paying such a URI (breaking for callers that paid `req-pop` URIs). diff --git a/bindings/ios/bitkitcore.swift b/bindings/ios/bitkitcore.swift index 5441cb7d..d9e567c6 100644 --- a/bindings/ios/bitkitcore.swift +++ b/bindings/ios/bitkitcore.swift @@ -12216,15 +12216,25 @@ public struct PubkySwapRouteStatus { public var providerPubky: String public var deliveredSat: UInt64 public var destinationAddress: String + /** + * The hash of the swap's hold invoice, so the app can find its payment, also for a swap + * `pubky_swaps_restore` rebuilt. Stop advancing once that payment failed. + */ + public var paymentHashHex: String // Default memberwise initializers are never public by default, so we // declare one manually. - public init(swapId: String, state: PubkySwapRouteState, providerPubky: String, deliveredSat: UInt64, destinationAddress: String) { + public init(swapId: String, state: PubkySwapRouteState, providerPubky: String, deliveredSat: UInt64, destinationAddress: String, + /** + * The hash of the swap's hold invoice, so the app can find its payment, also for a swap + * `pubky_swaps_restore` rebuilt. Stop advancing once that payment failed. + */paymentHashHex: String = "") { self.swapId = swapId self.state = state self.providerPubky = providerPubky self.deliveredSat = deliveredSat self.destinationAddress = destinationAddress + self.paymentHashHex = paymentHashHex } } @@ -12250,6 +12260,9 @@ extension PubkySwapRouteStatus: Equatable, Hashable { if lhs.destinationAddress != rhs.destinationAddress { return false } + if lhs.paymentHashHex != rhs.paymentHashHex { + return false + } return true } @@ -12259,6 +12272,7 @@ extension PubkySwapRouteStatus: Equatable, Hashable { hasher.combine(providerPubky) hasher.combine(deliveredSat) hasher.combine(destinationAddress) + hasher.combine(paymentHashHex) } } @@ -12277,7 +12291,8 @@ public struct FfiConverterTypePubkySwapRouteStatus: FfiConverterRustBuffer { state: FfiConverterTypePubkySwapRouteState.read(from: &buf), providerPubky: FfiConverterString.read(from: &buf), deliveredSat: FfiConverterUInt64.read(from: &buf), - destinationAddress: FfiConverterString.read(from: &buf) + destinationAddress: FfiConverterString.read(from: &buf), + paymentHashHex: FfiConverterString.read(from: &buf) ) } @@ -12287,6 +12302,7 @@ public struct FfiConverterTypePubkySwapRouteStatus: FfiConverterRustBuffer { FfiConverterString.write(value.providerPubky, into: &buf) FfiConverterUInt64.write(value.deliveredSat, into: &buf) FfiConverterString.write(value.destinationAddress, into: &buf) + FfiConverterString.write(value.paymentHashHex, into: &buf) } } diff --git a/src/modules/pubky_swaps/README.md b/src/modules/pubky_swaps/README.md index 00494dc7..5a5d9c36 100644 --- a/src/modules/pubky_swaps/README.md +++ b/src/modules/pubky_swaps/README.md @@ -99,7 +99,9 @@ drives each restored and known swap. than 6 blocks away, then reports `ClaimBroadcast` once a server took the claim, until it confirms, and `Claimed` after. Each call checks the HTLC's history: a claim that dropped out of the mempool is broadcast again, and a spend without the preimage in its witness (the provider's refund) or a -lockup that never came before the timeout ends in `Failed`. The claim's fee is +lockup that never came before the timeout ends in `Failed`. Each status carries the hold +invoice's `payment_hash_hex`, also for a restored swap, which keeps no invoice: once the node +shows that payment failed, the app stops advancing the swap. The claim's fee is fixed when the route is quoted and is never bumped, so swap routes are refused on mainnet. ## Wakes diff --git a/src/modules/pubky_swaps/client.rs b/src/modules/pubky_swaps/client.rs index 9f889083..fb5a2c35 100644 --- a/src/modules/pubky_swaps/client.rs +++ b/src/modules/pubky_swaps/client.rs @@ -712,13 +712,18 @@ fn snapshot_claimable(reply: SwapMessage, swap_id: Uuid) -> Result PubkySwapRouteStatus { +fn status_of( + record: &SwapRecord, + keys: &keys::SwapKeys, + state: PubkySwapRouteState, +) -> PubkySwapRouteStatus { PubkySwapRouteStatus { swap_id: record.swap_id.clone(), state, provider_pubky: record.provider_pubky.clone(), delivered_sat: record.delivered_sat, destination_address: record.destination_address.clone(), + payment_hash_hex: hex::encode(keys.payment_hash), } } @@ -730,11 +735,7 @@ fn advance_blocking( mnemonic: &str, passphrase: Option<&str>, ) -> Result { - let index: [u8; 32] = hex::decode(&record.key_index) - .ok() - .and_then(|index| index.try_into().ok()) - .ok_or_else(|| PubkySwapError::storage("invalid key index"))?; - let keys = keys::swap_keys(mnemonic, passphrase, &index)?; + let keys = record_keys(&record, mnemonic, passphrase)?; let provider = record .accept .provider_pubkey_hex @@ -803,7 +804,7 @@ fn advance_blocking( claim_state(&record, confirmations, required) } }; - Ok(status_of(&record, state)) + Ok(status_of(&record, &keys, state)) } /// Whether the saved claim spends `outpoint`. An unreadable one does not. @@ -1073,13 +1074,22 @@ fn record_payment_hash( mnemonic: &str, passphrase: Option<&str>, ) -> Result { + Ok(hex::encode( + record_keys(record, mnemonic, passphrase)?.payment_hash, + )) +} + +/// The keys of `record`, from the seed and the record's key index. +fn record_keys( + record: &SwapRecord, + mnemonic: &str, + passphrase: Option<&str>, +) -> Result { let index: [u8; 32] = hex::decode(&record.key_index) .ok() .and_then(|index| index.try_into().ok()) .ok_or_else(|| PubkySwapError::storage("invalid key index"))?; - Ok(hex::encode( - keys::swap_keys(mnemonic, passphrase, &index)?.payment_hash, - )) + keys::swap_keys(mnemonic, passphrase, &index) } /// A provider's list of this wallet's swaps, and whether it was read to the end. @@ -1378,6 +1388,15 @@ mod tests { assert!(record.quote.is_none() && record.claim_txid.is_none()); } + #[test] + fn a_restored_swaps_status_names_its_hold_payment() { + let snapshot = listed_swap(SEED, Some(keys::owner_ref(&keys::new_index()))); + let record = restore(&snapshot).expect("restored"); + let keys = record_keys(&record, SEED, None).unwrap(); + let status = status_of(&record, &keys, PubkySwapRouteState::AwaitingPayment); + assert_eq!(status.payment_hash_hex, snapshot.payment_hash_hex); + } + #[test] fn skips_swaps_this_wallet_cannot_rebuild() { let other_wallets = listed_swap(OTHER_SEED, Some(keys::owner_ref(&keys::new_index()))); diff --git a/src/modules/pubky_swaps/types.rs b/src/modules/pubky_swaps/types.rs index 170fd9ef..521bc864 100644 --- a/src/modules/pubky_swaps/types.rs +++ b/src/modules/pubky_swaps/types.rs @@ -123,4 +123,8 @@ pub struct PubkySwapRouteStatus { pub provider_pubky: String, pub delivered_sat: u64, pub destination_address: String, + /// The hash of the swap's hold invoice, so the app can find its payment, also for a swap + /// `pubky_swaps_restore` rebuilt. Stop advancing once that payment failed. + #[uniffi(default = "")] + pub payment_hash_hex: String, }