1515package api
1616
1717import (
18- "encoding/hex"
1918 "fmt"
2019 "io"
2120 "time"
@@ -32,7 +31,6 @@ import (
3231 "github.com/penglongli/gin-metrics/ginmetrics"
3332 swaggerFiles "github.com/swaggo/files" // swagger embed files
3433 ginSwagger "github.com/swaggo/gin-swagger" // gin-swagger middleware
35- "golang.org/x/crypto/blake2b"
3634
3735 _ "github.com/blinklabs-io/tx-submit-api/docs" // docs is generated by Swag CLI
3836)
@@ -258,22 +256,21 @@ func handleSubmitTx(c *gin.Context) {
258256 if err := c .Request .Body .Close (); err != nil {
259257 logger .Errorf ("failed to close request body: %s" , err )
260258 }
261- // Unwrap raw transaction bytes into a CBOR array
262- var txUnwrap []cbor.RawMessage
263- if err := cbor .Unmarshal (txRawBytes , & txUnwrap ); err != nil {
264- logger .Errorf ("failed to unwrap transaction CBOR: %s" , err )
265- c .JSON (400 , fmt .Sprintf ("failed to unwrap transaction CBOR: %s" , err ))
259+ // Determine transaction type (era)
260+ txType , err := ledger .DetermineTransactionType (txRawBytes )
261+ if err != nil {
262+ logger .Errorf ("could not parse transaction to determine type: %s" , err )
263+ c .JSON (400 , "could not parse transaction to determine type" )
264+ _ = ginmetrics .GetMonitor ().GetMetric ("tx_submit_fail_count" ).Inc (nil )
265+ return
266+ }
267+ tx , err := ledger .NewTransactionFromCbor (txType , txRawBytes )
268+ if err != nil {
269+ logger .Errorf ("failed to parse transaction CBOR: %s" , err )
270+ c .JSON (400 , fmt .Sprintf ("failed to parse transaction CBOR: %s" , err ))
266271 _ = ginmetrics .GetMonitor ().GetMetric ("tx_submit_fail_count" ).Inc (nil )
267272 return
268273 }
269- // index 0 is the transaction body
270- // Store index 0 (transaction body) as byte array
271- txBody := txUnwrap [0 ]
272-
273- // Convert the body into a blake2b256 hash string
274- txIdHash := blake2b .Sum256 (txBody )
275- // Encode hash string as byte array to hex string
276- txIdHex := hex .EncodeToString (txIdHash [:])
277274 // Connect to cardano-node and submit TX
278275 errorChan := make (chan error )
279276 oConn , err := ouroboros .NewConnection (
@@ -320,12 +317,6 @@ func handleSubmitTx(c *gin.Context) {
320317 // Close Ouroboros connection
321318 oConn .Close ()
322319 }()
323- // Determine transaction type (era)
324- txType , err := ledger .DetermineTransactionType (txRawBytes )
325- if err != nil {
326- c .JSON (400 , "could not parse transaction to determine type" )
327- return
328- }
329320 // Submit the transaction
330321 if err := oConn .LocalTxSubmission ().Client .SubmitTx (uint16 (txType ), txRawBytes ); err != nil {
331322 if c .GetHeader ("Accept" ) == "application/cbor" {
@@ -339,7 +330,7 @@ func handleSubmitTx(c *gin.Context) {
339330 return
340331 }
341332 // Return transaction ID
342- c .JSON (202 , txIdHex )
333+ c .JSON (202 , tx . Hash () )
343334 // Increment custom metric
344335 _ = ginmetrics .GetMonitor ().GetMetric ("tx_submit_count" ).Inc (nil )
345336}
0 commit comments