| | 1 | | namespace NLightning.Domain.Protocol.ValueObjects; |
| | 2 | |
|
| | 3 | | using Constants; |
| | 4 | |
|
| | 5 | | public readonly struct BitcoinNetwork : IEquatable<BitcoinNetwork> |
| | 6 | | { |
| 96 | 7 | | private static Dictionary<string, ChainHash> CustomChainHashes { get; } = new(); |
| | 8 | |
|
| 24 | 9 | | public static readonly BitcoinNetwork Mainnet = new(NetworkConstants.Mainnet); |
| 24 | 10 | | public static readonly BitcoinNetwork Testnet = new(NetworkConstants.Testnet); |
| 24 | 11 | | public static readonly BitcoinNetwork Regtest = new(NetworkConstants.Regtest); |
| 24 | 12 | | public static readonly BitcoinNetwork Signet = new(NetworkConstants.Signet); |
| | 13 | |
|
| 1188 | 14 | | public string Name { get; } |
| | 15 | |
|
| | 16 | | public BitcoinNetwork(string name) |
| | 17 | | { |
| 540 | 18 | | Name = name; |
| 540 | 19 | | } |
| | 20 | |
|
| | 21 | | public ChainHash ChainHash |
| | 22 | | { |
| | 23 | | get |
| | 24 | | { |
| 208 | 25 | | return Name switch |
| 208 | 26 | | { |
| 152 | 27 | | NetworkConstants.Mainnet => ChainConstants.Main, |
| 16 | 28 | | NetworkConstants.Testnet => ChainConstants.Testnet, |
| 8 | 29 | | NetworkConstants.Regtest => ChainConstants.Regtest, |
| 32 | 30 | | _ => CustomChainHashes.TryGetValue(Name.ToLowerInvariant(), out var hash) |
| 32 | 31 | | ? hash |
| 32 | 32 | | : throw new InvalidOperationException($"Chain not supported: {Name}") |
| 208 | 33 | | }; |
| | 34 | | } |
| | 35 | | } |
| | 36 | |
|
| 8 | 37 | | public override string ToString() => Name; |
| | 38 | |
|
| | 39 | | /// <summary> |
| | 40 | | /// Register a custom network mapping |
| | 41 | | /// </summary> |
| | 42 | | public void Register(string name, ChainHash chainHash) |
| | 43 | | { |
| 48 | 44 | | if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(nameof(name)); |
| | 45 | |
|
| 24 | 46 | | if (!CustomChainHashes.TryAdd(name.ToLowerInvariant(), chainHash)) |
| | 47 | | { |
| 4 | 48 | | if (CustomChainHashes.ContainsKey(name.ToLowerInvariant())) |
| 4 | 49 | | throw new InvalidOperationException($"Chain hash already registered: {name}"); |
| | 50 | |
|
| 0 | 51 | | throw new InvalidOperationException($"Failed to register chain hash: {name}"); |
| | 52 | | } |
| 20 | 53 | | } |
| | 54 | |
|
| | 55 | | /// <summary> |
| | 56 | | /// Unregister a custom network mapping |
| | 57 | | /// </summary> |
| | 58 | | public static void Unregister(string name) |
| | 59 | | { |
| 12 | 60 | | if (string.IsNullOrWhiteSpace(name)) throw new ArgumentNullException(nameof(name)); |
| 12 | 61 | | CustomChainHashes.Remove(name.ToLowerInvariant()); |
| 12 | 62 | | } |
| | 63 | |
|
| | 64 | | #region Implicit Conversions |
| | 65 | |
|
| 224 | 66 | | public static implicit operator string(BitcoinNetwork bitcoinNetwork) => bitcoinNetwork.Name.ToLowerInvariant(); |
| 260 | 67 | | public static implicit operator BitcoinNetwork(string value) => new(value.ToLowerInvariant()); |
| | 68 | |
|
| | 69 | | #endregion |
| | 70 | |
|
| | 71 | | #region Equality |
| | 72 | |
|
| | 73 | | public override bool Equals(object? obj) |
| | 74 | | { |
| 56 | 75 | | return obj is BitcoinNetwork network && ChainHash.Equals(network.ChainHash); |
| | 76 | | } |
| | 77 | |
|
| | 78 | | public bool Equals(BitcoinNetwork other) |
| | 79 | | { |
| 28 | 80 | | return Name == other.Name; |
| | 81 | | } |
| | 82 | |
|
| | 83 | | public override int GetHashCode() |
| | 84 | | { |
| 0 | 85 | | return Name.GetHashCode(); |
| | 86 | | } |
| | 87 | |
|
| | 88 | | public static bool operator ==(BitcoinNetwork left, BitcoinNetwork right) |
| | 89 | | { |
| 196 | 90 | | return left.Name == right.Name; |
| | 91 | | } |
| | 92 | |
|
| | 93 | | public static bool operator !=(BitcoinNetwork left, BitcoinNetwork right) |
| | 94 | | { |
| 68 | 95 | | return !(left == right); |
| | 96 | | } |
| | 97 | |
|
| | 98 | | #endregion |
| | 99 | | } |