| | | 1 | | using Microsoft.Extensions.Logging; |
| | | 2 | | |
| | | 3 | | namespace NLightning.Application.Channels.Handlers; |
| | | 4 | | |
| | | 5 | | using Domain.Channels.Enums; |
| | | 6 | | using Domain.Channels.Interfaces; |
| | | 7 | | using Domain.Crypto.ValueObjects; |
| | | 8 | | using Domain.Enums; |
| | | 9 | | using Domain.Exceptions; |
| | | 10 | | using Domain.Node; |
| | | 11 | | using Domain.Node.Options; |
| | | 12 | | using Domain.Protocol.Interfaces; |
| | | 13 | | using Domain.Protocol.Messages; |
| | | 14 | | using Domain.Protocol.Tlv; |
| | | 15 | | using Interfaces; |
| | | 16 | | |
| | | 17 | | public class OpenChannel1MessageHandler : IChannelMessageHandler<OpenChannel1Message> |
| | | 18 | | { |
| | | 19 | | private readonly IChannelFactory _channelFactory; |
| | | 20 | | private readonly IChannelMemoryRepository _channelMemoryRepository; |
| | | 21 | | private readonly ILogger<OpenChannel1MessageHandler> _logger; |
| | | 22 | | private readonly IMessageFactory _messageFactory; |
| | | 23 | | |
| | 0 | 24 | | public OpenChannel1MessageHandler(IChannelFactory channelFactory, IChannelMemoryRepository channelMemoryRepository, |
| | 0 | 25 | | ILogger<OpenChannel1MessageHandler> logger, IMessageFactory messageFactory) |
| | | 26 | | { |
| | 0 | 27 | | _channelFactory = channelFactory; |
| | 0 | 28 | | _channelMemoryRepository = channelMemoryRepository; |
| | 0 | 29 | | _logger = logger; |
| | 0 | 30 | | _messageFactory = messageFactory; |
| | 0 | 31 | | } |
| | | 32 | | |
| | | 33 | | public async Task<IChannelMessage?> HandleAsync(OpenChannel1Message message, ChannelState currentState, |
| | | 34 | | FeatureOptions negotiatedFeatures, CompactPubKey peerPubKey) |
| | | 35 | | { |
| | 0 | 36 | | _logger.LogTrace("Processing OpenChannel1Message with ChannelId: {ChannelId} from Peer: {PeerPubKey}", |
| | 0 | 37 | | message.Payload.ChannelId, peerPubKey); |
| | | 38 | | |
| | 0 | 39 | | var payload = message.Payload; |
| | | 40 | | |
| | 0 | 41 | | if (currentState != ChannelState.None) |
| | 0 | 42 | | throw new ChannelErrorException("A channel with this id already exists", payload.ChannelId); |
| | | 43 | | |
| | | 44 | | // Check if there's a temporary channel for this peer |
| | 0 | 45 | | if (_channelMemoryRepository.TryGetTemporaryChannelState(peerPubKey, payload.ChannelId, out currentState)) |
| | | 46 | | { |
| | 0 | 47 | | if (currentState != ChannelState.V1Opening) |
| | | 48 | | { |
| | 0 | 49 | | throw new ChannelErrorException("Channel had the wrong state", payload.ChannelId, |
| | 0 | 50 | | "This channel is already being negotiated with peer"); |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Create the channel |
| | 0 | 55 | | var channel = await _channelFactory.CreateChannelV1AsNonInitiatorAsync(message, negotiatedFeatures, peerPubKey); |
| | | 56 | | |
| | 0 | 57 | | _logger.LogTrace("Created Channel with fundingPubKey: {fundingPubKey}", |
| | 0 | 58 | | channel.LocalKeySet.FundingCompactPubKey); |
| | | 59 | | |
| | | 60 | | // Add the channel to dictionaries |
| | 0 | 61 | | _channelMemoryRepository.AddTemporaryChannel(peerPubKey, channel); |
| | | 62 | | |
| | | 63 | | // Create UpfrontShutdownScriptTlv if needed |
| | 0 | 64 | | UpfrontShutdownScriptTlv? upfrontShutdownScriptTlv = null; |
| | 0 | 65 | | if (channel.LocalUpfrontShutdownScript is not null) |
| | 0 | 66 | | upfrontShutdownScriptTlv = new UpfrontShutdownScriptTlv(channel.LocalUpfrontShutdownScript.Value); |
| | | 67 | | else |
| | 0 | 68 | | upfrontShutdownScriptTlv = new UpfrontShutdownScriptTlv(Array.Empty<byte>()); |
| | | 69 | | |
| | 0 | 70 | | var channelTypeFeatureSet = FeatureSet.NewBasicChannelType(); |
| | 0 | 71 | | if (negotiatedFeatures.OptionAnchors >= FeatureSupport.Optional) |
| | 0 | 72 | | channelTypeFeatureSet.SetFeature(Feature.OptionAnchors, |
| | 0 | 73 | | negotiatedFeatures.OptionAnchors == FeatureSupport.Compulsory); |
| | | 74 | | |
| | 0 | 75 | | if (channel.ChannelConfig.UseScidAlias >= FeatureSupport.Optional) |
| | 0 | 76 | | channelTypeFeatureSet.SetFeature(Feature.OptionScidAlias, |
| | 0 | 77 | | channel.ChannelConfig.UseScidAlias == FeatureSupport.Compulsory); |
| | | 78 | | |
| | 0 | 79 | | if (channel.ChannelConfig.MinimumDepth == 0) |
| | 0 | 80 | | channelTypeFeatureSet.SetFeature(Feature.OptionZeroconf, true); |
| | | 81 | | |
| | 0 | 82 | | var featureSetBytes = channelTypeFeatureSet.GetBytes() ?? throw new ChannelErrorException("The channel type is n |
| | 0 | 83 | | "Sorry, we had an internal error"); |
| | 0 | 84 | | var channelTypeTlv = new ChannelTypeTlv(featureSetBytes); |
| | | 85 | | |
| | | 86 | | // Create the reply message |
| | 0 | 87 | | var acceptChannel1ReplyMessage = _messageFactory |
| | 0 | 88 | | .CreateAcceptChannel1Message(channel.ChannelConfig.ChannelReserveAmount, channelTypeTlv, |
| | 0 | 89 | | channel.LocalKeySet.DelayedPaymentCompactBasepoint, |
| | 0 | 90 | | channel.LocalKeySet.CurrentPerCommitmentCompactPoint, |
| | 0 | 91 | | channel.LocalKeySet.FundingCompactPubKey, |
| | 0 | 92 | | channel.LocalKeySet.HtlcCompactBasepoint, |
| | 0 | 93 | | channel.ChannelConfig.MaxAcceptedHtlcs, |
| | 0 | 94 | | channel.ChannelConfig.MaxHtlcAmountInFlight, channel.ChannelConfig.MinimumDepth, |
| | 0 | 95 | | channel.LocalKeySet.PaymentCompactBasepoint, |
| | 0 | 96 | | channel.LocalKeySet.RevocationCompactBasepoint, channel.ChannelId, |
| | 0 | 97 | | channel.ChannelConfig.ToSelfDelay, upfrontShutdownScriptTlv); |
| | | 98 | | |
| | 0 | 99 | | return acceptChannel1ReplyMessage; |
| | 0 | 100 | | } |
| | | 101 | | } |