| | | 1 | | namespace NLightning.Domain.Bitcoin.ValueObjects; |
| | | 2 | | |
| | | 3 | | using Domain.Interfaces; |
| | | 4 | | using Domain.Utils.Extensions; |
| | | 5 | | |
| | | 6 | | public readonly struct BitcoinScript : IValueObject, IEquatable<BitcoinScript> |
| | | 7 | | { |
| | | 8 | | private readonly byte[] _value; |
| | | 9 | | |
| | 0 | 10 | | public static BitcoinScript Empty => new([]); |
| | | 11 | | |
| | 12 | 12 | | public int Length => _value.Length; |
| | | 13 | | |
| | | 14 | | public BitcoinScript(byte[] value) |
| | | 15 | | { |
| | 80 | 16 | | _value = value ?? throw new ArgumentNullException(nameof(value), "BitcoinScript cannot be null."); |
| | 80 | 17 | | } |
| | | 18 | | |
| | 52 | 19 | | public static implicit operator BitcoinScript(byte[] bytes) => new(bytes); |
| | 64 | 20 | | public static implicit operator byte[](BitcoinScript script) => script._value; |
| | 8 | 21 | | public static implicit operator ReadOnlyMemory<byte>(BitcoinScript compactPubKey) => compactPubKey._value; |
| | | 22 | | |
| | | 23 | | public override string ToString() |
| | | 24 | | { |
| | 0 | 25 | | return Convert.ToHexString(_value).ToLowerInvariant(); |
| | | 26 | | } |
| | | 27 | | |
| | | 28 | | public bool Equals(BitcoinScript other) |
| | | 29 | | { |
| | 12 | 30 | | return _value.SequenceEqual(other._value); |
| | | 31 | | } |
| | | 32 | | |
| | | 33 | | public override bool Equals(object? obj) |
| | | 34 | | { |
| | 0 | 35 | | return obj is BitcoinScript other && Equals(other); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | public override int GetHashCode() |
| | | 39 | | { |
| | 0 | 40 | | return _value.GetByteArrayHashCode(); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public static bool operator !=(BitcoinScript left, BitcoinScript right) |
| | | 44 | | { |
| | 0 | 45 | | return !left.Equals(right); |
| | | 46 | | } |
| | | 47 | | |
| | | 48 | | public static bool operator ==(BitcoinScript left, BitcoinScript right) |
| | | 49 | | { |
| | 0 | 50 | | return left.Equals(right); |
| | | 51 | | } |
| | | 52 | | } |