-
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.
- Loading branch information
Showing
6 changed files
with
137 additions
and
62 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
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.lightning.NodeParams | ||
import fr.acinq.lightning.crypto.KeyManager | ||
import kotlinx.coroutines.CoroutineScope | ||
import kotlinx.coroutines.flow.distinctUntilChangedBy | ||
import kotlinx.coroutines.launch | ||
import org.kodein.log.LoggerFactory | ||
import org.kodein.log.newLogger | ||
|
||
class FinalWallet( | ||
chain: NodeParams.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" } | ||
} | ||
} | ||
} | ||
} |
51 changes: 51 additions & 0 deletions
51
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,51 @@ | ||
package fr.acinq.lightning.blockchain.electrum | ||
|
||
import fr.acinq.lightning.NodeParams | ||
import fr.acinq.lightning.blockchain.electrum.WalletState.Companion.indexOrNull | ||
import fr.acinq.lightning.crypto.KeyManager | ||
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 | ||
import org.kodein.log.LoggerFactory | ||
import org.kodein.log.newLogger | ||
|
||
class SwapInWallet( | ||
chain: NodeParams.Chain, | ||
swapInKeys: KeyManager.SwapInOnChainKeys, | ||
electrum: IElectrumClient, | ||
addressGenerationWindow: Int, | ||
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) }, window = addressGenerationWindow) } | ||
|
||
init { | ||
scope.launch { | ||
// address rotation | ||
wallet.walletStateFlow.map { it -> | ||
// take the first unused address with the lowest index | ||
it.addresses | ||
.filter { it.value.unspent.isEmpty() } | ||
.mapNotNull { (key, value) -> value.meta.indexOrNull?.let { key to it } } | ||
.minByOrNull { it.second } | ||
} | ||
.filterNotNull() | ||
.distinctUntilChanged() | ||
.collect { (address, index) -> | ||
logger.info { "setting current swap-in address=$address index=$index" } | ||
swapInAddressFlow.emit(address to index) | ||
} | ||
} | ||
} | ||
|
||
} |
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
29 changes: 29 additions & 0 deletions
29
src/commonTest/kotlin/fr/acinq/lightning/blockchain/electrum/SwapInWalletTestsCommon.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,29 @@ | ||
package fr.acinq.lightning.blockchain.electrum | ||
|
||
import fr.acinq.bitcoin.MnemonicCode | ||
import fr.acinq.lightning.NodeParams | ||
import fr.acinq.lightning.crypto.LocalKeyManager | ||
import fr.acinq.lightning.tests.TestConstants | ||
import fr.acinq.lightning.tests.utils.LightningTestSuite | ||
import fr.acinq.lightning.tests.utils.runSuspendTest | ||
import fr.acinq.lightning.utils.toByteVector | ||
import kotlinx.coroutines.flow.filterNotNull | ||
import kotlinx.coroutines.flow.first | ||
import org.kodein.log.LoggerFactory | ||
import kotlin.test.Test | ||
import kotlin.test.assertEquals | ||
import kotlin.time.Duration.Companion.seconds | ||
|
||
class SwapInWalletTestsCommon : LightningTestSuite() { | ||
|
||
@Test | ||
fun `swap-in wallet test`() = runSuspendTest(timeout = 15000.seconds) { | ||
val mnemonics = "abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about".split(" ") | ||
val keyManager = LocalKeyManager(MnemonicCode.toSeed(mnemonics, "").toByteVector(), NodeParams.Chain.Testnet, TestConstants.aliceSwapInServerXpub) | ||
val client = connectToTestnetServer() | ||
val wallet = SwapInWallet(NodeParams.Chain.Testnet, keyManager.swapInOnChainWallet, client, addressGenerationWindow = 3, this, LoggerFactory.default) | ||
|
||
// addresses 0 to 5 have funds on them, the current address is the 6th | ||
assertEquals(6, wallet.swapInAddressFlow.filterNotNull().first().second) | ||
} | ||
} |