From 5c77853cfa7b68bb386a3da200c5dcdd51613e02 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Fri, 23 Oct 2026 12:22:32 +0200 Subject: [PATCH] feat(wallet): change goes to an address the group can find (Phase 3) Two open spends with change both paid to the first unused change address, since a spend waiting for signatures has not used it yet. Change indices are now reserved like coins: reservedChangeIndices reads the claims of every open spend, and the builder takes the lowest watched, unused, unheld index. It chooses among generated addresses only, so change never lands outside the window; CHANGE_LOOKAHEAD rises to 10 and past it a spend with change returns TooManyOpen. The change branch not having answered is FindingChange rather than NothingToSpend, and change too small to send back is reported as Built.absorbed and said in the preview. Co-Authored-By: Claude Opus 5.5 --- .../composeResources/values/strings.xml | 2 + .../DatabaseFrostSigningRepository.kt | 8 ++ .../compose/managers/FrostSigningManager.kt | 24 +++- .../repository/FrostSigningRepository.kt | 5 + .../compose/ui/composable/GroupSendScreen.kt | 21 +++- .../ui/view/model/GroupSendViewModel.kt | 3 +- .../compose/ui/view/state/GroupSendUIState.kt | 2 + .../compose/wallet/GroupOnchainWallet.kt | 27 ++++- .../bennu/compose/wallet/GroupSpend.kt | 14 +++ .../bennu/compose/wallet/GroupSpendBuilder.kt | 29 ++++- .../compose/wallet/GroupSpendBuilderTest.kt | 51 +++++++- .../managers/GroupSpendSigningJvmTest.kt | 85 +++++++++++++ .../wallet/GroupOnchainWalletJvmTest.kt | 19 ++- docs/group-send-an-amount.md | 113 ++++++++++-------- 14 files changed, 327 insertions(+), 76 deletions(-) diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 0eb11cab..64106570 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -657,6 +657,8 @@ Sats are whole numbers: there is nothing smaller than a sat to send. That is more sats than there will ever be. That isn't an amount in sats. + %1$s sats left over are too few to come back as change, and go to the fee. + The group has too many spends with change waiting for signatures. Wait for one to settle, or send everything. Couldn't ask the group to send. Sending needs a share of the group's key, and a wallet of your own on this profile to see the group's coins with. Nothing leaves the wallet until a quorum of members has seen this spend and signed it. diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/repository/DatabaseFrostSigningRepository.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/repository/DatabaseFrostSigningRepository.kt index 4b211f6b..d469f05b 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/repository/DatabaseFrostSigningRepository.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/database/repository/DatabaseFrostSigningRepository.kt @@ -21,6 +21,7 @@ import com.machankura.bennu.compose.database.model.intermdiate.FrostSignerTally import com.machankura.bennu.compose.database.model.intermdiate.LocalChatRoom import com.machankura.bennu.compose.database.model.intermdiate.LocalFrostSigningSession import com.machankura.bennu.compose.managers.FrostSigningManager +import com.machankura.bennu.compose.managers.GroupWalletDerivation import com.machankura.bennu.compose.nostr.dkg.DkgRitualEvents import com.machankura.bennu.compose.nostr.frost.FrostSigningEvents import com.machankura.bennu.compose.repository.FrostSigningRepository @@ -161,6 +162,13 @@ class DatabaseFrostSigningRepository( emptySet() } + override suspend fun reservedChangeIndices(chatRoomId: String, account: GroupWalletDerivation.Account): Set = try { + FrostSigningManager.reservedChangeIndices(database, chatRoomId, account) + } catch (e: Throwable) { + logger.e("Error reading the change addresses reserved in $chatRoomId", e) + emptySet() + } + override fun observeGroupTransactions(chatRoomId: String): Flow> = database.groupTransactionDao().observeByChatRoomId(chatRoomId) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/FrostSigningManager.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/FrostSigningManager.kt index e0e43cf1..b56c419d 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/FrostSigningManager.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/managers/FrostSigningManager.kt @@ -866,10 +866,30 @@ object FrostSigningManager { * the coins are still unspent as far as Electrum can say. */ suspend fun reservedOutPoints(database: BennuDatabase, chatRoomId: String): Set = + openSpends(database, chatRoomId) + .flatMap { psbt -> psbt.global.tx.txIn.map { it.outPoint } } + .toSet() + + /** + * The change addresses a room's open spends pay back to, under [account], + * which a new spend should not pay to as well. Held by the same sessions as + * [reservedOutPoints], for the same reason: until a spend is on chain, its + * change address reads as unused, and the next spend would pick it again. + */ + suspend fun reservedChangeIndices( + database: BennuDatabase, + chatRoomId: String, + account: GroupWalletDerivation.Account, + ): Set = + openSpends(database, chatRoomId) + .flatMap { GroupSpend.claimedChangeIndices(it, account) } + .toSet() + + /** The PSBT of every spend in the room whose session has not failed. */ + private suspend fun openSpends(database: BennuDatabase, chatRoomId: String): List = database.groupTransactionDao().getByChatRoomId(chatRoomId) .filter { database.frostSigningSessionDao().getSessionById(it.frostSigningSessionId)?.stage != FrostSigningStage.FAILED } - .flatMap { transaction -> GroupSpend.decode(transaction.psbt)?.global?.tx?.txIn?.map { it.outPoint }.orEmpty() } - .toSet() + .mapNotNull { GroupSpend.decode(it.psbt) } /** * A spend seen confirmed: recorded, and every other spend of the same coins diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/repository/FrostSigningRepository.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/repository/FrostSigningRepository.kt index 95b0df2c..34f0b0ef 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/repository/FrostSigningRepository.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/repository/FrostSigningRepository.kt @@ -1,6 +1,7 @@ package com.machankura.bennu.compose.repository import com.machankura.bennu.compose.database.model.GroupTransaction +import com.machankura.bennu.compose.managers.GroupWalletDerivation import com.machankura.bennu.compose.wallet.GroupSpend import fr.acinq.bitcoin.Chain import fr.acinq.bitcoin.OutPoint @@ -246,6 +247,9 @@ interface FrostSigningRepository { /** The coins the room's open spends are already spending. */ suspend fun reservedOutPoints(chatRoomId: String): Set + /** The change indices under [account] the room's open spends already pay back to. */ + suspend fun reservedChangeIndices(chatRoomId: String, account: GroupWalletDerivation.Account): Set + /** Every spend the room has proposed, newest first, as it moves from proposed to confirmed. */ fun observeGroupTransactions(chatRoomId: String): Flow> @@ -326,6 +330,7 @@ interface FrostSigningRepository { override suspend fun checkedSpend(sessionId: String): GroupSpend.Checked? = null override suspend fun reservedOutPoints(chatRoomId: String): Set = emptySet() + override suspend fun reservedChangeIndices(chatRoomId: String, account: GroupWalletDerivation.Account): Set = emptySet() override fun observeGroupTransactions(chatRoomId: String): Flow> = flowOf(emptyList()) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt index 5bb5195c..934c81ae 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupSendScreen.kt @@ -57,6 +57,8 @@ import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent import com.machankura.bennu.compose.repository.ChatRepository import com.machankura.bennu.compose.repository.FrostSigningRepository import bennu.composeapp.generated.resources.sats_are_whole_numbers +import bennu.composeapp.generated.resources.s_sats_left_over_go_to_the_fee +import bennu.composeapp.generated.resources.too_many_spends_are_open_with_change import bennu.composeapp.generated.resources.that_is_more_sats_than_there_will_ever_be import bennu.composeapp.generated.resources.that_isn_t_an_amount_in_sats import com.machankura.bennu.compose.text.GroupedSats @@ -208,12 +210,13 @@ fun GroupSendScreen( label = { Text(stringResource(Res.string.feerate_in_sats_per_byte)) }, ) - if (state.walletState == null) { - LoadingDataIndicator(fillScreen = false, text = stringResource(Res.string.finding_the_group_s_coins)) - } - + // Still finding change is still finding coins: neither is anything to correct. val preview = groupSendViewModel.preview() - preview?.let { SendPreview(it, chain) } + if (state.walletState == null || preview == GroupSpendBuilder.Result.FindingChange) { + LoadingDataIndicator(fillScreen = false, text = stringResource(Res.string.finding_the_group_s_coins)) + } else { + preview?.let { SendPreview(it, chain) } + } Text( text = stringResource(Res.string.nothing_leaves_until_a_quorum_signs), @@ -266,6 +269,13 @@ private fun SendPreview(result: GroupSpendBuilder.Result, chain: Chain) { color = MaterialTheme.colorScheme.onSurfaceVariant, ) } + if (result.absorbed.sat > 0) { + Text( + text = stringResource(Res.string.s_sats_left_over_go_to_the_fee, result.absorbed.grouped()), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + } } else -> Text( @@ -274,6 +284,7 @@ private fun SendPreview(result: GroupSpendBuilder.Result, chain: Chain) { stringResource(Res.string.that_isn_t_an_address_on_s, GroupWalletStatementEvent.chainName(chain)) GroupSpendBuilder.Result.NothingToSpend -> stringResource(Res.string.the_group_has_nothing_confirmed_to_spend) GroupSpendBuilder.Result.NotEnough -> stringResource(Res.string.the_group_doesn_t_have_that_much_confirmed) + GroupSpendBuilder.Result.TooManyOpen -> stringResource(Res.string.too_many_spends_are_open_with_change) is GroupSpendBuilder.Result.OverCap -> stringResource(Res.string.while_the_wallet_is_new_at_most_s_sats, result.cap.grouped()) is GroupSpendBuilder.Result.InvalidAmount -> when (result.input) { SatsInput.NotWhole -> stringResource(Res.string.sats_are_whole_numbers) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupSendViewModel.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupSendViewModel.kt index b77982c1..8ea223bd 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupSendViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupSendViewModel.kt @@ -78,6 +78,7 @@ class GroupSendViewModel( wallet = wallet, walletState = null, reserved = frostSigningRepository.reservedOutPoints(chatRoomId), + reservedChange = frostSigningRepository.reservedChangeIndices(chatRoomId, wallet.account), ) groupSendUIState = ready @@ -123,7 +124,7 @@ class GroupSendViewModel( val built = if (amount == null) { GroupSpendBuilder.sendAll(walletState, address, feerate, state.reserved) } else { - GroupSpendBuilder.send(walletState, address, amount, feerate, state.reserved) + GroupSpendBuilder.send(walletState, address, amount, feerate, state.reserved, state.reservedChange) } return GroupSpendBuilder.capped(built, GroupWalletRollout.capFor(chain)) } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupSendUIState.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupSendUIState.kt index b1736a00..6b2fef1a 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupSendUIState.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupSendUIState.kt @@ -16,6 +16,8 @@ sealed interface GroupSendUIState { val walletState: GroupWalletState?, /** The coins open spends already hold, which a new one leaves alone. */ val reserved: Set, + /** The change indices open spends already pay back to, which a new one pays past. */ + val reservedChange: Set = emptySet(), ): GroupSendUIState /** A room this device cannot propose from -- no wallet, no share, or no node. */ 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 f728c5e0..766023ca 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 @@ -87,8 +87,13 @@ class GroupOnchainWallet( */ const val RECEIVE_LOOKAHEAD: UInt = 10u - /** Change addresses are only ever chosen by a spend, one at a time. */ - const val CHANGE_LOOKAHEAD: UInt = 3u + /** + * Change addresses watched, counting the first unused one. Each open + * spend with change holds one until it confirms, and several members + * can each have one open, so this is how many such spends a group can + * have at once before the next is told to wait. + */ + const val CHANGE_LOOKAHEAD: UInt = 10u } } @@ -140,8 +145,22 @@ data class GroupWalletState( */ val receiveAddress: String? get() = receive.firstUnusedDerivedAddress?.first - /** The change index a spend should pay back to, or null until Electrum has answered. */ - val nextChangeIndex: Long? get() = change.firstUnusedDerivedAddress?.second?.index?.toLong() + /** 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 } + + /** + * The change index a spend should pay back to: the lowest this wallet + * watches that is unused and not in [reserved] -- the indices open spends + * already pay to. Null when there is none: before Electrum has answered, + * or when open spends hold every one inside the watched window, past which + * change would land on an address nobody is watching. + */ + fun nextChangeIndex(reserved: Set = emptySet()): Long? = + change.addresses.values + .filterNot { it.alreadyUsed } + .mapNotNull { it.meta.indexOrNull?.toLong() } + .filterNot { it in reserved } + .minOrNull() /** How many confirmations [utxo] has: 0 in the mempool, and 0 until the tip is known. */ fun confirmations(utxo: GroupUtxo): Int { 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 3d4c7219..f27490c5 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 @@ -194,6 +194,20 @@ object GroupSpend { return Result.Ok(Checked(account = account, psbt = psbt, inputs = inputs, outputs = outputs, fee = fee, weight = weight)) } + /** + * The change indices under [account] that [psbt]'s outputs claim: what an + * open spend holds, so the next one pays its change somewhere else. Claims, + * not checked derivations -- a spend this device would refuse still keeps + * its index from being handed out twice, and holding one too many costs + * nothing. + */ + fun claimedChangeIndices(psbt: Psbt, account: GroupWalletDerivation.Account): Set = + psbt.outputs + .flatMap { it.taprootDerivationPaths.values } + .filter { it.masterKeyFingerprint == account.fingerprint } + .mapNotNull { path -> path.keyPath.path.takeIf { it.size == 2 && it[0] == GroupWalletDerivation.CHANGE }?.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/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupSpendBuilder.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupSpendBuilder.kt index f0e3babf..b27368cc 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupSpendBuilder.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/wallet/GroupSpendBuilder.kt @@ -28,9 +28,20 @@ object GroupSpendBuilder { val DUST: Satoshi = Satoshi(546) sealed interface Result { - data class Built(val psbt: Psbt, val amount: Satoshi, val fee: Satoshi, val change: Satoshi?) : Result + data class Built( + val psbt: Psbt, + val amount: Satoshi, + val fee: Satoshi, + val change: Satoshi?, + /** What [fee] holds beyond the feerate: change too small to send back, paid to the miner instead. */ + val absorbed: Satoshi = Satoshi(0), + ) : Result data object NotAnAddress : Result data object NothingToSpend : Result + /** Electrum has not yet said which change addresses are used, so there is none to name. */ + data object FindingChange : Result + /** Open spends already pay change to every change address the wallet watches. */ + data object TooManyOpen : Result data object NotEnough : Result data object BelowDust : Result /** A bump has to pay more than the spend it replaces. */ @@ -65,7 +76,8 @@ object GroupSpendBuilder { /** * [amount] to [address], largest coins first, with what is left over paid - * back to the next change address -- or to fees, when it would be dust. + * back to the next change address no open spend holds -- or to fees, when + * it would be dust. */ fun send( state: GroupWalletState, @@ -73,14 +85,16 @@ object GroupSpendBuilder { amount: Satoshi, feeratePerVbyte: Long, reserved: Set, + reservedChange: Set = emptySet(), ): Result { val script = scriptOf(state, address) ?: return Result.NotAnAddress if (amount < DUST) return Result.BelowDust - val changeIndex = state.nextChangeIndex ?: return Result.NothingToSpend - val change = GroupWalletDerivation.derive(state.account, GroupWalletDerivation.CHANGE, changeIndex) - val coins = spendable(state, reserved).sortedByDescending { it.amount.sat }.ifEmpty { return Result.NothingToSpend } + if (!state.changeKnown) return Result.FindingChange + val changeIndex = state.nextChangeIndex(reservedChange) ?: return Result.TooManyOpen + val change = GroupWalletDerivation.derive(state.account, GroupWalletDerivation.CHANGE, changeIndex) + val chosen = mutableListOf() for (coin in coins) { chosen += coin @@ -97,7 +111,10 @@ object GroupSpendBuilder { val withoutChange = feeOf(chosen, listOf(script), feeratePerVbyte) if (total - amount >= withoutChange) { val tx = transaction(chosen, listOf(TxOut(amount, script))) - return Result.Built(psbtOf(state, tx, chosen, emptyMap()), amount, total - amount, change = null) + return Result.Built( + psbtOf(state, tx, chosen, emptyMap()), amount, total - amount, change = null, + absorbed = total - amount - withoutChange, + ) } } return Result.NotEnough diff --git a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/wallet/GroupSpendBuilderTest.kt b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/wallet/GroupSpendBuilderTest.kt index 6431d0f7..d7dcd885 100644 --- a/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/wallet/GroupSpendBuilderTest.kt +++ b/composeApp/src/commonTest/kotlin/com/machankura/bennu/compose/wallet/GroupSpendBuilderTest.kt @@ -46,9 +46,12 @@ class GroupSpendBuilderTest { ) } - private fun state(vararg coins: Pair): GroupWalletState { - val unusedChange = GroupWalletDerivation.derive(account, GroupWalletDerivation.CHANGE, 0) - val change = WalletState(mapOf(unusedChange.address(Chain.Regtest) to WalletState.AddressState(WalletState.AddressMeta.Derived(0), false, emptyList()))) + /** [coins] on the receive branch, and [changeWindow] unused change addresses watched -- none, before Electrum has answered. */ + private fun state(vararg coins: Pair, changeWindow: Int = 1): GroupWalletState { + val change = WalletState((0 until changeWindow).associate { index -> + GroupWalletDerivation.derive(account, GroupWalletDerivation.CHANGE, index.toLong()).address(Chain.Regtest) to + WalletState.AddressState(WalletState.AddressMeta.Derived(index), false, emptyList()) + }) return GroupWalletState(account, WalletState(coins.toMap()), change, tipHeight = 110) } @@ -92,14 +95,54 @@ class GroupSpendBuilderTest { assertEquals(GroupWalletDerivation.CHANGE, change.change!!.change) assertEquals(built.change, change.txOut.amount) assertEquals(Satoshi(30_000), spend.amountSent) + assertEquals(Satoshi(0), built.absorbed, "with change, the fee is the feerate's and nothing more") + } + + @Test + fun `change goes past the addresses open spends already pay to`() { + val result = GroupSpendBuilder.send( + state(coin(0, 60_000), changeWindow = 3), destination, Satoshi(30_000), feerate, emptySet(), reservedChange = setOf(0L), + ) + val built = assertIs(result) + + assertEquals(1L, checked(result).outputs.single { it.isChange }.change!!.index) + assertEquals(setOf(1L), GroupSpend.claimedChangeIndices(built.psbt, account), "and the spend after it reads index 1 as held") + } + + @Test + fun `with every watched change address held, a spend with change waits`() { + val wallet = state(coin(0, 60_000), changeWindow = 2) + + assertIs( + GroupSpendBuilder.send(wallet, destination, Satoshi(30_000), feerate, emptySet(), reservedChange = setOf(0L, 1L)) + ) + assertIs( + GroupSpendBuilder.sendAll(wallet, destination, feerate, emptySet()), "send-all has no change to place" + ) + } + + @Test + fun `before the change branch has answered, the builder is finding change rather than empty-handed`() { + val wallet = state(coin(0, 60_000), changeWindow = 0) + + assertIs(GroupSpendBuilder.send(wallet, destination, Satoshi(30_000), feerate, emptySet())) + assertIs( + GroupSpendBuilder.send(state(changeWindow = 0), destination, Satoshi(30_000), feerate, emptySet()), + "no coins is no coins, whatever change is doing", + ) } @Test fun `change that would be dust goes to fees instead`() { - val spend = checked(GroupSpendBuilder.send(state(coin(0, 30_800)), destination, Satoshi(30_000), feerate, emptySet())) + val result = GroupSpendBuilder.send(state(coin(0, 30_800)), destination, Satoshi(30_000), feerate, emptySet()) + val spend = checked(result) assertTrue(spend.outputs.none { it.isChange }) assertEquals(Satoshi(800), spend.fee) + val atTheFeerate = Satoshi(feerate * ((spend.weight + 3) / 4)) + val absorbed = assertIs(result).absorbed + assertTrue(absorbed.sat > 0) + assertEquals(spend.fee, atTheFeerate + absorbed, "what the screen says went to the fee is all that did") } @Test diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupSpendSigningJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupSpendSigningJvmTest.kt index dfc9fc3d..2b7e6b0a 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupSpendSigningJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/managers/GroupSpendSigningJvmTest.kt @@ -18,6 +18,9 @@ import com.machankura.bennu.compose.nostr.dkg.DkgRitualEvents import com.machankura.bennu.compose.nostr.frost.FrostSigningEvents import com.machankura.bennu.compose.nostr.wallet.GroupWalletStatementEvent import com.machankura.bennu.compose.wallet.GroupSpend +import com.machankura.bennu.compose.text.ProposedSpend +import com.machankura.bennu.compose.wallet.GroupSpendBuilder +import com.machankura.bennu.compose.wallet.GroupWalletState import com.vitorpamplona.quartz.nip01Core.core.Event import com.vitorpamplona.quartz.nip01Core.core.HexKey import com.vitorpamplona.quartz.nip01Core.crypto.EventHasher @@ -37,11 +40,13 @@ import fr.acinq.bitcoin.TxOut import fr.acinq.bitcoin.crypto.frost.Frost import fr.acinq.bitcoin.crypto.frost.KeyMaterial import fr.acinq.bitcoin.psbt.Psbt +import fr.acinq.lightning.blockchain.electrum.WalletState import kotlinx.coroutines.runBlocking import kotlin.random.Random import kotlin.test.AfterTest import kotlin.test.Test import kotlin.test.assertEquals +import kotlin.test.assertIs import kotlin.test.assertNotEquals import kotlin.test.assertNotNull import kotlin.test.assertNull @@ -352,9 +357,89 @@ class GroupSpendSigningJvmTest { val coins = setOf(OutPoint(funding, 0), OutPoint(funding, 1)) assertEquals(coins, FrostSigningManager.reservedOutPoints(other.db, roomId)) + assertEquals(setOf(0L), FrostSigningManager.reservedChangeIndices(other.db, roomId, account), "and its change address") // Abandoned however it was -- the stage is all the reservation reads. other.db.frostSigningSessionDao().upsert(other.session(session.id)!!.copy(stage = FrostSigningStage.FAILED)) assertEquals(emptySet(), FrostSigningManager.reservedOutPoints(other.db, roomId), "a failed spend releases its coins") + assertEquals(emptySet(), FrostSigningManager.reservedChangeIndices(other.db, roomId, account), "and its change address") + } + + /** The wallet as Electrum would report it holding [funding], with [changeWindow] change addresses watched and unused. */ + private fun fundedState(changeWindow: Int = 3): GroupWalletState { + fun derived(branch: Long, index: Int) = GroupWalletDerivation.derive(account, branch, index.toLong()) + val receive = listOf(first, second).mapIndexed { index, derivation -> + val meta = WalletState.AddressMeta.Derived(index) + derivation.address(chain) to WalletState.AddressState( + meta, alreadyUsed = true, utxos = listOf(WalletState.Utxo(funding.txid, index, 100, funding, meta)), + ) + }.toMap() + val change = (0 until changeWindow).associate { index -> + derived(GroupWalletDerivation.CHANGE, index).address(chain) to + WalletState.AddressState(WalletState.AddressMeta.Derived(index), alreadyUsed = false, utxos = emptyList()) + } + return GroupWalletState(account, WalletState(receive), WalletState(change), tipHeight = 110) + } + + @Test + fun `an amount the form built is signed by the group, its change is the group's on every device, and it comes back`() = runBlocking { + val proposer = device(members[0], signerIndex = 0) + val other = device(members[1], signerIndex = 1) + openWallet(proposer, other) + + // A first spend is open and holds change index 0, so the form pays past it. + FrostSigningManager.proposeSpend(proposer.db, proposer.room, proposer.publicKey, psbt(), chain) + pump(proposer, other) + val heldChange = FrostSigningManager.reservedChangeIndices(proposer.db, roomId, account) + assertEquals(setOf(0L), heldChange) + + // The second spends coins the first does not hold: a fresh funding of its own. + val more = Transaction( + 2, listOf(TxIn(OutPoint(TxId(ByteVector32(ByteArray(32) { 0x43 })), 0), ByteVector.empty, 0xffffffffL)), + listOf(TxOut(Satoshi(80_000), first.pubkeyScript)), 0, + ) + val wallet = fundedState().let { state -> + val meta = WalletState.AddressMeta.Derived(0) + val receive = state.receive.addresses + (first.address(chain) to WalletState.AddressState( + meta, alreadyUsed = true, utxos = listOf(WalletState.Utxo(more.txid, 0, 100, more, meta)), + )) + state.copy(receive = WalletState(receive)) + } + val reserved = FrostSigningManager.reservedOutPoints(proposer.db, roomId) + val built = assertIs( + GroupSpendBuilder.send(wallet, ProposedSpend.addressOf(elsewhere, chain), Satoshi(30_000), 3, reserved, heldChange) + ) + + val session = FrostSigningManager.proposeSpend(proposer.db, proposer.room, proposer.publicKey, built.psbt, chain) + pump(proposer, other) + FrostSigningManager.approve(other.db, other.room, session.id) + pump(proposer, other) + + // On the device that did not build it: the change derives, so it is change. + val seen = assertNotNull(FrostSigningManager.checkedSpend(other.db, session.id)) + val changeOutput = seen.outputs.single { it.isChange } + assertEquals(1L, changeOutput.change!!.index, "past the index the open spend holds") + assertEquals(built.change, changeOutput.txOut.amount) + assertEquals(Satoshi(30_000), seen.amountSent) + assertEquals(setOf(0L, 1L), FrostSigningManager.reservedChangeIndices(other.db, roomId, account)) + + listOf(proposer, other).forEach { device -> + assertEquals(FrostSigningStage.COMPLETE, device.session(session.id)?.stage) + val signed = Transaction.read(assertNotNull(device.transaction(session.id)?.signedTransaction)) + signed.correctlySpends(mapOf(OutPoint(more, 0) to more.txOut[0]), ScriptFlags.STANDARD_SCRIPT_VERIFY_FLAGS) + } + + // Confirmed, the change is a coin the wallet counts, on the branch it watches. + val signed = Transaction.read(other.transaction(session.id)!!.signedTransaction!!) + val changeMeta = WalletState.AddressMeta.Derived(1) + val changeAddress = changeOutput.change!!.address(chain) + val after = wallet.copy( + receive = WalletState(wallet.receive.addresses - first.address(chain)), + change = WalletState(wallet.change.addresses + (changeAddress to WalletState.AddressState( + changeMeta, alreadyUsed = true, utxos = listOf(WalletState.Utxo(signed.txid, changeOutput.index, 111, signed, changeMeta)), + ))), + ) + assertEquals(changeOutput.change, after.derivationOf(changeOutput.txOut.publicKeyScript)) + assertEquals(Satoshi(40_000) + built.change!!, after.confirmedBalance, "the second funding coin, and the change") } } 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 769eaec8..62627d0c 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 @@ -124,10 +124,10 @@ class GroupOnchainWalletJvmTest { val electrum = FakeElectrum() val wallet = GroupOnchainWallet(account, electrum, scope) - eventually { wallet.state.first { it.nextChangeIndex == 0L } } + eventually { wallet.state.first { it.nextChangeIndex() == 0L } } eventually { electrum.pay(change(0).pubkeyScript, Satoshi(7_000), height = 0) } - val state = eventually { wallet.state.first { it.nextChangeIndex == 1L && it.receiveAddress != null } } + val state = eventually { wallet.state.first { it.nextChangeIndex() == 1L && it.receiveAddress != null } } assertEquals(Satoshi(7_000), state.pendingBalance, "in the mempool, so pending") assertEquals(Satoshi(0), state.confirmedBalance) assertEquals(receive(0).address(Chain.Regtest), state.receiveAddress, "a change address is never offered for receiving") @@ -172,4 +172,19 @@ class GroupOnchainWalletJvmTest { } assertTrue(FakeElectrum.scriptHash(receive(9).pubkeyScript) in electrum.subscribed) } + + @Test + fun `every change address an open spend can be given is one the wallet watches`() { + val electrum = FakeElectrum() + val wallet = GroupOnchainWallet(account, electrum, scope) + val lookAhead = GroupOnchainWallet.CHANGE_LOOKAHEAD.toLong() + + val state = eventually { wallet.state.first { it.change.addresses.size == lookAhead.toInt() } } + assertTrue(FakeElectrum.scriptHash(change(lookAhead - 1).pubkeyScript) in electrum.subscribed) + + // Each open spend holds the next; the one after the last has nowhere watched to go. + val held = (0 until lookAhead).toSet() + assertEquals(lookAhead - 1, state.nextChangeIndex(held - (lookAhead - 1))) + assertEquals(null, state.nextChangeIndex(held)) + } } diff --git a/docs/group-send-an-amount.md b/docs/group-send-an-amount.md index d3286a9a..b31c6e0b 100644 --- a/docs/group-send-an-amount.md +++ b/docs/group-send-an-amount.md @@ -9,7 +9,7 @@ Read [group-chain-wallet.md](./group-chain-wallet.md) first, Phases 8 and 10 in particular. This plan is that note's third rollout gate, and it starts from the code as it stands at `96055da5`. -**Phase 1 built; Phases 2-7 not.** Written against `GroupSendScreen`, `GroupSendViewModel`, +**Phases 1 and 3 built; 2 and 4-7 not.** Written against `GroupSendScreen`, `GroupSendViewModel`, `GroupSpendBuilder`, `GroupSpend` and `GroupWalletRollout` as they are at that commit. @@ -188,62 +188,71 @@ removed it (Phase 6). ## Phase 3: change goes to an address the group can find -**One and a half days.** This fixes problems 3 and 4 above. It is the only phase -that touches the builder's choices, so it is the one that needs the most tests. +**One and a half days. Built.** This fixes problems 3 and 4 above, and the +silent dust in 5. It is the only phase that changes what the builder chooses. -- **Change indices are reserved like coins.** Next to - `FrostSigningManager.reservedOutPoints`, add `reservedChangeIndices(database, - chatRoomId): Set`. It decodes the PSBT of every `GroupTransaction` whose - session is not `FAILED`, and collects each output's change derivation. It - covers the same sessions as `reservedOutPoints`, for the same reason. Expose - it on `FrostSigningRepository`, and carry it on `GroupSendUIState.Ready` next - to `reserved`. -- **`GroupWalletState.nextChangeIndex(reserved: Set)`** returns the first - change index that is unused and not reserved. `GroupSpendBuilder.send` takes - that set and calls this. -- **Inside the watched window, or refused.** `CHANGE_LOOKAHEAD` is 3, and its - comment says *"change addresses are only ever chosen by a spend, one at a - time"*, which reservation makes untrue. The change wallet watches three - addresses past the first unused one. So a fifth open spend with change would - pay an address that Electrum is not watching. That is not a loss, because the - address derives, but the change would be missing from the balance until the - window moves. Two changes: - 1. raise `CHANGE_LOOKAHEAD` to 10, the same as receive, since several members - can now each hold an open spend with change; - 2. when the next free index is past the watched window, `send` returns a new - `Result.TooManyOpen`. The screen says to wait for an open spend to - settle, or to send everything. +- **Change indices are reserved like coins.** + `FrostSigningManager.reservedChangeIndices(database, chatRoomId, account)` + sits next to `reservedOutPoints`, and both read one private `openSpends`: + the PSBT of every `GroupTransaction` whose session has not `FAILED`. The + indices come from `GroupSpend.claimedChangeIndices`, which reads each + output's claimed path under the account's fingerprint. It reads *claims*, + not checked derivations, because holding an index too many costs nothing. + The account is a parameter because the plan's `(database, chatRoomId)` + signature could not tell one account's change from another's. The value is + exposed on `FrostSigningRepository` and carried on `GroupSendUIState.Ready` + as `reservedChange`. +- **`GroupWalletState.nextChangeIndex(reserved)`** is now a function. It + returns the lowest change index the wallet *watches* that is unused and not + reserved. The old `nextChangeIndex` property was the first unused index, and + `changeKnown` separates "no answer yet" from "all held". +- **The window, checked.** The plan read `CHANGE_LOOKAHEAD` as "watched past + the first unused address". `ElectrumMiniWallet.maybeGenerateNext` actually + keeps `lastDerived ≥ firstUnused + lookAhead − 1`, so `lookAhead` counts the + first unused address itself: at 3, three change addresses in all. Rather than + depend on that arithmetic, `nextChangeIndex` chooses only among the addresses + the wallet has actually generated, which is the watched set by definition. + So change can never be sent to an address nobody is watching, whatever the + look-ahead is. `CHANGE_LOOKAHEAD` is raised to 10, so a group can have ten + spends with change open at once. Past that, `send` returns `TooManyOpen`, + and the preview says to wait for one to settle or to send everything. + Send-all never needs a change address, so it is never refused this way. +- **Still finding is not nothing.** When there are no coins, the result is + `NothingToSpend` as before. When there are coins but no change address has + been answered for, the result is `FindingChange`, and the form shows the + *Finding the group's coins* indicator instead of an error. +- **Dust change is shown.** `Result.Built.absorbed` is what the fee holds beyond + the fee rate. It is zero for `sendAll`, for a build with change, and for a + bump. When it is positive, the preview adds *"N sats left over are too few + to come back as change, and go to the fee."* This line was meant for Phase 4, + but the new result cases meant `SendPreview` had to change in this phase, so + it went in now. - Check the window against `ElectrumMiniWallet`'s generator before trusting - either number. The wallet note's comment says the look-ahead counts past the - first *unused* address, and this phase depends on that being right. -- **The change branch not loaded yet is "still finding", not "nothing".** When - the change `WalletState` has no addresses yet, `send` returns a new - `Result.FindingChange`, and the form shows it with the same indicator as - `walletState == null` (*Finding the group's coins*), not as an error. -- **Dust change shows up in the preview.** Add `absorbed: Satoshi` to - `Result.Built`: what the build added to the fee because the change would have - been dust. It is zero for `sendAll`, for a build with change, and for a bump. +**Tests.** `GroupSpendBuilderTest` adds three: +- with index 0 held, change goes to 1, and the built PSBT claims 1; +- with every watched index held, the result is `TooManyOpen`, while send-all + still builds; +- with no change branch yet, the result is `FindingChange`, while no coins is + still `NothingToSpend`. -**Tests**, extending `GroupSpendBuilderTest`: +The dust test now asserts that `absorbed` plus the fee at the fee rate is the +fee. `GroupOnchainWalletJvmTest` asserts that the change window is +`CHANGE_LOOKAHEAD` addresses, all subscribed, and that holding all of them +leaves `null`. -- with change index 0 reserved, `send` pays change to index 1, and `GroupSpend.check` - labels it change; -- reserving every index in the watched window gives `TooManyOpen`; -- an empty change branch gives `FindingChange`, not `NothingToSpend`; -- the dust case's `absorbed` plus the fee at the rate equals its fee. +**The end-to-end case the removed gate was waiting for** is in +`GroupSpendSigningJvmTest`, not `GroupWalletSpendRoundTest` as planned: the +round test has no database or manager, and this case needs both. One spend is +left open, holding change index 0. Then `GroupSpendBuilder.send` builds a +second spend from a fresh coin, and two devices sign it to `COMPLETE`. The +test asserts: +- the device that did not build it labels index 1 as change; +- both devices' transactions pass `correctlySpends`; +- once confirmed, the change coin counts in `confirmedBalance`, and + `derivationOf` finds it. -And extending `GroupWalletSpendRoundTest` with **the end-to-end case the removed -gate was waiting for**: two devices, an amount with change, signed to `COMPLETE`. -Assert that the change output is labelled change on the device that did not -propose it, and that once the change coin is in the wallet state it counts in -`confirmedBalance`. That is the test version of "nobody reported a mislabelled -change output", and with no test-network release before mainnet, it is the -only version there is before the feature ships. - -`reservedChangeIndices` gets its own jvmTest next to whatever tests -`reservedOutPoints`. A `FAILED` session releases its index, and a `COMPLETE` -one keeps it until Electrum reports the address used. +The same file's reservation test now also asserts that a failed session +releases its change index. ---