2024-02-20 10:42:38 +01:00
|
|
|
#nullable enable
|
2020-06-29 04:44:35 +02:00
|
|
|
using System;
|
2024-02-20 10:42:38 +01:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-07-26 15:32:24 +02:00
|
|
|
using System.Text.RegularExpressions;
|
2020-06-24 06:44:26 +02:00
|
|
|
using BTCPayServer.Payments;
|
2018-07-26 15:32:24 +02:00
|
|
|
|
|
|
|
namespace BTCPayServer
|
|
|
|
{
|
2024-02-20 10:42:38 +01:00
|
|
|
public record WalletId
|
2018-07-26 15:32:24 +02:00
|
|
|
{
|
|
|
|
static readonly Regex _WalletStoreRegex = new Regex("^S-([a-zA-Z0-9]{30,60})-([a-zA-Z]{2,5})$");
|
2024-02-20 10:42:38 +01:00
|
|
|
public static bool TryParse(string str, [MaybeNullWhen(false)] out WalletId walletId)
|
2018-07-26 15:32:24 +02:00
|
|
|
{
|
2021-12-28 09:39:54 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(str);
|
2018-07-26 15:32:24 +02:00
|
|
|
walletId = null;
|
|
|
|
var match = _WalletStoreRegex.Match(str);
|
|
|
|
if (!match.Success)
|
|
|
|
return false;
|
2024-02-20 10:42:38 +01:00
|
|
|
var storeId = match.Groups[1].Value;
|
|
|
|
var cryptoCode = match.Groups[2].Value.ToUpperInvariant();
|
|
|
|
walletId = new WalletId(storeId, cryptoCode);
|
2018-07-26 15:32:24 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
public WalletId(string storeId, string cryptoCode)
|
|
|
|
{
|
2024-02-20 10:42:38 +01:00
|
|
|
ArgumentNullException.ThrowIfNull(storeId);
|
|
|
|
ArgumentNullException.ThrowIfNull(cryptoCode);
|
2018-07-26 15:32:24 +02:00
|
|
|
StoreId = storeId;
|
|
|
|
CryptoCode = cryptoCode;
|
2024-10-07 02:37:56 +02:00
|
|
|
PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(CryptoCode);
|
2018-07-26 15:32:24 +02:00
|
|
|
}
|
2024-02-20 10:42:38 +01:00
|
|
|
public string StoreId { get; }
|
|
|
|
public string CryptoCode { get; }
|
2024-10-07 02:37:56 +02:00
|
|
|
public PaymentMethodId PaymentMethodId { get; }
|
2024-02-20 10:42:38 +01:00
|
|
|
|
2019-08-02 17:42:30 +02:00
|
|
|
public static WalletId Parse(string id)
|
|
|
|
{
|
|
|
|
if (TryParse(id, out var v))
|
|
|
|
return v;
|
|
|
|
throw new FormatException("Invalid WalletId");
|
|
|
|
}
|
|
|
|
|
2018-07-26 15:32:24 +02:00
|
|
|
public override string ToString()
|
|
|
|
{
|
2018-10-09 16:44:32 +02:00
|
|
|
if (StoreId == null || CryptoCode == null)
|
|
|
|
return "";
|
2018-07-26 15:32:24 +02:00
|
|
|
return $"S-{StoreId}-{CryptoCode.ToUpperInvariant()}";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|