-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
166 additions
and
6 deletions.
There are no files selected for viewing
20 changes: 20 additions & 0 deletions
20
...-api/src/main/scala/org/ergoplatform/explorer/http/api/v1/defs/ErgoTreeEndpointDefs.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package org.ergoplatform.explorer.http.api.v1.defs | ||
|
||
import org.ergoplatform.explorer.http.api.ApiErr | ||
import org.ergoplatform.explorer.http.api.commonDirectives._ | ||
import org.ergoplatform.explorer.http.api.v1.models.{ErgoTreeConversionRequest, ErgoTreeHuman} | ||
import sttp.tapir._ | ||
import sttp.tapir.json.circe._ | ||
|
||
final class ErgoTreeEndpointDefs[F[_]]() { | ||
|
||
private val PathPrefix = "ergotree" | ||
|
||
def endpoints: List[Endpoint[_, _, _, _]] = List(convertErgoTreeDef) | ||
|
||
def convertErgoTreeDef: Endpoint[ErgoTreeConversionRequest, ApiErr, ErgoTreeHuman, Any] = | ||
baseEndpointDef.post | ||
.in(PathPrefix / "convert") | ||
.in(jsonBody[ErgoTreeConversionRequest]) | ||
.out(jsonBody[ErgoTreeHuman]) | ||
} |
32 changes: 32 additions & 0 deletions
32
...lorer-api/src/main/scala/org/ergoplatform/explorer/http/api/v1/models/ErgoTreeHuman.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.ergoplatform.explorer.http.api.v1.models | ||
|
||
import derevo.circe.{decoder, encoder} | ||
import derevo.derive | ||
import sttp.tapir.{Schema, Validator} | ||
|
||
@derive(encoder, decoder) | ||
final case class ErgoTreeHuman(constants: String, script: String) | ||
|
||
object ErgoTreeHuman { | ||
|
||
implicit val schema: Schema[ErgoTreeHuman] = | ||
Schema | ||
.derived[ErgoTreeHuman] | ||
.modify(_.constants)(_.description("Constants use in ergo script")) | ||
.modify(_.script)(_.description("Human readable ergo script")) | ||
|
||
implicit val validator: Validator[ErgoTreeHuman] = schema.validator | ||
} | ||
|
||
@derive(encoder, decoder) | ||
final case class ErgoTreeConversionRequest(hashed: String) | ||
|
||
object ErgoTreeConversionRequest { | ||
|
||
implicit val schema: Schema[ErgoTreeConversionRequest] = | ||
Schema | ||
.derived[ErgoTreeConversionRequest] | ||
.modify(_.hashed)(_.description("Hashed value of ergo script")) | ||
|
||
implicit val validator: Validator[ErgoTreeConversionRequest] = schema.validator | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
...orer-api/src/main/scala/org/ergoplatform/explorer/http/api/v1/models/PrettyErgoTree.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package org.ergoplatform.explorer.http.api.v1.models | ||
|
||
import org.ergoplatform.explorer.HexString | ||
import sigmastate.PrettyPrintErgoTree | ||
import sigmastate.lang.exceptions.SerializerException | ||
import sigmastate.serialization.ErgoTreeSerializer.DefaultSerializer | ||
|
||
object PrettyErgoTree { | ||
def fromString(s: String) : Either[PrettyErgoTreeError, ErgoTreeHuman] = { | ||
HexString.fromString[Either[Throwable, *]](s) match { | ||
case Left(_) => Left(PrettyErgoTreeError.BadEncoding) | ||
case Right(hexString) => fromHexString(hexString) | ||
} | ||
} | ||
|
||
def fromHexString(h: HexString): Either[PrettyErgoTreeError, ErgoTreeHuman] = { | ||
try { | ||
val ergoTree = DefaultSerializer.deserializeErgoTree(h.bytes) | ||
ergoTree.root match { | ||
case Left(_) => Left(PrettyErgoTreeError.UnparsedErgoTree) | ||
case Right(value) => | ||
val script = PrettyPrintErgoTree.prettyPrint(value, width = 160) | ||
val constants = ergoTree.constants.zipWithIndex.map { case (c, i) => s"$i: ${c.value}" }.mkString("\n") | ||
Right(ErgoTreeHuman(constants, script)) | ||
} | ||
} catch { | ||
case se: SerializerException => Left(PrettyErgoTreeError.DeserializeException(se.message)) | ||
} | ||
} | ||
} | ||
|
||
sealed trait PrettyErgoTreeError | ||
object PrettyErgoTreeError { | ||
case object BadEncoding extends PrettyErgoTreeError | ||
case class DeserializeException(msg: String) extends PrettyErgoTreeError | ||
case object UnparsedErgoTree extends PrettyErgoTreeError | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
...orer-api/src/main/scala/org/ergoplatform/explorer/http/api/v1/routes/ErgoTreeRoutes.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package org.ergoplatform.explorer.http.api.v1.routes | ||
|
||
import cats.MonadError | ||
import cats.effect.{Concurrent, ContextShift, Timer} | ||
import cats.syntax.semigroupk._ | ||
import cats.syntax.either._ | ||
import io.chrisdavenport.log4cats.Logger | ||
import org.ergoplatform.explorer.HexString | ||
import org.ergoplatform.explorer.http.api.ApiErr | ||
import org.ergoplatform.explorer.http.api.algebra.AdaptThrowable.AdaptThrowableEitherT | ||
import org.ergoplatform.explorer.http.api.syntax.adaptThrowable._ | ||
import org.ergoplatform.explorer.http.api.syntax.routes._ | ||
import org.ergoplatform.explorer.http.api.v1.defs.ErgoTreeEndpointDefs | ||
import org.ergoplatform.explorer.http.api.v1.models.{ErgoTreeHuman, PrettyErgoTree, PrettyErgoTreeError} | ||
import org.http4s.HttpRoutes | ||
import sttp.tapir.server.http4s._ | ||
|
||
|
||
final class ErgoTreeRoutes[ | ||
F[_]: Concurrent: ContextShift: Timer: AdaptThrowableEitherT[*[_], ApiErr] | ||
]()(implicit opts: Http4sServerOptions[F, F], F: MonadError[F, Throwable]) { | ||
|
||
val defs = new ErgoTreeEndpointDefs | ||
|
||
val routes: HttpRoutes[F] = convertErgoTreeR | ||
|
||
private def interpreter = Http4sServerInterpreter(opts) | ||
|
||
private def convertErgoTreeR: HttpRoutes[F] = | ||
interpreter.toRoutes(defs.convertErgoTreeDef) { req => { | ||
val maybeTree = PrettyErgoTree.fromString(req.hashed).leftMap{ | ||
case PrettyErgoTreeError.BadEncoding => ApiErr.badRequest(s"Could not decode hex string: ${req.hashed}") | ||
case PrettyErgoTreeError.UnparsedErgoTree => ApiErr.badRequest(s"Could not parse ergo tree") | ||
case PrettyErgoTreeError.DeserializeException(msg) => ApiErr.unknownErr(msg) | ||
} | ||
F.pure(maybeTree) | ||
}} | ||
} | ||
|
||
object ErgoTreeRoutes { | ||
def apply[F[_]: Concurrent: ContextShift: Timer: Logger]()(implicit opts: Http4sServerOptions[F, F]): HttpRoutes[F] = | ||
new ErgoTreeRoutes[F]().routes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters