| | 1 | | namespace NLightning.Infrastructure.Protocol.Factories; |
| | 2 | |
|
| | 3 | | using Crypto.Hashes; |
| | 4 | | using Domain.Bitcoin.ValueObjects; |
| | 5 | | using Domain.Channels.ValueObjects; |
| | 6 | | using Domain.Crypto.ValueObjects; |
| | 7 | | using Domain.Protocol.Interfaces; |
| | 8 | |
|
| | 9 | | public class ChannelIdFactory : IChannelIdFactory |
| | 10 | | { |
| | 11 | | public ChannelId CreateV1(TxId fundingTxId, ushort fundingOutputIndex) |
| | 12 | | { |
| 0 | 13 | | Span<byte> channelId = stackalloc byte[32]; |
| 0 | 14 | | ((ReadOnlySpan<byte>)fundingTxId).CopyTo(channelId); |
| | 15 | |
|
| | 16 | | // XOR the last 2 bytes with the funding_output_index |
| 0 | 17 | | channelId[30] ^= (byte)(fundingOutputIndex >> 8); |
| 0 | 18 | | channelId[31] ^= (byte)(fundingOutputIndex & 0xFF); |
| | 19 | |
|
| 0 | 20 | | return new ChannelId(channelId); |
| | 21 | | } |
| | 22 | |
|
| | 23 | | public ChannelId CreateV2(CompactPubKey lesserRevocationBasepoint, CompactPubKey greaterRevocationBasepoint) |
| | 24 | | { |
| 4 | 25 | | Span<byte> combined = stackalloc byte[66]; |
| 4 | 26 | | ((ReadOnlySpan<byte>)lesserRevocationBasepoint).CopyTo(combined); |
| 4 | 27 | | ((ReadOnlySpan<byte>)greaterRevocationBasepoint).CopyTo(combined[33..]); |
| | 28 | |
|
| 4 | 29 | | using var hasher = new Sha256(); |
| 4 | 30 | | hasher.AppendData(combined); |
| 4 | 31 | | Span<byte> hash = stackalloc byte[32]; |
| 4 | 32 | | hasher.GetHashAndReset(hash); |
| 4 | 33 | | return new ChannelId(hash); |
| 4 | 34 | | } |
| | 35 | | } |