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
|
|
|
{
|
|
|
|
var result = paymentRequestData.Blob == null
|
2020-05-19 19:59:23 +02:00
|
|
|
? new PaymentRequestBaseData()
|
2022-02-02 12:08:07 +01:00
|
|
|
: ParseBlob(paymentRequestData.Blob);
|
2019-08-29 17:24:42 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2022-02-02 12:08:07 +01:00
|
|
|
private static PaymentRequestBaseData ParseBlob(byte[] blob)
|
|
|
|
{
|
|
|
|
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>();
|
|
|
|
}
|
|
|
|
|
2020-05-19 19:59:23 +02:00
|
|
|
public static bool SetBlob(this PaymentRequestData paymentRequestData, PaymentRequestBaseData blob)
|
2019-08-29 17:24:42 +02:00
|
|
|
{
|
2019-11-15 10:53:20 +01:00
|
|
|
var original = new Serializer(null).ToString(paymentRequestData.GetBlob());
|
|
|
|
var newBlob = new Serializer(null).ToString(blob);
|
2019-08-29 17:24:42 +02:00
|
|
|
if (original == newBlob)
|
|
|
|
return false;
|
|
|
|
paymentRequestData.Blob = ZipUtils.Zip(newBlob);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|