From 51db6175841d24b1fdabbfb3f4f6528bfd58dc60 Mon Sep 17 00:00:00 2001 From: Kukks Date: Fri, 17 Apr 2020 11:55:24 +0200 Subject: [PATCH] fixes --- .../PayJoin/PayJoinEndpointController.cs | 5 ++- BTCPayServer/Services/PayjoinClient.cs | 33 ++++++++++++++++--- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs b/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs index 1cad6f693..6efdc22d9 100644 --- a/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs +++ b/BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs @@ -451,8 +451,11 @@ namespace BTCPayServer.Payments.PayJoin foreach (var selectedUtxo in selectedUTXOs.Select(o => o.Value)) { var signedInput = newPsbt.Inputs.FindIndexedInput(selectedUtxo.Outpoint); - signedInput.UpdateFromCoin(selectedUtxo.AsCoin()); + var coin = selectedUtxo.AsCoin(derivationSchemeSettings.AccountDerivation); + signedInput.UpdateFromCoin(coin); var privateKey = accountKey.Derive(selectedUtxo.KeyPath).PrivateKey; + //hack until UpdateFromCoin is fixed in NBitcoin when the coin is p2sh + signedInput.WitnessUtxo = coin.TxOut; signedInput.Sign(privateKey); signedInput.FinalizeInput(); newTx.Inputs[signedInput.Index].WitScript = newPsbt.Inputs[(int)signedInput.Index].FinalScriptWitness; diff --git a/BTCPayServer/Services/PayjoinClient.cs b/BTCPayServer/Services/PayjoinClient.cs index 3e617bdba..17e95dcc9 100644 --- a/BTCPayServer/Services/PayjoinClient.cs +++ b/BTCPayServer/Services/PayjoinClient.cs @@ -34,9 +34,9 @@ namespace BTCPayServer.Services if (i.WitnessUtxo.ScriptPubKey.IsScriptType(ScriptType.P2WPKH)) return ScriptPubKeyType.Segwit; if (i.WitnessUtxo.ScriptPubKey.IsScriptType(ScriptType.P2SH) && - i.FinalScriptWitness.GetSigner().ScriptPubKey.IsScriptType(ScriptType.P2WPKH)) + PayToWitPubKeyHashTemplate.Instance.ExtractWitScriptParameters(i.FinalScriptWitness) is {}) return ScriptPubKeyType.SegwitP2SH; - return null as ScriptPubKeyType?; + return null; } } @@ -212,8 +212,33 @@ namespace BTCPayServer.Services if (sentAfter > sentBefore) { var overPaying = sentAfter - sentBefore; - if (!newPSBT.TryGetEstimatedFeeRate(out var newFeeRate) || !newPSBT.TryGetVirtualSize(out var newVirtualSize)) - throw new PayjoinSenderException("The payjoin receiver did not included UTXO information to calculate fee correctly"); + + //hack until GetAllCoins is fixed in NBitcoin when the coin is p2sh (redeem needs to be loaded from RedeemScript and fallback to i.FinalScriptSig extraction) + int newVirtualSize = 0; + if (type == ScriptPubKeyType.SegwitP2SH) + { + if (!newPSBT.TryGetFee(out var fee)) + { + throw new PayjoinSenderException("The payjoin receiver did not included UTXO information to calculate fee correctly"); + } + var transactionBuilder = originalTx.Network.CreateTransactionBuilder(); + transactionBuilder.AddCoins(newPSBT.Inputs.Select(i =>i.GetCoin().ToScriptCoin(i.RedeemScript??PayToScriptHashTemplate.Instance.ExtractScriptSigParameters(i.FinalScriptSig).RedeemScript))); + try + { + newVirtualSize = transactionBuilder.EstimateSize(newPSBT.GetGlobalTransaction(), true); + } + catch + { + throw new PayjoinSenderException("The payjoin receiver did not included UTXO information to calculate fee correctly"); + } + + } + else + { + if (!newPSBT.TryGetEstimatedFeeRate(out var newFeeRate) || !newPSBT.TryGetVirtualSize(out newVirtualSize)) + throw new PayjoinSenderException("The payjoin receiver did not included UTXO information to calculate fee correctly"); + } + var additionalFee = newPSBT.GetFee() - originalFee; if (overPaying > additionalFee) throw new PayjoinSenderException("The payjoin receiver is sending more money to himself");