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:
+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