diff --git a/src/commonMain/kotlin/fr/acinq/lightning/blockchain/electrum/ElectrumMiniWallet.kt b/src/commonMain/kotlin/fr/acinq/lightning/blockchain/electrum/ElectrumMiniWallet.kt index b99f34486..ef839160b 100644 --- a/src/commonMain/kotlin/fr/acinq/lightning/blockchain/electrum/ElectrumMiniWallet.kt +++ b/src/commonMain/kotlin/fr/acinq/lightning/blockchain/electrum/ElectrumMiniWallet.kt @@ -165,7 +165,14 @@ class ElectrumMiniWallet( * Depending on the status of the electrum connection, the subscription may or may not be sent to a server. * It is the responsibility of the caller to resubscribe on reconnection. */ - suspend fun subscribe(bitcoinAddress: String): Pair? { + suspend fun subscribe(scriptHash: ByteVector32, bitcoinAddress: String) { + kotlin.runCatching { client.startScriptHashSubscription(scriptHash) }.map { response -> + logger.info { "subscribed to address=$bitcoinAddress scriptHash=$scriptHash" } + _walletStateFlow.value = _walletStateFlow.value.processSubscriptionResponse(response) + } + } + + fun computeScriptHash(bitcoinAddress: String): ByteVector32? { return when (val result = Bitcoin.addressToPublicKeyScript(chainHash, bitcoinAddress)) { is AddressToPublicKeyScriptResult.Failure -> { logger.error { "cannot subscribe to $bitcoinAddress ($result)" } @@ -173,12 +180,7 @@ class ElectrumMiniWallet( } is AddressToPublicKeyScriptResult.Success -> { val pubkeyScript = ByteVector(Script.write(result.script)) - val scriptHash = ElectrumClient.computeScriptHash(pubkeyScript) - kotlin.runCatching { client.startScriptHashSubscription(scriptHash) }.map { response -> - logger.info { "subscribed to address=$bitcoinAddress pubkeyScript=$pubkeyScript scriptHash=$scriptHash" } - _walletStateFlow.value = _walletStateFlow.value.processSubscriptionResponse(response) - } - scriptHash to bitcoinAddress + return ElectrumClient.computeScriptHash(pubkeyScript) } } } @@ -197,7 +199,7 @@ class ElectrumMiniWallet( when (it) { is WalletCommand.Companion.ElectrumConnected -> { logger.mdcinfo { "electrum connected" } - scriptHashes.values.forEach { scriptHash -> subscribe(scriptHash) } + scriptHashes.forEach { (scriptHash, address) -> subscribe(scriptHash, address) } } is WalletCommand.Companion.ElectrumNotification -> { if (it.msg is ScriptHashSubscriptionResponse) { @@ -205,9 +207,12 @@ class ElectrumMiniWallet( } } is WalletCommand.Companion.AddAddress -> { - logger.mdcinfo { "adding new address=${it.bitcoinAddress}" } - subscribe(it.bitcoinAddress)?.let { - scriptHashes = scriptHashes + it + computeScriptHash(it.bitcoinAddress)?.let { scriptHash -> + if (!scriptHashes.containsKey(scriptHash)) { + logger.mdcinfo { "adding new address=${it.bitcoinAddress}" } + scriptHashes = scriptHashes + (scriptHash to it.bitcoinAddress) + subscribe(scriptHash, it.bitcoinAddress) + } } } }