diff --git a/paymail_service_provider.go b/paymail_service_provider.go index 5c8bd1b7..54266c9f 100644 --- a/paymail_service_provider.go +++ b/paymail_service_provider.go @@ -157,6 +157,10 @@ func (p *PaymailDefaultServiceProvider) RecordTransaction(ctx context.Context, metadata[p2pMetadataField] = p2pTx.MetaData metadata[ReferenceIDField] = p2pTx.Reference + var rts recordIncomingTxStrategy + if err := rts.Validate(); err != nil { + return nil, err + } // Record the transaction rts, err := getIncomingTxRecordStrategy(ctx, p.client, p2pTx.Hex) if err != nil { diff --git a/record_tx_strategy_external_incoming_tx.go b/record_tx_strategy_external_incoming_tx.go index b4b68b65..5641df23 100644 --- a/record_tx_strategy_external_incoming_tx.go +++ b/record_tx_strategy_external_incoming_tx.go @@ -59,6 +59,10 @@ func (strategy *externalIncomingTx) Validate() error { return ErrMissingFieldHex } + if _, err := bt.NewTxFromString(strategy.Hex); err != nil { + return fmt.Errorf("invalid hex: %w", err) + } + return nil // is valid } diff --git a/record_tx_strategy_internal_incoming_tx.go b/record_tx_strategy_internal_incoming_tx.go index 20ecf12d..fa9b04e5 100644 --- a/record_tx_strategy_internal_incoming_tx.go +++ b/record_tx_strategy_internal_incoming_tx.go @@ -5,6 +5,7 @@ import ( "errors" "fmt" + "github.com/libsv/go-bt/v2" "github.com/rs/zerolog" ) @@ -52,6 +53,10 @@ func (strategy *internalIncomingTx) Validate() error { return errors.New("tx cannot be nil") } + if _, err := bt.NewTxFromString(strategy.Tx.Hex); err != nil { + return fmt.Errorf("invalid hex: %w", err) + } + return nil // is valid } diff --git a/record_tx_strategy_outgoing_tx.go b/record_tx_strategy_outgoing_tx.go index ab2d20b0..84a237d6 100644 --- a/record_tx_strategy_outgoing_tx.go +++ b/record_tx_strategy_outgoing_tx.go @@ -70,6 +70,10 @@ func (strategy *outgoingTx) Validate() error { return ErrMissingFieldHex } + if _, err := bt.NewTxFromString(strategy.Hex); err != nil { + return fmt.Errorf("invalid hex: %w", err) + } + if strategy.RelatedDraftID == "" { return errors.New("empty RelatedDraftID") } @@ -82,12 +86,7 @@ func (strategy *outgoingTx) Validate() error { } func (strategy *outgoingTx) TxID() string { - btTx, err := bt.NewTxFromString(strategy.Hex) - - // Return hex if hex is invalid. Handle error in error handlers - if err != nil { - return strategy.Hex - } + btTx, _ := bt.NewTxFromString(strategy.Hex) return btTx.TxID() }