-
Notifications
You must be signed in to change notification settings - Fork 3
feat: add boltz savings swap #1081
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
coreyphillips
wants to merge
7
commits into
master
Choose a base branch
from
feat/savings-swap
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
45fce11
feat: add boltz swap support
coreyphillips cdce3eb
fix: keep savings slider off back gesture
coreyphillips e0c952a
fix: refresh balances entering savings
coreyphillips 289b950
chore: bump bitkit-core to 0.5.1
coreyphillips eba8bc8
fix: gate swap claim and harden boltz flow
coreyphillips 41c6c85
fix: harden savings swap races and lifecycle
coreyphillips 1c7a3e5
Merge branch 'master' into feat/savings-swap
coreyphillips File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,202 @@ | ||
| package to.bitkit.services | ||
|
|
||
| import com.synonym.bitkitcore.BoltzEventListener | ||
| import com.synonym.bitkitcore.BoltzNetwork | ||
| import com.synonym.bitkitcore.BoltzPairInfo | ||
| import com.synonym.bitkitcore.BoltzSwap | ||
| import com.synonym.bitkitcore.BoltzSwapEvent | ||
| import com.synonym.bitkitcore.ReverseSwapResponse | ||
| import com.synonym.bitkitcore.SubmarineSwapResponse | ||
| import com.synonym.bitkitcore.boltzClaimReverseSwap | ||
| import com.synonym.bitkitcore.boltzCreateReverseSwap | ||
| import com.synonym.bitkitcore.boltzCreateSubmarineSwap | ||
| import com.synonym.bitkitcore.boltzGetReverseLimits | ||
| import com.synonym.bitkitcore.boltzGetSubmarineLimits | ||
| import com.synonym.bitkitcore.boltzGetSwap | ||
| import com.synonym.bitkitcore.boltzListPendingSwaps | ||
| import com.synonym.bitkitcore.boltzListSwaps | ||
| import com.synonym.bitkitcore.boltzRefundSubmarineSwap | ||
| import com.synonym.bitkitcore.boltzStartSwapUpdates | ||
| import com.synonym.bitkitcore.boltzStopSwapUpdates | ||
| import kotlinx.coroutines.flow.MutableSharedFlow | ||
| import kotlinx.coroutines.flow.SharedFlow | ||
| import kotlinx.coroutines.flow.asSharedFlow | ||
| import kotlinx.coroutines.flow.first | ||
| import org.lightningdevkit.ldknode.Network | ||
| import to.bitkit.data.SettingsStore | ||
| import to.bitkit.data.keychain.Keychain | ||
| import to.bitkit.env.Env | ||
| import to.bitkit.utils.Logger | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| /** | ||
| * Thin wrapper around the bitkit-core Boltz swaps FFI (submarine + reverse swaps | ||
| * between onchain Bitcoin and Lightning). | ||
| * | ||
| * Mirrors the existing service pattern (e.g. [TrezorService]): a Hilt singleton | ||
| * that wraps the FFI and bridges the [BoltzEventListener] foreign callback to a | ||
| * [SharedFlow]. bitkit-core persists only a derivation index, never key material; | ||
| * swap keys are re-derived on demand from the wallet mnemonic. | ||
| * | ||
| * The Lightning side (issuing/paying invoices, fresh onchain addresses) is owned | ||
| * by [LightningService]; this service only talks to Boltz + the chain. | ||
| */ | ||
| @Suppress("TooManyFunctions") | ||
| @Singleton | ||
| class BoltzService @Inject constructor( | ||
| private val keychain: Keychain, | ||
| private val settingsStore: SettingsStore, | ||
| ) { | ||
| private val _events = MutableSharedFlow<BoltzSwapEvent>(extraBufferCapacity = 64) | ||
|
|
||
| /** Swap lifecycle events emitted while the updates stream is running. */ | ||
| val events: SharedFlow<BoltzSwapEvent> = _events.asSharedFlow() | ||
|
|
||
| private val listener = object : BoltzEventListener { | ||
| override fun onEvent(event: BoltzSwapEvent) { | ||
| Logger.info("Boltz event: $event", context = TAG) | ||
| _events.tryEmit(event) | ||
| } | ||
| } | ||
|
|
||
| // region Limits | ||
|
|
||
| suspend fun submarineLimits(network: BoltzNetwork = boltzNetwork()): BoltzPairInfo = | ||
| boltzGetSubmarineLimits(network = network) | ||
|
|
||
| suspend fun reverseLimits(network: BoltzNetwork = boltzNetwork()): BoltzPairInfo = | ||
| boltzGetReverseLimits(network = network) | ||
|
|
||
| // endregion | ||
|
|
||
| // region Create | ||
|
|
||
| /** Submarine swap: onchain BTC -> Lightning. Fund the returned lockup address. */ | ||
| suspend fun createSubmarineSwap( | ||
| invoice: String, | ||
| network: BoltzNetwork = boltzNetwork(), | ||
| electrumUrl: String? = null, | ||
| ): SubmarineSwapResponse { | ||
| val (mnemonic, passphrase) = credentials() | ||
| return boltzCreateSubmarineSwap( | ||
| network = network, | ||
| electrumUrl = electrumUrl ?: electrumUrl(), | ||
| invoice = invoice, | ||
| mnemonic = mnemonic, | ||
| bip39Passphrase = passphrase, | ||
| ).also { Logger.info("Created Boltz submarine swap ${it.id}", context = TAG) } | ||
| } | ||
|
|
||
| /** Reverse swap: Lightning -> onchain BTC. Pay the returned hold invoice. */ | ||
| suspend fun createReverseSwap( | ||
| amountSat: ULong, | ||
| claimAddress: String, | ||
| network: BoltzNetwork = boltzNetwork(), | ||
| electrumUrl: String? = null, | ||
| ): ReverseSwapResponse { | ||
| val (mnemonic, passphrase) = credentials() | ||
| return boltzCreateReverseSwap( | ||
| network = network, | ||
| electrumUrl = electrumUrl ?: electrumUrl(), | ||
| amountSat = amountSat, | ||
| claimAddress = claimAddress, | ||
| mnemonic = mnemonic, | ||
| bip39Passphrase = passphrase, | ||
| ).also { Logger.info("Created Boltz reverse swap ${it.id}", context = TAG) } | ||
| } | ||
|
|
||
| // endregion | ||
|
|
||
| // region Query | ||
|
|
||
| suspend fun listSwaps(): List<BoltzSwap> = boltzListSwaps() | ||
|
|
||
| suspend fun listPendingSwaps(): List<BoltzSwap> = boltzListPendingSwaps() | ||
|
|
||
| suspend fun getSwap(id: String): BoltzSwap? = boltzGetSwap(swapId = id) | ||
|
|
||
| // endregion | ||
|
|
||
| // region Manual claim / refund | ||
|
|
||
| suspend fun claimReverseSwap(id: String, feeRateSatPerVb: Double? = null): String { | ||
| val (mnemonic, passphrase) = credentials() | ||
| return boltzClaimReverseSwap( | ||
| swapId = id, | ||
| mnemonic = mnemonic, | ||
| bip39Passphrase = passphrase, | ||
| feeRateSatPerVb = feeRateSatPerVb, | ||
| ) | ||
| } | ||
|
|
||
| suspend fun refundSubmarineSwap(id: String, refundAddress: String, feeRateSatPerVb: Double? = null): String { | ||
| val (mnemonic, passphrase) = credentials() | ||
| return boltzRefundSubmarineSwap( | ||
| swapId = id, | ||
| refundAddress = refundAddress, | ||
| mnemonic = mnemonic, | ||
| bip39Passphrase = passphrase, | ||
| feeRateSatPerVb = feeRateSatPerVb, | ||
| ) | ||
| } | ||
|
|
||
| // endregion | ||
|
|
||
| // region Updates stream | ||
|
|
||
| /** | ||
| * Open the Boltz updates WebSocket, subscribe all pending swaps and auto-claim | ||
| * confirmed reverse swaps. [feeRateSatPerVb] is the rate used for those | ||
| * auto-claims (Bitkit owns fee estimation). Replaces any running stream. | ||
| */ | ||
| suspend fun startUpdates( | ||
| feeRateSatPerVb: Double?, | ||
| network: BoltzNetwork = boltzNetwork(), | ||
| ) { | ||
| val (mnemonic, passphrase) = credentials() | ||
| boltzStartSwapUpdates( | ||
| network = network, | ||
| listener = listener, | ||
| mnemonic = mnemonic, | ||
| bip39Passphrase = passphrase, | ||
| feeRateSatPerVb = feeRateSatPerVb, | ||
| ) | ||
| Logger.info("Started Boltz updates stream on $network", context = TAG) | ||
| } | ||
|
|
||
| suspend fun stopUpdates() { | ||
| boltzStopSwapUpdates() | ||
| Logger.info("Stopped Boltz updates stream", context = TAG) | ||
| } | ||
|
|
||
| // endregion | ||
|
|
||
| // region Helpers | ||
|
|
||
| /** The Boltz network matching the app's configured network. */ | ||
| fun boltzNetwork(network: Network = Env.network): BoltzNetwork = when (network) { | ||
| Network.BITCOIN -> BoltzNetwork.MAINNET | ||
| Network.TESTNET -> BoltzNetwork.TESTNET | ||
| Network.REGTEST -> BoltzNetwork.REGTEST | ||
| // Boltz does not operate on signet; fall back to testnet for development. | ||
| else -> BoltzNetwork.TESTNET | ||
| } | ||
|
|
||
| /** Current Electrum URL, used by Boltz for claim/refund broadcasting. */ | ||
| suspend fun electrumUrl(): String = settingsStore.data.first().electrumServer | ||
|
|
||
| private suspend fun credentials(): Pair<String, String?> { | ||
| val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) | ||
| ?: error("Mnemonic not found") | ||
| val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name) | ||
| ?.takeIf { it.isNotEmpty() } | ||
| return mnemonic to passphrase | ||
| } | ||
|
|
||
| // endregion | ||
|
|
||
| companion object { | ||
| private const val TAG = "BoltzService" | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.