feat: parse NIP-47 transaction metadata for sender/recipient display
Add NwcTransactionMetadata parser that extracts comment, payer_data, recipient_data, and nostr zap data from the untyped metadata field. The transaction list UI now shows the Nostr user profile picture and name for zap senders/recipients, falls back to payer name/email or lightning address, and displays comment when it differs from description. https://claude.ai/code/session_01JFogiwyR4CPVP8cRznqahJ
This commit is contained in:
+105
-3
@@ -59,7 +59,10 @@ import androidx.compose.ui.unit.dp
|
||||
import androidx.lifecycle.viewmodel.compose.viewModel
|
||||
import com.vitorpamplona.amethyst.R
|
||||
import com.vitorpamplona.amethyst.ui.navigation.navs.INav
|
||||
import com.vitorpamplona.amethyst.ui.note.UserPicture
|
||||
import com.vitorpamplona.amethyst.ui.note.UsernameDisplay
|
||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.AccountViewModel
|
||||
import com.vitorpamplona.amethyst.ui.screen.loggedIn.chats.rooms.LoadUser
|
||||
import com.vitorpamplona.amethyst.ui.stringRes
|
||||
import com.vitorpamplona.quartz.nip47WalletConnect.NwcTransaction
|
||||
import com.vitorpamplona.quartz.nip47WalletConnect.NwcTransactionType
|
||||
@@ -143,7 +146,7 @@ fun WalletTransactionsScreen(
|
||||
modifier = Modifier.padding(padding),
|
||||
) {
|
||||
items(transactions) { tx ->
|
||||
TransactionItem(tx)
|
||||
TransactionItem(tx, accountViewModel, nav)
|
||||
HorizontalDivider()
|
||||
}
|
||||
}
|
||||
@@ -152,7 +155,11 @@ fun WalletTransactionsScreen(
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TransactionItem(tx: NwcTransaction) {
|
||||
private fun TransactionItem(
|
||||
tx: NwcTransaction,
|
||||
accountViewModel: AccountViewModel,
|
||||
nav: INav,
|
||||
) {
|
||||
val isIncoming = tx.type == NwcTransactionType.INCOMING
|
||||
val amountSats = (tx.amount ?: 0L) / 1000L
|
||||
val formattedAmount =
|
||||
@@ -169,6 +176,36 @@ private fun TransactionItem(tx: NwcTransaction) {
|
||||
} ?: ""
|
||||
}
|
||||
|
||||
val parsed = remember(tx.metadata) { tx.parsedMetadata() }
|
||||
|
||||
// For incoming: show who sent it (nostr pubkey or payer name/email)
|
||||
// For outgoing: show who received it (nostr recipient or recipient identifier)
|
||||
val counterpartyPubkeyHex =
|
||||
remember(parsed) {
|
||||
if (isIncoming) parsed?.senderPubkeyHex() else parsed?.recipientPubkeyHex()
|
||||
}
|
||||
|
||||
val counterpartyDisplayName =
|
||||
remember(parsed) {
|
||||
if (isIncoming) {
|
||||
parsed?.senderDisplayName()
|
||||
} else {
|
||||
parsed?.recipientIdentifier()
|
||||
}
|
||||
}
|
||||
|
||||
// Show comment only if it differs from description
|
||||
val commentText =
|
||||
remember(parsed, tx.description) {
|
||||
parsed?.comment?.let { comment ->
|
||||
if (tx.description == null || !comment.equals(tx.description, ignoreCase = true)) {
|
||||
comment
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Row(
|
||||
modifier =
|
||||
Modifier
|
||||
@@ -176,6 +213,15 @@ private fun TransactionItem(tx: NwcTransaction) {
|
||||
.padding(horizontal = 16.dp, vertical = 12.dp),
|
||||
verticalAlignment = Alignment.CenterVertically,
|
||||
) {
|
||||
if (counterpartyPubkeyHex != null) {
|
||||
UserPicture(
|
||||
userHex = counterpartyPubkeyHex,
|
||||
size = 40.dp,
|
||||
accountViewModel = accountViewModel,
|
||||
nav = nav,
|
||||
)
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
} else {
|
||||
Icon(
|
||||
imageVector =
|
||||
if (isIncoming) Icons.Filled.ArrowDownward else Icons.Filled.ArrowUpward,
|
||||
@@ -193,10 +239,21 @@ private fun TransactionItem(tx: NwcTransaction) {
|
||||
MaterialTheme.colorScheme.onSurfaceVariant
|
||||
},
|
||||
)
|
||||
|
||||
Spacer(modifier = Modifier.width(12.dp))
|
||||
}
|
||||
|
||||
Column(modifier = Modifier.weight(1f)) {
|
||||
if (counterpartyPubkeyHex != null) {
|
||||
TransactionUserName(counterpartyPubkeyHex, counterpartyDisplayName, accountViewModel)
|
||||
} else if (counterpartyDisplayName != null) {
|
||||
Text(
|
||||
text = counterpartyDisplayName,
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
fontWeight = FontWeight.Medium,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
} else {
|
||||
Text(
|
||||
text = tx.description ?: if (isIncoming) stringRes(R.string.wallet_incoming) else stringRes(R.string.wallet_outgoing),
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
@@ -204,6 +261,26 @@ private fun TransactionItem(tx: NwcTransaction) {
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
|
||||
if (commentText != null) {
|
||||
Text(
|
||||
text = commentText,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
} else if (counterpartyPubkeyHex != null || counterpartyDisplayName != null) {
|
||||
val descOrType = tx.description ?: if (isIncoming) stringRes(R.string.wallet_incoming) else stringRes(R.string.wallet_outgoing)
|
||||
Text(
|
||||
text = descOrType,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
color = MaterialTheme.colorScheme.onSurfaceVariant,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
|
||||
Text(
|
||||
text = dateText,
|
||||
style = MaterialTheme.typography.bodySmall,
|
||||
@@ -224,3 +301,28 @@ private fun TransactionItem(tx: NwcTransaction) {
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
@Composable
|
||||
private fun TransactionUserName(
|
||||
pubkeyHex: String,
|
||||
fallbackName: String?,
|
||||
accountViewModel: AccountViewModel,
|
||||
) {
|
||||
LoadUser(baseUserHex = pubkeyHex, accountViewModel = accountViewModel) { user ->
|
||||
if (user != null) {
|
||||
UsernameDisplay(
|
||||
baseUser = user,
|
||||
fontWeight = FontWeight.Medium,
|
||||
accountViewModel = accountViewModel,
|
||||
)
|
||||
} else {
|
||||
Text(
|
||||
text = fallbackName ?: pubkeyHex.take(8) + "...",
|
||||
style = MaterialTheme.typography.bodyMedium,
|
||||
fontWeight = FontWeight.Medium,
|
||||
maxLines = 1,
|
||||
overflow = TextOverflow.Ellipsis,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+3
-1
@@ -63,7 +63,9 @@ class NwcTransaction(
|
||||
var settled_at: Long? = null,
|
||||
var settle_deadline: Long? = null,
|
||||
var metadata: Any? = null,
|
||||
)
|
||||
) {
|
||||
fun parsedMetadata(): NwcTransactionMetadata? = NwcTransactionMetadata.parse(metadata)
|
||||
}
|
||||
|
||||
class TlvRecord(
|
||||
var type: Long? = null,
|
||||
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2025 Vitor Pamplona
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy of
|
||||
* this software and associated documentation files (the "Software"), to deal in
|
||||
* the Software without restriction, including without limitation the rights to use,
|
||||
* copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the
|
||||
* Software, and to permit persons to whom the Software is furnished to do so,
|
||||
* subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
|
||||
* FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
||||
* COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
|
||||
* AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
||||
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
||||
*/
|
||||
package com.vitorpamplona.quartz.nip47WalletConnect
|
||||
|
||||
import com.vitorpamplona.quartz.nip19Bech32.decodePublicKeyAsHexOrNull
|
||||
|
||||
class NwcTransactionMetadata(
|
||||
val comment: String?,
|
||||
val payerData: PayerData?,
|
||||
val recipientData: RecipientData?,
|
||||
val nostr: NostrZapData?,
|
||||
) {
|
||||
class PayerData(
|
||||
val name: String?,
|
||||
val email: String?,
|
||||
val pubkey: String?,
|
||||
)
|
||||
|
||||
class RecipientData(
|
||||
val identifier: String?,
|
||||
)
|
||||
|
||||
class NostrZapData(
|
||||
val pubkeyHex: String?,
|
||||
val recipientPubkeyHex: String?,
|
||||
)
|
||||
|
||||
fun senderPubkeyHex(): String? = nostr?.pubkeyHex ?: payerData?.pubkey?.let { decodePublicKeyAsHexOrNull(it) }
|
||||
|
||||
fun senderDisplayName(): String? = payerData?.name ?: payerData?.email
|
||||
|
||||
fun recipientIdentifier(): String? = recipientData?.identifier
|
||||
|
||||
fun recipientPubkeyHex(): String? = nostr?.recipientPubkeyHex
|
||||
|
||||
companion object {
|
||||
fun parse(metadata: Any?): NwcTransactionMetadata? {
|
||||
val map = metadata as? Map<*, *> ?: return null
|
||||
|
||||
val comment = map["comment"] as? String
|
||||
|
||||
val payerData = (map["payer_data"] as? Map<*, *>)?.let { pd ->
|
||||
PayerData(
|
||||
name = pd["name"] as? String,
|
||||
email = pd["email"] as? String,
|
||||
pubkey = pd["pubkey"] as? String,
|
||||
)
|
||||
}
|
||||
|
||||
val recipientData = (map["recipient_data"] as? Map<*, *>)?.let { rd ->
|
||||
RecipientData(
|
||||
identifier = rd["identifier"] as? String,
|
||||
)
|
||||
}
|
||||
|
||||
val nostr = (map["nostr"] as? Map<*, *>)?.let { n ->
|
||||
val rawPubkey = n["pubkey"] as? String
|
||||
val pubkeyHex = rawPubkey?.let { decodePublicKeyAsHexOrNull(it) }
|
||||
|
||||
val tags = n["tags"] as? List<*>
|
||||
val recipientHex = tags?.firstNotNullOfOrNull { tag ->
|
||||
val tagList = tag as? List<*>
|
||||
if (tagList != null && tagList.size >= 2 && tagList[0] == "p") {
|
||||
tagList[1] as? String
|
||||
} else {
|
||||
null
|
||||
}
|
||||
}
|
||||
|
||||
NostrZapData(
|
||||
pubkeyHex = pubkeyHex,
|
||||
recipientPubkeyHex = recipientHex,
|
||||
)
|
||||
}
|
||||
|
||||
if (comment == null && payerData == null && recipientData == null && nostr == null) {
|
||||
return null
|
||||
}
|
||||
|
||||
return NwcTransactionMetadata(
|
||||
comment = comment,
|
||||
payerData = payerData,
|
||||
recipientData = recipientData,
|
||||
nostr = nostr,
|
||||
)
|
||||
}
|
||||
}
|
||||
}
|
||||
+72
@@ -500,6 +500,78 @@ class AlbyInteropTest {
|
||||
assertNotNull(response.result?.metadata)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMetadataParserComment() {
|
||||
val json =
|
||||
"""{"result_type":"lookup_invoice","result":{"type":"incoming","state":"settled","invoice":"lnbc...","payment_hash":"hash","amount":5000,"created_at":1000,"settled_at":2000,"metadata":{"comment":"Great post!"}}}"""
|
||||
val response = OptimizedJsonMapper.fromJsonTo<Response>(json)
|
||||
assertIs<LookupInvoiceSuccessResponse>(response)
|
||||
val parsed = response.result?.parsedMetadata()
|
||||
assertNotNull(parsed)
|
||||
assertEquals("Great post!", parsed.comment)
|
||||
assertNull(parsed.payerData)
|
||||
assertNull(parsed.nostr)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMetadataParserPayerData() {
|
||||
val json =
|
||||
"""{"result_type":"lookup_invoice","result":{"type":"incoming","state":"settled","invoice":"lnbc...","payment_hash":"hash","amount":5000,"created_at":1000,"settled_at":2000,"metadata":{"payer_data":{"name":"Alice","email":"alice@example.com","pubkey":"abc123"}}}}"""
|
||||
val response = OptimizedJsonMapper.fromJsonTo<Response>(json)
|
||||
assertIs<LookupInvoiceSuccessResponse>(response)
|
||||
val parsed = response.result?.parsedMetadata()
|
||||
assertNotNull(parsed)
|
||||
assertEquals("Alice", parsed.payerData?.name)
|
||||
assertEquals("alice@example.com", parsed.payerData?.email)
|
||||
assertEquals("abc123", parsed.payerData?.pubkey)
|
||||
assertEquals("Alice", parsed.senderDisplayName())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMetadataParserNostrZap() {
|
||||
val senderHex = "7e7e9c42a91bfef19fa929e5fda1b72e0ebc1a4c1141673e2794234d86addf4e"
|
||||
val recipientHex = "460c25e682fda7832b52d1f22d3d22b3176d972f60dcdc3212ed8c92ef85065c"
|
||||
val json =
|
||||
"""{"result_type":"lookup_invoice","result":{"type":"incoming","state":"settled","invoice":"lnbc...","payment_hash":"hash","amount":21000,"created_at":1000,"settled_at":2000,"metadata":{"nostr":{"pubkey":"$senderHex","tags":[["p","$recipientHex"],["amount","21000"]]}}}}"""
|
||||
val response = OptimizedJsonMapper.fromJsonTo<Response>(json)
|
||||
assertIs<LookupInvoiceSuccessResponse>(response)
|
||||
val parsed = response.result?.parsedMetadata()
|
||||
assertNotNull(parsed)
|
||||
assertEquals(senderHex, parsed.senderPubkeyHex())
|
||||
assertEquals(recipientHex, parsed.recipientPubkeyHex())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMetadataParserRecipientData() {
|
||||
val json =
|
||||
"""{"result_type":"lookup_invoice","result":{"type":"outgoing","state":"settled","invoice":"lnbc...","payment_hash":"hash","amount":5000,"created_at":1000,"settled_at":2000,"metadata":{"recipient_data":{"identifier":"alice@getalby.com"}}}}"""
|
||||
val response = OptimizedJsonMapper.fromJsonTo<Response>(json)
|
||||
assertIs<LookupInvoiceSuccessResponse>(response)
|
||||
val parsed = response.result?.parsedMetadata()
|
||||
assertNotNull(parsed)
|
||||
assertEquals("alice@getalby.com", parsed.recipientIdentifier())
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMetadataParserNullForSimpleMetadata() {
|
||||
val json =
|
||||
"""{"result_type":"lookup_invoice","result":{"type":"incoming","state":"settled","invoice":"lnbc...","payment_hash":"hash","amount":5000,"created_at":1000,"settled_at":2000,"metadata":{"a":123}}}"""
|
||||
val response = OptimizedJsonMapper.fromJsonTo<Response>(json)
|
||||
assertIs<LookupInvoiceSuccessResponse>(response)
|
||||
val parsed = response.result?.parsedMetadata()
|
||||
assertNull(parsed)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testMetadataParserNullMetadata() {
|
||||
val json =
|
||||
"""{"result_type":"lookup_invoice","result":{"type":"incoming","state":"settled","invoice":"lnbc...","payment_hash":"hash","amount":5000,"created_at":1000,"settled_at":2000}}"""
|
||||
val response = OptimizedJsonMapper.fromJsonTo<Response>(json)
|
||||
assertIs<LookupInvoiceSuccessResponse>(response)
|
||||
val parsed = response.result?.parsedMetadata()
|
||||
assertNull(parsed)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testJsSdkGetInfoWithAllMethods() {
|
||||
// JS SDK advertises all 13 single methods + notifications
|
||||
|
||||
Reference in New Issue
Block a user