From 9d355650cf536f6f0d6f19d610af1c6bc182cf18 Mon Sep 17 00:00:00 2001 From: Alexis Hernandez Date: Sun, 17 Jun 2018 20:39:01 -0500 Subject: [PATCH] server: Add endpoint "POST /transactions" This is a part for #26, it allows to push a hex-encoded transaction to the network. --- .../models/request/SendRawTransactionRequest.scala | 10 ++++++++++ .../xsn/explorer/services/TransactionService.scala | 13 +++++++++++-- server/app/controllers/TransactionsController.scala | 6 ++++++ server/conf/routes | 1 + 4 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 server/app/com/xsn/explorer/models/request/SendRawTransactionRequest.scala diff --git a/server/app/com/xsn/explorer/models/request/SendRawTransactionRequest.scala b/server/app/com/xsn/explorer/models/request/SendRawTransactionRequest.scala new file mode 100644 index 00000000..083f25ca --- /dev/null +++ b/server/app/com/xsn/explorer/models/request/SendRawTransactionRequest.scala @@ -0,0 +1,10 @@ +package com.xsn.explorer.models.request + +import play.api.libs.json.{Json, Reads} + +case class SendRawTransactionRequest(hex: String) + +object SendRawTransactionRequest { + + implicit val reads: Reads[SendRawTransactionRequest] = Json.reads[SendRawTransactionRequest] +} diff --git a/server/app/com/xsn/explorer/services/TransactionService.scala b/server/app/com/xsn/explorer/services/TransactionService.scala index e12752a2..294c6401 100644 --- a/server/app/com/xsn/explorer/services/TransactionService.scala +++ b/server/app/com/xsn/explorer/services/TransactionService.scala @@ -7,11 +7,11 @@ import com.alexitc.playsonify.core.{FutureApplicationResult, FuturePaginatedResu import com.alexitc.playsonify.models.PaginatedQuery import com.alexitc.playsonify.validators.PaginatedQueryValidator import com.xsn.explorer.data.async.TransactionFutureDataHandler -import com.xsn.explorer.errors.{AddressFormatError, TransactionFormatError, TransactionNotFoundError} +import com.xsn.explorer.errors.{AddressFormatError, InvalidRawTransactionError, TransactionFormatError, TransactionNotFoundError} import com.xsn.explorer.models._ import com.xsn.explorer.models.rpc.TransactionVIN import org.scalactic.{Bad, Good, One, Or} -import play.api.libs.json.JsValue +import play.api.libs.json.{JsObject, JsString, JsValue} import scala.concurrent.{ExecutionContext, Future} @@ -89,6 +89,15 @@ class TransactionService @Inject() ( result.toFuture } + def sendRawTransaction(hexString: String): FutureApplicationResult[JsValue] = { + val result = for { + hex <- Or.from(HexString.from(hexString), One(InvalidRawTransactionError)).toFutureOr + _ <- xsnService.sendRawTransaction(hex).toFutureOr + } yield JsObject.empty + ("hex" -> JsString(hex.string)) + + result.toFuture + } + private def getTransactionValue(vin: TransactionVIN): FutureApplicationResult[TransactionValue] = { val valueMaybe = for { value <- vin.value diff --git a/server/app/controllers/TransactionsController.scala b/server/app/controllers/TransactionsController.scala index 054b640f..fa2a7d52 100644 --- a/server/app/controllers/TransactionsController.scala +++ b/server/app/controllers/TransactionsController.scala @@ -2,6 +2,8 @@ package controllers import javax.inject.Inject +import com.alexitc.playsonify.models.PublicContextWithModel +import com.xsn.explorer.models.request.SendRawTransactionRequest import com.xsn.explorer.services.TransactionService import controllers.common.{MyJsonController, MyJsonControllerComponents} @@ -17,4 +19,8 @@ class TransactionsController @Inject() ( def getRawTransaction(txid: String) = publicNoInput { _ => transactionService.getRawTransaction(txid) } + + def sendRawTransaction() = publicWithInput { ctx: PublicContextWithModel[SendRawTransactionRequest] => + transactionService.sendRawTransaction(ctx.model.hex) + } } diff --git a/server/conf/routes b/server/conf/routes index 81f88b5c..ee54c62f 100644 --- a/server/conf/routes +++ b/server/conf/routes @@ -7,6 +7,7 @@ GET /health controllers.HealthController.check() GET /transactions/:txid controllers.TransactionsController.getTransaction(txid: String) GET /transactions/:txid/raw controllers.TransactionsController.getRawTransaction(txid: String) +POST /transactions controllers.TransactionsController.sendRawTransaction() GET /addresses/:address controllers.AddressesController.getDetails(address: String) GET /addresses/:address/transactions controllers.AddressesController.getTransactions(address: String, offset: Int ?= 0, limit: Int ?= 10)