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 <noreply@anthropic.com>
This commit is contained in:
Kgothatso NgakoandClaude Opus 5.5 committed 2026-10-24 07:21:57 +02:00
1 parent 4c6cc9d1a0
commit 39c3e128fb
3 files changed
+118

No files matched your search

@@ -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<Long> = 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 }
@@ -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<Long> =
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.
@@ -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<Long>()
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)
}
}