2022-10-11 17:34:29 +09:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
|
|
|
using System.Linq;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.EntityFrameworkCore.Infrastructure;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Data
|
|
|
|
{
|
2023-11-28 11:38:09 +01:00
|
|
|
public class WalletObjectData : IEqualityComparer<WalletObjectData>
|
2022-10-11 17:34:29 +09:00
|
|
|
{
|
|
|
|
public class Types
|
|
|
|
{
|
2023-02-22 03:47:02 +01:00
|
|
|
public static readonly HashSet<string> AllTypes;
|
|
|
|
static Types()
|
|
|
|
{
|
|
|
|
AllTypes = typeof(Types).GetFields()
|
|
|
|
.Where(f => f.FieldType == typeof(string))
|
|
|
|
.Select(f => (string)f.GetValue(null)).ToHashSet(StringComparer.OrdinalIgnoreCase);
|
|
|
|
}
|
2022-10-11 17:34:29 +09:00
|
|
|
public const string Label = "label";
|
|
|
|
public const string Tx = "tx";
|
2022-11-17 10:24:49 +09:00
|
|
|
public const string Payjoin = "payjoin";
|
|
|
|
public const string Invoice = "invoice";
|
|
|
|
public const string PaymentRequest = "payment-request";
|
|
|
|
public const string App = "app";
|
|
|
|
public const string PayjoinExposed = "pj-exposed";
|
|
|
|
public const string Payout = "payout";
|
|
|
|
public const string PullPayment = "pull-payment";
|
2022-12-13 09:09:25 +09:00
|
|
|
public const string Address = "address";
|
2022-12-01 01:54:55 +01:00
|
|
|
public const string Utxo = "utxo";
|
2022-10-11 17:34:29 +09:00
|
|
|
}
|
|
|
|
public string WalletId { get; set; }
|
|
|
|
public string Type { get; set; }
|
|
|
|
public string Id { get; set; }
|
|
|
|
public string Data { get; set; }
|
|
|
|
|
2022-11-25 12:06:57 +09:00
|
|
|
public List<WalletObjectLinkData> Bs { get; set; }
|
|
|
|
public List<WalletObjectLinkData> As { get; set; }
|
2022-10-11 17:34:29 +09:00
|
|
|
|
2022-11-19 00:04:46 +09:00
|
|
|
public IEnumerable<(string type, string id, string linkdata, string objectdata)> GetLinks()
|
|
|
|
{
|
2022-11-25 12:06:57 +09:00
|
|
|
if (Bs is not null)
|
|
|
|
foreach (var c in Bs)
|
2022-11-19 00:04:46 +09:00
|
|
|
{
|
2022-11-25 12:06:57 +09:00
|
|
|
yield return (c.BType, c.BId, c.Data, c.B?.Data);
|
2022-11-19 00:04:46 +09:00
|
|
|
}
|
2022-11-25 12:06:57 +09:00
|
|
|
if (As is not null)
|
|
|
|
foreach (var c in As)
|
2022-11-19 00:04:46 +09:00
|
|
|
{
|
2022-11-25 12:06:57 +09:00
|
|
|
yield return (c.AType, c.AId, c.Data, c.A?.Data);
|
2022-11-19 00:04:46 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public IEnumerable<WalletObjectData> GetNeighbours()
|
|
|
|
{
|
2022-11-25 12:06:57 +09:00
|
|
|
if (Bs != null)
|
|
|
|
foreach (var c in Bs)
|
2022-11-19 00:04:46 +09:00
|
|
|
{
|
2022-11-25 12:06:57 +09:00
|
|
|
if (c.B != null)
|
|
|
|
yield return c.B;
|
2022-11-19 00:04:46 +09:00
|
|
|
}
|
2022-11-25 12:06:57 +09:00
|
|
|
if (As != null)
|
|
|
|
foreach (var c in As)
|
2022-11-19 00:04:46 +09:00
|
|
|
{
|
2022-11-25 12:06:57 +09:00
|
|
|
if (c.A != null)
|
|
|
|
yield return c.A;
|
2022-11-19 00:04:46 +09:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-11 17:34:29 +09:00
|
|
|
internal static void OnModelCreating(ModelBuilder builder, DatabaseFacade databaseFacade)
|
|
|
|
{
|
|
|
|
builder.Entity<WalletObjectData>().HasKey(o =>
|
|
|
|
new
|
|
|
|
{
|
|
|
|
o.WalletId,
|
|
|
|
o.Type,
|
|
|
|
o.Id,
|
|
|
|
});
|
2022-11-19 23:39:41 +09:00
|
|
|
builder.Entity<WalletObjectData>().HasIndex(o =>
|
|
|
|
new
|
|
|
|
{
|
|
|
|
o.Type,
|
|
|
|
o.Id
|
|
|
|
});
|
2022-10-11 17:34:29 +09:00
|
|
|
|
2024-04-15 19:08:25 +09:00
|
|
|
builder.Entity<WalletObjectData>()
|
|
|
|
.Property(o => o.Data)
|
|
|
|
.HasColumnType("JSONB");
|
2022-10-11 17:34:29 +09:00
|
|
|
}
|
2023-11-28 11:38:09 +01:00
|
|
|
|
|
|
|
public bool Equals(WalletObjectData x, WalletObjectData y)
|
|
|
|
{
|
|
|
|
if (ReferenceEquals(x, y)) return true;
|
|
|
|
if (ReferenceEquals(x, null)) return false;
|
|
|
|
if (ReferenceEquals(y, null)) return false;
|
|
|
|
if (x.GetType() != y.GetType()) return false;
|
|
|
|
return string.Equals(x.WalletId, y.WalletId, StringComparison.InvariantCultureIgnoreCase) &&
|
|
|
|
string.Equals(x.Type, y.Type, StringComparison.InvariantCultureIgnoreCase) &&
|
|
|
|
string.Equals(x.Id, y.Id, StringComparison.InvariantCultureIgnoreCase);
|
|
|
|
}
|
|
|
|
|
|
|
|
public int GetHashCode(WalletObjectData obj)
|
|
|
|
{
|
|
|
|
HashCode hashCode = new HashCode();
|
|
|
|
hashCode.Add(obj.WalletId, StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
hashCode.Add(obj.Type, StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
hashCode.Add(obj.Id, StringComparer.InvariantCultureIgnoreCase);
|
|
|
|
return hashCode.ToHashCode();
|
|
|
|
}
|
2022-10-11 17:34:29 +09:00
|
|
|
}
|
|
|
|
}
|