btcpayserver/BTCPayServer/WalletId.cs

51 lines
1.7 KiB
C#
Raw Normal View History

#nullable enable
2020-06-29 04:44:35 +02:00
using System;
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
{
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})$");
public static bool TryParse(string str, [MaybeNullWhen(false)] out WalletId walletId)
2018-07-26 15:32:24 +02:00
{
ArgumentNullException.ThrowIfNull(str);
2018-07-26 15:32:24 +02:00
walletId = null;
var match = _WalletStoreRegex.Match(str);
if (!match.Success)
return false;
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)
{
ArgumentNullException.ThrowIfNull(storeId);
ArgumentNullException.ThrowIfNull(cryptoCode);
2018-07-26 15:32:24 +02:00
StoreId = storeId;
CryptoCode = cryptoCode;
PaymentMethodId = PaymentTypes.CHAIN.GetPaymentMethodId(CryptoCode);
2018-07-26 15:32:24 +02:00
}
public string StoreId { get; }
public string CryptoCode { get; }
public PaymentMethodId PaymentMethodId { get; }
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()}";
}
}
}