@@ -18,6 +18,7 @@ import (
1818 "github.com/btcsuite/btcd/chaincfg/chainhash"
1919 "github.com/btcsuite/btcd/txscript"
2020 "github.com/btcsuite/btcd/wire"
21+ "github.com/btcsuite/btcwallet/wallet/txrules"
2122 "github.com/davecgh/go-spew/spew"
2223 "github.com/lightninglabs/taproot-assets/address"
2324 "github.com/lightninglabs/taproot-assets/asset"
@@ -28,7 +29,6 @@ import (
2829 "github.com/lightninglabs/taproot-assets/tapgarden"
2930 "github.com/lightninglabs/taproot-assets/tappsbt"
3031 "github.com/lightninglabs/taproot-assets/tapscript"
31- "github.com/lightningnetwork/lnd/input"
3232 "github.com/lightningnetwork/lnd/keychain"
3333 "github.com/lightningnetwork/lnd/lnwallet/chainfee"
3434)
@@ -1654,61 +1654,30 @@ func addAnchorPsbtInputs(btcPkt *psbt.Packet, vPkt *tappsbt.VPacket,
16541654 }
16551655
16561656 // Now that we've added an extra input, we'll want to re-calculate the
1657- // total weight of the transaction, so we can ensure we're paying
1658- // enough in fees.
1659- var (
1660- weightEstimator input.TxWeightEstimator
1661- inputAmt , outputAmt int64
1662- )
1663- for _ , pIn := range btcPkt .Inputs {
1664- inputAmt += pIn .WitnessUtxo .Value
1665-
1666- inputPkScript := pIn .WitnessUtxo .PkScript
1667- switch {
1668- case txscript .IsPayToWitnessPubKeyHash (inputPkScript ):
1669- weightEstimator .AddP2WKHInput ()
1670-
1671- case txscript .IsPayToScriptHash (inputPkScript ):
1672- weightEstimator .AddNestedP2WKHInput ()
1673-
1674- case txscript .IsPayToTaproot (inputPkScript ):
1675- weightEstimator .AddTaprootKeySpendInput (
1676- txscript .SigHashDefault ,
1677- )
1678- default :
1679- return fmt .Errorf ("unknown pkScript: %x" ,
1680- inputPkScript )
1657+ // total vsize of the transaction and the necessary fee at the desired
1658+ // fee rate.
1659+ inputScripts := make ([][]byte , 0 , len (btcPkt .Inputs ))
1660+ for inputIdx , input := range btcPkt .Inputs {
1661+ if input .WitnessUtxo == nil {
1662+ return fmt .Errorf ("PSBT input %d doesn't specify " +
1663+ "witness UTXO, which is not supported" ,
1664+ inputIdx )
16811665 }
1682- }
1683- for _ , txOut := range btcPkt .UnsignedTx .TxOut {
1684- outputAmt += txOut .Value
16851666
1686- addrType , _ , _ , err := txscript .ExtractPkScriptAddrs (
1687- txOut .PkScript , params ,
1688- )
1689- if err != nil {
1690- return err
1667+ if len (input .WitnessUtxo .PkScript ) == 0 {
1668+ return fmt .Errorf ("input %d on psbt missing " +
1669+ "pkscript" , inputIdx )
16911670 }
16921671
1693- switch addrType {
1694- case txscript .WitnessV0PubKeyHashTy :
1695- weightEstimator .AddP2WKHOutput ()
1696-
1697- case txscript .WitnessV0ScriptHashTy :
1698- weightEstimator .AddP2WSHOutput ()
1699-
1700- case txscript .WitnessV1TaprootTy :
1701- weightEstimator .AddP2TROutput ()
1702- default :
1703- return fmt .Errorf ("unknown pkscript: %x" ,
1704- txOut .PkScript )
1705- }
1672+ inputScripts = append (inputScripts , input .WitnessUtxo .PkScript )
17061673 }
17071674
1708- // With this, we can now calculate the total fee we need to pay. We'll
1709- // also make sure to round up the required fee to the floor.
1710- totalWeight := int64 (weightEstimator .Weight ())
1711- requiredFee := feeRate .FeeForWeight (totalWeight )
1675+ estimatedSize , requiredFee := tapscript .EstimateFee (
1676+ inputScripts , btcPkt .UnsignedTx .TxOut , feeRate ,
1677+ )
1678+ log .Infof ("Estimated TX vsize: %d" , estimatedSize )
1679+ log .Infof ("TX required fee before change adjustment: %d at feerate " +
1680+ "%d sat/vB" , requiredFee , feeRate .FeePerKVByte ()/ 1000 )
17121681
17131682 // Given the current fee (which doesn't account for our input) and the
17141683 // total fee we want to pay, we'll adjust the wallet's change output
@@ -1729,8 +1698,8 @@ func addAnchorPsbtInputs(btcPkt *psbt.Packet, vPkt *tappsbt.VPacket,
17291698 // The fee may exceed the total value of the change output, which means
17301699 // this spend is impossible with the given inputs and fee rate.
17311700 if changeValue - feeDelta < 0 {
1732- return fmt .Errorf ("fee of %d sats exceeds change amount of %d " +
1733- "sats " , requiredFee , changeValue )
1701+ return fmt .Errorf ("fee exceeds change amount: (fee=%d, " +
1702+ "change=%d) " , requiredFee , changeValue )
17341703 }
17351704
17361705 btcPkt .UnsignedTx .TxOut [lastIdx ].Value -= feeDelta
0 commit comments