Skip to content

Commit 4403a2c

Browse files
authored
Merge pull request #49 from cloudstruct/feature/parse-tx-for-type
Parse transaction to determine type/era
2 parents 371857a + eefe7ab commit 4403a2c

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ go 1.17
44

55
require (
66
github.com/Bitrue-exchange/libada-go v0.0.1-rc.0.20220817020305-79d8b4c4dd9c
7+
github.com/cloudstruct/go-cardano-ledger v0.2.1
78
github.com/cloudstruct/go-ouroboros-network v0.14.0
89
github.com/fxamacker/cbor/v2 v2.4.0
910
github.com/gin-contrib/zap v0.0.2

go.sum

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWR
6161
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
6262
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
6363
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
64+
github.com/cloudstruct/go-cardano-ledger v0.2.1 h1:J5DAs5GiP4iROwnukEgQhRdSh94cFFGifjnFNnFBSgU=
65+
github.com/cloudstruct/go-cardano-ledger v0.2.1/go.mod h1:EBB9JG+3nBs3IV0p751yz1468XYkzJce1/A2X+xH8L8=
6466
github.com/cloudstruct/go-ouroboros-network v0.14.0 h1:1X56wtgo2a8BLYNuCADjWH1mC6Z0NjkkYrfNtXbyzE0=
6567
github.com/cloudstruct/go-ouroboros-network v0.14.0/go.mod h1:06ggUMerJLwKJkVi5DLX1QMDz8r+R8o92rht7Z4ss9k=
6668
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
@@ -349,6 +351,7 @@ golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5y
349351
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
350352
golang.org/x/crypto v0.0.0-20211215153901-e495a2d5b3d3/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
351353
golang.org/x/crypto v0.0.0-20220131195533-30dcbda58838/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
354+
golang.org/x/crypto v0.0.0-20220824171710-5757bc0c5503/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
352355
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d h1:3qF+Z8Hkrw9sOhrFHti9TlB1Hkac1x+DNRkv0XQiFjo=
353356
golang.org/x/crypto v0.0.0-20220826181053-bd7e27e6170d/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
354357
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=

internal/api/api.go

Lines changed: 37 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ package api
33
import (
44
"encoding/hex"
55
"fmt"
6+
"github.com/cloudstruct/go-cardano-ledger"
67
"github.com/cloudstruct/go-cardano-submit-api/internal/config"
78
"github.com/cloudstruct/go-cardano-submit-api/internal/logging"
89
ouroboros "github.com/cloudstruct/go-ouroboros-network"
9-
"github.com/cloudstruct/go-ouroboros-network/block"
1010
"github.com/cloudstruct/go-ouroboros-network/protocol/localtxsubmission"
1111
"github.com/fxamacker/cbor/v2"
1212
ginzap "github.com/gin-contrib/zap"
@@ -54,8 +54,8 @@ func Start(cfg *config.Config) error {
5454
}
5555
router.Use(ginzap.GinzapWithConfig(accessLogger, &ginzap.Config{
5656
TimeFormat: time.RFC3339,
57-
UTC: true,
58-
SkipPaths: skipPaths,
57+
UTC: true,
58+
SkipPaths: skipPaths,
5959
}))
6060
router.Use(ginzap.RecoveryWithZap(accessLogger, true))
6161

@@ -235,12 +235,44 @@ func handleSubmitTx(c *gin.Context) {
235235
},
236236
}
237237
oConn.LocalTxSubmission.Start(localTxSubmissionCallbackConfig)
238-
// TODO: figure out better way to determine era
239-
if err = oConn.LocalTxSubmission.SubmitTx(block.TX_TYPE_ALONZO, txRawBytes); err != nil {
238+
// Determine transaction type (era)
239+
txType, err := determineTransactionType(txRawBytes)
240+
if err != nil {
241+
c.String(400, "could not parse transaction to determine type")
242+
return
243+
}
244+
// Submit the transaction
245+
if err := oConn.LocalTxSubmission.SubmitTx(txType, txRawBytes); err != nil {
240246
logger.Errorf("failure submitting transaction: %s", err)
241247
c.String(500, "failure communicating with node")
242248
return
243249
}
244250
// Wait for async process to finish
245251
<-doneChan
246252
}
253+
254+
func determineTransactionType(data []byte) (uint16, error) {
255+
// TODO: uncomment this once the following issue is resolved:
256+
// https://github.com/cloudstruct/go-cardano-ledger/issues/9
257+
/*
258+
if _, err := ledger.NewByronTransactionFromCbor(data); err == nil {
259+
return ledger.TX_TYPE_BYRON, nil
260+
}
261+
*/
262+
if _, err := ledger.NewShelleyTransactionFromCbor(data); err == nil {
263+
return ledger.TX_TYPE_SHELLEY, nil
264+
}
265+
if _, err := ledger.NewAllegraTransactionFromCbor(data); err == nil {
266+
return ledger.TX_TYPE_ALLEGRA, nil
267+
}
268+
if _, err := ledger.NewMaryTransactionFromCbor(data); err == nil {
269+
return ledger.TX_TYPE_MARY, nil
270+
}
271+
if _, err := ledger.NewAlonzoTransactionFromCbor(data); err == nil {
272+
return ledger.TX_TYPE_ALONZO, nil
273+
}
274+
if _, err := ledger.NewBabbageTransactionFromCbor(data); err == nil {
275+
return ledger.TX_TYPE_BABBAGE, nil
276+
}
277+
return 0, fmt.Errorf("unknown transaction type")
278+
}

0 commit comments

Comments
 (0)