mirror of
https://github.com/btcpayserver/btcpayserver.git
synced 2025-02-23 22:46:49 +01:00
* Payjoin label fixes * When a payjoin label was applied, coin selection filter would not work * When a payjoin happened with a receive address wallet, the payjoin label was not applied * Coin selection shows when a utxo is currently reserved for a payjoin. Applies both to UI and to GF API * remove reserved label * Update BTCPayServer/Payments/PayJoin/PayJoinEndpointController.cs
82 lines
2.5 KiB
C#
82 lines
2.5 KiB
C#
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using BTCPayServer.Data;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using NBitcoin;
|
|
|
|
namespace BTCPayServer.Payments.PayJoin
|
|
{
|
|
public class UTXOLocker : IUTXOLocker
|
|
{
|
|
private readonly ApplicationDbContextFactory _dbContextFactory;
|
|
|
|
public UTXOLocker(ApplicationDbContextFactory dbContextFactory)
|
|
{
|
|
_dbContextFactory = dbContextFactory;
|
|
}
|
|
|
|
public async Task<bool> TryLock(OutPoint outpoint)
|
|
{
|
|
using var ctx = _dbContextFactory.CreateContext();
|
|
ctx.PayjoinLocks.Add(new PayjoinLock() {Id = outpoint.ToString()});
|
|
try
|
|
{
|
|
return await ctx.SaveChangesAsync() == 1;
|
|
}
|
|
catch (DbUpdateException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> TryUnlock(params OutPoint[] outPoints)
|
|
{
|
|
using var ctx = _dbContextFactory.CreateContext();
|
|
foreach (OutPoint outPoint in outPoints)
|
|
{
|
|
ctx.PayjoinLocks.Remove(new PayjoinLock() {Id = outPoint.ToString()});
|
|
}
|
|
|
|
try
|
|
{
|
|
return await ctx.SaveChangesAsync() == outPoints.Length;
|
|
}
|
|
catch (DbUpdateException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<bool> TryLockInputs(OutPoint[] outPoints)
|
|
{
|
|
using var ctx = _dbContextFactory.CreateContext();
|
|
foreach (OutPoint outPoint in outPoints)
|
|
{
|
|
ctx.PayjoinLocks.Add(new PayjoinLock()
|
|
{
|
|
// Random flag so it does not lock same id
|
|
// as the lock utxo
|
|
Id = "K-" + outPoint.ToString()
|
|
});
|
|
}
|
|
|
|
try
|
|
{
|
|
return await ctx.SaveChangesAsync() == outPoints.Length;
|
|
}
|
|
catch (DbUpdateException)
|
|
{
|
|
return false;
|
|
}
|
|
}
|
|
|
|
public async Task<HashSet<OutPoint>> FindLocks(OutPoint[] outpoints)
|
|
{
|
|
var outPointsStr = outpoints.Select(o => o.ToString());
|
|
await using var ctx = _dbContextFactory.CreateContext();
|
|
return (await ctx.PayjoinLocks.Where(l => outPointsStr.Contains(l.Id)).ToArrayAsync())
|
|
.Select(l => OutPoint.Parse(l.Id)).ToHashSet();
|
|
}
|
|
}
|
|
}
|