| | | 1 | | namespace NLightning.Domain.Channels.ValueObjects; |
| | | 2 | | |
| | | 3 | | using Domain.Interfaces; |
| | | 4 | | |
| | | 5 | | /// <summary> |
| | | 6 | | /// Represents a short channel id. |
| | | 7 | | /// </summary> |
| | | 8 | | /// <remarks> |
| | | 9 | | /// The short channel id is a unique description of the funding transaction. |
| | | 10 | | /// </remarks> |
| | | 11 | | public readonly struct ShortChannelId : IEquatable<ShortChannelId>, IValueObject |
| | | 12 | | { |
| | | 13 | | private readonly byte[] _value; |
| | | 14 | | |
| | | 15 | | public const int Length = 8; |
| | | 16 | | |
| | | 17 | | public readonly uint BlockHeight; |
| | | 18 | | public readonly uint TransactionIndex; |
| | | 19 | | public readonly ushort OutputIndex; |
| | | 20 | | |
| | | 21 | | public ShortChannelId(uint blockHeight, uint transactionIndex, ushort outputIndex) |
| | | 22 | | { |
| | 116 | 23 | | BlockHeight = blockHeight; |
| | 116 | 24 | | TransactionIndex = transactionIndex; |
| | 116 | 25 | | OutputIndex = outputIndex; |
| | | 26 | | |
| | 116 | 27 | | _value = |
| | 116 | 28 | | [ |
| | 116 | 29 | | (byte)(BlockHeight >> 16), |
| | 116 | 30 | | (byte)(BlockHeight >> 8), |
| | 116 | 31 | | (byte)BlockHeight, |
| | 116 | 32 | | (byte)(TransactionIndex >> 16), |
| | 116 | 33 | | (byte)(TransactionIndex >> 8), |
| | 116 | 34 | | (byte)TransactionIndex, |
| | 116 | 35 | | (byte)(OutputIndex >> 8), |
| | 116 | 36 | | (byte)OutputIndex |
| | 116 | 37 | | ]; |
| | 116 | 38 | | } |
| | | 39 | | |
| | | 40 | | public ShortChannelId(byte[] value) |
| | | 41 | | { |
| | 32 | 42 | | if (value.Length != Length) |
| | | 43 | | { |
| | 4 | 44 | | throw new ArgumentException($"ShortChannelId must be {Length} bytes", nameof(value)); |
| | | 45 | | } |
| | | 46 | | |
| | 28 | 47 | | _value = value; |
| | | 48 | | |
| | 28 | 49 | | BlockHeight = (uint)((value[0] << 16) | (value[1] << 8) | value[2]); |
| | 28 | 50 | | TransactionIndex = (uint)((value[3] << 16) | (value[4] << 8) | value[5]); |
| | 28 | 51 | | OutputIndex = (ushort)((value[6] << 8) | value[7]); |
| | 28 | 52 | | } |
| | | 53 | | |
| | 4 | 54 | | public ShortChannelId(ulong channelId) : this( |
| | 4 | 55 | | (uint)((channelId >> 40) & 0xFFFFFF), // BLOCK_HEIGHT |
| | 4 | 56 | | (uint)((channelId >> 16) & 0xFFFF), // TRANSACTION_INDEX |
| | 4 | 57 | | (ushort)(channelId & 0xFF) // OUTPUT_INDEX |
| | 4 | 58 | | ) |
| | | 59 | | { |
| | 4 | 60 | | } |
| | | 61 | | |
| | | 62 | | public static ShortChannelId Parse(string shortChannelId) |
| | | 63 | | { |
| | 32 | 64 | | var parts = shortChannelId.Split('x'); |
| | 32 | 65 | | if (parts.Length != 3) |
| | | 66 | | { |
| | 4 | 67 | | throw new FormatException("Invalid short_channel_id format"); |
| | | 68 | | } |
| | | 69 | | |
| | 28 | 70 | | return new ShortChannelId( |
| | 28 | 71 | | uint.Parse(parts[0]), |
| | 28 | 72 | | uint.Parse(parts[1]), |
| | 28 | 73 | | ushort.Parse(parts[2]) |
| | 28 | 74 | | ); |
| | | 75 | | } |
| | | 76 | | |
| | | 77 | | #region Overrides |
| | | 78 | | |
| | | 79 | | public override string ToString() |
| | | 80 | | { |
| | 4 | 81 | | return $"{BlockHeight}x{TransactionIndex}x{OutputIndex}"; |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | public bool Equals(ShortChannelId other) |
| | | 85 | | { |
| | 32 | 86 | | return _value.SequenceEqual(other._value); |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public override bool Equals(object? obj) |
| | | 90 | | { |
| | 0 | 91 | | return obj is ShortChannelId other && Equals(other); |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | public override int GetHashCode() |
| | | 95 | | { |
| | 8 | 96 | | return HashCode.Combine(BlockHeight, TransactionIndex, OutputIndex); |
| | | 97 | | } |
| | | 98 | | |
| | | 99 | | #endregion |
| | | 100 | | |
| | | 101 | | #region Implicit Operators |
| | | 102 | | |
| | 48 | 103 | | public static implicit operator byte[](ShortChannelId s) => s._value; |
| | 8 | 104 | | public static implicit operator ShortChannelId(byte[] value) => new(value); |
| | 4 | 105 | | public static implicit operator ReadOnlyMemory<byte>(ShortChannelId s) => s._value; |
| | 28 | 106 | | public static implicit operator ReadOnlySpan<byte>(ShortChannelId s) => s._value; |
| | 0 | 107 | | public static implicit operator ShortChannelId(Span<byte> value) => new(value.ToArray()); |
| | 0 | 108 | | public static implicit operator ShortChannelId(ulong value) => new(value); |
| | | 109 | | |
| | | 110 | | public static bool operator !=(ShortChannelId left, ShortChannelId right) |
| | | 111 | | { |
| | 0 | 112 | | return !left.Equals(right); |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | public static bool operator ==(ShortChannelId left, ShortChannelId right) |
| | | 116 | | { |
| | 8 | 117 | | return left.Equals(right); |
| | | 118 | | } |
| | | 119 | | |
| | | 120 | | #endregion |
| | | 121 | | } |