Skip to content

fix: use spark send key in retry - #2020

Draft
ovitrif wants to merge 6 commits into
masterfrom
fix/spark-send-keys
Draft

ovitrif wants to merge 6 commits into
masterfrom
fix/spark-send-keys

Conversation

@ovitrif

@ovitrif ovitrif commented Nov 24, 2026 •

Copy link
Copy Markdown
Collaborator

Closes #2019
Closes #2021
Twin: synonymdev/bitkit-ios#1415
Refs: #2014

Description

  • Fixes a Lightning invoice paid through Spark being paid twice when a send is retried after an outcome the app didn't learn (a crash, a timeout, a relaunch, a restore before Spark synced), because spark-sdk 0.28.3 dedupes only on the idempotency key and each attempt passed a new random one.
  • Adds SparkSendKey: each attempt's key is a UUIDv5 of a secret derived from the wallet seed, the payment hash and the attempt number, so a retry reuses its attempt's key and the Spark operators return the transfer it committed instead of sending again. A seed secret, not the bare hash, so no other wallet derives the same transfer id and the operators can't link the transfer to the invoice. iOS derives the same keys (shared test vector), so a restore across apps keeps them.
  • Adds the attempt number per invoice (sparkSendAttempts, kept until the invoice expires). A send moves to the next attempt only past keys Spark's local list shows failed, as a failed key can't send again.
  • Keeps a send held when Spark reports a short balance under a reused key: Spark picks leaves before the operators see the key, so that error can hide the committed transfer; the hold keeps the node rail off the invoice until Spark syncs.
  • Retries a send briefly under the same key when Spark holds the balance but can't select leaves to spend ("Failed to select leaves after all retries"), as right after a move to Spark lands. Spark selects leaves before anything reaches the operators, so the retry can't pay twice. If it still fails, the send is refused under a never-used key and stays held under a reused one, as a short balance is.
  • A send refused before anything reached Spark's operators puts back the attempt kept before it, so a Try Again is refused the same way instead of held Pending until the invoice expires. A send whose own result comes back failed releases its invoice, as when its outcome settles, and a retry that gets back a failure already reported is refused instead of waiting for an event that won't come.

