mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-03 10:46:42 +01:00
Add LNURL writers (#4927)
This commit is contained in:
parent
96392edde7
commit
f8247c427d
3 changed files with 33 additions and 9 deletions
|
@ -16,6 +16,7 @@ import org.bitcoins.core.wallet.fee.SatoshisPerVirtualByte
|
|||
import org.bitcoins.crypto._
|
||||
import play.api.libs.json._
|
||||
|
||||
import java.net.URL
|
||||
import scala.collection.mutable
|
||||
|
||||
// for mapWrites below
|
||||
|
@ -23,6 +24,8 @@ import scala.language.implicitConversions
|
|||
|
||||
object JsonWriters {
|
||||
|
||||
implicit val urlWrites: Writes[URL] = (url: URL) => JsString(url.toString)
|
||||
|
||||
implicit object HashTypeWrites extends Writes[HashType] {
|
||||
|
||||
override def writes(hash: HashType): JsValue =
|
||||
|
|
|
@ -73,21 +73,20 @@ class LnURLClient(proxyParams: Option[Socks5ProxyParams])(implicit
|
|||
def getInvoice(
|
||||
pay: LnURLPayResponse,
|
||||
amount: LnCurrencyUnit): Future[LnInvoice] = {
|
||||
getInvoice(pay, amount.toSatoshis)
|
||||
}
|
||||
|
||||
def getInvoice(
|
||||
pay: LnURLPayResponse,
|
||||
amount: MilliSatoshis): Future[LnInvoice] = {
|
||||
getInvoice(pay, amount.toSatoshis)
|
||||
getInvoice(pay, amount.toMSat)
|
||||
}
|
||||
|
||||
def getInvoice(
|
||||
pay: LnURLPayResponse,
|
||||
amount: CurrencyUnit): Future[LnInvoice] = {
|
||||
val msats = MilliSatoshis(amount)
|
||||
getInvoice(pay, MilliSatoshis(amount))
|
||||
}
|
||||
|
||||
def getInvoice(
|
||||
pay: LnURLPayResponse,
|
||||
amount: MilliSatoshis): Future[LnInvoice] = {
|
||||
val symbol = if (pay.callback.toString.contains("?")) "&" else "?"
|
||||
val url = s"${pay.callback}${symbol}amount=${msats.toLong}"
|
||||
val url = s"${pay.callback}${symbol}amount=${amount.toLong}"
|
||||
sendRequestAndParse[LnURLPayInvoice](Get(url)).map(_.pr)
|
||||
}
|
||||
|
||||
|
|
|
@ -4,6 +4,7 @@ import org.bitcoins.core.protocol.ln.currency.MilliSatoshis
|
|||
import org.bitcoins.lnurl.json.LnURLTag._
|
||||
import play.api.libs.json._
|
||||
import org.bitcoins.commons.serializers.JsonReaders._
|
||||
import org.bitcoins.commons.serializers.JsonWriters._
|
||||
import org.bitcoins.core.protocol.ln.LnInvoice
|
||||
|
||||
import java.net._
|
||||
|
@ -22,6 +23,9 @@ object LnURLJsonModels {
|
|||
|
||||
implicit val LnURLStatusReads: Reads[LnURLStatus] = Json.reads[LnURLStatus]
|
||||
|
||||
implicit val LnURLStatusWrites: OWrites[LnURLStatus] =
|
||||
Json.writes[LnURLStatus]
|
||||
|
||||
case class LnURLSuccessAction(
|
||||
tag: SuccessActionTag,
|
||||
message: Option[String],
|
||||
|
@ -33,6 +37,9 @@ object LnURLJsonModels {
|
|||
implicit val LnURLSuccessActionReads: Reads[LnURLSuccessAction] =
|
||||
Json.reads[LnURLSuccessAction]
|
||||
|
||||
implicit val LnURLSuccessActionWrites: OWrites[LnURLSuccessAction] =
|
||||
Json.writes[LnURLSuccessAction]
|
||||
|
||||
case class LnURLPayResponse(
|
||||
callback: URL,
|
||||
maxSendable: MilliSatoshis,
|
||||
|
@ -46,6 +53,10 @@ object LnURLJsonModels {
|
|||
implicit val LnURLPayResponseReads: Reads[LnURLPayResponse] =
|
||||
Json.reads[LnURLPayResponse]
|
||||
|
||||
implicit val LnURLPayResponseWrites: OWrites[LnURLPayResponse] = { o =>
|
||||
Json.writes[LnURLPayResponse].writes(o) ++ Json.obj("tag" -> "payRequest")
|
||||
}
|
||||
|
||||
case class LnURLPayInvoice(
|
||||
pr: LnInvoice,
|
||||
successAction: Option[LnURLSuccessAction])
|
||||
|
@ -54,6 +65,11 @@ object LnURLJsonModels {
|
|||
implicit val LnURLPayInvoiceReads: Reads[LnURLPayInvoice] =
|
||||
Json.reads[LnURLPayInvoice]
|
||||
|
||||
implicit val LnURLPayInvoiceWrites: OWrites[LnURLPayInvoice] = { o =>
|
||||
Json.writes[LnURLPayInvoice].writes(o) ++ Json.obj(
|
||||
"routes" -> JsArray.empty)
|
||||
}
|
||||
|
||||
case class LnURLWithdrawResponse(
|
||||
callback: URL,
|
||||
k1: String,
|
||||
|
@ -67,6 +83,12 @@ object LnURLJsonModels {
|
|||
implicit val LnURLWithdrawResponseReads: Reads[LnURLWithdrawResponse] =
|
||||
Json.reads[LnURLWithdrawResponse]
|
||||
|
||||
implicit val LnURLWithdrawResponseWrites: OWrites[LnURLWithdrawResponse] = {
|
||||
o =>
|
||||
Json.writes[LnURLWithdrawResponse].writes(o) ++ Json.obj(
|
||||
"tag" -> "withdrawRequest")
|
||||
}
|
||||
|
||||
implicit val LnURLResponseReads: Reads[LnURLResponse] = {
|
||||
case other @ (JsNull | _: JsBoolean | JsNumber(_) | JsString(_) | JsArray(
|
||||
_)) =>
|
||||
|
|
Loading…
Add table
Reference in a new issue