mirror of
https://github.com/bitcoin-s/bitcoin-s.git
synced 2025-03-03 10:46:42 +01:00
Add get oracle name to explorer client (#2969)
This commit is contained in:
parent
a194adba98
commit
7b600bb5ba
5 changed files with 47 additions and 13 deletions
|
@ -302,6 +302,13 @@ object JsonReaders {
|
|||
SerializerUtil.processJsString[ECPublicKey](ECPublicKey.fromHex)(json)
|
||||
}
|
||||
|
||||
implicit object SchnorrPublicKeyReads extends Reads[SchnorrPublicKey] {
|
||||
|
||||
override def reads(json: JsValue): JsResult[SchnorrPublicKey] =
|
||||
SerializerUtil.processJsString[SchnorrPublicKey](
|
||||
SchnorrPublicKey.fromHex)(json)
|
||||
}
|
||||
|
||||
implicit object P2PKHAddressReads extends Reads[P2PKHAddress] {
|
||||
|
||||
override def reads(json: JsValue): JsResult[P2PKHAddress] =
|
||||
|
|
|
@ -1,21 +1,17 @@
|
|||
package org.bitcoins.explorer.client
|
||||
|
||||
import akka.actor.ActorSystem
|
||||
import akka.http.scaladsl.model.{
|
||||
ContentTypes,
|
||||
HttpEntity,
|
||||
HttpMethods,
|
||||
HttpRequest,
|
||||
Uri
|
||||
}
|
||||
import akka.http.scaladsl.model._
|
||||
import akka.http.scaladsl.{Http, HttpExt}
|
||||
import akka.util.ByteString
|
||||
import org.bitcoins.core.protocol.tlv.OracleAnnouncementTLV
|
||||
import org.bitcoins.crypto.Sha256Digest
|
||||
import org.bitcoins.core.util.FutureUtil
|
||||
import org.bitcoins.crypto.{SchnorrPublicKey, Sha256Digest}
|
||||
import org.bitcoins.explorer.env.ExplorerEnv
|
||||
import org.bitcoins.explorer.model.{
|
||||
CreateAnnouncementExplorer,
|
||||
CreateAttestations,
|
||||
Oracle,
|
||||
SbAnnouncementEvent
|
||||
}
|
||||
import org.bitcoins.explorer.picklers.ExplorerPicklers
|
||||
|
@ -117,6 +113,21 @@ case class SbExplorerClient(env: ExplorerEnv)(implicit system: ActorSystem) {
|
|||
responseF.map(_ => ())
|
||||
}
|
||||
|
||||
def getOracleName(pubkey: SchnorrPublicKey): Future[Option[String]] = {
|
||||
val base = env.baseUri
|
||||
val uri = Uri(base + s"oracle/${pubkey.hex}")
|
||||
val httpReq = HttpRequest(uri = uri)
|
||||
val responseF = sendRequest(httpReq)
|
||||
responseF.flatMap { response =>
|
||||
val result = response.validate[Oracle]
|
||||
result match {
|
||||
case success: JsSuccess[Oracle] =>
|
||||
Future.successful(Some(success.value.oracleName))
|
||||
case _: JsError => FutureUtil.none
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private def sendRequest(httpReq: HttpRequest): Future[JsValue] = {
|
||||
|
||||
val responsePayloadF: Future[String] = {
|
||||
|
|
|
@ -0,0 +1,5 @@
|
|||
package org.bitcoins.explorer.model
|
||||
|
||||
import org.bitcoins.crypto.SchnorrPublicKey
|
||||
|
||||
case class Oracle(pubkey: SchnorrPublicKey, oracleName: String)
|
|
@ -1,10 +1,12 @@
|
|||
package org.bitcoins.explorer.picklers
|
||||
|
||||
import org.bitcoins.commons.serializers.JsonReaders
|
||||
import org.bitcoins.explorer.model.SbAnnouncementEvent
|
||||
import play.api.libs.json.Json
|
||||
import org.bitcoins.explorer.model._
|
||||
import org.bitcoins.commons.serializers.JsonReaders._
|
||||
import play.api.libs.json.{Json, Reads}
|
||||
|
||||
object ExplorerPicklers {
|
||||
import JsonReaders._
|
||||
implicit val explorerEventRW = Json.reads[SbAnnouncementEvent]
|
||||
|
||||
implicit val explorerEventRW: Reads[SbAnnouncementEvent] =
|
||||
Json.reads[SbAnnouncementEvent]
|
||||
implicit val oracleRW: Reads[Oracle] = Json.reads[Oracle]
|
||||
}
|
||||
|
|
|
@ -48,6 +48,15 @@ class SbExplorerClientTest extends BitcoinSAsyncTest {
|
|||
}
|
||||
}
|
||||
|
||||
it must "get an oracle name" in {
|
||||
val key = announcement.publicKey
|
||||
for {
|
||||
name <- explorerClient.getOracleName(key)
|
||||
} yield {
|
||||
assert(name.contains("Chris_Stewart_5"))
|
||||
}
|
||||
}
|
||||
|
||||
it must "return failure from get an event if the event DNE" in {
|
||||
val hash = Sha256Digest.empty
|
||||
recoverToSucceededIf[RuntimeException] {
|
||||
|
|
Loading…
Add table
Reference in a new issue