mirror of
https://github.com/aljazceru/btcpayserver.git
synced 2025-12-18 06:24:24 +01:00
@@ -1,32 +1,57 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using BTCPayServer.Services.Labels;
|
||||
using Newtonsoft.Json;
|
||||
using Newtonsoft.Json.Linq;
|
||||
using Newtonsoft.Json.Serialization;
|
||||
|
||||
namespace BTCPayServer.Data
|
||||
{
|
||||
public class WalletTransactionInfo
|
||||
{
|
||||
public string Comment { get; set; } = string.Empty;
|
||||
[JsonIgnore]
|
||||
public Dictionary<string, Label> Labels { get; set; } = new Dictionary<string, Label>();
|
||||
}
|
||||
public static class WalletTransactionDataExtensions
|
||||
{
|
||||
public static WalletTransactionInfo GetBlobInfo(this WalletTransactionData walletTransactionData)
|
||||
{
|
||||
WalletTransactionInfo blobInfo;
|
||||
if (walletTransactionData.Blob == null || walletTransactionData.Blob.Length == 0)
|
||||
{
|
||||
return new WalletTransactionInfo();
|
||||
}
|
||||
var blobInfo = JsonConvert.DeserializeObject<WalletTransactionInfo>(ZipUtils.Unzip(walletTransactionData.Blob));
|
||||
blobInfo = new WalletTransactionInfo();
|
||||
else
|
||||
blobInfo = JsonConvert.DeserializeObject<WalletTransactionInfo>(ZipUtils.Unzip(walletTransactionData.Blob));
|
||||
if (!string.IsNullOrEmpty(walletTransactionData.Labels))
|
||||
{
|
||||
if (walletTransactionData.Labels.StartsWith('['))
|
||||
{
|
||||
blobInfo.Labels.AddRange(JArray.Parse(walletTransactionData.Labels).Values<string>());
|
||||
foreach (var jtoken in JArray.Parse(walletTransactionData.Labels))
|
||||
{
|
||||
var l = jtoken.Type == JTokenType.String ? Label.Parse(jtoken.Value<string>())
|
||||
: Label.Parse(jtoken.ToString());
|
||||
blobInfo.Labels.TryAdd(l.Text, l);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
blobInfo.Labels.AddRange(walletTransactionData.Labels.Split(',',
|
||||
StringSplitOptions.RemoveEmptyEntries));
|
||||
// Legacy path
|
||||
foreach (var token in walletTransactionData.Labels.Split(',',
|
||||
StringSplitOptions.RemoveEmptyEntries))
|
||||
{
|
||||
var l = Label.Parse(token);
|
||||
blobInfo.Labels.TryAdd(l.Text, l);
|
||||
}
|
||||
}
|
||||
}
|
||||
return blobInfo;
|
||||
}
|
||||
static JsonSerializerSettings LabelSerializerSettings = new JsonSerializerSettings()
|
||||
{
|
||||
ContractResolver = new CamelCasePropertyNamesContractResolver(),
|
||||
Formatting = Formatting.None
|
||||
};
|
||||
public static void SetBlobInfo(this WalletTransactionData walletTransactionData, WalletTransactionInfo blobInfo)
|
||||
{
|
||||
if (blobInfo == null)
|
||||
@@ -35,8 +60,11 @@ namespace BTCPayServer.Data
|
||||
walletTransactionData.Blob = Array.Empty<byte>();
|
||||
return;
|
||||
}
|
||||
|
||||
walletTransactionData.Labels = JArray.FromObject(blobInfo.Labels).ToString();
|
||||
walletTransactionData.Labels = new JArray(
|
||||
blobInfo.Labels.Select(l => JsonConvert.SerializeObject(l.Value, LabelSerializerSettings))
|
||||
.Select(l => JObject.Parse(l))
|
||||
.OfType<JToken>()
|
||||
.ToArray()).ToString();
|
||||
walletTransactionData.Blob = ZipUtils.Zip(JsonConvert.SerializeObject(blobInfo));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user