btcpayserver/BTCPayServer/WalletId.cs

80 lines
2.3 KiB
C#
Raw Normal View History

2020-06-29 04:44:35 +02:00
using System;
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 class WalletId
{
static readonly Regex _WalletStoreRegex = new Regex("^S-([a-zA-Z0-9]{30,60})-([a-zA-Z]{2,5})$");
public static bool TryParse(string str, out WalletId walletId)
{
ArgumentNullException.ThrowIfNull(str);
2018-07-26 15:32:24 +02:00
walletId = null;
WalletId w = new WalletId();
var match = _WalletStoreRegex.Match(str);
if (!match.Success)
return false;
w.StoreId = match.Groups[1].Value;
w.CryptoCode = match.Groups[2].Value.ToUpperInvariant();
walletId = w;
return true;
}
public WalletId()
{
}
public WalletId(string storeId, string cryptoCode)
{
StoreId = storeId;
CryptoCode = cryptoCode;
}
public string StoreId { get; set; }
public string CryptoCode { get; set; }
2018-10-09 16:44:32 +02:00
2020-06-24 06:44:26 +02:00
public PaymentMethodId GetPaymentMethodId()
{
return new PaymentMethodId(CryptoCode, PaymentTypes.BTCLike);
}
2018-10-09 16:44:32 +02:00
public override bool Equals(object obj)
{
WalletId item = obj as WalletId;
if (item == null)
return false;
return ToString().Equals(item.ToString(), StringComparison.InvariantCulture);
}
public static WalletId Parse(string id)
{
if (TryParse(id, out var v))
return v;
throw new FormatException("Invalid WalletId");
}
2018-10-09 16:44:32 +02:00
public static bool operator ==(WalletId a, WalletId b)
{
if (System.Object.ReferenceEquals(a, b))
return true;
if (((object)a == null) || ((object)b == null))
return false;
return a.ToString() == b.ToString();
}
public static bool operator !=(WalletId a, WalletId b)
{
return !(a == b);
}
public override int GetHashCode()
{
return ToString().GetHashCode(StringComparison.Ordinal);
}
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()}";
}
}
}