2022-02-02 12:08:07 +01:00
|
|
|
using System;
|
2020-06-29 04:44:35 +02:00
|
|
|
using BTCPayServer.Client.Models;
|
2019-08-29 17:24:42 +02:00
|
|
|
using NBXplorer;
|
|
|
|
using Newtonsoft.Json.Linq;
|
|
|
|
|
|
|
|
namespace BTCPayServer.Data
|
|
|
|
{
|
|
|
|
public static class PaymentRequestDataExtensions
|
|
|
|
{
|
2020-05-19 19:59:23 +02:00
|
|
|
public static PaymentRequestBaseData GetBlob(this PaymentRequestData paymentRequestData)
|
2019-08-29 17:24:42 +02:00
|
|
|
{
|
2023-02-21 07:06:34 +01:00
|
|
|
if (paymentRequestData.Blob2 is not null)
|
|
|
|
{
|
|
|
|
return paymentRequestData.HasTypedBlob<PaymentRequestBaseData>().GetBlob();
|
|
|
|
}
|
|
|
|
#pragma warning disable CS0618 // Type or member is obsolete
|
|
|
|
else if (paymentRequestData.Blob is not null)
|
|
|
|
{
|
|
|
|
return ParseBlob(paymentRequestData.Blob);
|
|
|
|
}
|
|
|
|
#pragma warning restore CS0618 // Type or member is obsolete
|
|
|
|
return new PaymentRequestBaseData();
|
2019-08-29 17:24:42 +02:00
|
|
|
}
|
|
|
|
|
2023-02-21 07:06:34 +01:00
|
|
|
static PaymentRequestBaseData ParseBlob(byte[] blob)
|
2022-02-02 12:08:07 +01:00
|
|
|
{
|
|
|
|
var jobj = JObject.Parse(ZipUtils.Unzip(blob));
|
|
|
|
// Fixup some legacy payment requests
|
|
|
|
if (jobj["expiryDate"].Type == JTokenType.Date)
|
|
|
|
jobj["expiryDate"] = new JValue(NBitcoin.Utils.DateTimeToUnixTime(jobj["expiryDate"].Value<DateTime>()));
|
|
|
|
return jobj.ToObject<PaymentRequestBaseData>();
|
|
|
|
}
|
|
|
|
|
2023-02-21 07:06:34 +01:00
|
|
|
public static void SetBlob(this PaymentRequestData paymentRequestData, PaymentRequestBaseData blob)
|
2019-08-29 17:24:42 +02:00
|
|
|
{
|
2023-02-21 07:06:34 +01:00
|
|
|
paymentRequestData.HasTypedBlob<PaymentRequestBaseData>().SetBlob(blob);
|
2019-08-29 17:24:42 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|