| | | 1 | | namespace NLightning.Domain.Protocol.ValueObjects; |
| | | 2 | | |
| | | 3 | | using Crypto.Constants; |
| | | 4 | | using Domain.Interfaces; |
| | | 5 | | |
| | | 6 | | /// <summary> |
| | | 7 | | /// Represents a chain hash. |
| | | 8 | | /// </summary> |
| | | 9 | | /// <remarks> |
| | | 10 | | /// A chain hash is a 32-byte hash used to identify a chain. |
| | | 11 | | /// </remarks> |
| | | 12 | | public readonly struct ChainHash : IEquatable<ChainHash>, IValueObject |
| | | 13 | | { |
| | 324 | 14 | | public byte[] Value { get; } |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Initializes a new instance of the <see cref="ChainHash"/> struct. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <param name="value">The value of the chain hash.</param> |
| | | 20 | | /// <exception cref="ArgumentOutOfRangeException">Thrown when the value is not 32 bytes.</exception> |
| | | 21 | | public ChainHash(ReadOnlySpan<byte> value) |
| | | 22 | | { |
| | 136 | 23 | | if (value.Length != CryptoConstants.Sha256HashLen) |
| | | 24 | | { |
| | 4 | 25 | | throw new ArgumentOutOfRangeException(nameof(value), |
| | 4 | 26 | | $"ChainHash must be {CryptoConstants.Sha256HashLen} bytes"); |
| | | 27 | | } |
| | | 28 | | |
| | 132 | 29 | | Value = value.ToArray(); |
| | 132 | 30 | | } |
| | | 31 | | |
| | 24 | 32 | | public static implicit operator byte[](ChainHash c) => c.Value; |
| | 12 | 33 | | public static implicit operator ReadOnlyMemory<byte>(ChainHash c) => c.Value; |
| | 8 | 34 | | public static implicit operator ChainHash(byte[] value) => new(value); |
| | | 35 | | |
| | | 36 | | public static bool operator !=(ChainHash left, ChainHash right) |
| | | 37 | | { |
| | 8 | 38 | | return !left.Equals(right); |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | public static bool operator ==(ChainHash left, ChainHash right) |
| | | 42 | | { |
| | 8 | 43 | | return left.Equals(right); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | public bool Equals(ChainHash other) |
| | | 47 | | { |
| | 144 | 48 | | return Value.SequenceEqual(other.Value); |
| | | 49 | | } |
| | | 50 | | |
| | | 51 | | public override bool Equals(object? obj) |
| | | 52 | | { |
| | 0 | 53 | | return obj is ChainHash other && Equals(other); |
| | | 54 | | } |
| | | 55 | | |
| | | 56 | | public override int GetHashCode() |
| | | 57 | | { |
| | 0 | 58 | | return Value.GetHashCode(); |
| | | 59 | | } |
| | | 60 | | } |