package com.bitcointxoko.gudariwallet.api import okhttp3.MediaType.Companion.toMediaType import okhttp3.OkHttpClient import retrofit2.Retrofit import retrofit2.converter.kotlinx.serialization.asConverterFactory import java.util.concurrent.TimeUnit /** * Resolves a Lightning node pubkey to a human-readable alias. * Tries mempool.space first, falls back to 1ml.com. * Returns null if neither source has the node or both fail. */ class NodeAliasClient { private val client = OkHttpClient.Builder() .connectTimeout(5, TimeUnit.SECONDS) .readTimeout(5, TimeUnit.SECONDS) .build() private val converter = JsonProvider.json .asConverterFactory("application/json; charset=UTF-8".toMediaType()) private val mempoolApi: MempoolApi = Retrofit.Builder() .baseUrl("https://mempool.space/") .client(client) .addConverterFactory(converter) .build() .create(MempoolApi::class.java) private val oneMlApi: OneMlApi = Retrofit.Builder() .baseUrl("https://1ml.com/") .client(client) .addConverterFactory(converter) .build() .create(OneMlApi::class.java) suspend fun resolveAlias(pubkey: String): String? { if (pubkey.isBlank()) return null // 1. Try mempool.space runCatching { mempoolApi.getNode(pubkey) } .onSuccess { response -> val alias = response.alias?.takeIf { it.isNotBlank() } if (alias != null) return alias } // 2. Fall back to 1ml.com runCatching { oneMlApi.getNode(pubkey) } .onSuccess { response -> val alias = response.alias?.takeIf { it.isNotBlank() } if (alias != null) return alias } return null } }