@@ -3,17 +3,15 @@ package api
33import (
44 "bytes"
55 "context"
6- "encoding/hex"
76 "fmt"
87 "io"
98 "net/http"
109 "net/http/httptrace"
1110 "time"
1211
13- "github.com/fxamacker/cbor/v2 "
12+ "github.com/blinklabs-io/gouroboros/ledger "
1413 ginzap "github.com/gin-contrib/zap"
1514 "github.com/gin-gonic/gin"
16- "golang.org/x/crypto/blake2b"
1715
1816 "github.com/blinklabs-io/tx-submit-api-mirror/config"
1917 "github.com/blinklabs-io/tx-submit-api-mirror/logging"
@@ -66,16 +64,20 @@ func handleSubmitTx(c *gin.Context) {
6664 logger .Errorf ("failed to close request body: %s" , err )
6765 }
6866 logger .Debugf ("transaction dump: %x" , rawTx )
69- // Unwrap transaction and calculate ID
70- var txUnwrap []cbor.RawMessage
71- if err := cbor .Unmarshal (rawTx , & txUnwrap ); err != nil {
72- logger .Errorf ("failed to unwrap transaction CBOR: %s" , err )
73- c .String (400 , fmt .Sprintf ("failed to unwrap transaction CBOR: %s" , err ))
67+ // Determine transaction type (era)
68+ txType , err := ledger .DetermineTransactionType (rawTx )
69+ if err != nil {
70+ logger .Errorf ("could not parse transaction to determine type: %s" , err )
71+ c .JSON (400 , "could not parse transaction to determine type" )
72+ return
73+ }
74+ tx , err := ledger .NewTransactionFromCbor (txType , rawTx )
75+ if err != nil {
76+ logger .Errorf ("failed to parse transaction CBOR: %s" , err )
77+ c .String (400 , fmt .Sprintf ("failed to parse transaction CBOR: %s" , err ))
7478 return
7579 }
76- txId := blake2b .Sum256 (txUnwrap [0 ])
77- txIdHex := hex .EncodeToString (txId [:])
78- logger .Debugf ("calculated transaction ID %s" , txIdHex )
80+ logger .Debugf ("transaction ID %s" , tx .Hash ())
7981 // Send request to each backend
8082 for _ , backend := range cfg .Backends {
8183 go func (backend string ) {
@@ -107,12 +109,12 @@ func handleSubmitTx(c *gin.Context) {
107109 }
108110 defer resp .Body .Close ()
109111 if resp .StatusCode == 202 {
110- logger .Infow (fmt .Sprintf ("successfully submitted transaction %s to backend %s" , txIdHex , backend ), "latency" , elapsedTime .Seconds (), "connReused" , connReused )
112+ logger .Infow (fmt .Sprintf ("successfully submitted transaction %s to backend %s" , tx . Hash () , backend ), "latency" , elapsedTime .Seconds (), "connReused" , connReused )
111113 } else {
112114 logger .Errorw (fmt .Sprintf ("failed to send request to backend %s: got response %d, %s" , backend , resp .StatusCode , string (respBody )), "latency" , elapsedTime .Seconds (), "connReused" , connReused )
113115 }
114116 }(backend )
115117 }
116118 // Return transaction ID
117- c .String (202 , txIdHex )
119+ c .String (202 , tx . Hash () )
118120}
0 commit comments