From 39c3e128fb2a6dc93371c4b71ee2149747b8a0f8 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Sat, 24 Oct 2026 07:21:57 +0200 Subject: [PATCH] feat(wallet): an ancestor funds a guarded wallet two past its current address (Phase 1) GroupWalletState.fundingAddress picks the lowest unused, unreserved receive index from current + ADDRESSES_AHEAD up to the end of the watched window, and GroupSpend.claimedFundingIndices reads what open fundings already hold. Co-Authored-By: Claude Opus 5.5 --- .../compose/wallet/GroupOnchainWallet.kt | 29 +++++++ .../bennu/compose/wallet/GroupSpend.kt | 13 ++++ .../wallet/GroupOnchainWalletJvmTest.kt | 76 +++++++++++++++++++ 3 files changed, 118 insertions(+) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupOnchainWallet.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupOnchainWallet.kt index 6b51bf65..b7294d5e 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupOnchainWallet.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupOnchainWallet.kt @@ -230,6 +230,35 @@ data class GroupWalletState( } } + /** + * The receive address an ancestor pays when it funds this wallet: the lowest + * unused index from [GroupOnchainWallet.ADDRESSES_AHEAD] past the current one + * that [reserved] -- the indices open fundings already pay -- does not hold, + * and that the wallet watches. Null before Electrum has answered, for the + * reason [receiveAddress] is, and when every such index is held. + * + * Not the current address: the wallet's own members are handing that one + * out, and every device computes the same one. Starting at the top of the + * address list's window and going up keeps an ancestor out of their way. + * See `docs/guarded-wallet-funding.md`, D2. + */ + fun fundingAddress(reserved: Set = emptySet()): GroupAddress? { + val current = receive.firstUnusedDerivedAddress?.second?.index ?: return null + val window = (current + GroupOnchainWallet.ADDRESSES_AHEAD) until (current + GroupOnchainWallet.RECEIVE_LOOKAHEAD.toInt()) + val (address, index) = receive.addresses.entries + .filterNot { it.value.alreadyUsed } + .mapNotNull { (address, addressState) -> addressState.meta.indexOrNull?.let { address to it } } + .filter { (_, index) -> index in window && index.toLong() !in reserved } + .minByOrNull { (_, index) -> index } + ?: return null + return GroupAddress( + derivation = GroupWalletDerivation.derive(account, GroupWalletDerivation.RECEIVE, index.toLong()), + address = address, + status = GroupAddress.Status.NEXT, + balance = Satoshi(0), + ) + } + /** Whether Electrum has answered for any change address yet: before it has, there is no next one to name. */ val changeKnown: Boolean get() = change.addresses.values.any { !it.alreadyUsed } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupSpend.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupSpend.kt index 22204027..77eeb3e5 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupSpend.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupSpend.kt @@ -298,6 +298,19 @@ object GroupSpend { .mapNotNull { path -> path.keyPath.path.takeIf { it.size == 2 && it[0] == GroupWalletDerivation.CHANGE }?.get(1) } .toSet() + /** + * The receive indices under [account] -- a descendant's guarded wallet -- + * that [psbt]'s outputs claim to fund: what an open funding holds, so the + * next one pays a different address. Claims, as [claimedChangeIndices] are, + * for the same reason. See `docs/guarded-wallet-funding.md`. + */ + fun claimedFundingIndices(psbt: Psbt, account: GroupWalletDerivation.Account): Set = + psbt.outputs + .flatMap { it.taprootDerivationPaths.values } + .filter { isOurs(it, account) } + .mapNotNull { path -> path.keyPath.path.takeIf { it.size == 2 && it[0] == GroupWalletDerivation.RECEIVE }?.get(1) } + .toSet() + /** * The one derivation under [account] a PSBT field names, or null when it names * none, several, or one this wallet will not walk. diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/wallet/GroupOnchainWalletJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/wallet/GroupOnchainWalletJvmTest.kt index 61150c11..d1915e8b 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/wallet/GroupOnchainWalletJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/wallet/GroupOnchainWalletJvmTest.kt @@ -7,6 +7,7 @@ import fr.acinq.bitcoin.Chain import fr.acinq.bitcoin.PrivateKey import fr.acinq.bitcoin.Satoshi import fr.acinq.lightning.blockchain.electrum.HeaderSubscriptionResponse +import fr.acinq.lightning.blockchain.electrum.WalletState import fr.acinq.secp256k1.Hex import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.Dispatchers @@ -242,4 +243,79 @@ class GroupOnchainWalletJvmTest { assertEquals(Satoshi(7_000), used.balance) assertEquals(before.listed(), state.listed()) } + + @Test + fun `a fresh wallet is funded two past the current address`() { + val wallet = GroupOnchainWallet(account, FakeElectrum(), scope) + + val state = eventually { wallet.state.first { it.receive.addresses.size == GroupOnchainWallet.RECEIVE_LOOKAHEAD.toInt() } } + val funding = state.fundingAddress()!! + assertEquals(receive(2), funding.derivation) + assertEquals(receive(2).address(Chain.Regtest), funding.address) + assertEquals(GroupAddress.Status.NEXT, funding.status) + } + + @Test + fun `an open funding holds its address and the window ends where the watching does`() { + val wallet = GroupOnchainWallet(account, FakeElectrum(), scope) + val lookAhead = GroupOnchainWallet.RECEIVE_LOOKAHEAD.toLong() + + val state = eventually { wallet.state.first { it.receive.addresses.size == lookAhead.toInt() } } + assertEquals(receive(3), state.fundingAddress(setOf(2L))!!.derivation) + val held = (GroupOnchainWallet.ADDRESSES_AHEAD.toLong() until lookAhead).toSet() + assertEquals(receive(lookAhead - 1), state.fundingAddress(held - (lookAhead - 1))!!.derivation) + assertNull(state.fundingAddress(held), "past the last watched address the child would not see the payment") + } + + @Test + fun `being paid moves the funding address on`() { + val electrum = FakeElectrum() + val wallet = GroupOnchainWallet(account, electrum, scope) + + eventually { wallet.state.first { it.receiveAddress != null } } + eventually { electrum.pay(receive(0).pubkeyScript, Satoshi(25_000)) } + + val state = eventually { wallet.state.first { it.receiveAddress == receive(1).address(Chain.Regtest) } } + assertEquals(receive(3), state.fundingAddress()!!.derivation) + } + + @Test + fun `a paid address above the current one is never funded`() { + val electrum = FakeElectrum() + val wallet = GroupOnchainWallet(account, electrum, scope) + + eventually { wallet.state.first { it.receiveAddress != null } } + eventually { electrum.pay(receive(4).pubkeyScript, Satoshi(10_000)) } + + val state = eventually { wallet.state.first { it.receiveAddresses?.any { it.status == GroupAddress.Status.USED } == true } } + assertEquals(receive(0).address(Chain.Regtest), state.receiveAddress) + assertEquals(receive(2), state.fundingAddress()!!.derivation) + assertEquals(receive(3), state.fundingAddress(setOf(2L))!!.derivation) + assertEquals(receive(5), state.fundingAddress(setOf(2L, 3L))!!.derivation) + } + + @Test + fun `no funding address before Electrum has answered`() { + val state = GroupWalletState(account, WalletState.empty, WalletState.empty, tipHeight = null) + assertNull(state.fundingAddress()) + } + + @Test + fun `every funding address is one the wallet watches`() { + val electrum = FakeElectrum() + val wallet = GroupOnchainWallet(account, electrum, scope) + + eventually { wallet.state.first { it.receiveAddress != null } } + eventually { electrum.pay(receive(0).pubkeyScript, Satoshi(1_000)) } + + val state = eventually { wallet.state.first { it.receiveAddress == receive(1).address(Chain.Regtest) } } + val watched = state.receive.addresses.keys + var reserved = emptySet() + while (true) { + val funding = state.fundingAddress(reserved) ?: break + assertTrue(funding.address in watched, "${funding.derivation.index} is funded but not watched") + reserved = reserved + funding.derivation.index + } + assertEquals(GroupOnchainWallet.RECEIVE_LOOKAHEAD.toInt() - GroupOnchainWallet.ADDRESSES_AHEAD, reserved.size) + } }