-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rotate taproot swap-in addresses (#584)
Addresses are generated one by one, as soon as we detect that the last generated address has been used. There is no lookahead. Scanning 'synchronous' (not really synchronous, but we rely the least possible on the mailbox). This makes the `window` disappear because we are scanning addresses one by one. Some cleanup by extracting wallets outside of `Peer`. --------- Co-authored-by: sstone <fabrice.drouin@acinq.fr>
- Loading branch information
Showing
19 changed files
with
518 additions
and
127 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
32 changes: 32 additions & 0 deletions
32
src/commonMain/kotlin/fr/acinq/lightning/blockchain/electrum/FinalWallet.kt
This file contains 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,32 @@ | ||
package fr.acinq.lightning.blockchain.electrum | ||
|
||
import fr.acinq.bitcoin.Bitcoin | ||
import fr.acinq.lightning.crypto.KeyManager | ||
import fr.acinq.lightning.logging.LoggerFactory | ||
import fr.acinq.lightning.logging.info | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.distinctUntilChangedBy | ||
import kotlinx.coroutines.launch | ||
|
||
class FinalWallet( | ||
chain: Bitcoin.Chain, | ||
finalWalletKeys: KeyManager.Bip84OnChainKeys, | ||
electrum: IElectrumClient, | ||
scope: CoroutineScope, | ||
loggerFactory: LoggerFactory | ||
) { | ||
private val logger = loggerFactory.newLogger(this::class) | ||
|
||
val wallet = ElectrumMiniWallet(chain.chainHash, electrum, scope, logger) | ||
val finalAddress: String = finalWalletKeys.address(addressIndex = 0L).also { wallet.addAddress(it) } | ||
|
||
init { | ||
scope.launch { | ||
wallet.walletStateFlow | ||
.distinctUntilChangedBy { it.totalBalance } | ||
.collect { wallet -> | ||
logger.info { "${wallet.totalBalance} available on final wallet with ${wallet.utxos.size} utxos" } | ||
} | ||
} | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
src/commonMain/kotlin/fr/acinq/lightning/blockchain/electrum/SwapInWallet.kt
This file contains 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,44 @@ | ||
package fr.acinq.lightning.blockchain.electrum | ||
|
||
import fr.acinq.bitcoin.Bitcoin | ||
import fr.acinq.lightning.crypto.KeyManager | ||
import fr.acinq.lightning.logging.LoggerFactory | ||
import fr.acinq.lightning.logging.info | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.MutableStateFlow | ||
import kotlinx.coroutines.flow.distinctUntilChanged | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.flow.map | ||
import kotlinx.coroutines.launch | ||
|
||
class SwapInWallet( | ||
chain: Bitcoin.Chain, | ||
swapInKeys: KeyManager.SwapInOnChainKeys, | ||
electrum: IElectrumClient, | ||
scope: CoroutineScope, | ||
loggerFactory: LoggerFactory | ||
) { | ||
private val logger = loggerFactory.newLogger(this::class) | ||
|
||
val wallet = ElectrumMiniWallet(chain.chainHash, electrum, scope, logger) | ||
|
||
val legacySwapInAddress: String = swapInKeys.legacySwapInProtocol.address(chain) | ||
.also { wallet.addAddress(it) } | ||
val swapInAddressFlow = MutableStateFlow<Pair<String, Int>?>(null) | ||
.also { wallet.addAddressGenerator(generator = { index -> swapInKeys.getSwapInProtocol(index).address(chain) }) } | ||
|
||
init { | ||
scope.launch { | ||
// address rotation | ||
wallet.walletStateFlow | ||
.map { it.lastDerivedAddress } | ||
.filterNotNull() | ||
.distinctUntilChanged() | ||
.collect { (address, derived) -> | ||
logger.info { "setting current swap-in address=$address index=${derived.index}" } | ||
swapInAddressFlow.emit(address to derived.index) | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.