btcpayserver/BTCPayServer/Services/PayjoinClient.cs

280 lines
12 KiB
C#
Raw Normal View History

2020-03-29 17:28:22 +02:00
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Google.Apis.Http;
2020-03-29 17:28:22 +02:00
using NBitcoin;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using IHttpClientFactory = System.Net.Http.IHttpClientFactory;
2020-03-29 17:28:22 +02:00
namespace BTCPayServer.Services
{
2020-04-08 08:20:19 +02:00
public static class PSBTExtensions
{
2020-04-08 15:14:16 +02:00
public static ScriptPubKeyType? GetInputsScriptPubKeyType(this PSBT psbt)
2020-04-08 08:20:19 +02:00
{
2020-04-08 15:14:16 +02:00
if (!psbt.IsAllFinalized() || psbt.Inputs.Any(i => i.WitnessUtxo == null))
throw new InvalidOperationException("The psbt should be finalized with witness information");
var coinsPerTypes = psbt.Inputs.Select(i =>
{
2020-04-09 12:44:16 +02:00
return ((PSBTCoin)i, i.GetInputScriptPubKeyType());
2020-04-08 15:14:16 +02:00
}).GroupBy(o => o.Item2, o => o.Item1).ToArray();
if (coinsPerTypes.Length != 1)
return default;
return coinsPerTypes[0].Key;
2020-04-08 08:20:19 +02:00
}
2020-04-09 12:44:16 +02:00
public static ScriptPubKeyType? GetInputScriptPubKeyType(this PSBTInput i)
{
if (i.WitnessUtxo.ScriptPubKey.IsScriptType(ScriptType.P2WPKH))
return ScriptPubKeyType.Segwit;
if (i.WitnessUtxo.ScriptPubKey.IsScriptType(ScriptType.P2SH) &&
2020-04-17 11:55:24 +02:00
PayToWitPubKeyHashTemplate.Instance.ExtractWitScriptParameters(i.FinalScriptWitness) is {})
2020-04-09 12:44:16 +02:00
return ScriptPubKeyType.SegwitP2SH;
2020-04-17 11:55:24 +02:00
return null;
2020-04-09 12:44:16 +02:00
}
2020-04-08 08:20:19 +02:00
}
2020-03-29 17:28:22 +02:00
public class PayjoinClient
{
public const string PayjoinOnionNamedClient = "payjoin.onion";
public const string PayjoinClearnetNamedClient = "payjoin.clearnet";
2020-04-08 08:20:19 +02:00
public static readonly ScriptPubKeyType[] SupportedFormats = {
ScriptPubKeyType.Segwit,
ScriptPubKeyType.SegwitP2SH
};
2020-04-13 11:52:22 +02:00
public const string BIP21EndpointKey = "pj";
2020-04-08 08:20:19 +02:00
2020-03-29 17:28:22 +02:00
private readonly ExplorerClientProvider _explorerClientProvider;
private IHttpClientFactory _httpClientFactory;
2020-03-29 17:28:22 +02:00
public PayjoinClient(ExplorerClientProvider explorerClientProvider, IHttpClientFactory httpClientFactory)
2020-03-29 17:28:22 +02:00
{
if (httpClientFactory == null) throw new ArgumentNullException(nameof(httpClientFactory));
_explorerClientProvider =
explorerClientProvider ?? throw new ArgumentNullException(nameof(explorerClientProvider));
_httpClientFactory = httpClientFactory;
2020-03-29 17:28:22 +02:00
}
public async Task<PSBT> RequestPayjoin(Uri endpoint, DerivationSchemeSettings derivationSchemeSettings,
PSBT originalTx, CancellationToken cancellationToken)
{
if (endpoint == null) throw new ArgumentNullException(nameof(endpoint));
if (derivationSchemeSettings == null) throw new ArgumentNullException(nameof(derivationSchemeSettings));
if (originalTx == null) throw new ArgumentNullException(nameof(originalTx));
2020-04-08 11:24:04 +02:00
if (originalTx.IsAllFinalized())
throw new InvalidOperationException("The original PSBT should not be finalized.");
2020-03-29 17:28:22 +02:00
2020-04-08 08:20:19 +02:00
var type = derivationSchemeSettings.AccountDerivation.ScriptPubKeyType();
if (!SupportedFormats.Contains(type))
{
throw new PayjoinSenderException($"The wallet does not support payjoin");
}
2020-03-29 17:28:22 +02:00
var signingAccount = derivationSchemeSettings.GetSigningAccountKeySettings();
var sentBefore = -originalTx.GetBalance(derivationSchemeSettings.AccountDerivation,
signingAccount.AccountKey,
signingAccount.GetRootedKeyPath());
var oldGlobalTx = originalTx.GetGlobalTransaction();
if (!originalTx.TryGetEstimatedFeeRate(out var originalFeeRate) || !originalTx.TryGetVirtualSize(out var oldVirtualSize))
2020-03-29 17:28:22 +02:00
throw new ArgumentException("originalTx should have utxo information", nameof(originalTx));
var originalFee = originalTx.GetFee();
2020-03-29 17:28:22 +02:00
var cloned = originalTx.Clone();
2020-04-08 11:24:04 +02:00
if (!cloned.TryFinalize(out var errors))
2020-03-29 17:28:22 +02:00
{
return null;
}
// We make sure we don't send unnecessary information to the receiver
foreach (var finalized in cloned.Inputs.Where(i => i.IsFinalized()))
{
finalized.ClearForFinalize();
}
foreach (var output in cloned.Outputs)
{
output.HDKeyPaths.Clear();
}
cloned.GlobalXPubs.Clear();
using HttpClient client = CreateHttpClient(endpoint);
var bpuresponse = await client.PostAsync(endpoint,
2020-03-29 17:28:22 +02:00
new StringContent(cloned.ToHex(), Encoding.UTF8, "text/plain"), cancellationToken);
if (!bpuresponse.IsSuccessStatusCode)
{
var errorStr = await bpuresponse.Content.ReadAsStringAsync();
try
{
var error = JObject.Parse(errorStr);
throw new PayjoinReceiverException((int)bpuresponse.StatusCode, error["errorCode"].Value<string>(),
error["message"].Value<string>());
}
catch (JsonReaderException)
{
// will throw
bpuresponse.EnsureSuccessStatusCode();
throw;
}
}
var hex = await bpuresponse.Content.ReadAsStringAsync();
var newPSBT = PSBT.Parse(hex, originalTx.Network);
// Checking that the PSBT of the receiver is clean
if (newPSBT.GlobalXPubs.Any())
{
throw new PayjoinSenderException("GlobalXPubs should not be included in the receiver's PSBT");
}
if (newPSBT.Outputs.Any(o => o.HDKeyPaths.Count != 0) || newPSBT.Inputs.Any(o => o.HDKeyPaths.Count != 0))
{
throw new PayjoinSenderException("Keypath information should not be included in the receiver's PSBT");
}
////////////
newPSBT = await _explorerClientProvider.UpdatePSBT(derivationSchemeSettings, newPSBT);
if (newPSBT.CheckSanity() is IList<PSBTError> errors2 && errors2.Count != 0)
{
throw new PayjoinSenderException($"The PSBT of the receiver is insane ({errors2[0]})");
}
// We make sure we don't sign things what should not be signed
foreach (var finalized in newPSBT.Inputs.Where(i => i.IsFinalized()))
{
finalized.ClearForFinalize();
}
// Make sure only the only our output have any information
foreach (var output in newPSBT.Outputs)
{
output.HDKeyPaths.Clear();
foreach (var originalOutput in originalTx.Outputs)
{
if (output.ScriptPubKey == originalOutput.ScriptPubKey)
output.UpdateFrom(originalOutput);
}
}
// Making sure that our inputs are finalized, and that some of our inputs have not been added
var newGlobalTx = newPSBT.GetGlobalTransaction();
2020-03-29 17:28:22 +02:00
int ourInputCount = 0;
if (newGlobalTx.Version != oldGlobalTx.Version)
throw new PayjoinSenderException("The version field of the transaction has been modified");
if (newGlobalTx.LockTime != oldGlobalTx.LockTime)
throw new PayjoinSenderException("The LockTime field of the transaction has been modified");
2020-03-29 17:28:22 +02:00
foreach (var input in newPSBT.Inputs.CoinsFor(derivationSchemeSettings.AccountDerivation,
signingAccount.AccountKey, signingAccount.GetRootedKeyPath()))
{
2020-04-08 10:42:50 +02:00
if (oldGlobalTx.Inputs.FindIndexedInput(input.PrevOut) is IndexedTxIn ourInput)
2020-03-29 17:28:22 +02:00
{
ourInputCount++;
if (input.IsFinalized())
throw new PayjoinSenderException("A PSBT input from us should not be finalized");
2020-04-08 10:51:22 +02:00
if (newGlobalTx.Inputs[input.Index].Sequence != ourInput.TxIn.Sequence)
throw new PayjoinSenderException("The sequence of one of our input has been modified");
2020-03-29 17:28:22 +02:00
}
else
{
throw new PayjoinSenderException(
"The payjoin receiver added some of our own inputs in the proposal");
}
}
foreach (var input in newPSBT.Inputs)
{
if (originalTx.Inputs.FindIndexedInput(input.PrevOut) is null)
{
if (!input.IsFinalized())
throw new PayjoinSenderException("The payjoin receiver included a non finalized input");
2020-04-09 12:44:16 +02:00
// Making sure that the receiver's inputs are finalized and match format
var payjoinInputType = input.GetInputScriptPubKeyType();
if (payjoinInputType is null || payjoinInputType.Value != type)
{
throw new PayjoinSenderException("The payjoin receiver included an input that is not the same segwit input type");
}
}
2020-03-29 17:28:22 +02:00
}
if (ourInputCount < originalTx.Inputs.Count)
throw new PayjoinSenderException("The payjoin receiver removed some of our inputs");
// We limit the number of inputs the receiver can add
var addedInputs = newPSBT.Inputs.Count - originalTx.Inputs.Count;
if (addedInputs == 0)
throw new PayjoinSenderException("The payjoin receiver did not added any input");
2020-03-29 17:28:22 +02:00
var sentAfter = -newPSBT.GetBalance(derivationSchemeSettings.AccountDerivation,
signingAccount.AccountKey,
signingAccount.GetRootedKeyPath());
if (sentAfter > sentBefore)
{
var overPaying = sentAfter - sentBefore;
2020-04-17 11:55:24 +02:00
2020-04-18 08:29:07 +02:00
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");
2020-04-17 11:55:24 +02:00
var additionalFee = newPSBT.GetFee() - originalFee;
if (overPaying > additionalFee)
throw new PayjoinSenderException("The payjoin receiver is sending more money to himself");
2020-04-07 08:10:19 +02:00
if (overPaying > originalFee)
throw new PayjoinSenderException("The payjoin receiver is making us pay more than twice the original fee");
2020-03-29 17:28:22 +02:00
// Let's check the difference is only for the fee and that feerate
// did not changed that much
var expectedFee = originalFeeRate.GetFee(newVirtualSize);
2020-03-29 17:28:22 +02:00
// Signing precisely is hard science, give some breathing room for error.
expectedFee += originalFeeRate.GetFee(newPSBT.Inputs.Count * 2);
2020-04-07 08:10:19 +02:00
if (overPaying > (expectedFee - originalFee))
throw new PayjoinSenderException("The payjoin receiver increased the fee rate we are paying too much");
2020-03-29 17:28:22 +02:00
}
return newPSBT;
}
private HttpClient CreateHttpClient(Uri uri)
{
if (uri.IsOnion())
return _httpClientFactory.CreateClient(PayjoinOnionNamedClient);
else
return _httpClientFactory.CreateClient(PayjoinClearnetNamedClient);
}
2020-03-29 17:28:22 +02:00
}
public class PayjoinException : Exception
{
public PayjoinException(string message) : base(message)
{
}
}
public class PayjoinReceiverException : PayjoinException
{
public PayjoinReceiverException(int httpCode, string errorCode, string message) : base(FormatMessage(httpCode,
errorCode, message))
{
HttpCode = httpCode;
ErrorCode = errorCode;
ErrorMessage = message;
}
public int HttpCode { get; }
public string ErrorCode { get; }
public string ErrorMessage { get; }
private static string FormatMessage(in int httpCode, string errorCode, string message)
{
return $"{errorCode}: {message} (HTTP: {httpCode})";
}
}
public class PayjoinSenderException : PayjoinException
{
public PayjoinSenderException(string message) : base(message)
{
}
}
}