diff --git a/BTCPayServer/Controllers/WalletsController.PSBT.cs b/BTCPayServer/Controllers/WalletsController.PSBT.cs index 66506d97b..d3f8ac069 100644 --- a/BTCPayServer/Controllers/WalletsController.PSBT.cs +++ b/BTCPayServer/Controllers/WalletsController.PSBT.cs @@ -159,7 +159,7 @@ namespace BTCPayServer.Controllers - private async Task TryGetBPProposedTX(string bpu, PSBT psbt, DerivationSchemeSettings derivationSchemeSettings, BTCPayNetwork btcPayNetwork) + private async Task TryGetPayjoinProposedTX(string bpu, PSBT psbt, DerivationSchemeSettings derivationSchemeSettings, BTCPayNetwork btcPayNetwork) { if (!string.IsNullOrEmpty(bpu) && Uri.TryCreate(bpu, UriKind.Absolute, out var endpoint)) { @@ -192,6 +192,7 @@ namespace BTCPayServer.Controllers if (!input.TryFinalizeInput(out _)) { + //a new input was provided but was invalid. valid = false; break; } @@ -204,6 +205,17 @@ namespace BTCPayServer.Controllers return null; } + //check if output sum to self is the same. + var signingAccountKeySettings = derivationSchemeSettings.GetSigningAccountKeySettings(); + var newOutputSumToSelfSum = newPSBT.Outputs.CoinsFor(derivationSchemeSettings.AccountDerivation, signingAccountKeySettings.AccountKey, + signingAccountKeySettings.GetRootedKeyPath()).Sum(output => output.Value); + + var originalOutputSumToSelf = psbt.Outputs.Sum(output => output.Value); + if (originalOutputSumToSelf < newOutputSumToSelfSum) + { + return null; + } + newPSBT = await UpdatePSBT(derivationSchemeSettings, newPSBT, btcPayNetwork); TempData.SetStatusMessageModel(new StatusMessageModel() { diff --git a/BTCPayServer/Controllers/WalletsController.cs b/BTCPayServer/Controllers/WalletsController.cs index 5759c0838..86d9a3b6e 100644 --- a/BTCPayServer/Controllers/WalletsController.cs +++ b/BTCPayServer/Controllers/WalletsController.cs @@ -688,7 +688,7 @@ namespace BTCPayServer.Controllers WalletId walletId, WalletSendVaultModel model) { var network = NetworkProvider.GetNetwork(walletId.CryptoCode); - var newPSBT = await TryGetBPProposedTX(model.PayJoinEndpointUrl, PSBT.Parse(model.PSBT, network.NBitcoinNetwork), GetDerivationSchemeSettings(walletId), network); + var newPSBT = await TryGetPayjoinProposedTX(model.PayJoinEndpointUrl, PSBT.Parse(model.PSBT, network.NBitcoinNetwork), GetDerivationSchemeSettings(walletId), network); model.PayJoinEndpointUrl = null; if (newPSBT != null) { @@ -849,7 +849,7 @@ namespace BTCPayServer.Controllers return View(viewModel); } ModelState.Remove(nameof(viewModel.PSBT)); - var newPSBT = await TryGetBPProposedTX(viewModel.PayJoinEndpointUrl,psbt, GetDerivationSchemeSettings(walletId), network); + var newPSBT = await TryGetPayjoinProposedTX(viewModel.PayJoinEndpointUrl,psbt, GetDerivationSchemeSettings(walletId), network); viewModel.PayJoinEndpointUrl = null; if (newPSBT != null) {