diff --git a/internal/api/api.go b/internal/api/api.go index afb37eb..6bd871f 100644 --- a/internal/api/api.go +++ b/internal/api/api.go @@ -15,7 +15,6 @@ package api import ( - "encoding/hex" "fmt" "io" "time" @@ -32,7 +31,6 @@ import ( "github.com/penglongli/gin-metrics/ginmetrics" swaggerFiles "github.com/swaggo/files" // swagger embed files ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware - "golang.org/x/crypto/blake2b" _ "github.com/blinklabs-io/tx-submit-api/docs" // docs is generated by Swag CLI ) @@ -258,22 +256,21 @@ func handleSubmitTx(c *gin.Context) { if err := c.Request.Body.Close(); err != nil { logger.Errorf("failed to close request body: %s", err) } - // Unwrap raw transaction bytes into a CBOR array - var txUnwrap []cbor.RawMessage - if err := cbor.Unmarshal(txRawBytes, &txUnwrap); err != nil { - logger.Errorf("failed to unwrap transaction CBOR: %s", err) - c.JSON(400, fmt.Sprintf("failed to unwrap transaction CBOR: %s", err)) + // Determine transaction type (era) + txType, err := ledger.DetermineTransactionType(txRawBytes) + if err != nil { + logger.Errorf("could not parse transaction to determine type: %s", err) + c.JSON(400, "could not parse transaction to determine type") + _ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil) + return + } + tx, err := ledger.NewTransactionFromCbor(txType, txRawBytes) + if err != nil { + logger.Errorf("failed to parse transaction CBOR: %s", err) + c.JSON(400, fmt.Sprintf("failed to parse transaction CBOR: %s", err)) _ = ginmetrics.GetMonitor().GetMetric("tx_submit_fail_count").Inc(nil) return } - // index 0 is the transaction body - // Store index 0 (transaction body) as byte array - txBody := txUnwrap[0] - - // Convert the body into a blake2b256 hash string - txIdHash := blake2b.Sum256(txBody) - // Encode hash string as byte array to hex string - txIdHex := hex.EncodeToString(txIdHash[:]) // Connect to cardano-node and submit TX errorChan := make(chan error) oConn, err := ouroboros.NewConnection( @@ -320,12 +317,6 @@ func handleSubmitTx(c *gin.Context) { // Close Ouroboros connection oConn.Close() }() - // Determine transaction type (era) - txType, err := ledger.DetermineTransactionType(txRawBytes) - if err != nil { - c.JSON(400, "could not parse transaction to determine type") - return - } // Submit the transaction if err := oConn.LocalTxSubmission().Client.SubmitTx(uint16(txType), txRawBytes); err != nil { if c.GetHeader("Accept") == "application/cbor" { @@ -339,7 +330,7 @@ func handleSubmitTx(c *gin.Context) { return } // Return transaction ID - c.JSON(202, txIdHex) + c.JSON(202, tx.Hash()) // Increment custom metric _ = ginmetrics.GetMonitor().GetMetric("tx_submit_count").Inc(nil) }