package com.example.gudariwallet.util import org.json.JSONArray /** * Stateless helpers for parsing LNURL-pay step-1 metadata fields. * Shared between [com.example.gudariwallet.data.LNbitsWalletRepository] * and [com.example.gudariwallet.ui.WalletViewModel] so neither duplicates * the logic. */ object LnurlMetadataParser { /** * Extracts the `text/plain` description from a LNURL-pay metadata JSON string. * The metadata field is a JSON array of [type, value] pairs, e.g.: * [["text/plain","Pay me"],["image/png;base64","..."]] * Returns an empty string if the field is null, blank, or unparseable. */ fun parseDescription(metadata: String?): String { if (metadata.isNullOrBlank()) return "" return runCatching { val array = JSONArray(metadata) (0 until array.length()) .map { array.getJSONArray(it) } .firstOrNull { it.getString(0) == "text/plain" } ?.getString(1) ?: "" }.getOrDefault("") } /** * Extracts the host portion of [callback] for display purposes. * Returns an empty string if [callback] is null, blank, or not a valid URI. */ fun extractDomain(callback: String?): String { if (callback.isNullOrBlank()) return "" return runCatching { java.net.URI(callback).host ?: "" }.getOrDefault("") } } /** * Returns the display label for a LNURL payment target. * If [lnurl] is a Lightning Address (contains '@'), returns it as-is. * Otherwise returns the [domain] of the LNURL endpoint. */ fun payingToLabel(lnurl: String, domain: String): String = if ('@' in lnurl) lnurl else domain