Skip to content

Commit

Permalink
fix the offer parser
Browse files Browse the repository at this point in the history
  • Loading branch information
rorp committed Jul 14, 2024
1 parent 4e728f3 commit 481038d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 44 deletions.
Original file line number Diff line number Diff line change
@@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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]
}

Expand Down

0 comments on commit 481038d

Please sign in to comment.