Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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=<BOLT11>` (served over NFC by Numo point-of-sale terminals, and planned by Square, which serves `lightning:<BOLT11>` 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).
Expand Down
20 changes: 18 additions & 2 deletions bindings/ios/bitkitcore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand All @@ -12250,6 +12260,9 @@ extension PubkySwapRouteStatus: Equatable, Hashable {
if lhs.destinationAddress != rhs.destinationAddress {
return false
}
if lhs.paymentHashHex != rhs.paymentHashHex {
return false
}
return true
}

Expand All @@ -12259,6 +12272,7 @@ extension PubkySwapRouteStatus: Equatable, Hashable {
hasher.combine(providerPubky)
hasher.combine(deliveredSat)
hasher.combine(destinationAddress)
hasher.combine(paymentHashHex)
}
}

Expand All @@ -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)
)
}

Expand All @@ -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)
}
}

Expand Down
4 changes: 3 additions & 1 deletion src/modules/pubky_swaps/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
39 changes: 29 additions & 10 deletions src/modules/pubky_swaps/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,13 +712,18 @@ fn snapshot_claimable(reply: SwapMessage, swap_id: Uuid) -> Result<bool, PubkySw
}
}

fn status_of(record: &SwapRecord, state: PubkySwapRouteState) -> 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),
}
}

Expand All @@ -730,11 +735,7 @@ fn advance_blocking(
mnemonic: &str,
passphrase: Option<&str>,
) -> Result<PubkySwapRouteStatus, PubkySwapError> {
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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -1073,13 +1074,22 @@ fn record_payment_hash(
mnemonic: &str,
passphrase: Option<&str>,
) -> Result<String, PubkySwapError> {
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<keys::SwapKeys, PubkySwapError> {
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.
Expand Down Expand Up @@ -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())));
Expand Down
4 changes: 4 additions & 0 deletions src/modules/pubky_swaps/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Loading