diff --git a/api/api.go b/api/api.go index 1d6488a..e937fd8 100644 --- a/api/api.go +++ b/api/api.go @@ -3,17 +3,15 @@ package api import ( "bytes" "context" - "encoding/hex" "fmt" "io" "net/http" "net/http/httptrace" "time" - "github.com/fxamacker/cbor/v2" + "github.com/blinklabs-io/gouroboros/ledger" ginzap "github.com/gin-contrib/zap" "github.com/gin-gonic/gin" - "golang.org/x/crypto/blake2b" "github.com/blinklabs-io/tx-submit-api-mirror/config" "github.com/blinklabs-io/tx-submit-api-mirror/logging" @@ -66,16 +64,20 @@ func handleSubmitTx(c *gin.Context) { logger.Errorf("failed to close request body: %s", err) } logger.Debugf("transaction dump: %x", rawTx) - // Unwrap transaction and calculate ID - var txUnwrap []cbor.RawMessage - if err := cbor.Unmarshal(rawTx, &txUnwrap); err != nil { - logger.Errorf("failed to unwrap transaction CBOR: %s", err) - c.String(400, fmt.Sprintf("failed to unwrap transaction CBOR: %s", err)) + // Determine transaction type (era) + txType, err := ledger.DetermineTransactionType(rawTx) + if err != nil { + logger.Errorf("could not parse transaction to determine type: %s", err) + c.JSON(400, "could not parse transaction to determine type") + return + } + tx, err := ledger.NewTransactionFromCbor(txType, rawTx) + if err != nil { + logger.Errorf("failed to parse transaction CBOR: %s", err) + c.String(400, fmt.Sprintf("failed to parse transaction CBOR: %s", err)) return } - txId := blake2b.Sum256(txUnwrap[0]) - txIdHex := hex.EncodeToString(txId[:]) - logger.Debugf("calculated transaction ID %s", txIdHex) + logger.Debugf("transaction ID %s", tx.Hash()) // Send request to each backend for _, backend := range cfg.Backends { go func(backend string) { @@ -107,12 +109,12 @@ func handleSubmitTx(c *gin.Context) { } defer resp.Body.Close() if resp.StatusCode == 202 { - logger.Infow(fmt.Sprintf("successfully submitted transaction %s to backend %s", txIdHex, backend), "latency", elapsedTime.Seconds(), "connReused", connReused) + logger.Infow(fmt.Sprintf("successfully submitted transaction %s to backend %s", tx.Hash(), backend), "latency", elapsedTime.Seconds(), "connReused", connReused) } else { 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) } }(backend) } // Return transaction ID - c.String(202, txIdHex) + c.String(202, tx.Hash()) } diff --git a/go.mod b/go.mod index 5fc9ebd..47f92ed 100644 --- a/go.mod +++ b/go.mod @@ -3,12 +3,11 @@ module github.com/blinklabs-io/tx-submit-api-mirror go 1.20 require ( - github.com/fxamacker/cbor/v2 v2.5.0 + github.com/blinklabs-io/gouroboros v0.57.0 github.com/gin-contrib/zap v0.2.0 github.com/gin-gonic/gin v1.9.1 github.com/kelseyhightower/envconfig v1.4.0 go.uber.org/zap v1.26.0 - golang.org/x/crypto v0.14.0 gopkg.in/yaml.v2 v2.4.0 ) @@ -16,6 +15,7 @@ require ( github.com/bytedance/sonic v1.10.0 // indirect github.com/chenzhuoyu/base64x v0.0.0-20230717121745-296ad89f973d // indirect github.com/chenzhuoyu/iasm v0.9.0 // indirect + github.com/fxamacker/cbor/v2 v2.5.0 // indirect github.com/gabriel-vasile/mimetype v1.4.2 // indirect github.com/gin-contrib/sse v0.1.0 // indirect github.com/go-playground/locales v0.14.1 // indirect @@ -23,6 +23,7 @@ require ( github.com/go-playground/validator/v10 v10.15.3 // indirect github.com/goccy/go-json v0.10.2 // indirect github.com/google/go-cmp v0.5.8 // indirect + github.com/jinzhu/copier v0.4.0 // indirect github.com/json-iterator/go v1.1.12 // indirect github.com/klauspost/cpuid/v2 v2.2.5 // indirect github.com/leodido/go-urn v1.2.4 // indirect @@ -35,6 +36,7 @@ require ( github.com/x448/float16 v0.8.4 // indirect go.uber.org/multierr v1.11.0 // indirect golang.org/x/arch v0.5.0 // indirect + golang.org/x/crypto v0.14.0 // indirect golang.org/x/net v0.17.0 // indirect golang.org/x/sys v0.13.0 // indirect golang.org/x/text v0.13.0 // indirect diff --git a/go.sum b/go.sum index 1f9503b..ca30678 100644 --- a/go.sum +++ b/go.sum @@ -1,3 +1,5 @@ +github.com/blinklabs-io/gouroboros v0.57.0 h1:k5Y706vvYAGM3bCtEhh6WRHGrvS3S6n1MT9vNLFAe/E= +github.com/blinklabs-io/gouroboros v0.57.0/go.mod h1:D5YJka8EyVmiXNMbRvjH23H9lNMLA4+qSlNNC/j7R0k= github.com/bytedance/sonic v1.5.0/go.mod h1:ED5hyg4y6t3/9Ku1R6dU/4KyJ48DZ4jPhfY1O2AihPM= github.com/bytedance/sonic v1.10.0-rc/go.mod h1:ElCzW+ufi8qKqNW0FY314xriJhyJhuoJ3gFZdAHF7NM= github.com/bytedance/sonic v1.10.0 h1:qtNZduETEIWJVIyDl01BeNxur2rW9OwTQ/yBqFRkKEk= @@ -35,6 +37,8 @@ github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/ github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg= github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg= +github.com/jinzhu/copier v0.4.0 h1:w3ciUoD19shMCRargcpm0cm91ytaBhDvuRpz1ODO/U8= +github.com/jinzhu/copier v0.4.0/go.mod h1:DfbEm0FYsaqBcKcFuvmOZb218JkPGtvSHsKg8S8hyyg= github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM= github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo= github.com/kelseyhightower/envconfig v1.4.0 h1:Im6hONhd3pLkfDFsbRgu68RDNkGF1r3dvMUtDTo2cv8=