|
| 1 | +package to.bitkit.paykit |
| 2 | + |
| 3 | +import kotlinx.coroutines.sync.Mutex |
| 4 | +import kotlinx.coroutines.sync.withLock |
| 5 | +import org.lightningdevkit.ldknode.Network |
| 6 | +import to.bitkit.env.Env |
| 7 | +import to.bitkit.paykit.executors.BitkitBitcoinExecutor |
| 8 | +import to.bitkit.paykit.executors.BitkitLightningExecutor |
| 9 | +import to.bitkit.repositories.LightningRepo |
| 10 | +import to.bitkit.utils.Logger |
| 11 | +import javax.inject.Inject |
| 12 | +import javax.inject.Singleton |
| 13 | + |
| 14 | +/** |
| 15 | + * Manages PaykitClient lifecycle and executor registration for Bitkit Android. |
| 16 | + * |
| 17 | + * Usage: |
| 18 | + * ```kotlin |
| 19 | + * val manager = PaykitManager.getInstance() |
| 20 | + * manager.initialize() |
| 21 | + * manager.registerExecutors(lightningRepo) |
| 22 | + * ``` |
| 23 | + * |
| 24 | + * Note: PaykitMobile bindings must be generated and linked before this class |
| 25 | + * is fully functional. See INTEGRATION_DISCOVERY.md for setup instructions. |
| 26 | + */ |
| 27 | +@Singleton |
| 28 | +class PaykitManager @Inject constructor() { |
| 29 | + |
| 30 | + companion object { |
| 31 | + private const val TAG = "PaykitManager" |
| 32 | + |
| 33 | + @Volatile |
| 34 | + private var instance: PaykitManager? = null |
| 35 | + |
| 36 | + fun getInstance(): PaykitManager { |
| 37 | + return instance ?: synchronized(this) { |
| 38 | + instance ?: PaykitManager().also { instance = it } |
| 39 | + } |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // The underlying Paykit client (null until initialized) |
| 44 | + // Type: PaykitClient from PaykitMobile bindings |
| 45 | + private var client: Any? = null |
| 46 | + |
| 47 | + // Bitcoin executor instance (stored to prevent garbage collection) |
| 48 | + private var bitcoinExecutor: BitkitBitcoinExecutor? = null |
| 49 | + |
| 50 | + // Lightning executor instance (stored to prevent garbage collection) |
| 51 | + private var lightningExecutor: BitkitLightningExecutor? = null |
| 52 | + |
| 53 | + // Mutex for thread-safe initialization |
| 54 | + private val mutex = Mutex() |
| 55 | + |
| 56 | + // Whether the manager has been initialized |
| 57 | + var isInitialized: Boolean = false |
| 58 | + private set |
| 59 | + |
| 60 | + // Whether executors have been registered |
| 61 | + var hasExecutors: Boolean = false |
| 62 | + private set |
| 63 | + |
| 64 | + // Bitcoin network configuration |
| 65 | + val bitcoinNetwork: BitcoinNetworkConfig = mapNetwork(Env.network).first |
| 66 | + |
| 67 | + // Lightning network configuration |
| 68 | + val lightningNetwork: LightningNetworkConfig = mapNetwork(Env.network).second |
| 69 | + |
| 70 | + /** |
| 71 | + * Maps LDK Network to Paykit network configs. |
| 72 | + */ |
| 73 | + private fun mapNetwork(network: Network): Pair<BitcoinNetworkConfig, LightningNetworkConfig> { |
| 74 | + return when (network) { |
| 75 | + Network.BITCOIN -> BitcoinNetworkConfig.MAINNET to LightningNetworkConfig.MAINNET |
| 76 | + Network.TESTNET -> BitcoinNetworkConfig.TESTNET to LightningNetworkConfig.TESTNET |
| 77 | + Network.REGTEST -> BitcoinNetworkConfig.REGTEST to LightningNetworkConfig.REGTEST |
| 78 | + Network.SIGNET -> BitcoinNetworkConfig.TESTNET to LightningNetworkConfig.TESTNET |
| 79 | + } |
| 80 | + } |
| 81 | + |
| 82 | + /** |
| 83 | + * Initialize the Paykit client with network configuration. |
| 84 | + * |
| 85 | + * Call this during app startup, after the wallet is ready. |
| 86 | + * |
| 87 | + * @throws PaykitException if initialization fails |
| 88 | + */ |
| 89 | + suspend fun initialize() = mutex.withLock { |
| 90 | + if (isInitialized) { |
| 91 | + Logger.debug("PaykitManager already initialized", context = TAG) |
| 92 | + return@withLock |
| 93 | + } |
| 94 | + |
| 95 | + Logger.info("Initializing PaykitManager with network: $bitcoinNetwork", context = TAG) |
| 96 | + |
| 97 | + // TODO: Uncomment when PaykitMobile bindings are available |
| 98 | + // client = PaykitClient.newWithNetwork( |
| 99 | + // bitcoinNetwork = bitcoinNetwork.toFfi(), |
| 100 | + // lightningNetwork = lightningNetwork.toFfi() |
| 101 | + // ) |
| 102 | + |
| 103 | + isInitialized = true |
| 104 | + Logger.info("PaykitManager initialized successfully", context = TAG) |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * Register Bitcoin and Lightning executors with the Paykit client. |
| 109 | + * |
| 110 | + * This connects Bitkit's LightningRepo to Paykit for payment execution. |
| 111 | + * Must be called after [initialize]. |
| 112 | + * |
| 113 | + * @param lightningRepo The LightningRepo instance for payment operations |
| 114 | + * @throws PaykitException if registration fails or client not initialized |
| 115 | + */ |
| 116 | + suspend fun registerExecutors(lightningRepo: LightningRepo) = mutex.withLock { |
| 117 | + if (!isInitialized) { |
| 118 | + throw PaykitException.NotInitialized |
| 119 | + } |
| 120 | + |
| 121 | + if (hasExecutors) { |
| 122 | + Logger.debug("Executors already registered", context = TAG) |
| 123 | + return@withLock |
| 124 | + } |
| 125 | + |
| 126 | + Logger.info("Registering Paykit executors", context = TAG) |
| 127 | + |
| 128 | + // Create executor instances |
| 129 | + bitcoinExecutor = BitkitBitcoinExecutor(lightningRepo) |
| 130 | + lightningExecutor = BitkitLightningExecutor(lightningRepo) |
| 131 | + |
| 132 | + // TODO: Uncomment when PaykitMobile bindings are available |
| 133 | + // val paykitClient = client as? PaykitClient |
| 134 | + // ?: throw PaykitException.NotInitialized |
| 135 | + // |
| 136 | + // paykitClient.registerBitcoinExecutor(bitcoinExecutor!!) |
| 137 | + // paykitClient.registerLightningExecutor(lightningExecutor!!) |
| 138 | + |
| 139 | + hasExecutors = true |
| 140 | + Logger.info("Paykit executors registered successfully", context = TAG) |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Reset the manager state (for testing or logout scenarios). |
| 145 | + */ |
| 146 | + fun reset() { |
| 147 | + client = null |
| 148 | + bitcoinExecutor = null |
| 149 | + lightningExecutor = null |
| 150 | + isInitialized = false |
| 151 | + hasExecutors = false |
| 152 | + Logger.info("PaykitManager reset", context = TAG) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +/** |
| 157 | + * Bitcoin network configuration for Paykit. |
| 158 | + */ |
| 159 | +enum class BitcoinNetworkConfig { |
| 160 | + MAINNET, |
| 161 | + TESTNET, |
| 162 | + REGTEST; |
| 163 | + |
| 164 | + // TODO: Uncomment when PaykitMobile bindings are available |
| 165 | + // fun toFfi(): BitcoinNetworkFfi = when (this) { |
| 166 | + // MAINNET -> BitcoinNetworkFfi.MAINNET |
| 167 | + // TESTNET -> BitcoinNetworkFfi.TESTNET |
| 168 | + // REGTEST -> BitcoinNetworkFfi.REGTEST |
| 169 | + // } |
| 170 | +} |
| 171 | + |
| 172 | +/** |
| 173 | + * Lightning network configuration for Paykit. |
| 174 | + */ |
| 175 | +enum class LightningNetworkConfig { |
| 176 | + MAINNET, |
| 177 | + TESTNET, |
| 178 | + REGTEST; |
| 179 | + |
| 180 | + // TODO: Uncomment when PaykitMobile bindings are available |
| 181 | + // fun toFfi(): LightningNetworkFfi = when (this) { |
| 182 | + // MAINNET -> LightningNetworkFfi.MAINNET |
| 183 | + // TESTNET -> LightningNetworkFfi.TESTNET |
| 184 | + // REGTEST -> LightningNetworkFfi.REGTEST |
| 185 | + // } |
| 186 | +} |
| 187 | + |
| 188 | +/** |
| 189 | + * Errors that can occur during Paykit operations. |
| 190 | + */ |
| 191 | +sealed class PaykitException(message: String) : Exception(message) { |
| 192 | + object NotInitialized : PaykitException("PaykitManager has not been initialized") |
| 193 | + data class ExecutorRegistrationFailed(val reason: String) : PaykitException("Failed to register executor: $reason") |
| 194 | + data class PaymentFailed(val reason: String) : PaykitException("Payment failed: $reason") |
| 195 | + object Timeout : PaykitException("Operation timed out") |
| 196 | + data class Unknown(val reason: String) : PaykitException("Unknown error: $reason") |
| 197 | +} |
0 commit comments