Guards:

  • The refusal while Spark's list can't be read (fix: close spark retry hold gaps #2014): relaxed to invoices an older build sent under a random key, since a retry under this build's key can't pay twice.
  • refuseUnverifiedRestoredInvoice: kept, as it covers sends by older builds and the node rail, which have no derived key.
  • The send holds and the in-flight gate: kept, as they decide what the send screen shows while an attempt is unresolved.
  • The fix: spark retry follow-ups #2007 attempt identity: kept, as the derived key is still the attempt's payment id.

Out of Scope

  • On-chain withdrawals from Spark keep their random key: they have no payment hash and their own journal.
  • fix: close spark retry hold gaps #2014's open case of an invoice another device paid before this one's first sync is covered once both devices run this change.

Design

N/A — no UI changes.

Preview

N/A

QA Notes

Setup

  • Case 1: bitkit-docker regtest with Spark (operators and SSP), a dev build of this branch, a channel to the LSP node with a balance, and a Spark balance that covers the payment only with the channel balance.
  • The key-reuse cases need crashes mid-send and restores; they are covered by the regtest proof and unit tests below, not by device tests.

Journeys

N/A — the combined payment has no journey yet; case 1 covers it.

Manual Tests

  • 1. Local regtest, a wallet with a channel balance and some Spark balance: pay an invoice only the two together cover, so the app moves the channels to Spark first. Expected: the payment completes once the move lands; it never shows Payment Pending that doesn't resolve.

Automated Checks

  • added SparkSendKeyTest.kt — derives the shared test vector iOS asserts too
  • added SparkRepoTest.kt — a send goes out under its first attempt's key while Spark's list can't be read
  • added SparkRepoTest.kt — a retry reuses its attempt's key until Spark lists that attempt failed
  • added SparkRepoTest.kt — a retry takes the next attempt's key once Spark lists its attempt failed
  • added SparkRepoTest.kt — a short balance under a reused key keeps the send held
  • added SparkRepoTest.kt — a send retries under its key while Spark can't select its leaves
  • added SparkRepoTest.kt — a send whose leaves Spark can't select is refused under a new key
  • added SparkRepoTest.kt — leaves Spark can't select under a reused key keep the send held
  • added SparkRepoTest.kt — a send refused before reaching Spark is refused again on retry
  • added SparkRepoTest.kt — a refused retry keeps the attempt Spark listed failed before it
  • added SparkRepoTest.kt — a retry that gets back a failure already reported is refused
  • added CacheStoreTest.kt — a send attempt drops expired ones and restores the earlier one
  • updated SparkRepoTest.kt — the unreadable-list refusal applies to an older build's send
  • updated LightningRepoTest.kt — stubs the send key

Evidence

  • ran just compile, just test and just lint at HEAD: all pass.

Regtest proof of the SDK behaviour this relies on (spark-sdk 0.28.3 client; operator breez/spark@b54845c and Breez's test SSP, see Gaps). Each "nothing sent" was checked on the operators' transfers, the SSP's send requests and the receiver's balance.

Case Route Observed
Same key after a committed send Spark, SSP The stored payment comes back; one transfer, receiver paid once
Same key after a wipe and restore, before sync Spark, SSP The committed transfer is recovered and written as a local row; nothing sent twice
Same key after a wipe and restore, after sync Spark, SSP Sync imports the payment under the key; the repeat returns it
Same key after a failure before commit Spark, SSP The retry sends once
Failed under attempt 0, then attempt 1 SSP Resending attempt 0 returns its failure and sends nothing; attempt 1 sends once
Failed under attempt 0, wipe and restore, resend attempt 0 SSP The failure comes back as a local Failed row; nothing paid
Same key after a restore with a balance below the amount Spark, SSP "Insufficient funds", no local row, nothing sent twice; the app keeps the send held until Spark syncs

On the SSP route a recovered transfer resumes its Lightning request, which the SSP keys by transfer id, so one key never pays twice; a second payment needs a new attempt's key.

Regtest repro of the leaf-selection failure (found by the swipe-to-pay review run J1 on #2017, local regtest, master at its base): a combined payment moved 29 602 sats to Spark, and Spark's send about 1 s later failed with Tree service error: generic error: Failed to select leaves after all retries. Master treated that as an unknown outcome, so the payment went Pending and never resolved ("Stopped polling ... until Spark syncs again"); the funds stayed in Spark and a retry paid. 0.28.3 raises it in select_leaves_with_retry after its balance check and before any operator call, so a send under the same key can be retried safely.

Case 1 on local regtest with Spark (breez/spark@b54845c, Breez's test SSP), emulator, build of a80d84b (before the attempt-restore and failed-result commits), host load under 14:

Case Wallet Amount Amount + fee an exact sum of leaves? Result
1a channel 30 775, Spark 30 000 50 000 (moves the channel to Spark first) no Payment Failed after 3 retries under one key
1a control same, after the move 48 955 yes Bitcoin Sent
1b channel + Spark 30 000 29 700 / 29 879 no / yes Payment Failed / Bitcoin Sent
1c Spark 30 000 only 10 000 / 8 158 no / yes Payment Failed / Bitcoin Sent

This operator build rejects every leaf swap (unable to validate adaptor signature, 51 of 51, including Spark's own leaf optimization after each receive), so Spark pays only amounts it can cover with leaves it already holds. Every failure behaved as this PR intends: retries under one key, then refused with Payment Failed, never an unresolved Payment Pending, funds left in Spark. Case 1 stays unchecked until it can pass on a stack whose leaf swaps work.

Gaps

  • Leaf selection comes before the operators only on the SDK's first try: after an operator error it may retry (with_leafs_spent_retry!) and select again, so a short balance or a leaf-selection failure after such a retry could follow a committed transfer. Under a new key that path refuses the send, as it already did for a short balance.
  • The regtest operator and SSP are public and test builds (breez/spark@b54845c, Breez's test SSP), not the servers 0.28.3 pins or Lightspark's production SSP; their duplicate-transfer-id rejection and SSP dedup are proven on these builds only.
  • Background sync was off in the "before sync" runs; on a real restore the retry races Spark's first sync.
  • A leaf swap during a repeated send could not run on this operator; on production it runs before the duplicate-id rejection (it moves leaves, it pays nothing).
  • After a recovered repeat, Spark's local balance reads low by the amount until its next sync.
  • Case 1 can't pass on this regtest stack: its operator build rejects every leaf swap, so a payment Spark can't make from the leaves it holds fails there. The move-then-pay path is unproven on a device until a stack with working leaf swaps (or staging) runs it.
  • While Spark's payment list can't be read, a retry reuses the latest key without knowing its outcome; a retry that then fails under that key, or a send whose failed result returns before the send screen subscribes to its events, can leave the screen on Pending. This needs a storage failure in Spark's list and is not covered by tests.
  • The key-reuse cases need crashes mid-send and restores; they are covered by the regtest proof and unit tests below, not by device tests.

Journeys

N/A — the combined payment has no journey yet; case 1 covers it.

Manual Tests

  • 1. Local regtest, a wallet with a channel balance and some Spark balance: pay an invoice only the two together cover, so the app moves the channels to Spark first. Expected: the payment completes once the move lands; it never shows Payment Pending that doesn't resolve.

Automated Checks

  • added SparkSendKeyTest.kt — derives the shared test vector iOS asserts too
  • added SparkRepoTest.kt — a send goes out under its first attempt's key while Spark's list can't be read
  • added SparkRepoTest.kt — a retry reuses its attempt's key until Spark lists that attempt failed
  • added SparkRepoTest.kt — a retry takes the next attempt's key once Spark lists its attempt failed
  • added SparkRepoTest.kt — a short balance under a reused key keeps the send held
  • added SparkRepoTest.kt — a send retries under its key while Spark can't select its leaves
  • added SparkRepoTest.kt — a send whose leaves Spark can't select is refused under a new key
  • added SparkRepoTest.kt — leaves Spark can't select under a reused key keep the send held
  • added SparkRepoTest.kt — a send refused before reaching Spark is refused again on retry
  • added SparkRepoTest.kt — a refused retry keeps the attempt Spark listed failed before it
  • added SparkRepoTest.kt — a retry that gets back a failure already reported is refused
  • added CacheStoreTest.kt — a send attempt drops expired ones and restores the earlier one
  • updated SparkRepoTest.kt — the unreadable-list refusal applies to an older build's send
  • updated LightningRepoTest.kt — stubs the send key

Evidence

  • ran just compile, just test and just lint at HEAD: all pass.

Regtest proof of the SDK behaviour this relies on (spark-sdk 0.28.3 client; operator breez/spark@b54845c and Breez's test SSP, see Gaps). Each "nothing sent" was checked on the operators' transfers, the SSP's send requests and the receiver's balance.

Case Route Observed
Same key after a committed send Spark, SSP The stored payment comes back; one transfer, receiver paid once
Same key after a wipe and restore, before sync Spark, SSP The committed transfer is recovered and written as a local row; nothing sent twice
Same key after a wipe and restore, after sync Spark, SSP Sync imports the payment under the key; the repeat returns it
Same key after a failure before commit Spark, SSP The retry sends once
Failed under attempt 0, then attempt 1 SSP Resending attempt 0 returns its failure and sends nothing; attempt 1 sends once
Failed under attempt 0, wipe and restore, resend attempt 0 SSP The failure comes back as a local Failed row; nothing paid
Same key after a restore with a balance below the amount Spark, SSP "Insufficient funds", no local row, nothing sent twice; the app keeps the send held until Spark syncs

On the SSP route a recovered transfer resumes its Lightning request, which the SSP keys by transfer id, so one key never pays twice; a second payment needs a new attempt's key.

Regtest repro of the leaf-selection failure (found by the swipe-to-pay review run J1 on #2017, local regtest, master at its base): a combined payment moved 29 602 sats to Spark, and Spark's send about 1 s later failed with Tree service error: generic error: Failed to select leaves after all retries. Master treated that as an unknown outcome, so the payment went Pending and never resolved ("Stopped polling ... until Spark syncs again"); the funds stayed in Spark and a retry paid. 0.28.3 raises it in select_leaves_with_retry after its balance check and before any operator call, so a send under the same key can be retried safely.

Case 1 on local regtest with Spark (breez/spark@b54845c, Breez's test SSP), emulator, build of a80d84b (before the attempt-restore and failed-result commits), host load under 14:

Case Wallet Amount Amount + fee an exact sum of leaves? Result
1a channel 30 775, Spark 30 000 50 000 (moves the channel to Spark first) no Payment Failed after 3 retries under one key
1a control same, after the move 48 955 yes Bitcoin Sent
1b channel + Spark 30 000 29 700 / 29 879 no / yes Payment Failed / Bitcoin Sent
1c Spark 30 000 only 10 000 / 8 158 no / yes Payment Failed / Bitcoin Sent

This operator build rejects every leaf swap (unable to validate adaptor signature, 51 of 51, including Spark's own leaf optimization after each receive), so Spark pays only amounts it can cover with leaves it already holds. Every failure behaved as this PR intends: retries under one key, then refused with Payment Failed, never an unresolved Payment Pending, funds left in Spark. Case 1 stays unchecked until it can pass on a stack whose leaf swaps work.

Gaps

  • Leaf selection comes before the operators only on the SDK's first try: after an operator error it may retry (with_leafs_spent_retry!) and select again, so a short balance or a leaf-selection failure after such a retry could follow a committed transfer. Under a new key that path refuses the send, as it already did for a short balance.
  • The regtest operator and SSP are public and test builds (breez/spark@b54845c, Breez's test SSP), not the servers 0.28.3 pins or Lightspark's production SSP; their duplicate-transfer-id rejection and SSP dedup are proven on these builds only.
  • Background sync was off in the "before sync" runs; on a real restore the retry races Spark's first sync.
  • A leaf swap during a repeated send could not run on this operator; on production it runs before the duplicate-id rejection (it moves leaves, it pays nothing).
  • After a recovered repeat, Spark's local balance reads low by the amount until its next sync.
  • Case 1 did not pass on its only run (below), and a plain Spark send in the same wallet failed the same way, so the cause is not isolated: the regtest host was heavily overloaded during the run. Other regtest runs point at Spark's leaf denominations rather than timing: a 50 000-sat payment after a move failed the same way on both apps (on iOS, although its combined flow waits for Spark's balance first), while 75 000 and 80 000 paid. It needs a rerun on a quiet host, with a payment that needs a leaf swap and one that doesn't, before this PR is ready.
  • The key-reuse cases need crashes mid-send and restores; they are covered by the regtest proof and unit tests, not by a device run.

Models used

  • Planning/scoping: Claude Opus 5.5 (reasoning: medium)
  • Implementation: Claude Opus 5.5 (reasoning: medium), Claude Sonnet 5.5 (reasoning: high)
  • Review: gemini-4-neon-high (reasoning: Not exposed)
  • Review rounds: 4

Metadata note: the Gemini self-review records the model only, not a reasoning setting.

@ovitrif ovitrif self-assigned this Nov 24, 2026
@github-actions

github-actions Bot commented Nov 24, 2026 •

Copy link
Copy Markdown
Contributor

Regtest APK

Built from 521a8d7 (run).

Download bitkit-dev-debug universal APK (expires in 30 days).

@ovitrif ovitrif changed the title fix: reuse a spark send's key across retries fix: use spark send key in retry Nov 25, 2026
Derive each Spark send's idempotency key from the seed, the payment hash
and an attempt number, so a retry reuses its attempt's key and Spark never
pays an invoice twice. The attempt moves on only past keys Spark lists as
failed. A short balance under a reused key keeps the send held, as it can
hide the transfer an earlier attempt committed. The refusal while Spark's
list can't be read now applies only to an invoice an older build sent
under a random key.

Verified: just compile, just test, just lint
Verified: just compile, just test, just lint
@ovitrif
ovitrif force-pushed the fix/spark-send-keys branch from 9de4067 to a80d84b Compare November 25, 2026 09:49
A send refused before anything reached Spark's operators (a short balance
or leaves Spark can't select under a never-used key, an expired invoice, a
deadline or exit) kept its attempt as the invoice's latest, so a retry
counted that unused key as reused and held the invoice pending until it
expired. The refusal now puts back the attempt kept before it.

Verified: just compile, just test, just lint (after the last edit: lint and test)
A retry under a key whose failure this process already reported, as when
Spark's list couldn't be read to move past it, got the failure back with
no event, leaving the send screen waiting; it is now refused. A send that
lost Spark's key secret is refused instead of throwing, the leaf-selection
refusal says so, and the restored cache log leaves out send attempts.

Verified: just compile, just test, just lint
A send whose own result came back failed kept the invoice's send hash, so
a retry while Spark's list couldn't be read showed it pending; the failed
attempt paid nothing, so the hash goes, as when its outcome settles. The
leaf-selection retry checks the deadline and exit before it waits, and a
connection keeps the send key's secret before it reports connected.

Verified: just compile, just test, just lint
Also publishes Spark's connection to other threads with the send key's
secret already set, and drops a needless suspend from the retry check.

Verified: just compile, just test, just lint

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: payment after a spark move stays pending fix: use spark send key in retry

1 participant