diff --git a/src/main/scala/io/github/rorp/bolt12addressplugin/Bolt12Address.scala b/src/main/scala/io/github/rorp/bolt12addressplugin/Bolt12Address.scala index e66ea85..3023f9c 100644 --- a/src/main/scala/io/github/rorp/bolt12addressplugin/Bolt12Address.scala +++ b/src/main/scala/io/github/rorp/bolt12addressplugin/Bolt12Address.scala @@ -1,9 +1,5 @@ package io.github.rorp.bolt12addressplugin -import fr.acinq.eclair.wire.protocol.OfferTypes.Offer -import org.minidns.hla.{DnssecResolverApi, ResolverResult} -import org.minidns.record.TXT - import scala.util.Try case class Bolt12Address(address: String) { diff --git a/src/main/scala/io/github/rorp/bolt12addressplugin/OfferFetcher.scala b/src/main/scala/io/github/rorp/bolt12addressplugin/OfferFetcher.scala index f9de4eb..45fe330 100644 --- a/src/main/scala/io/github/rorp/bolt12addressplugin/OfferFetcher.scala +++ b/src/main/scala/io/github/rorp/bolt12addressplugin/OfferFetcher.scala @@ -12,7 +12,7 @@ import sttp.model.{HeaderNames, Uri} import java.net.InetSocketAddress import scala.concurrent.duration.{Duration, DurationInt, FiniteDuration} import scala.concurrent.{Await, ExecutionContext, Future} -import scala.util.{Failure, Try} +import scala.util.Try trait OfferFetcher { def fetchOffer(bolt12Address: Bolt12Address): Future[Offer] @@ -57,7 +57,7 @@ class Dns extends OfferFetcher { } else { throw new RuntimeException(s"invalid DNS data: `$txt`") } - res = offerString :: res + res = offerString.replace(" / ", "") :: res } res } @@ -71,28 +71,6 @@ class DnsOverHttps(socksProxy_opt: Option[Socks5ProxyParams])(implicit ec: Execu private val sttp = createSttpBackend(socksProxy_opt) - private def extractOfferStrings(body: String): Try[Seq[String]] = Try { - import io.github.rorp.bolt12addressplugin.DnsOverHttps.DnsResponse - val serialization = org.json4s.jackson.Serialization - implicit val formats = org.json4s.DefaultFormats - val json = serialization.read[DnsResponse](body) - val res = json.Answer.map { data => - val txt = { - val data = json.Answer.headOption.getOrElse(throw new RuntimeException(s"invalid DNS response: $json")).data - val data1 = if (data.startsWith("\"")) data.tail else data - if (data1.endsWith("\"")) data1.init else data1 - } - if (txt.startsWith(Bolt12Address.Prefix)) { - txt.substring(Bolt12Address.Prefix.length) - } else if (txt.startsWith(Bolt12Address.LegacyPrefix)) { - txt.substring(Bolt12Address.LegacyPrefix.length) - } else { - throw new RuntimeException(s"invalid DNS data: `$txt`") - } - } - res - } - override def fetchOffer(bolt12Address: Bolt12Address): Future[Offer] = { val parametrizedUri = BaseUri.addParam("name", bolt12Address.toDomainName.get).addParam("type", "TXT") val request = basicRequest @@ -112,6 +90,29 @@ class DnsOverHttps(socksProxy_opt: Option[Socks5ProxyParams])(implicit ec: Execu } } + private def extractOfferStrings(body: String): Try[Seq[String]] = Try { + import io.github.rorp.bolt12addressplugin.DnsOverHttps.DnsResponse + val serialization = org.json4s.jackson.Serialization + implicit val formats = org.json4s.DefaultFormats + val json = serialization.read[DnsResponse](body) + val answer = json.Answer.filter(_.`type` == 16) // TXT + if (answer.isEmpty) throw new RuntimeException(s"invalid DNS response: $json") + val res = answer.map { dns => + val txt = { + val data = dns.data + data.replace("\"", "") + } + if (txt.startsWith(Bolt12Address.Prefix)) { + txt.substring(Bolt12Address.Prefix.length) + } else if (txt.startsWith(Bolt12Address.LegacyPrefix)) { + txt.substring(Bolt12Address.LegacyPrefix.length) + } else { + throw new RuntimeException(s"invalid DNS data: `$txt`") + } + } + res + } + private def createSttpBackend(socksProxy_opt: Option[Socks5ProxyParams]): SttpBackend[Future, _] = { val options = SttpBackendOptions(connectionTimeout = 30.seconds, proxy = None) val sttpBackendOptions: SttpBackendOptions = socksProxy_opt match { diff --git a/src/main/scala/io/github/rorp/bolt12addressplugin/PaymentHandler.scala b/src/main/scala/io/github/rorp/bolt12addressplugin/PaymentHandler.scala index 1a7f48c..94dfa77 100644 --- a/src/main/scala/io/github/rorp/bolt12addressplugin/PaymentHandler.scala +++ b/src/main/scala/io/github/rorp/bolt12addressplugin/PaymentHandler.scala @@ -26,26 +26,26 @@ case class PaymentHandler(appKit: Kit) { private val externalIdMaxLength = 66 def payOffer(offer: Offer, - amount: MilliSatoshi, - quantity: Long, - externalId_opt: Option[String], - maxAttempts_opt: Option[Int], - maxFeeFlat_opt: Option[Satoshi], - maxFeePct_opt: Option[Double], - pathFindingExperimentName_opt: Option[String], - connectDirectly: Boolean)(implicit timeout: Timeout): Future[UUID] = { + amount: MilliSatoshi, + quantity: Long, + externalId_opt: Option[String], + maxAttempts_opt: Option[Int], + maxFeeFlat_opt: Option[Satoshi], + maxFeePct_opt: Option[Double], + pathFindingExperimentName_opt: Option[String], + connectDirectly: Boolean)(implicit timeout: Timeout): Future[UUID] = { payOfferInternal(offer, amount, quantity, None, Nil, externalId_opt, maxAttempts_opt, maxFeeFlat_opt, maxFeePct_opt, pathFindingExperimentName_opt, connectDirectly, blocking = false).mapTo[UUID] } def payOfferBlocking(offer: Offer, - amount: MilliSatoshi, - quantity: Long, - externalId_opt: Option[String], - maxAttempts_opt: Option[Int], - maxFeeFlat_opt: Option[Satoshi], - maxFeePct_opt: Option[Double], - pathFindingExperimentName_opt: Option[String], - connectDirectly: Boolean)(implicit timeout: Timeout): Future[PaymentEvent] = { + amount: MilliSatoshi, + quantity: Long, + externalId_opt: Option[String], + maxAttempts_opt: Option[Int], + maxFeeFlat_opt: Option[Satoshi], + maxFeePct_opt: Option[Double], + pathFindingExperimentName_opt: Option[String], + connectDirectly: Boolean)(implicit timeout: Timeout): Future[PaymentEvent] = { payOfferInternal(offer, amount, quantity, None, Nil, externalId_opt, maxAttempts_opt, maxFeeFlat_opt, maxFeePct_opt, pathFindingExperimentName_opt, connectDirectly, blocking = true).mapTo[PaymentEvent] }