Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Added validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Nazarii-4chain committed Feb 1, 2024
1 parent 7ca3886 commit 058ff37
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 6 deletions.
4 changes: 4 additions & 0 deletions paymail_service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 4 additions & 0 deletions record_tx_strategy_external_incoming_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions record_tx_strategy_internal_incoming_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"fmt"

"github.com/libsv/go-bt/v2"
"github.com/rs/zerolog"
)

Expand Down Expand Up @@ -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
}

Expand Down
11 changes: 5 additions & 6 deletions record_tx_strategy_outgoing_tx.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand All @@ -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()
}

Expand Down

0 comments on commit 058ff37

Please sign in to comment.