|
| 1 | +package to.bitkit.services |
| 2 | + |
| 3 | +import com.synonym.bitkitcore.BoltzEventListener |
| 4 | +import com.synonym.bitkitcore.BoltzNetwork |
| 5 | +import com.synonym.bitkitcore.BoltzPairInfo |
| 6 | +import com.synonym.bitkitcore.BoltzSwap |
| 7 | +import com.synonym.bitkitcore.BoltzSwapEvent |
| 8 | +import com.synonym.bitkitcore.ReverseSwapResponse |
| 9 | +import com.synonym.bitkitcore.SubmarineSwapResponse |
| 10 | +import com.synonym.bitkitcore.boltzClaimReverseSwap |
| 11 | +import com.synonym.bitkitcore.boltzCreateReverseSwap |
| 12 | +import com.synonym.bitkitcore.boltzCreateSubmarineSwap |
| 13 | +import com.synonym.bitkitcore.boltzGetReverseLimits |
| 14 | +import com.synonym.bitkitcore.boltzGetSubmarineLimits |
| 15 | +import com.synonym.bitkitcore.boltzGetSwap |
| 16 | +import com.synonym.bitkitcore.boltzListPendingSwaps |
| 17 | +import com.synonym.bitkitcore.boltzListSwaps |
| 18 | +import com.synonym.bitkitcore.boltzRefundSubmarineSwap |
| 19 | +import com.synonym.bitkitcore.boltzStartSwapUpdates |
| 20 | +import com.synonym.bitkitcore.boltzStopSwapUpdates |
| 21 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 22 | +import kotlinx.coroutines.flow.SharedFlow |
| 23 | +import kotlinx.coroutines.flow.asSharedFlow |
| 24 | +import kotlinx.coroutines.flow.first |
| 25 | +import org.lightningdevkit.ldknode.Network |
| 26 | +import to.bitkit.data.SettingsStore |
| 27 | +import to.bitkit.data.keychain.Keychain |
| 28 | +import to.bitkit.env.Env |
| 29 | +import to.bitkit.utils.Logger |
| 30 | +import javax.inject.Inject |
| 31 | +import javax.inject.Singleton |
| 32 | + |
| 33 | +/** |
| 34 | + * Thin wrapper around the bitkit-core Boltz swaps FFI (submarine + reverse swaps |
| 35 | + * between onchain Bitcoin and Lightning). |
| 36 | + * |
| 37 | + * Mirrors the existing service pattern (e.g. [TrezorService]): a Hilt singleton |
| 38 | + * that wraps the FFI and bridges the [BoltzEventListener] foreign callback to a |
| 39 | + * [SharedFlow]. bitkit-core persists only a derivation index, never key material; |
| 40 | + * swap keys are re-derived on demand from the wallet mnemonic. |
| 41 | + * |
| 42 | + * The Lightning side (issuing/paying invoices, fresh onchain addresses) is owned |
| 43 | + * by [LightningService]; this service only talks to Boltz + the chain. |
| 44 | + */ |
| 45 | +@Singleton |
| 46 | +class BoltzService @Inject constructor( |
| 47 | + private val keychain: Keychain, |
| 48 | + private val settingsStore: SettingsStore, |
| 49 | +) { |
| 50 | + private val _events = MutableSharedFlow<BoltzSwapEvent>(extraBufferCapacity = 64) |
| 51 | + |
| 52 | + /** Swap lifecycle events emitted while the updates stream is running. */ |
| 53 | + val events: SharedFlow<BoltzSwapEvent> = _events.asSharedFlow() |
| 54 | + |
| 55 | + private val listener = object : BoltzEventListener { |
| 56 | + override fun onEvent(event: BoltzSwapEvent) { |
| 57 | + Logger.info("Boltz event: $event", context = TAG) |
| 58 | + _events.tryEmit(event) |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + // region Limits |
| 63 | + |
| 64 | + suspend fun submarineLimits(network: BoltzNetwork = boltzNetwork()): BoltzPairInfo = |
| 65 | + boltzGetSubmarineLimits(network = network) |
| 66 | + |
| 67 | + suspend fun reverseLimits(network: BoltzNetwork = boltzNetwork()): BoltzPairInfo = |
| 68 | + boltzGetReverseLimits(network = network) |
| 69 | + |
| 70 | + // endregion |
| 71 | + |
| 72 | + // region Create |
| 73 | + |
| 74 | + /** Submarine swap: onchain BTC -> Lightning. Fund the returned lockup address. */ |
| 75 | + suspend fun createSubmarineSwap( |
| 76 | + invoice: String, |
| 77 | + network: BoltzNetwork = boltzNetwork(), |
| 78 | + electrumUrl: String? = null, |
| 79 | + ): SubmarineSwapResponse { |
| 80 | + val (mnemonic, passphrase) = credentials() |
| 81 | + return boltzCreateSubmarineSwap( |
| 82 | + network = network, |
| 83 | + electrumUrl = electrumUrl ?: electrumUrl(), |
| 84 | + invoice = invoice, |
| 85 | + mnemonic = mnemonic, |
| 86 | + bip39Passphrase = passphrase, |
| 87 | + ).also { Logger.info("Created Boltz submarine swap ${it.id}", context = TAG) } |
| 88 | + } |
| 89 | + |
| 90 | + /** Reverse swap: Lightning -> onchain BTC. Pay the returned hold invoice. */ |
| 91 | + suspend fun createReverseSwap( |
| 92 | + amountSat: ULong, |
| 93 | + claimAddress: String, |
| 94 | + network: BoltzNetwork = boltzNetwork(), |
| 95 | + electrumUrl: String? = null, |
| 96 | + ): ReverseSwapResponse { |
| 97 | + val (mnemonic, passphrase) = credentials() |
| 98 | + return boltzCreateReverseSwap( |
| 99 | + network = network, |
| 100 | + electrumUrl = electrumUrl ?: electrumUrl(), |
| 101 | + amountSat = amountSat, |
| 102 | + claimAddress = claimAddress, |
| 103 | + mnemonic = mnemonic, |
| 104 | + bip39Passphrase = passphrase, |
| 105 | + ).also { Logger.info("Created Boltz reverse swap ${it.id}", context = TAG) } |
| 106 | + } |
| 107 | + |
| 108 | + // endregion |
| 109 | + |
| 110 | + // region Query |
| 111 | + |
| 112 | + suspend fun listSwaps(): List<BoltzSwap> = boltzListSwaps() |
| 113 | + |
| 114 | + suspend fun listPendingSwaps(): List<BoltzSwap> = boltzListPendingSwaps() |
| 115 | + |
| 116 | + suspend fun getSwap(id: String): BoltzSwap? = boltzGetSwap(swapId = id) |
| 117 | + |
| 118 | + // endregion |
| 119 | + |
| 120 | + // region Manual claim / refund |
| 121 | + |
| 122 | + suspend fun claimReverseSwap(id: String, feeRateSatPerVb: Double? = null): String { |
| 123 | + val (mnemonic, passphrase) = credentials() |
| 124 | + return boltzClaimReverseSwap( |
| 125 | + swapId = id, |
| 126 | + mnemonic = mnemonic, |
| 127 | + bip39Passphrase = passphrase, |
| 128 | + feeRateSatPerVb = feeRateSatPerVb, |
| 129 | + ) |
| 130 | + } |
| 131 | + |
| 132 | + suspend fun refundSubmarineSwap(id: String, refundAddress: String, feeRateSatPerVb: Double? = null): String { |
| 133 | + val (mnemonic, passphrase) = credentials() |
| 134 | + return boltzRefundSubmarineSwap( |
| 135 | + swapId = id, |
| 136 | + refundAddress = refundAddress, |
| 137 | + mnemonic = mnemonic, |
| 138 | + bip39Passphrase = passphrase, |
| 139 | + feeRateSatPerVb = feeRateSatPerVb, |
| 140 | + ) |
| 141 | + } |
| 142 | + |
| 143 | + // endregion |
| 144 | + |
| 145 | + // region Updates stream |
| 146 | + |
| 147 | + /** |
| 148 | + * Open the Boltz updates WebSocket, subscribe all pending swaps and auto-claim |
| 149 | + * confirmed reverse swaps. [feeRateSatPerVb] is the rate used for those |
| 150 | + * auto-claims (Bitkit owns fee estimation). Replaces any running stream. |
| 151 | + */ |
| 152 | + suspend fun startUpdates( |
| 153 | + feeRateSatPerVb: Double?, |
| 154 | + network: BoltzNetwork = boltzNetwork(), |
| 155 | + ) { |
| 156 | + val (mnemonic, passphrase) = credentials() |
| 157 | + boltzStartSwapUpdates( |
| 158 | + network = network, |
| 159 | + listener = listener, |
| 160 | + mnemonic = mnemonic, |
| 161 | + bip39Passphrase = passphrase, |
| 162 | + feeRateSatPerVb = feeRateSatPerVb, |
| 163 | + ) |
| 164 | + Logger.info("Started Boltz updates stream on $network", context = TAG) |
| 165 | + } |
| 166 | + |
| 167 | + suspend fun stopUpdates() { |
| 168 | + boltzStopSwapUpdates() |
| 169 | + Logger.info("Stopped Boltz updates stream", context = TAG) |
| 170 | + } |
| 171 | + |
| 172 | + // endregion |
| 173 | + |
| 174 | + // region Helpers |
| 175 | + |
| 176 | + /** The Boltz network matching the app's configured network. */ |
| 177 | + fun boltzNetwork(network: Network = Env.network): BoltzNetwork = when (network) { |
| 178 | + Network.BITCOIN -> BoltzNetwork.MAINNET |
| 179 | + Network.TESTNET -> BoltzNetwork.TESTNET |
| 180 | + Network.REGTEST -> BoltzNetwork.REGTEST |
| 181 | + // Boltz does not operate on signet; fall back to testnet for development. |
| 182 | + else -> BoltzNetwork.TESTNET |
| 183 | + } |
| 184 | + |
| 185 | + /** Current Electrum URL, used by Boltz for claim/refund broadcasting. */ |
| 186 | + suspend fun electrumUrl(): String = settingsStore.data.first().electrumServer |
| 187 | + |
| 188 | + private suspend fun credentials(): Pair<String, String?> { |
| 189 | + val mnemonic = keychain.loadString(Keychain.Key.BIP39_MNEMONIC.name) |
| 190 | + ?: error("Mnemonic not found") |
| 191 | + val passphrase = keychain.loadString(Keychain.Key.BIP39_PASSPHRASE.name) |
| 192 | + ?.takeIf { it.isNotEmpty() } |
| 193 | + return mnemonic to passphrase |
| 194 | + } |
| 195 | + |
| 196 | + // endregion |
| 197 | + |
| 198 | + companion object { |
| 199 | + private const val TAG = "BoltzService" |
| 200 | + } |
| 201 | +} |
0 commit comments