From b712d6d835eb75c6c5bae05585cb2ff875d45c69 Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Fri, 23 Oct 2026 12:24:54 +0200 Subject: [PATCH] feat(wallet): the used change addresses, under the receive ones (Phase 4) GroupWalletState.changeAddresses lists the change addresses that have been used, newest first -- never a current or a next one, since change is chosen by a spend. The address list shows them in a section of their own once there are any, with copy and nothing else. Co-Authored-By: Claude Opus 5.5 --- .../composable/GroupWalletAddressesScreen.kt | 31 ++++++++++++--- .../model/GroupWalletAddressesViewModel.kt | 2 +- .../view/state/GroupWalletAddressesUIState.kt | 2 + .../compose/wallet/GroupOnchainWallet.kt | 39 ++++++++++++++----- .../wallet/GroupOnchainWalletJvmTest.kt | 24 ++++++++++++ 5 files changed, 82 insertions(+), 16 deletions(-) diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletAddressesScreen.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletAddressesScreen.kt index 8b286c7f..32ad5341 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletAddressesScreen.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/composable/GroupWalletAddressesScreen.kt @@ -43,6 +43,7 @@ import bennu.composeapp.generated.resources.address_s_was_just_paid import bennu.composeapp.generated.resources.addresses import bennu.composeapp.generated.resources.addresses_explainer import bennu.composeapp.generated.resources.being_handed_out_now +import bennu.composeapp.generated.resources.change import bennu.composeapp.generated.resources.copy_address import bennu.composeapp.generated.resources.copy_address_s import bennu.composeapp.generated.resources.finding_the_group_s_coins @@ -166,6 +167,7 @@ fun GroupWalletAddressesScreen( AddressList( modifier = Modifier.padding(padding), receive = receive, + change = state.change, notify = notify, onShow = { index, paid -> addressesViewModel.show(index) { notify(paid) } }, ) @@ -189,11 +191,18 @@ fun GroupWalletAddressesScreen( private fun AddressList( modifier: Modifier, receive: List, + change: List, notify: (String) -> Unit, onShow: (index: Long, paidMessage: String) -> Unit, ) { val clipboardManager = LocalClipboardManager.current val addressCopied = stringResource(Res.string.address_copied) + val copy = { address: GroupAddress -> + clipboardManager.setText(buildAnnotatedString { append(address.address) }) + notify(addressCopied) + } + // Both branches start at index 0, so the index alone is not a row's identity. + val key = { address: GroupAddress -> "${address.derivation.change}/${address.derivation.index}" } LazyColumn( modifier = modifier.readableContent().fillMaxSize() @@ -208,16 +217,13 @@ private fun AddressList( ) } - items(receive, key = { it.derivation.index }) { address -> + items(receive, key = key) { address -> // Read here rather than in the click: the index is the row's, and a // click handler is not composable. val paid = stringResource(Res.string.address_s_was_just_paid, address.derivation.index.toString()) AddressRow( address = address, - onCopy = { - clipboardManager.setText(buildAnnotatedString { append(address.address) }) - notify(addressCopied) - }, + onCopy = { copy(address) }, // A used address is never offered for scanning: copying it is for // checking, a QR code is for being paid. onShow = if (address.status != GroupAddress.Status.USED) { @@ -225,6 +231,21 @@ private fun AddressList( } else null, ) } + + // The group's money too, and what a watch-only import will show beside + // the receive addresses -- but chosen by a spend, so never offered here. + if (change.isNotEmpty()) { + item { + Text( + text = stringResource(Res.string.change), + style = MaterialTheme.typography.titleMedium, + modifier = Modifier.padding(top = MaterialTheme.spacing.sectionGap), + ) + } + items(change, key = key) { address -> + AddressRow(address = address, onCopy = { copy(address) }, onShow = null) + } + } } } diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletAddressesViewModel.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletAddressesViewModel.kt index e7fc3a80..b40ce5e3 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletAddressesViewModel.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletAddressesViewModel.kt @@ -70,7 +70,7 @@ class GroupWalletAddressesViewModel( uiState = GroupWalletAddressesUIState.Loaded(receive = null) wallets.walletFor(wallet.account).state.collect { state -> val receive = state.receiveAddresses - uiState = GroupWalletAddressesUIState.Loaded(receive = receive) + uiState = GroupWalletAddressesUIState.Loaded(receive = receive, change = state.changeAddresses.orEmpty()) val index = shown ?: return@collect val status = receive?.firstOrNull { it.derivation.index == index }?.status ?: return@collect diff --git a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletAddressesUIState.kt b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletAddressesUIState.kt index 8e99e9f6..e175f29c 100644 --- a/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletAddressesUIState.kt +++ b/composeApp/src/commonMain/kotlin/com/machankura/bennu/compose/ui/view/state/GroupWalletAddressesUIState.kt @@ -12,6 +12,8 @@ sealed interface GroupWalletAddressesUIState { */ data class Loaded( val receive: List?, + /** The change addresses that have been used, newest first: empty for a wallet that has never sent. */ + val change: List = emptyList(), ): GroupWalletAddressesUIState /** 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 e990aafd..c7edff94 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 @@ -103,7 +103,7 @@ class GroupOnchainWallet( } } -/** One receive address as the address list shows it. */ +/** One of the wallet's addresses as the address list shows it. */ data class GroupAddress( val derivation: GroupWalletDerivation.Derived, val address: String, @@ -183,25 +183,44 @@ data class GroupWalletState( */ val receiveAddresses: List? by lazy { val current = receive.firstUnusedDerivedAddress?.second?.index ?: return@lazy null - val derived = receive.addresses.mapNotNull { (address, state) -> - (state.meta as? WalletState.AddressMeta.Derived)?.let { Triple(address, it.index, state) } + listed(GroupWalletDerivation.RECEIVE, receive, current) + } + + /** + * The change addresses that have been used, newest first, or null until + * Electrum has answered. Never a current or a next one: change is chosen by + * a spend, and is not handed out to be paid. + */ + val changeAddresses: List? by lazy { + change.firstUnusedDerivedAddress ?: return@lazy null + listed(GroupWalletDerivation.CHANGE, change, current = null) + } + + /** + * [branch]'s addresses up to the highest used one, and -- given the branch's + * [current] index -- up to [GroupOnchainWallet.ADDRESSES_AHEAD] past that. + * Without a current index, only the used ones. + */ + private fun listed(branch: Long, state: WalletState, current: Int?): List { + val derived = state.addresses.mapNotNull { (address, addressState) -> + (addressState.meta as? WalletState.AddressMeta.Derived)?.let { Triple(address, it.index, addressState) } } val highestUsed = derived.filter { it.third.alreadyUsed }.maxOfOrNull { it.second } ?: -1 - val last = maxOf(current + GroupOnchainWallet.ADDRESSES_AHEAD, highestUsed) + val last = current?.let { maxOf(it + GroupOnchainWallet.ADDRESSES_AHEAD, highestUsed) } ?: highestUsed - derived - .filter { (_, index, _) -> index <= last } + return derived + .filter { (_, index, addressState) -> index <= last && (current != null || addressState.alreadyUsed) } .sortedByDescending { (_, index, _) -> index } - .map { (address, index, state) -> + .map { (address, index, addressState) -> GroupAddress( - derivation = GroupWalletDerivation.derive(account, GroupWalletDerivation.RECEIVE, index.toLong()), + derivation = GroupWalletDerivation.derive(account, branch, index.toLong()), address = address, status = when { - state.alreadyUsed -> GroupAddress.Status.USED + addressState.alreadyUsed -> GroupAddress.Status.USED index == current -> GroupAddress.Status.CURRENT else -> GroupAddress.Status.NEXT }, - balance = state.utxos.map { it.amount }.sum(), + balance = addressState.utxos.map { it.amount }.sum(), ) } } 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 baf1400c..b47e2b60 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 @@ -203,4 +203,28 @@ class GroupOnchainWalletJvmTest { assertTrue(state.receiveAddresses!!.none { it.address == change(0).address(Chain.Regtest) }) assertTrue(state.receiveAddresses!!.all { it.derivation.change == GroupWalletDerivation.RECEIVE }) } + + @Test + fun `a wallet that has never sent lists no change addresses`() { + val wallet = GroupOnchainWallet(account, FakeElectrum(), scope) + + val state = eventually { wallet.state.first { it.changeAddresses != null } } + assertEquals(emptyList(), state.changeAddresses, "unused change is never offered, not even as the next one") + } + + @Test + fun `used change addresses are listed and the receive list is untouched`() { + val electrum = FakeElectrum() + val wallet = GroupOnchainWallet(account, electrum, scope) + + val before = eventually { wallet.state.first { it.receiveAddresses != null && it.changeAddresses != null } } + eventually { electrum.pay(change(0).pubkeyScript, Satoshi(7_000)) } + + val state = eventually { wallet.state.first { it.changeAddresses?.isNotEmpty() == true } } + val used = state.changeAddresses!!.single() + assertEquals(change(0), used.derivation) + assertEquals(GroupAddress.Status.USED, used.status) + assertEquals(Satoshi(7_000), used.balance) + assertEquals(before.listed(), state.listed()) + } }