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.