| | 1 | | namespace NLightning.Domain.ValueObjects; |
| | 2 | |
|
| | 3 | | using Protocol.Constants; |
| | 4 | |
|
| | 5 | | public readonly struct Network : IEquatable<Network>, IEquatable<NBitcoin.Network> |
| | 6 | | { |
| 24 | 7 | | public static readonly Network MAINNET = new(NetworkConstants.MAINNET); |
| 24 | 8 | | public static readonly Network TESTNET = new(NetworkConstants.TESTNET); |
| 24 | 9 | | public static readonly Network REGTEST = new(NetworkConstants.REGTEST); |
| 24 | 10 | | public static readonly Network SIGNET = new(NetworkConstants.SIGNET); |
| | 11 | |
|
| 992 | 12 | | public string Name { get; } |
| | 13 | |
|
| | 14 | | public Network(string name) |
| | 15 | | { |
| 468 | 16 | | Name = name; |
| 468 | 17 | | } |
| | 18 | |
|
| | 19 | | public ChainHash ChainHash |
| | 20 | | { |
| | 21 | | get |
| | 22 | | { |
| 36 | 23 | | return Name switch |
| 36 | 24 | | { |
| 24 | 25 | | NetworkConstants.MAINNET => ChainConstants.MAIN, |
| 4 | 26 | | NetworkConstants.TESTNET => ChainConstants.TESTNET, |
| 4 | 27 | | NetworkConstants.REGTEST => ChainConstants.REGTEST, |
| 4 | 28 | | _ => throw new Exception("Chain not supported.") |
| 36 | 29 | | }; |
| | 30 | | } |
| | 31 | | } |
| | 32 | |
|
| 4 | 33 | | public override string ToString() => Name; |
| | 34 | |
|
| | 35 | | #region Implicit Conversions |
| 4 | 36 | | public static implicit operator string(Network network) => network.Name; |
| 236 | 37 | | public static implicit operator Network(string value) => new(value); |
| | 38 | |
|
| | 39 | | public static implicit operator NBitcoin.Network(Network network) |
| | 40 | | { |
| 184 | 41 | | return network.Name switch |
| 184 | 42 | | { |
| 180 | 43 | | NetworkConstants.MAINNET => NBitcoin.Network.Main, |
| 4 | 44 | | NetworkConstants.TESTNET => NBitcoin.Network.TestNet, |
| 0 | 45 | | NetworkConstants.REGTEST => NBitcoin.Network.RegTest, |
| 0 | 46 | | _ => throw new ArgumentException("Unsupported network type", nameof(network)), |
| 184 | 47 | | }; |
| | 48 | | } |
| | 49 | | public static implicit operator Network(NBitcoin.Network network) |
| | 50 | | { |
| 44 | 51 | | return network.Name switch |
| 44 | 52 | | { |
| 40 | 53 | | "Main" => MAINNET, |
| 4 | 54 | | "TestNet" => TESTNET, |
| 0 | 55 | | "RegTest" => REGTEST, |
| 0 | 56 | | _ => throw new ArgumentException("Unsupported network type", nameof(network)), |
| 44 | 57 | | }; |
| | 58 | | } |
| | 59 | | #endregion |
| | 60 | |
|
| | 61 | | #region Equality |
| | 62 | | public override bool Equals(object? obj) |
| | 63 | | { |
| 56 | 64 | | return obj is Network network && Name == network.Name; |
| | 65 | | } |
| | 66 | |
|
| | 67 | | public bool Equals(Network other) |
| | 68 | | { |
| 32 | 69 | | return Name == other.Name; |
| | 70 | | } |
| | 71 | |
|
| | 72 | | public bool Equals(NBitcoin.Network? other) |
| | 73 | | { |
| 0 | 74 | | return other is not null && ((byte[])ChainHash).SequenceEqual(other.GenesisHash.ToBytes()); |
| | 75 | | } |
| | 76 | |
|
| | 77 | | public override int GetHashCode() |
| | 78 | | { |
| 0 | 79 | | return Name.GetHashCode(); |
| | 80 | | } |
| | 81 | |
|
| | 82 | | public static bool operator ==(Network left, Network right) |
| | 83 | | { |
| 196 | 84 | | return left.Name == right.Name; |
| | 85 | | } |
| | 86 | |
|
| | 87 | | public static bool operator !=(Network left, Network right) |
| | 88 | | { |
| 68 | 89 | | return !(left == right); |
| | 90 | | } |
| | 91 | | #endregion |
| | 92 | | } |