@@ -99,7 +99,7 @@ object Helpers {
9999 }
100100
101101 /** Called by the fundee of a single-funded channel. */
102- def validateParamsSingleFundedFundee (nodeParams : NodeParams , channelType : SupportedChannelType , localFeatures : Features [InitFeature ], open : OpenChannel , remoteNodeId : PublicKey , remoteFeatures : Features [InitFeature ]): Either [ChannelException , (ChannelFeatures , Option [ByteVector ])] = {
102+ def validateParamsSingleFundedFundee (nodeParams : NodeParams , localFeatures : Features [InitFeature ], open : OpenChannel , remoteNodeId : PublicKey , remoteFeatures : Features [InitFeature ]): Either [ChannelException , (ChannelFeatures , Option [ByteVector ])] = {
103103 // BOLT #2: if the chain_hash value, within the open_channel, message is set to a hash of a chain that is unknown to the receiver:
104104 // MUST reject the channel.
105105 if (nodeParams.chainHash != open.chainHash) return Left (InvalidChainHash (open.temporaryChannelId, local = nodeParams.chainHash, remote = open.chainHash))
@@ -133,6 +133,10 @@ object Helpers {
133133 return Left (ChannelReserveNotMet (open.temporaryChannelId, toLocalMsat, toRemoteMsat, open.channelReserveSatoshis))
134134 }
135135
136+ val channelType = ChannelTypes .areCompatible(open.temporaryChannelId, localFeatures, open.channelType_opt) match {
137+ case Left (f) => return Left (f)
138+ case Right (proposedChannelType) => proposedChannelType
139+ }
136140 val channelFeatures = ChannelFeatures (channelType, localFeatures, remoteFeatures, open.channelFlags.announceChannel)
137141 channelType.commitmentFormat match {
138142 case _ : SimpleTaprootChannelCommitmentFormat => if (open.commitNonce_opt.isEmpty) return Left (MissingCommitNonce (open.temporaryChannelId, TxId (ByteVector32 .Zeroes ), commitmentNumber = 0 ))
@@ -154,7 +158,6 @@ object Helpers {
154158
155159 /** Called by the non-initiator of a dual-funded channel. */
156160 def validateParamsDualFundedNonInitiator (nodeParams : NodeParams ,
157- channelType : SupportedChannelType ,
158161 open : OpenDualFundedChannel ,
159162 fundingScript : ByteVector ,
160163 remoteNodeId : PublicKey ,
@@ -184,6 +187,10 @@ object Helpers {
184187 if (open.dustLimit < Channel .MIN_DUST_LIMIT ) return Left (DustLimitTooSmall (open.temporaryChannelId, open.dustLimit, Channel .MIN_DUST_LIMIT ))
185188 if (open.dustLimit > nodeParams.channelConf.maxRemoteDustLimit) return Left (DustLimitTooLarge (open.temporaryChannelId, open.dustLimit, nodeParams.channelConf.maxRemoteDustLimit))
186189
190+ val channelType = ChannelTypes .areCompatible(open.temporaryChannelId, localFeatures, open.channelType_opt) match {
191+ case Left (f) => return Left (f)
192+ case Right (proposedChannelType) => proposedChannelType
193+ }
187194 val channelFeatures = ChannelFeatures (channelType, localFeatures, remoteFeatures, open.channelFlags.announceChannel)
188195
189196 // BOLT #2: The receiving node MUST fail the channel if: it considers feerate_per_kw too small for timely processing or unreasonably large.
@@ -196,22 +203,19 @@ object Helpers {
196203 } yield (channelFeatures, script_opt, willFund_opt)
197204 }
198205
199- private def validateChannelType (channelId : ByteVector32 , channelType : SupportedChannelType , openChannelType_opt : Option [ChannelType ], acceptChannelType_opt : Option [ChannelType ]): Option [ChannelException ] = {
200- if (openChannelType_opt.isEmpty || acceptChannelType_opt.isEmpty) {
201- Some (MissingChannelType (channelId))
202- } else if (! openChannelType_opt.contains(channelType) || ! acceptChannelType_opt.contains(channelType)) {
203- Some (InvalidChannelType (channelId, acceptChannelType_opt.get))
204- } else {
205- // we agree on channel type
206- None
206+ private def validateChannelTypeInitiator (channelId : ByteVector32 , openChannelType_opt : Option [ChannelType ], acceptChannelType_opt : Option [ChannelType ]): Either [ChannelException , SupportedChannelType ] = {
207+ (openChannelType_opt, acceptChannelType_opt) match {
208+ case (Some (proposed : SupportedChannelType ), Some (received)) if proposed == received => Right (proposed)
209+ case (Some (_), Some (received)) => Left (InvalidChannelType (channelId, received))
210+ case _ => Left (MissingChannelType (channelId))
207211 }
208212 }
209213
210214 /** Called by the funder of a single-funded channel. */
211- def validateParamsSingleFundedFunder (nodeParams : NodeParams , channelType : SupportedChannelType , localFeatures : Features [InitFeature ], remoteFeatures : Features [InitFeature ], open : OpenChannel , accept : AcceptChannel ): Either [ChannelException , (ChannelFeatures , Option [ByteVector ])] = {
212- validateChannelType (open.temporaryChannelId, channelType , open.channelType_opt, accept.channelType_opt) match {
213- case Some (t) => return Left (t)
214- case None => // we agree on channel type
215+ def validateParamsSingleFundedFunder (nodeParams : NodeParams , localFeatures : Features [InitFeature ], remoteFeatures : Features [InitFeature ], open : OpenChannel , accept : AcceptChannel ): Either [ChannelException , (ChannelFeatures , Option [ByteVector ])] = {
216+ val channelType = validateChannelTypeInitiator (open.temporaryChannelId, open.channelType_opt, accept.channelType_opt) match {
217+ case Left (t) => return Left (t)
218+ case Right (channelType) => channelType
215219 }
216220
217221 if (accept.maxAcceptedHtlcs > Channel .MAX_ACCEPTED_HTLCS ) return Left (InvalidMaxAcceptedHtlcs (accept.temporaryChannelId, accept.maxAcceptedHtlcs, Channel .MAX_ACCEPTED_HTLCS ))
@@ -248,14 +252,13 @@ object Helpers {
248252 /** Called by the initiator of a dual-funded channel. */
249253 def validateParamsDualFundedInitiator (nodeParams : NodeParams ,
250254 remoteNodeId : PublicKey ,
251- channelType : SupportedChannelType ,
252255 localFeatures : Features [InitFeature ],
253256 remoteFeatures : Features [InitFeature ],
254257 open : OpenDualFundedChannel ,
255258 accept : AcceptDualFundedChannel ): Either [ChannelException , (ChannelFeatures , Option [ByteVector ], Option [LiquidityAds .Purchase ])] = {
256- validateChannelType (open.temporaryChannelId, channelType , open.channelType_opt, accept.channelType_opt) match {
257- case Some (t) => return Left (t)
258- case None => // we agree on channel type
259+ val channelType = validateChannelTypeInitiator (open.temporaryChannelId, open.channelType_opt, accept.channelType_opt) match {
260+ case Left (t) => return Left (t)
261+ case Right (channelType) => channelType
259262 }
260263
261264 // BOLT #2: Channel funding limits
0 commit comments