Skip to content

Commit

Permalink
btcutil/psbt: add method Packet.GetTxFee
Browse files Browse the repository at this point in the history
This commit adds a GetTxFee method to the psbt.Packet structure. The method returns the PSBT's fee.
  • Loading branch information
ffranr committed Nov 4, 2022
1 parent 1d767de commit 586729c
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions btcutil/psbt/psbt.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
"bytes"
"encoding/base64"
"errors"

"io"

"github.com/btcsuite/btcd/btcutil"
"github.com/btcsuite/btcd/wire"
)

Expand All @@ -30,7 +30,7 @@ var (

// MaxPsbtValueLength is the size of the largest transaction serialization
// that could be passed in a NonWitnessUtxo field. This is definitely
//less than 4M.
// less than 4M.
const MaxPsbtValueLength = 4000000

// MaxPsbtKeyLength is the length of the largest key that we'll successfully
Expand Down Expand Up @@ -395,3 +395,20 @@ func (p *Packet) SanityCheck() error {

return nil
}

// GetTxFee returns the transaction fee. An error is returned if a transaction
// input does not contain any UTXO information.
func (p *Packet) GetTxFee() (btcutil.Amount, error) {
sumInputs, err := SumUtxoInputValues(p)
if err != nil {
return 0, err
}

var sumOutputs int64
for _, txOut := range p.UnsignedTx.TxOut {
sumOutputs += txOut.Value
}

fee := sumInputs - sumOutputs
return btcutil.Amount(fee), nil
}

0 comments on commit 586729c

Please sign in to comment.