diff --git a/BTCPayServer.Plugins.Payjoin.IntegrationTests/PayjoinPluginIntegrationTests.cs b/BTCPayServer.Plugins.Payjoin.IntegrationTests/PayjoinPluginIntegrationTests.cs index 62db7a89..d1409b2d 100644 --- a/BTCPayServer.Plugins.Payjoin.IntegrationTests/PayjoinPluginIntegrationTests.cs +++ b/BTCPayServer.Plugins.Payjoin.IntegrationTests/PayjoinPluginIntegrationTests.cs @@ -10,6 +10,7 @@ using BTCPayServer.Plugins.Payjoin.Services; using BTCPayServer.Services.Invoices; using BTCPayServer.Services.Stores; +using BTCPayServer.Services.Wallets; using BTCPayServer.Tests; using NBitcoin; using NBitcoin.Payment; @@ -43,6 +44,47 @@ public async Task CreateInvoiceAndPayItThroughThePayjoinPluginWithExternalPayer( PayjoinIntegrationTestSupport.AssertSuccessfulPayjoinTransaction(paymentResult); } + [Fact] + [Trait("Integration", "Integration")] + public async Task CreateInvoiceAndPayItWithTaprootReceiverInput() + { + using var cts = new CancellationTokenSource(PayjoinIntegrationTestSupport.TestTimeout); + using var tester = CreateServerTester(newDb: true); + var context = await PayjoinAccountTestHelper.CreateInitializedTestContextAsync( + tester, + initialFundingUtxoCount: 1, + scriptPubKeyType: ScriptPubKeyType.TaprootBIP86, + cancellationToken: cts.Token).ConfigureAwait(true); + var payer = await PayjoinAccountTestHelper.CreateInitializedAccountAsync(tester, context.Network, cancellationToken: cts.Token).ConfigureAwait(true); + + var wallet = tester.PayTester.GetService().GetWallet(context.Network); + Assert.NotNull(wallet); + var receiverCoin = Assert.Single(await wallet.GetUnspentCoins(context.Merchant.DerivationScheme, excludeUnconfirmed: true, cancellation: cts.Token).ConfigureAwait(true)); + Assert.True(receiverCoin.Confirmations > 0); + Assert.True(receiverCoin.ScriptPubKey.IsScriptType(ScriptType.Taproot)); + Assert.Equal(Money.Coins(1.0m), receiverCoin.Coin.Amount); + + await PayjoinIntegrationTestSupport.EnablePayjoinAsync(tester, context.Merchant.StoreId, cancellationToken: cts.Token).ConfigureAwait(true); + var invoice = await PayjoinInvoiceTestHelper.PreparePayjoinInvoiceAsync(tester, context.Merchant, context.Network, cts.Token).ConfigureAwait(true); + await PayjoinReceiverTestHelper.AssertReceiverSessionEventuallyCreatedAsync(tester, invoice.InvoiceId, cts.Token).ConfigureAwait(true); + + var payjoinPayer = new PayjoinTestPayer(tester, payer, context.Network); + var payment = await payjoinPayer.PayAsync(invoice.PaymentUrl, invoice.OhttpRelayUrls, cts.Token).ConfigureAwait(true); + var paymentResult = await PayjoinInvoiceTestHelper.FinalizePayjoinPaymentAsync( + tester, context.Merchant, invoice, payment.TransactionId, cts.Token).ConfigureAwait(true); + PayjoinIntegrationTestSupport.AssertSuccessfulPayjoinTransaction(paymentResult); + + var receiverInput = Assert.Single(paymentResult.PayjoinTransaction.Inputs, input => input.PrevOut == receiverCoin.OutPoint); + Assert.Empty(receiverInput.ScriptSig.ToBytes()); + var signature = Assert.Single(receiverInput.WitScript.Pushes); + Assert.Equal(64, signature.Length); + var witnessSize = receiverInput.WitScript.ToBytes().Length; + Assert.Equal(66, witnessSize); + Assert.Equal(230, receiverInput.ToBytes().Length * 4 + witnessSize); + + await PayjoinReceiverTestHelper.AssertReceiverSessionEventuallyRemovedAsync(tester, invoice.InvoiceId, cts.Token).ConfigureAwait(true); + } + [Fact] [Trait("Integration", "Integration")] public async Task CreateInvoiceAndPayItThroughThePayjoinPluginWithExternalPayerFallsBackAcrossConfiguredRelays() diff --git a/BTCPayServer.Plugins.Payjoin.IntegrationTests/TestUtils/PayjoinAccountTestHelper.cs b/BTCPayServer.Plugins.Payjoin.IntegrationTests/TestUtils/PayjoinAccountTestHelper.cs index 6ec398d3..34fdecd1 100644 --- a/BTCPayServer.Plugins.Payjoin.IntegrationTests/TestUtils/PayjoinAccountTestHelper.cs +++ b/BTCPayServer.Plugins.Payjoin.IntegrationTests/TestUtils/PayjoinAccountTestHelper.cs @@ -13,26 +13,26 @@ internal static class PayjoinAccountTestHelper private static readonly TimeSpan PollInterval = TimeSpan.FromMilliseconds(250); private static readonly TimeSpan WalletFundingConfirmationTimeout = TimeSpan.FromSeconds(30); - public static async Task CreateInitializedTestContextAsync(ServerTester tester, bool confirmFunding = true, int initialFundingUtxoCount = DefaultInitialFundingUtxoCount, CancellationToken cancellationToken = default) + public static async Task CreateInitializedTestContextAsync(ServerTester tester, bool confirmFunding = true, int initialFundingUtxoCount = DefaultInitialFundingUtxoCount, ScriptPubKeyType scriptPubKeyType = ScriptPubKeyType.Segwit, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(tester); await tester.StartAsync().WaitAsync(cancellationToken).ConfigureAwait(true); var network = GetBitcoinNetwork(tester); - var merchant = await CreateInitializedAccountAsync(tester, network, confirmFunding, initialFundingUtxoCount, cancellationToken).ConfigureAwait(true); + var merchant = await CreateInitializedAccountAsync(tester, network, confirmFunding, initialFundingUtxoCount, scriptPubKeyType, cancellationToken).ConfigureAwait(true); return new TestContext(network, merchant); } - public static async Task CreateInitializedAccountAsync(ServerTester tester, BTCPayNetwork network, bool confirmFunding = true, int initialFundingUtxoCount = DefaultInitialFundingUtxoCount, CancellationToken cancellationToken = default) + public static async Task CreateInitializedAccountAsync(ServerTester tester, BTCPayNetwork network, bool confirmFunding = true, int initialFundingUtxoCount = DefaultInitialFundingUtxoCount, ScriptPubKeyType scriptPubKeyType = ScriptPubKeyType.Segwit, CancellationToken cancellationToken = default) { ArgumentNullException.ThrowIfNull(tester); ArgumentNullException.ThrowIfNull(network); var user = tester.NewAccount(); await user.GrantAccessAsync().WaitAsync(cancellationToken).ConfigureAwait(true); - await user.RegisterDerivationSchemeAsync(PayjoinConstants.BitcoinCode, ScriptPubKeyType.Segwit, true).WaitAsync(cancellationToken).ConfigureAwait(true); + await user.RegisterDerivationSchemeAsync(PayjoinConstants.BitcoinCode, scriptPubKeyType, true).WaitAsync(cancellationToken).ConfigureAwait(true); await FundWalletAsync(user, network, initialFundingUtxoCount, cancellationToken).ConfigureAwait(true); if (confirmFunding) { diff --git a/BTCPayServer.Plugins.Payjoin.Tests/Services/PayjoinReceiverWalletAdapterTests.cs b/BTCPayServer.Plugins.Payjoin.Tests/Services/PayjoinReceiverWalletAdapterTests.cs index 3c76ece1..ace89d09 100644 --- a/BTCPayServer.Plugins.Payjoin.Tests/Services/PayjoinReceiverWalletAdapterTests.cs +++ b/BTCPayServer.Plugins.Payjoin.Tests/Services/PayjoinReceiverWalletAdapterTests.cs @@ -11,10 +11,12 @@ namespace BTCPayServer.Plugins.Payjoin.Tests.Services; /// public class PayjoinReceiverWalletAdapterTests { - [Fact] - public void CreateInputPairRoundTripsTheCoinOutpointThroughTheLibrary() + [Theory] + [InlineData(ScriptPubKeyType.Segwit)] + [InlineData(ScriptPubKeyType.TaprootBIP86)] + public void CreateInputPairRoundTripsTheCoinOutpointThroughTheLibrary(ScriptPubKeyType scriptPubKeyType) { - var coin = CreateCoin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 3, 25_000); + var coin = CreateCoin("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa", 3, 25_000, scriptPubKeyType); using var inputPair = PayjoinReceiverWalletAdapter.CreateInputPair(coin); var outpoint = inputPair.Outpoint(); @@ -58,11 +60,11 @@ public void ResolveSelectedCandidateReturnsNullForAnUnknownOutpoint() Assert.Null(resolved); } - private static ReceivedCoin CreateCoin(string txId, uint vout, long valueSats) + private static ReceivedCoin CreateCoin(string txId, uint vout, long valueSats, ScriptPubKeyType scriptPubKeyType = ScriptPubKeyType.Segwit) { using var key = new Key(); var outPoint = new OutPoint(uint256.Parse(txId), vout); - var scriptPubKey = key.PubKey.WitHash.ScriptPubKey; + var scriptPubKey = key.PubKey.GetAddress(scriptPubKeyType, Network.RegTest).ScriptPubKey; return new ReceivedCoin { OutPoint = outPoint, diff --git a/BTCPayServer.Plugins.Payjoin/Services/PayjoinReceiverWalletAdapter.cs b/BTCPayServer.Plugins.Payjoin/Services/PayjoinReceiverWalletAdapter.cs index 5d24096f..e78a5abf 100644 --- a/BTCPayServer.Plugins.Payjoin/Services/PayjoinReceiverWalletAdapter.cs +++ b/BTCPayServer.Plugins.Payjoin/Services/PayjoinReceiverWalletAdapter.cs @@ -78,12 +78,22 @@ public async Task GetConfirmedReceiverCoinsAsync( internal static PayjoinInputPair CreateInputPair(ReceivedCoin coin) { + var outpoint = new PayjoinOutPoint(coin.OutPoint.Hash.ToString(), coin.OutPoint.N); + var txout = new PayjoinTxOut(checked((ulong)coin.Coin.Amount.Satoshi), coin.ScriptPubKey.ToBytes()); + if (coin.ScriptPubKey.IsScriptType(NBitcoin.ScriptType.Taproot)) + { + return PayjoinInputPair.NewP2trKeyspend(txout, outpoint); + } + if (coin.ScriptPubKey.IsScriptType(NBitcoin.ScriptType.P2WPKH)) + { + return PayjoinInputPair.NewP2wpkh(txout, outpoint); + } + var txin = new PayjoinTxIn( - new PayjoinOutPoint(coin.OutPoint.Hash.ToString(), coin.OutPoint.N), + outpoint, Array.Empty(), uint.MaxValue, Array.Empty()); - var txout = new PayjoinTxOut(checked((ulong)coin.Coin.Amount.Satoshi), coin.ScriptPubKey.ToBytes()); var psbtIn = new PayjoinPsbtInput(txout, null, null); return new PayjoinInputPair(txin, psbtIn, null); } diff --git a/rust-payjoin b/rust-payjoin index e2b837e1..4667d87b 160000 --- a/rust-payjoin +++ b/rust-payjoin @@ -1 +1 @@ -Subproject commit e2b837e11dd896907386695cf7a9f2551bb1c7c1 +Subproject commit 4667d87b790450f97b9bb2db68c682ecf7935986