diff --git a/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs b/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs index 29816498d..1cad6f693 100644 --- a/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs +++ b/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs @@ -22,6 +22,7 @@ using NicolasDorier.RateLimits; using Microsoft.Extensions.Logging; using NBXplorer.DerivationStrategy; using System.Diagnostics.CodeAnalysis; +using BTCPayServer.Logging; namespace BTCPayServer.Payments.PayJoin { @@ -384,8 +385,18 @@ namespace BTCPayServer.Payments.PayJoin Money ourFeeContribution = Money.Zero; // We need to adjust the fee to keep a constant fee rate var txBuilder = network.NBitcoinNetwork.CreateTransactionBuilder(); - txBuilder.AddCoins(psbt.Inputs.Select(i => i.GetCoin())); - txBuilder.AddCoins(selectedUTXOs.Select(o => o.Value.AsCoin())); + var coins = new List(); + if (sendersInputType != ScriptPubKeyType.SegwitP2SH) + { + coins.AddRange(psbt.Inputs.Select(i => i.GetCoin())); + coins.AddRange(selectedUTXOs.Select(o => o.Value.AsCoin())); + } + else + { + coins.AddRange(psbt.Inputs.Select(i =>i.GetCoin().ToScriptCoin(PayToScriptHashTemplate.Instance.ExtractScriptSigParameters(i.FinalScriptSig).RedeemScript))); + coins.AddRange(selectedUTXOs.Select(pair => pair.Value.AsCoin(derivationSchemeSettings.AccountDerivation))); + } + txBuilder.AddCoins(coins); Money expectedFee = txBuilder.EstimateFees(newTx, originalFeeRate); Money actualFee = newTx.GetFee(txBuilder.FindSpentCoins(newTx)); Money additionalFee = expectedFee - actualFee;