@@ -3,17 +3,15 @@ package api
3
3
import (
4
4
"bytes"
5
5
"context"
6
- "encoding/hex"
7
6
"fmt"
8
7
"io"
9
8
"net/http"
10
9
"net/http/httptrace"
11
10
"time"
12
11
13
- "github.com/fxamacker/cbor/v2 "
12
+ "github.com/blinklabs-io/gouroboros/ledger "
14
13
ginzap "github.com/gin-contrib/zap"
15
14
"github.com/gin-gonic/gin"
16
- "golang.org/x/crypto/blake2b"
17
15
18
16
"github.com/blinklabs-io/tx-submit-api-mirror/config"
19
17
"github.com/blinklabs-io/tx-submit-api-mirror/logging"
@@ -66,16 +64,20 @@ func handleSubmitTx(c *gin.Context) {
66
64
logger .Errorf ("failed to close request body: %s" , err )
67
65
}
68
66
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 ))
74
78
return
75
79
}
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 ())
79
81
// Send request to each backend
80
82
for _ , backend := range cfg .Backends {
81
83
go func (backend string ) {
@@ -107,12 +109,12 @@ func handleSubmitTx(c *gin.Context) {
107
109
}
108
110
defer resp .Body .Close ()
109
111
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 )
111
113
} else {
112
114
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 )
113
115
}
114
116
}(backend )
115
117
}
116
118
// Return transaction ID
117
- c .String (202 , txIdHex )
119
+ c .String (202 , tx . Hash () )
118
120
}
0 commit comments