private void OnShutdownMessage(IPeer peer, ShutdownMessage message) { var channel = _channelService.Channels.SingleOrDefault(c => c.ChannelId == message.ChannelId.ToHex()); if (channel == null) { _logger.LogDebug($"Remote sent us a {nameof(ShutdownMessage)}, but there is no matching channel."); peer.Messaging.Send(ErrorMessage.UnknownChannel(message.ChannelId)); return; } if (channel.RemoteChannelParameters.ShutdownScriptPubKey != null && !channel.RemoteChannelParameters.ShutdownScriptPubKey.SequenceEqual(message.ScriptPubKey)) { _channelLoggingService.LogError(channel, LocalChannelError.InvalidShutdownScriptPubKey, "Received shutdown message with invalid ShutdownScriptPubKey. Will do an unilateral close"); channel.CloseReason = CloseReason.InvalidShutdownPubKey; UnilateralClose(channel); return; } channel.RemoteChannelParameters.ShutdownScriptPubKey = message.ScriptPubKey; if (channel.State == LocalChannelState.NormalOperation) { channel.CloseReason = CloseReason.RemoteMutualClose; RespondWithShutdown(peer, channel, message); } else if (channel.State == LocalChannelState.Shutdown || channel.State == LocalChannelState.ClosingSigned) { RespondWithClosingSigned(peer, channel); } _channelService.UpdateChannel(channel); }
public void Handle(IPeer peer, FundingSignedMessage message, PendingChannel pendingChannel) { var channel = pendingChannel.Channel; var oldState = channel.State; var signature = SignatureConverter.RawToTransactionSignature(message.Signature); if (!_commitmentService.IsValidRemoteCommitmentSignature(channel, signature)) { _channelLoggingService.LogError(channel, LocalChannelError.InvalidCommitmentSignature, $"Remote {peer.NodeAddress} sent us an invalid commitment signature"); string error = "Invalid commitment transaction signature."; channel.State = LocalChannelState.FundingFailed; peer.Messaging.Send(new ErrorMessage(message.ChannelId, error)); throw new ChannelException(error, channel); } channel.LocalCommitmentTxParameters.RemoteSignature = signature; channel.State = LocalChannelState.FundingSigned; _channelService.AddChannel(peer, channel); _fundingService.BroadcastFundingTransaction(channel); _blockchainMonitorService.WatchForTransactionId(channel.FundingTransactionId, (ushort)channel.MinimumDepth); _channelService.RemovePendingChannel(pendingChannel); _channelLoggingService.LogStateUpdate(channel, oldState); }