Skip to content

Commit

Permalink
Merge pull request #146 from blinklabs-io/fix/use-gouroboros-hash
Browse files Browse the repository at this point in the history
fix: use gouroboros tx hash
  • Loading branch information
wolf31o2 authored Oct 12, 2023
2 parents 6ced438 + aa6ff43 commit ae8be90
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions internal/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package api

import (
"encoding/hex"
"fmt"
"io"
"time"
Expand All @@ -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
)
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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" {
Expand All @@ -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)
}

0 comments on commit ae8be90

Please sign in to comment.