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