2017-09-13 15:47:34 +09:00
|
|
|
|
using BTCPayServer.Authentication;
|
2017-09-23 01:31:29 +09:00
|
|
|
|
using BTCPayServer.Configuration;
|
2017-10-20 22:37:01 -05:00
|
|
|
|
using Microsoft.AspNetCore.Html;
|
2017-09-27 15:16:30 +09:00
|
|
|
|
using Microsoft.AspNetCore.Http;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
2017-09-23 01:31:29 +09:00
|
|
|
|
using Microsoft.Extensions.Configuration;
|
|
|
|
|
using Microsoft.Extensions.DependencyInjection;
|
2017-10-20 22:37:01 -05:00
|
|
|
|
using Newtonsoft.Json;
|
|
|
|
|
using Newtonsoft.Json.Serialization;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Text;
|
2017-10-26 22:27:15 -05:00
|
|
|
|
using System.Text.Encodings.Web;
|
2017-11-06 00:31:02 -08:00
|
|
|
|
using NBitcoin;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
using NBXplorer;
|
|
|
|
|
using NBXplorer.Models;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Threading;
|
2018-01-07 02:16:42 +09:00
|
|
|
|
using BTCPayServer.Services.Wallets;
|
2018-01-08 02:36:41 +09:00
|
|
|
|
using System.IO;
|
2018-01-17 15:02:53 +09:00
|
|
|
|
using BTCPayServer.Logging;
|
|
|
|
|
using Microsoft.Extensions.Logging;
|
2018-02-13 03:27:36 +09:00
|
|
|
|
using System.Net.WebSockets;
|
2018-02-19 02:38:03 +09:00
|
|
|
|
using BTCPayServer.Services.Invoices;
|
|
|
|
|
using NBitpayClient;
|
|
|
|
|
using BTCPayServer.Payments;
|
2017-09-13 15:47:34 +09:00
|
|
|
|
|
|
|
|
|
namespace BTCPayServer
|
|
|
|
|
{
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public static class Extensions
|
|
|
|
|
{
|
2018-03-18 02:26:33 +09:00
|
|
|
|
public static decimal RoundUp(decimal value, int precision)
|
|
|
|
|
{
|
|
|
|
|
for (int i = 0; i < precision; i++)
|
|
|
|
|
{
|
|
|
|
|
value = value * 10m;
|
|
|
|
|
}
|
|
|
|
|
value = Math.Ceiling(value);
|
|
|
|
|
for (int i = 0; i < precision; i++)
|
|
|
|
|
{
|
|
|
|
|
value = value / 10m;
|
|
|
|
|
}
|
|
|
|
|
return value;
|
|
|
|
|
}
|
2018-02-19 15:09:05 +09:00
|
|
|
|
public static PaymentMethodId GetpaymentMethodId(this InvoiceCryptoInfo info)
|
2018-02-19 02:38:03 +09:00
|
|
|
|
{
|
2018-02-19 15:09:05 +09:00
|
|
|
|
return new PaymentMethodId(info.CryptoCode, Enum.Parse<PaymentTypes>(info.PaymentType));
|
2018-02-19 02:38:03 +09:00
|
|
|
|
}
|
2018-02-13 03:27:36 +09:00
|
|
|
|
public static async Task CloseSocket(this WebSocket webSocket)
|
|
|
|
|
{
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
if (webSocket.State == WebSocketState.Open)
|
|
|
|
|
{
|
|
|
|
|
CancellationTokenSource cts = new CancellationTokenSource();
|
|
|
|
|
cts.CancelAfter(5000);
|
|
|
|
|
await webSocket.CloseAsync(WebSocketCloseStatus.NormalClosure, "Closing", cts.Token);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
catch { }
|
|
|
|
|
finally { try { webSocket.Dispose(); } catch { } }
|
|
|
|
|
}
|
2018-01-06 18:57:56 +09:00
|
|
|
|
public static bool SupportDropColumn(this Microsoft.EntityFrameworkCore.Migrations.Migration migration, string activeProvider)
|
|
|
|
|
{
|
|
|
|
|
return activeProvider != "Microsoft.EntityFrameworkCore.Sqlite";
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-19 17:13:29 +09:00
|
|
|
|
public static bool IsCoinAverage(this string exchangeName)
|
|
|
|
|
{
|
|
|
|
|
string[] coinAverages = new[] { "coinaverage", "bitcoinaverage" };
|
|
|
|
|
return String.IsNullOrWhiteSpace(exchangeName) ? true : coinAverages.Contains(exchangeName, StringComparer.OrdinalIgnoreCase) ? true : false;
|
|
|
|
|
}
|
|
|
|
|
|
2018-01-11 17:29:48 +09:00
|
|
|
|
public static async Task<Dictionary<uint256, TransactionResult>> GetTransactions(this BTCPayWallet client, uint256[] hashes, CancellationToken cts = default(CancellationToken))
|
2017-11-06 00:31:02 -08:00
|
|
|
|
{
|
|
|
|
|
hashes = hashes.Distinct().ToArray();
|
|
|
|
|
var transactions = hashes
|
2018-01-11 17:29:48 +09:00
|
|
|
|
.Select(async o => await client.GetTransactionAsync(o, cts))
|
2017-11-06 00:31:02 -08:00
|
|
|
|
.ToArray();
|
|
|
|
|
await Task.WhenAll(transactions).ConfigureAwait(false);
|
|
|
|
|
return transactions.Select(t => t.Result).Where(t => t != null).ToDictionary(o => o.Transaction.GetHash());
|
|
|
|
|
}
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public static string WithTrailingSlash(this string str)
|
|
|
|
|
{
|
2018-02-17 13:18:16 +09:00
|
|
|
|
if (str.EndsWith("/", StringComparison.InvariantCulture))
|
2017-10-27 17:53:04 +09:00
|
|
|
|
return str;
|
|
|
|
|
return str + "/";
|
|
|
|
|
}
|
2017-09-27 15:16:30 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public static string GetAbsoluteRoot(this HttpRequest request)
|
|
|
|
|
{
|
|
|
|
|
return string.Concat(
|
|
|
|
|
request.Scheme,
|
|
|
|
|
"://",
|
|
|
|
|
request.Host.ToUriComponent(),
|
|
|
|
|
request.PathBase.ToUriComponent());
|
|
|
|
|
}
|
2017-09-23 01:31:29 +09:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public static IServiceCollection ConfigureBTCPayServer(this IServiceCollection services, IConfiguration conf)
|
|
|
|
|
{
|
|
|
|
|
services.Configure<BTCPayServerOptions>(o =>
|
|
|
|
|
{
|
|
|
|
|
o.LoadArgs(conf);
|
|
|
|
|
});
|
|
|
|
|
return services;
|
|
|
|
|
}
|
2017-09-23 01:31:29 +09:00
|
|
|
|
|
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
public static BitIdentity GetBitIdentity(this Controller controller, bool throws = true)
|
|
|
|
|
{
|
|
|
|
|
if (!(controller.User.Identity is BitIdentity))
|
|
|
|
|
return throws ? throw new UnauthorizedAccessException("no-bitid") : (BitIdentity)null;
|
|
|
|
|
return (BitIdentity)controller.User.Identity;
|
|
|
|
|
}
|
2017-10-20 22:37:01 -05:00
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
private static JsonSerializerSettings jsonSettings = new JsonSerializerSettings { ContractResolver = new CamelCasePropertyNamesContractResolver() };
|
|
|
|
|
public static string ToJson(this object o)
|
|
|
|
|
{
|
|
|
|
|
var res = JsonConvert.SerializeObject(o, Formatting.None, jsonSettings);
|
|
|
|
|
return res;
|
|
|
|
|
}
|
2017-10-26 22:27:15 -05:00
|
|
|
|
|
2017-12-17 01:04:20 +09:00
|
|
|
|
public static HtmlString ToJSVariableModel(this object o, string variableName)
|
2017-10-27 17:53:04 +09:00
|
|
|
|
{
|
|
|
|
|
var encodedJson = JavaScriptEncoder.Default.Encode(o.ToJson());
|
2017-12-17 01:04:20 +09:00
|
|
|
|
return new HtmlString($"var {variableName} = JSON.parse('" + encodedJson + "');");
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
2017-10-26 22:27:15 -05:00
|
|
|
|
|
|
|
|
|
|
2017-10-27 17:53:04 +09:00
|
|
|
|
}
|
2017-09-13 15:47:34 +09:00
|
|
|
|
}
|