Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -43,6 +44,47 @@ public async Task CreateInvoiceAndPayItThroughThePayjoinPluginWithExternalPayer(
PayjoinIntegrationTestSupport.AssertSuccessfulPayjoinTransaction(paymentResult);
}

[Fact]
[Trait("Integration", "Integration")]
public async Task CreateInvoiceAndPayItWithTaprootReceiverInput()
Comment thread
ValeraFinebits marked this conversation as resolved.
Resolved
{
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<BTCPayWalletProvider>().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);
Comment thread
ValeraFinebits marked this conversation as resolved.
Resolved
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);
Comment thread
ValeraFinebits marked this conversation as resolved.
Resolved

await PayjoinReceiverTestHelper.AssertReceiverSessionEventuallyRemovedAsync(tester, invoice.InvoiceId, cts.Token).ConfigureAwait(true);
}

[Fact]
[Trait("Integration", "Integration")]
public async Task CreateInvoiceAndPayItThroughThePayjoinPluginWithExternalPayerFallsBackAcrossConfiguredRelays()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<TestContext> CreateInitializedTestContextAsync(ServerTester tester, bool confirmFunding = true, int initialFundingUtxoCount = DefaultInitialFundingUtxoCount, CancellationToken cancellationToken = default)
public static async Task<TestContext> 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<TestAccount> CreateInitializedAccountAsync(ServerTester tester, BTCPayNetwork network, bool confirmFunding = true, int initialFundingUtxoCount = DefaultInitialFundingUtxoCount, CancellationToken cancellationToken = default)
public static async Task<TestAccount> 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)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ namespace BTCPayServer.Plugins.Payjoin.Tests.Services;
/// </summary>
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();
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,22 @@ public async Task<ReceivedCoin[]> 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);
Comment thread
ValeraFinebits marked this conversation as resolved.
Resolved
}
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<byte>(),
uint.MaxValue,
Array.Empty<byte[]>());
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);
}
Expand Down
2 changes: 1 addition & 1 deletion rust-payjoin
Submodule rust-payjoin updated 174 files
Loading