From eb1f126adffaa4a6edfdf813373b5204911a4cfd Mon Sep 17 00:00:00 2001 From: Kgothatso Ngako Date: Fri, 23 Oct 2026 12:23:05 +0200 Subject: [PATCH] feat(wallet): hand out any unused address as a QR code (Phase 3) Tapping the current or a next address on the list opens it in a sheet -- the receive card's QR code, selectable text and copy, titled with the index so a member can say which one they gave. A used address has copy and nothing else: copying is for checking, a QR code is for being paid. The sheet closes itself, with "Address N was just paid", when Electrum reports a payment to the address it is showing. Co-Authored-By: Claude Opus 5.5 --- .../composeResources/values/strings.xml | 3 + .../composable/GroupWalletAddressesScreen.kt | 96 ++++++++++++++++++- .../model/GroupWalletAddressesViewModel.kt | 36 ++++++- .../GroupWalletAddressesViewModelJvmTest.kt | 36 +++++++ 4 files changed, 169 insertions(+), 2 deletions(-) diff --git a/composeApp/src/commonMain/composeResources/values/strings.xml b/composeApp/src/commonMain/composeResources/values/strings.xml index 14a978dc..8b92f59c 100644 --- a/composeApp/src/commonMain/composeResources/values/strings.xml +++ b/composeApp/src/commonMain/composeResources/values/strings.xml @@ -630,6 +630,9 @@ Paid · %1$s sats Paid · spent since Copy address %1$s + Address %1$s + Show as a QR code + Address %1$s was just paid Finding the group's coins… Nothing has been sent to this wallet yet. This profile has no wallet of its own to watch the chain with, so the balance and the next address can't be shown here. 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 c32f3d3c..8b286c7f 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 @@ -1,11 +1,16 @@ package com.machankura.bennu.compose.ui.composable +import androidx.compose.foundation.clickable import androidx.compose.foundation.layout.Arrangement +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.Spacer import androidx.compose.foundation.layout.fillMaxSize import androidx.compose.foundation.layout.fillMaxWidth import androidx.compose.foundation.layout.padding +import androidx.compose.foundation.layout.width import androidx.compose.foundation.lazy.LazyColumn import androidx.compose.foundation.lazy.items +import androidx.compose.foundation.text.selection.SelectionContainer import androidx.compose.material.icons.Icons import androidx.compose.material.icons.filled.ContentCopy import androidx.compose.material.icons.filled.Visibility @@ -18,10 +23,13 @@ import androidx.compose.material3.MaterialTheme import androidx.compose.material3.Scaffold import androidx.compose.material3.SnackbarHost import androidx.compose.material3.Text +import androidx.compose.material3.TextButton import androidx.compose.material3.TopAppBar +import androidx.compose.material3.minimumInteractiveComponentSize import androidx.compose.runtime.Composable import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.rememberCoroutineScope +import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.platform.LocalClipboardManager import androidx.compose.ui.text.buildAnnotatedString @@ -30,23 +38,30 @@ import androidx.compose.ui.text.style.TextOverflow import androidx.lifecycle.viewmodel.compose.viewModel import bennu.composeapp.generated.resources.Res import bennu.composeapp.generated.resources.address_copied +import bennu.composeapp.generated.resources.address_s +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.copy_address import bennu.composeapp.generated.resources.copy_address_s import bennu.composeapp.generated.resources.finding_the_group_s_coins import bennu.composeapp.generated.resources.not_used_yet import bennu.composeapp.generated.resources.paid_s_sats import bennu.composeapp.generated.resources.paid_spent_since +import bennu.composeapp.generated.resources.show_as_a_qr_code import bennu.composeapp.generated.resources.this_profile_cannot_watch_the_chain import com.machankura.bennu.compose.repository.ChatRepository import com.machankura.bennu.compose.text.grouped +import com.machankura.bennu.compose.ui.composable.widgets.Decorative import com.machankura.bennu.compose.ui.composable.widgets.EmptyState import com.machankura.bennu.compose.ui.composable.widgets.ErrorState import com.machankura.bennu.compose.ui.composable.widgets.LoadingDataIndicator import com.machankura.bennu.compose.ui.composable.widgets.LocalSnackbarHostState import com.machankura.bennu.compose.ui.composable.widgets.NavigateBackButton +import com.machankura.bennu.compose.ui.composable.widgets.QRCodeView import com.machankura.bennu.compose.ui.composable.widgets.ScreenStateTransition +import com.machankura.bennu.compose.ui.composable.widgets.dialogs.ModalBottomSheet import com.machankura.bennu.compose.ui.composable.widgets.rememberNotifier import com.machankura.bennu.compose.ui.theme.readableContent import com.machankura.bennu.compose.ui.theme.spacing @@ -54,6 +69,7 @@ import com.machankura.bennu.compose.ui.view.model.GroupWalletAddressesViewModel import com.machankura.bennu.compose.ui.view.state.GroupWalletAddressesUIState import com.machankura.bennu.compose.wallet.GroupAddress import com.machankura.bennu.compose.wallet.GroupOnchainWallets +import com.machankura.bennu.compose.wallet.bip21Uri import fr.acinq.bitcoin.Chain import fr.acinq.bitcoin.Satoshi import org.jetbrains.compose.resources.stringResource @@ -66,6 +82,8 @@ import org.jetbrains.compose.resources.stringResource * worth handing out. The addresses after the current one are there so that two * members sharing the wallet at once can each give a payer a different one; * the sentence above the list says so, or nobody would use anything but the top. + * Tapping an unused one opens it as a QR code, in a sheet that closes itself if + * the address is paid while it is open. * * No block explorer: a tap that sends every address of the group's wallet to a * third party's server is the leak the watch-only export warns about, made @@ -149,7 +167,17 @@ fun GroupWalletAddressesScreen( modifier = Modifier.padding(padding), receive = receive, notify = notify, + onShow = { index, paid -> addressesViewModel.show(index) { notify(paid) } }, ) + + val shown = addressesViewModel.shown?.let { index -> receive.firstOrNull { it.derivation.index == index } } + if (shown != null) { + AddressSheet( + address = shown, + notify = notify, + onDismiss = addressesViewModel::dismiss, + ) + } } } } @@ -162,6 +190,7 @@ private fun AddressList( modifier: Modifier, receive: List, notify: (String) -> Unit, + onShow: (index: Long, paidMessage: String) -> Unit, ) { val clipboardManager = LocalClipboardManager.current val addressCopied = stringResource(Res.string.address_copied) @@ -180,17 +209,78 @@ private fun AddressList( } items(receive, key = { it.derivation.index }) { 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) }, + // 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) { + { onShow(address.derivation.index, paid) } + } else null, ) } } } +/** + * One unused address, ready to be paid: what the wallet screen's receive card + * shows for the current address, for whichever one this member is handing out. + * + * The title carries the index so a member reading it to a payer can say which + * one they gave. + */ +@Composable +private fun AddressSheet( + address: GroupAddress, + notify: (String) -> Unit, + onDismiss: () -> Unit, +) { + val clipboardManager = LocalClipboardManager.current + val addressCopied = stringResource(Res.string.address_copied) + + ModalBottomSheet( + onDismiss = onDismiss, + skipPartiallyExpanded = true, + horizontalAlignment = Alignment.CenterHorizontally, + ) { + Column( + horizontalAlignment = Alignment.CenterHorizontally, + verticalArrangement = Arrangement.spacedBy(MaterialTheme.spacing.itemGap) + ) { + Text( + text = stringResource(Res.string.address_s, address.derivation.index.toString()), + style = MaterialTheme.typography.titleMedium, + ) + Text( + text = address.status.label(address.balance), + style = MaterialTheme.typography.bodyMedium, + color = MaterialTheme.colorScheme.onSurfaceVariant, + ) + // BIP21, so a scanning wallet knows what the text is. + QRCodeView(bip21Uri(address.address)) + SelectionContainer { + Text(text = address.address, style = MaterialTheme.typography.bodyMedium) + } + TextButton( + onClick = { + clipboardManager.setText(buildAnnotatedString { append(address.address) }) + notify(addressCopied) + } + ) { + Icon(Icons.Default.ContentCopy, contentDescription = Decorative) + Spacer(modifier = Modifier.width(MaterialTheme.spacing.relatedGap)) + Text(stringResource(Res.string.copy_address)) + } + } + } +} + /** * One address: its index, the address itself, and what it has been used for. * @@ -199,7 +289,8 @@ private fun AddressList( * measured in all six schemes. */ @Composable -private fun AddressRow(address: GroupAddress, onCopy: () -> Unit) { +private fun AddressRow(address: GroupAddress, onCopy: () -> Unit, onShow: (() -> Unit)?) { + val showLabel = stringResource(Res.string.show_as_a_qr_code) val colors = if (address.status == GroupAddress.Status.CURRENT) { val onContainer = MaterialTheme.colorScheme.onSecondaryContainer ListItemDefaults.colors( @@ -214,6 +305,9 @@ private fun AddressRow(address: GroupAddress, onCopy: () -> Unit) { } ListItem( + modifier = if (onShow != null) { + Modifier.clickable(onClickLabel = showLabel, onClick = onShow).minimumInteractiveComponentSize() + } else Modifier, colors = colors, leadingContent = { Text( 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 b2a6ba98..e7fc3a80 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 @@ -10,10 +10,12 @@ import androidx.lifecycle.viewmodel.initializer import androidx.lifecycle.viewmodel.viewModelFactory import com.machankura.bennu.compose.repository.ChatRepository import com.machankura.bennu.compose.ui.view.state.GroupWalletAddressesUIState +import com.machankura.bennu.compose.wallet.GroupAddress import com.machankura.bennu.compose.wallet.GroupOnchainWallets import fr.acinq.bitcoin.Chain import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext /** * The group wallet's addresses, watched for as long as the screen is open. @@ -32,6 +34,27 @@ class GroupWalletAddressesViewModel( var uiState: GroupWalletAddressesUIState by mutableStateOf(initialUIState) private set + /** The index of the address the sheet is showing as a QR code, or null when it is closed. */ + var shown: Long? by mutableStateOf(null) + private set + + private var onShownPaid: (() -> Unit)? = null + + /** + * Opens the sheet on the receive address at [index]. [onPaid] runs once if + * that address is paid while the sheet is open, as the sheet closes: whoever + * is scanning it then has already paid, and nobody else should. + */ + fun show(index: Long, onPaid: () -> Unit) { + shown = index + onShownPaid = onPaid + } + + fun dismiss() { + shown = null + onShownPaid = null + } + fun initiate() { viewModelScope.launch(Dispatchers.IO) { val wallet = chatRepository.groupWallet(chatRoomId, chain) @@ -46,7 +69,18 @@ class GroupWalletAddressesViewModel( uiState = GroupWalletAddressesUIState.Loaded(receive = null) wallets.walletFor(wallet.account).state.collect { state -> - uiState = GroupWalletAddressesUIState.Loaded(receive = state.receiveAddresses) + val receive = state.receiveAddresses + uiState = GroupWalletAddressesUIState.Loaded(receive = receive) + + val index = shown ?: return@collect + val status = receive?.firstOrNull { it.derivation.index == index }?.status ?: return@collect + if (status == GroupAddress.Status.USED) { + val paid = onShownPaid + withContext(Dispatchers.Main) { + dismiss() + paid?.invoke() + } + } } } } diff --git a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletAddressesViewModelJvmTest.kt b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletAddressesViewModelJvmTest.kt index 371bbdd3..8da5153d 100644 --- a/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletAddressesViewModelJvmTest.kt +++ b/composeApp/src/jvmTest/kotlin/com/machankura/bennu/compose/ui/view/model/GroupWalletAddressesViewModelJvmTest.kt @@ -30,6 +30,7 @@ import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertIs +import kotlin.test.assertNull import kotlin.time.Instant /** Which of its states the address list is in, and that it follows the chain while it is open. */ @@ -118,4 +119,39 @@ class GroupWalletAddressesViewModelJvmTest { val current = assertIs(state).receive!!.single { it.status == GroupAddress.Status.CURRENT } assertEquals(1L, current.derivation.index) } + + private fun GroupWalletAddressesViewModel.untilListed(index: Long, status: GroupAddress.Status) = until { loaded -> + (loaded as? GroupWalletAddressesUIState.Loaded)?.receive?.any { it.derivation.index == index && it.status == status } == true + } + + @Test + fun `the sheet closes once when the address it shows is paid`() { + val electrum = FakeElectrum() + val addresses = viewModel(opened = wallet, wallets = GroupOnchainWallets(electrum, scope)) + addresses.untilListed(1L, GroupAddress.Status.NEXT) + + var paid = 0 + addresses.show(1L) { paid++ } + runBlocking { electrum.pay(receive(1).pubkeyScript, Satoshi(25_000)) } + addresses.untilListed(1L, GroupAddress.Status.USED) + runBlocking { withTimeout(10_000) { while (addresses.shown != null) delay(10) } } + + assertNull(addresses.shown) + assertEquals(1, paid) + } + + @Test + fun `the sheet stays open when some other address is paid`() { + val electrum = FakeElectrum() + val addresses = viewModel(opened = wallet, wallets = GroupOnchainWallets(electrum, scope)) + addresses.untilListed(1L, GroupAddress.Status.NEXT) + + var paid = 0 + addresses.show(1L) { paid++ } + runBlocking { electrum.pay(receive(0).pubkeyScript, Satoshi(25_000)) } + addresses.untilListed(1L, GroupAddress.Status.CURRENT) + + assertEquals(1L, addresses.shown, "the address it shows became the current one, which is still unused") + assertEquals(0, paid) + } }