| | 1 | | using Microsoft.Extensions.Options; |
| | 2 | | using NBitcoin; |
| | 3 | | using NBitcoin.Crypto; |
| | 4 | |
|
| | 5 | | namespace NLightning.Application.Factories; |
| | 6 | |
|
| | 7 | | using Domain.Factories; |
| | 8 | | using Domain.Money; |
| | 9 | | using Domain.Node.Options; |
| | 10 | | using Domain.Protocol.Messages; |
| | 11 | | using Domain.Protocol.Messages.Interfaces; |
| | 12 | | using Domain.Protocol.Models; |
| | 13 | | using Domain.Protocol.Payloads; |
| | 14 | | using Domain.Protocol.Tlv; |
| | 15 | | using Domain.ValueObjects; |
| | 16 | |
|
| | 17 | | /// <summary> |
| | 18 | | /// Factory for creating messages. |
| | 19 | | /// </summary> |
| | 20 | | public class MessageFactory : IMessageFactory |
| | 21 | | { |
| | 22 | | private readonly NodeOptions _nodeOptions; |
| | 23 | | private readonly Network _network; |
| | 24 | |
|
| 8 | 25 | | public MessageFactory(IOptions<NodeOptions> nodeOptions) |
| | 26 | | { |
| 8 | 27 | | _nodeOptions = nodeOptions.Value; |
| 8 | 28 | | _network = _nodeOptions.Network; |
| 8 | 29 | | } |
| | 30 | |
|
| | 31 | | #region Init Message |
| | 32 | | /// <summary> |
| | 33 | | /// Create an Init message. |
| | 34 | | /// </summary> |
| | 35 | | /// <returns>The Init message.</returns> |
| | 36 | | /// <seealso cref="InitMessage"/> |
| | 37 | | /// <seealso cref="InitPayload"/> |
| | 38 | | public InitMessage CreateInitMessage() |
| | 39 | | { |
| | 40 | | // Get features from options |
| 44 | 41 | | var features = _nodeOptions.Features.GetNodeFeatures(); |
| 44 | 42 | | var payload = new InitPayload(features); |
| | 43 | |
|
| 44 | 44 | | return new InitMessage(payload, _nodeOptions.Features.GetInitTlvs()); |
| | 45 | | } |
| | 46 | | #endregion |
| | 47 | |
|
| | 48 | | #region Control Messages |
| | 49 | | /// <summary> |
| | 50 | | /// Create a Warning message. |
| | 51 | | /// </summary> |
| | 52 | | /// <param name="message">The message to be sent.</param> |
| | 53 | | /// <param name="channelId">The channel id, if any.</param> |
| | 54 | | /// <returns>The Warning message.</returns> |
| | 55 | | /// <seealso cref="WarningMessage"/> |
| | 56 | | /// <seealso cref="ChannelId"/> |
| | 57 | | /// <seealso cref="ErrorPayload"/> |
| | 58 | | public WarningMessage CreateWarningMessage(string message, ChannelId? channelId) |
| | 59 | | { |
| 0 | 60 | | var payload = channelId is null ? new ErrorPayload(message) : new ErrorPayload(channelId.Value, message); |
| 0 | 61 | | return new WarningMessage(payload); |
| | 62 | | } |
| | 63 | |
|
| | 64 | | /// <summary> |
| | 65 | | /// Create a Warning message. |
| | 66 | | /// </summary> |
| | 67 | | /// <param name="data">The data to be sent.</param> |
| | 68 | | /// <param name="channelId">The channel id, if any.</param> |
| | 69 | | /// <returns>The Warning message.</returns> |
| | 70 | | /// <seealso cref="WarningMessage"/> |
| | 71 | | /// <seealso cref="ChannelId"/> |
| | 72 | | /// <seealso cref="ErrorPayload"/> |
| | 73 | | public WarningMessage CreateWarningMessage(byte[] data, ChannelId? channelId) |
| | 74 | | { |
| 0 | 75 | | var payload = channelId is null ? new ErrorPayload(data) : new ErrorPayload(channelId.Value, data); |
| 0 | 76 | | return new WarningMessage(payload); |
| | 77 | | } |
| | 78 | |
|
| | 79 | | /// <summary> |
| | 80 | | /// Create a Stfu message. |
| | 81 | | /// </summary> |
| | 82 | | /// <param name="channelId">The channel id, if any.</param> |
| | 83 | | /// <param name="initiator">If we are the sender of the message.</param> |
| | 84 | | /// <returns>The Stfu message.</returns> |
| | 85 | | /// <seealso cref="StfuMessage"/> |
| | 86 | | /// <seealso cref="ChannelId"/> |
| | 87 | | /// <seealso cref="StfuPayload"/> |
| | 88 | | public StfuMessage CreateStfuMessage(ChannelId channelId, bool initiator) |
| | 89 | | { |
| 0 | 90 | | var payload = new StfuPayload(channelId, initiator); |
| 0 | 91 | | return new StfuMessage(payload); |
| | 92 | | } |
| | 93 | |
|
| | 94 | | /// <summary> |
| | 95 | | /// Create an Error message. |
| | 96 | | /// </summary> |
| | 97 | | /// <param name="message">The message to be sent.</param> |
| | 98 | | /// <param name="channelId">The channel id, if any.</param> |
| | 99 | | /// <returns>The Error message.</returns> |
| | 100 | | /// <seealso cref="ErrorMessage"/> |
| | 101 | | /// <seealso cref="ChannelId"/> |
| | 102 | | /// <seealso cref="ErrorPayload"/> |
| | 103 | | public ErrorMessage CreateErrorMessage(string message, ChannelId? channelId) |
| | 104 | | { |
| 0 | 105 | | var payload = channelId is null ? new ErrorPayload(message) : new ErrorPayload(channelId.Value, message); |
| 0 | 106 | | return new ErrorMessage(payload); |
| | 107 | | } |
| | 108 | |
|
| | 109 | | /// <summary> |
| | 110 | | /// Create an Error message. |
| | 111 | | /// </summary> |
| | 112 | | /// <param name="data">The data to be sent.</param> |
| | 113 | | /// <param name="channelId">The channel id, if any.</param> |
| | 114 | | /// <returns>The Error message.</returns> |
| | 115 | | /// <seealso cref="ErrorMessage"/> |
| | 116 | | /// <seealso cref="ChannelId"/> |
| | 117 | | /// <seealso cref="ErrorPayload"/> |
| | 118 | | public ErrorMessage CreateErrorMessage(byte[] data, ChannelId? channelId) |
| | 119 | | { |
| 0 | 120 | | var payload = channelId is null ? new ErrorPayload(data) : new ErrorPayload(channelId.Value, data); |
| 0 | 121 | | return new ErrorMessage(payload); |
| | 122 | | } |
| | 123 | |
|
| | 124 | | /// <summary> |
| | 125 | | /// Create a Ping message. |
| | 126 | | /// </summary> |
| | 127 | | /// <returns>The Ping message.</returns> |
| | 128 | | /// <seealso cref="PingMessage"/> |
| | 129 | | /// <seealso cref="PingPayload"/> |
| | 130 | | public PingMessage CreatePingMessage() |
| | 131 | | { |
| 8 | 132 | | return new PingMessage(); |
| | 133 | | } |
| | 134 | |
|
| | 135 | | /// <summary> |
| | 136 | | /// Create a Pong message. |
| | 137 | | /// </summary> |
| | 138 | | /// <param name="pingMessage">The ping message we're responding to</param> |
| | 139 | | /// <returns>The Pong message.</returns> |
| | 140 | | /// <seealso cref="PongMessage"/> |
| | 141 | | /// <seealso cref="PongPayload"/> |
| | 142 | | public PongMessage CreatePongMessage(IMessage pingMessage) |
| | 143 | | { |
| 8 | 144 | | if (pingMessage is not PingMessage ping) |
| | 145 | | { |
| 0 | 146 | | throw new ArgumentException("Ping message is required", nameof(pingMessage)); |
| | 147 | | } |
| | 148 | |
|
| 8 | 149 | | return new PongMessage(ping.Payload.NumPongBytes); |
| | 150 | | } |
| | 151 | | #endregion |
| | 152 | |
|
| | 153 | | #region Interactive Transaction Construction |
| | 154 | | /// <summary> |
| | 155 | | /// Create a TxAddInput message. |
| | 156 | | /// </summary> |
| | 157 | | /// <param name="channelId">The channel id.</param> |
| | 158 | | /// <param name="serialId">The serial id.</param> |
| | 159 | | /// <param name="prevTx">The previous transaction.</param> |
| | 160 | | /// <param name="prevTxVout">The previous transaction vout.</param> |
| | 161 | | /// <param name="sequence">The sequence number.</param> |
| | 162 | | /// <returns>The TxAddInput message.</returns> |
| | 163 | | /// <seealso cref="TxAddInputMessage"/> |
| | 164 | | /// <seealso cref="ChannelId"/> |
| | 165 | | /// <seealso cref="TxAddInputPayload"/> |
| | 166 | | public TxAddInputMessage CreateTxAddInputMessage(ChannelId channelId, ulong serialId, byte[] prevTx, uint prevTxVout |
| | 167 | | uint sequence) |
| | 168 | | { |
| 0 | 169 | | var payload = new TxAddInputPayload(channelId, serialId, prevTx, prevTxVout, sequence); |
| | 170 | |
|
| 0 | 171 | | return new TxAddInputMessage(payload); |
| | 172 | | } |
| | 173 | |
|
| | 174 | | /// <summary> |
| | 175 | | /// Create a TxAddOutput message. |
| | 176 | | /// </summary> |
| | 177 | | /// <param name="channelId">The channel id.</param> |
| | 178 | | /// <param name="serialId">The serial id.</param> |
| | 179 | | /// <param name="amount">The number of satoshis.</param> |
| | 180 | | /// <param name="script">The script.</param> |
| | 181 | | /// <returns>The TxAddOutput message.</returns> |
| | 182 | | /// <seealso cref="TxAddOutputMessage"/> |
| | 183 | | /// <seealso cref="ChannelId"/> |
| | 184 | | /// <seealso cref="TxAddOutputPayload"/> |
| | 185 | | public TxAddOutputMessage CreateTxAddOutputMessage(ChannelId channelId, ulong serialId, LightningMoney amount, Scrip |
| | 186 | | { |
| 0 | 187 | | var payload = new TxAddOutputPayload(amount, channelId, script, serialId); |
| | 188 | |
|
| 0 | 189 | | return new TxAddOutputMessage(payload); |
| | 190 | | } |
| | 191 | |
|
| | 192 | | /// <summary> |
| | 193 | | /// Create a TxRemoveInput message. |
| | 194 | | /// </summary> |
| | 195 | | /// <param name="channelId">The channel id.</param> |
| | 196 | | /// <param name="serialId">The serial id.</param> |
| | 197 | | /// <returns>The TxRemoveInput message.</returns> |
| | 198 | | /// <seealso cref="TxRemoveInputMessage"/> |
| | 199 | | /// <seealso cref="ChannelId"/> |
| | 200 | | /// <seealso cref="TxRemoveInputPayload"/> |
| | 201 | | public TxRemoveInputMessage CreateTxRemoveInputMessage(ChannelId channelId, ulong serialId) |
| | 202 | | { |
| 0 | 203 | | var payload = new TxRemoveInputPayload(channelId, serialId); |
| | 204 | |
|
| 0 | 205 | | return new TxRemoveInputMessage(payload); |
| | 206 | | } |
| | 207 | |
|
| | 208 | | /// <summary> |
| | 209 | | /// Create a TxRemoveOutput message. |
| | 210 | | /// </summary> |
| | 211 | | /// <param name="channelId">The channel id.</param> |
| | 212 | | /// <param name="serialId">The serial id.</param> |
| | 213 | | /// <returns>The TxRemoveOutput message.</returns> |
| | 214 | | /// <seealso cref="TxRemoveOutputMessage"/> |
| | 215 | | /// <seealso cref="ChannelId"/> |
| | 216 | | /// <seealso cref="TxRemoveOutputPayload"/> |
| | 217 | | public TxRemoveOutputMessage CreateTxRemoveOutputMessage(ChannelId channelId, ulong serialId) |
| | 218 | | { |
| 0 | 219 | | var payload = new TxRemoveOutputPayload(channelId, serialId); |
| | 220 | |
|
| 0 | 221 | | return new TxRemoveOutputMessage(payload); |
| | 222 | | } |
| | 223 | |
|
| | 224 | | /// <summary> |
| | 225 | | /// Create a TxComplete message. |
| | 226 | | /// </summary> |
| | 227 | | /// <param name="channelId">The channel id.</param> |
| | 228 | | /// <returns>The TxComplete message.</returns> |
| | 229 | | /// <seealso cref="TxCompleteMessage"/> |
| | 230 | | /// <seealso cref="ChannelId"/> |
| | 231 | | /// <seealso cref="TxCompletePayload"/> |
| | 232 | | public TxCompleteMessage CreateTxCompleteMessage(ChannelId channelId) |
| | 233 | | { |
| 0 | 234 | | var payload = new TxCompletePayload(channelId); |
| | 235 | |
|
| 0 | 236 | | return new TxCompleteMessage(payload); |
| | 237 | | } |
| | 238 | |
|
| | 239 | | /// <summary> |
| | 240 | | /// Create a TxSignatures message. |
| | 241 | | /// </summary> |
| | 242 | | /// <param name="channelId">The channel id.</param> |
| | 243 | | /// <param name="txId">The transaction id.</param> |
| | 244 | | /// <param name="witnesses">The witnesses.</param> |
| | 245 | | /// <returns>The TxSignatures message.</returns> |
| | 246 | | /// <seealso cref="TxSignaturesMessage"/> |
| | 247 | | /// <seealso cref="ChannelId"/> |
| | 248 | | /// <seealso cref="TxSignaturesPayload"/> |
| | 249 | | public TxSignaturesMessage CreateTxSignaturesMessage(ChannelId channelId, byte[] txId, List<Witness> witnesses) |
| | 250 | | { |
| 0 | 251 | | var payload = new TxSignaturesPayload(channelId, txId, witnesses); |
| | 252 | |
|
| 0 | 253 | | return new TxSignaturesMessage(payload); |
| | 254 | | } |
| | 255 | |
|
| | 256 | | /// <summary> |
| | 257 | | /// Create a TxInitRbf message. |
| | 258 | | /// </summary> |
| | 259 | | /// <param name="channelId">The channel id.</param> |
| | 260 | | /// <param name="locktime">The locktime.</param> |
| | 261 | | /// <param name="feerate">The feerate</param> |
| | 262 | | /// <param name="fundingOutputContrubution">The output contribution.</param> |
| | 263 | | /// <param name="requireConfirmedInputs">How many confirmed inputs we need.</param> |
| | 264 | | /// <returns>The TxInitRbf message.</returns> |
| | 265 | | /// <seealso cref="TxInitRbfMessage"/> |
| | 266 | | /// <seealso cref="ChannelId"/> |
| | 267 | | /// <seealso cref="TxInitRbfPayload"/> |
| | 268 | | public TxInitRbfMessage CreateTxInitRbfMessage(ChannelId channelId, uint locktime, uint feerate, |
| | 269 | | long fundingOutputContrubution, bool requireConfirmedInputs) |
| | 270 | | { |
| 0 | 271 | | FundingOutputContributionTlv? fundingOutputContributionTlv = null; |
| 0 | 272 | | RequireConfirmedInputsTlv? requireConfirmedInputsTlv = null; |
| | 273 | |
|
| 0 | 274 | | if (fundingOutputContrubution > 0) |
| | 275 | | { |
| 0 | 276 | | fundingOutputContributionTlv = new FundingOutputContributionTlv(fundingOutputContrubution); |
| | 277 | | } |
| | 278 | |
|
| 0 | 279 | | if (requireConfirmedInputs) |
| | 280 | | { |
| 0 | 281 | | requireConfirmedInputsTlv = new RequireConfirmedInputsTlv(); |
| | 282 | | } |
| | 283 | |
|
| 0 | 284 | | var payload = new TxInitRbfPayload(channelId, locktime, feerate); |
| | 285 | |
|
| 0 | 286 | | return new TxInitRbfMessage(payload, fundingOutputContributionTlv, requireConfirmedInputsTlv); |
| | 287 | | } |
| | 288 | |
|
| | 289 | | /// <summary> |
| | 290 | | /// Create a TxAckRbf message. |
| | 291 | | /// </summary> |
| | 292 | | /// <param name="channelId">The channel id.</param> |
| | 293 | | /// <param name="fundingOutputContrubution">The output contribution.</param> |
| | 294 | | /// <param name="requireConfirmedInputs">How many confirmed inputs we need.</param> |
| | 295 | | /// <returns>The TxAckRbf message.</returns> |
| | 296 | | /// <seealso cref="TxAckRbfMessage"/> |
| | 297 | | /// <seealso cref="ChannelId"/> |
| | 298 | | /// <seealso cref="TxAckRbfPayload"/> |
| | 299 | | /// <seealso cref="TlvStream"/> |
| | 300 | | /// <seealso cref="FundingOutputContributionTlv"/> |
| | 301 | | /// <seealso cref="RequireConfirmedInputsTlv"/> |
| | 302 | | public TxAckRbfMessage CreateTxAckRbfMessage(ChannelId channelId, long fundingOutputContrubution, |
| | 303 | | bool requireConfirmedInputs) |
| | 304 | | { |
| 0 | 305 | | FundingOutputContributionTlv? fundingOutputContributionTlv = null; |
| 0 | 306 | | RequireConfirmedInputsTlv? requireConfirmedInputsTlv = null; |
| | 307 | |
|
| 0 | 308 | | if (fundingOutputContrubution > 0) |
| | 309 | | { |
| 0 | 310 | | fundingOutputContributionTlv = new FundingOutputContributionTlv(fundingOutputContrubution); |
| | 311 | | } |
| | 312 | |
|
| 0 | 313 | | if (requireConfirmedInputs) |
| | 314 | | { |
| 0 | 315 | | requireConfirmedInputsTlv = new RequireConfirmedInputsTlv(); |
| | 316 | | } |
| | 317 | |
|
| 0 | 318 | | var payload = new TxAckRbfPayload(channelId); |
| | 319 | |
|
| 0 | 320 | | return new TxAckRbfMessage(payload, fundingOutputContributionTlv, requireConfirmedInputsTlv); |
| | 321 | | } |
| | 322 | |
|
| | 323 | | /// <summary> |
| | 324 | | /// Create a TxAbort message. |
| | 325 | | /// </summary> |
| | 326 | | /// <param name="channelId">The channel id.</param> |
| | 327 | | /// <param name="data">The data.</param> |
| | 328 | | /// <returns>The TxAbort message.</returns> |
| | 329 | | /// <seealso cref="TxAbortMessage"/> |
| | 330 | | /// <seealso cref="ChannelId"/> |
| | 331 | | /// <seealso cref="TxAbortPayload"/> |
| | 332 | | public TxAbortMessage CreateTxAbortMessage(ChannelId channelId, byte[] data) |
| | 333 | | { |
| 0 | 334 | | var payload = new TxAbortPayload(channelId, data); |
| | 335 | |
|
| 0 | 336 | | return new TxAbortMessage(payload); |
| | 337 | | } |
| | 338 | | #endregion |
| | 339 | |
|
| | 340 | | #region Channel Messages |
| | 341 | | /// <summary> |
| | 342 | | /// Create a ChannelReady message. |
| | 343 | | /// </summary> |
| | 344 | | /// <param name="channelId">The channel id.</param> |
| | 345 | | /// <param name="secondPerCommitmentPoint">The second per commitment point.</param> |
| | 346 | | /// <param name="shortChannelId">The channel's shortChannelId.</param> |
| | 347 | | /// <returns>The ChannelReady message.</returns> |
| | 348 | | /// <seealso cref="ChannelReadyMessage"/> |
| | 349 | | /// <seealso cref="ChannelId"/> |
| | 350 | | /// <seealso cref="PubKey"/> |
| | 351 | | /// <seealso cref="ShortChannelId"/> |
| | 352 | | /// <seealso cref="ChannelReadyPayload"/> |
| | 353 | | public ChannelReadyMessage CreateChannelReadyMessage(ChannelId channelId, PubKey secondPerCommitmentPoint, |
| | 354 | | ShortChannelId? shortChannelId = null) |
| | 355 | | { |
| 0 | 356 | | var payload = new ChannelReadyPayload(channelId, secondPerCommitmentPoint); |
| | 357 | |
|
| 0 | 358 | | return new ChannelReadyMessage(payload, |
| 0 | 359 | | shortChannelId is null ? null : new ShortChannelIdTlv(shortChannelId.Value)); |
| | 360 | | } |
| | 361 | |
|
| | 362 | | /// <summary> |
| | 363 | | /// Create a Shutdown message. |
| | 364 | | /// </summary> |
| | 365 | | /// <param name="channelId">The channel id.</param> |
| | 366 | | /// <param name="scriptPubkey">The ScriptPubKey to send closing funds to.</param> |
| | 367 | | /// <returns>The Shutdown message.</returns> |
| | 368 | | /// <seealso cref="ShutdownMessage"/> |
| | 369 | | /// <seealso cref="ChannelId"/> |
| | 370 | | /// <seealso cref="Script"/> |
| | 371 | | /// <seealso cref="ShutdownPayload"/> |
| | 372 | | public ShutdownMessage CreateShutdownMessage(ChannelId channelId, Script scriptPubkey) |
| | 373 | | { |
| 0 | 374 | | var payload = new ShutdownPayload(channelId, scriptPubkey); |
| | 375 | |
|
| 0 | 376 | | return new ShutdownMessage(payload); |
| | 377 | | } |
| | 378 | |
|
| | 379 | | /// <summary> |
| | 380 | | /// Create a ClosingSigned message. |
| | 381 | | /// </summary> |
| | 382 | | /// <param name="channelId">The channel id.</param> |
| | 383 | | /// <param name="feeSatoshis">The fee we want them to pay for closing the channel.</param> |
| | 384 | | /// <param name="signature">The signature for closing the channel.</param> |
| | 385 | | /// <param name="minFeeSatoshis">The min fee we will accept them to pay to close the channel.</param> |
| | 386 | | /// <param name="maxFeeSatoshis">The max fee we will accept them to pay to close the channel.</param> |
| | 387 | | /// <returns>The ClosingSigned message.</returns> |
| | 388 | | /// <seealso cref="ClosingSignedMessage"/> |
| | 389 | | /// <seealso cref="ChannelId"/> |
| | 390 | | /// <seealso cref="ECDSASignature"/> |
| | 391 | | /// <seealso cref="ClosingSignedPayload"/> |
| | 392 | | public ClosingSignedMessage CreateClosingSignedMessage(ChannelId channelId, ulong feeSatoshis, ECDSASignature signat |
| | 393 | | ulong minFeeSatoshis, ulong maxFeeSatoshis) |
| | 394 | | { |
| 0 | 395 | | var payload = new ClosingSignedPayload(channelId, feeSatoshis, signature); |
| | 396 | |
|
| 0 | 397 | | return new ClosingSignedMessage(payload, new FeeRangeTlv(minFeeSatoshis, maxFeeSatoshis)); |
| | 398 | | } |
| | 399 | |
|
| | 400 | | /// <summary> |
| | 401 | | /// Create a OpenChannel2 message. |
| | 402 | | /// </summary> |
| | 403 | | /// <param name="temporaryChannelId">The temporary channel id.</param> |
| | 404 | | /// <param name="fundingFeeRatePerKw">The funding fee rate to open the channel.</param> |
| | 405 | | /// <param name="commitmentFeeRatePerKw">The commitment fee rate.</param> |
| | 406 | | /// <param name="fundingSatoshis">The amount of satoshis we're adding to the channel.</param> |
| | 407 | | /// <param name="fundingPubKey">The funding pubkey of the channel.</param> |
| | 408 | | /// <param name="revocationBasepoint">The revocation pubkey.</param> |
| | 409 | | /// <param name="paymentBasepoint">The payment pubkey.</param> |
| | 410 | | /// <param name="delayedPaymentBasepoint">The delayed payment pubkey.</param> |
| | 411 | | /// <param name="htlcBasepoint">The htlc pubkey.</param> |
| | 412 | | /// <param name="firstPerCommitmentPoint">The first per commitment pubkey.</param> |
| | 413 | | /// <param name="secondPerCommitmentPoint">The second per commitment pubkey.</param> |
| | 414 | | /// <param name="channelFlags">The flags for the channel.</param> |
| | 415 | | /// <param name="shutdownScriptPubkey">The shutdown script to be used when closing the channel.</param> |
| | 416 | | /// <param name="channelType">The type of the channel.</param> |
| | 417 | | /// <param name="requireConfirmedInputs">If we want confirmed inputs to open the channel.</param> |
| | 418 | | /// <returns>The OpenChannel2 message.</returns> |
| | 419 | | /// <seealso cref="OpenChannel2Message"/> |
| | 420 | | /// <seealso cref="ChannelId"/> |
| | 421 | | /// <seealso cref="PubKey"/> |
| | 422 | | /// <seealso cref="ChannelFlags"/> |
| | 423 | | /// <seealso cref="Script"/> |
| | 424 | | /// <seealso cref="OpenChannel2Payload"/> |
| | 425 | | public OpenChannel2Message CreateOpenChannel2Message(ChannelId temporaryChannelId, uint fundingFeeRatePerKw, |
| | 426 | | uint commitmentFeeRatePerKw, ulong fundingSatoshis, PubKey fundingPubKey, |
| | 427 | | PubKey revocationBasepoint, PubKey paymentBasepoint, |
| | 428 | | PubKey delayedPaymentBasepoint, PubKey htlcBasepoint, |
| | 429 | | PubKey firstPerCommitmentPoint, PubKey secondPerCommitmentPoint, |
| | 430 | | ChannelFlags channelFlags, Script? shutdownScriptPubkey = null, |
| | 431 | | byte[]? channelType = null, bool requireConfirmedInputs = false) |
| | 432 | | { |
| 0 | 433 | | var payload = new OpenChannel2Payload(_network.ChainHash, channelFlags, commitmentFeeRatePerKw, |
| 0 | 434 | | delayedPaymentBasepoint, _nodeOptions.DustLimitAmount, |
| 0 | 435 | | firstPerCommitmentPoint, fundingSatoshis, fundingFeeRatePerKw, |
| 0 | 436 | | fundingPubKey, htlcBasepoint, _nodeOptions.HtlcMinimumAmount, |
| 0 | 437 | | _nodeOptions.Locktime, _nodeOptions.MaxAcceptedHtlcs, |
| 0 | 438 | | _nodeOptions.MaxHtlcValueInFlight, paymentBasepoint, revocationBasepoint, |
| 0 | 439 | | secondPerCommitmentPoint, _nodeOptions.ToSelfDelay, temporaryChannelId); |
| | 440 | |
|
| 0 | 441 | | return new OpenChannel2Message(payload, |
| 0 | 442 | | shutdownScriptPubkey is null |
| 0 | 443 | | ? null |
| 0 | 444 | | : new UpfrontShutdownScriptTlv(shutdownScriptPubkey), |
| 0 | 445 | | channelType is null ? |
| 0 | 446 | | null |
| 0 | 447 | | : new ChannelTypeTlv(channelType), |
| 0 | 448 | | requireConfirmedInputs ? new RequireConfirmedInputsTlv() : null); |
| | 449 | | } |
| | 450 | |
|
| | 451 | | /// <summary> |
| | 452 | | /// Create a AcceptChannel2 message. |
| | 453 | | /// </summary> |
| | 454 | | /// <param name="temporaryChannelId">The temporary channel id.</param> |
| | 455 | | /// <param name="fundingSatoshis">The amount of satoshis we're adding to the channel.</param> |
| | 456 | | /// <param name="fundingPubKey">The funding pubkey of the channel.</param> |
| | 457 | | /// <param name="revocationBasepoint">The revocation pubkey.</param> |
| | 458 | | /// <param name="paymentBasepoint">The payment pubkey.</param> |
| | 459 | | /// <param name="delayedPaymentBasepoint">The delayed payment pubkey.</param> |
| | 460 | | /// <param name="htlcBasepoint">The htlc pubkey.</param> |
| | 461 | | /// <param name="firstPerCommitmentPoint">The first per commitment pubkey.</param> |
| | 462 | | /// <param name="shutdownScriptPubkey">The shutdown script to be used when closing the channel.</param> |
| | 463 | | /// <param name="channelType">The type of the channel.</param> |
| | 464 | | /// <param name="requireConfirmedInputs">If we want confirmed inputs to open the channel.</param> |
| | 465 | | /// <returns>The AcceptChannel2 message.</returns> |
| | 466 | | /// <seealso cref="AcceptChannel2Message"/> |
| | 467 | | /// <seealso cref="ChannelId"/> |
| | 468 | | /// <seealso cref="PubKey"/> |
| | 469 | | /// <seealso cref="Script"/> |
| | 470 | | /// <seealso cref="AcceptChannel2Payload"/> |
| | 471 | | public AcceptChannel2Message CreateAcceptChannel2Message(ChannelId temporaryChannelId, LightningMoney fundingSatoshi |
| | 472 | | PubKey fundingPubKey, PubKey revocationBasepoint, |
| | 473 | | PubKey paymentBasepoint, PubKey delayedPaymentBasepoint, |
| | 474 | | PubKey htlcBasepoint, PubKey firstPerCommitmentPoint, |
| | 475 | | Script? shutdownScriptPubkey = null, byte[]? channelType = null, |
| | 476 | | bool requireConfirmedInputs = false) |
| | 477 | | { |
| 0 | 478 | | var payload = new AcceptChannel2Payload(delayedPaymentBasepoint, _nodeOptions.DustLimitAmount, |
| 0 | 479 | | firstPerCommitmentPoint, fundingSatoshis, fundingPubKey, |
| 0 | 480 | | htlcBasepoint, _nodeOptions.HtlcMinimumAmount, |
| 0 | 481 | | _nodeOptions.MaxAcceptedHtlcs, _nodeOptions.MaxHtlcValueInFlight, |
| 0 | 482 | | _nodeOptions.MinimumDepth, paymentBasepoint, revocationBasepoint, |
| 0 | 483 | | temporaryChannelId, _nodeOptions.ToSelfDelay); |
| | 484 | |
|
| 0 | 485 | | return new AcceptChannel2Message(payload, |
| 0 | 486 | | shutdownScriptPubkey is null |
| 0 | 487 | | ? null |
| 0 | 488 | | : new UpfrontShutdownScriptTlv(shutdownScriptPubkey), |
| 0 | 489 | | channelType is null ? |
| 0 | 490 | | null |
| 0 | 491 | | : new ChannelTypeTlv(channelType), |
| 0 | 492 | | requireConfirmedInputs ? new RequireConfirmedInputsTlv() : null); |
| | 493 | | } |
| | 494 | | #endregion |
| | 495 | |
|
| | 496 | | #region Commitment |
| | 497 | | /// <summary> |
| | 498 | | /// Create a UpdateAddHtlc message. |
| | 499 | | /// </summary> |
| | 500 | | /// <param name="channelId">The channel id.</param> |
| | 501 | | /// <param name="id">The htlc id.</param> |
| | 502 | | /// <param name="amountMsat">The amount for this htlc.</param> |
| | 503 | | /// <param name="paymentHash">The htlc payment hash.</param> |
| | 504 | | /// <param name="cltvExpiry">The cltv expiry.</param> |
| | 505 | | /// <param name="onionRoutingPacket">The onion routing packet.</param> |
| | 506 | | /// <returns>The UpdateAddHtlc message.</returns> |
| | 507 | | /// <seealso cref="UpdateAddHtlcMessage"/> |
| | 508 | | /// <seealso cref="ChannelId"/> |
| | 509 | | /// <seealso cref="UpdateAddHtlcPayload"/> |
| | 510 | | public UpdateAddHtlcMessage CreateUpdateAddHtlcMessage(ChannelId channelId, ulong id, ulong amountMsat, |
| | 511 | | ReadOnlyMemory<byte> paymentHash, uint cltvExpiry, |
| | 512 | | ReadOnlyMemory<byte>? onionRoutingPacket = null) |
| | 513 | | { |
| 0 | 514 | | var payload = new UpdateAddHtlcPayload(amountMsat, channelId, cltvExpiry, id, paymentHash, onionRoutingPacket); |
| | 515 | |
|
| 0 | 516 | | return new UpdateAddHtlcMessage(payload); |
| | 517 | | } |
| | 518 | |
|
| | 519 | | /// <summary> |
| | 520 | | /// Create a UpdateFulfillHtlc message. |
| | 521 | | /// </summary> |
| | 522 | | /// <param name="channelId">The channel id.</param> |
| | 523 | | /// <param name="id">The htlc id.</param> |
| | 524 | | /// <param name="preimage">The preimage for this htlc.</param> |
| | 525 | | /// <returns>The UpdateFulfillHtlc message.</returns> |
| | 526 | | /// <seealso cref="UpdateFulfillHtlcMessage"/> |
| | 527 | | /// <seealso cref="ChannelId"/> |
| | 528 | | /// <seealso cref="UpdateFulfillHtlcPayload"/> |
| | 529 | | public UpdateFulfillHtlcMessage CreateUpdateFulfillHtlcMessage(ChannelId channelId, ulong id, ReadOnlyMemory<byte> p |
| | 530 | | { |
| 0 | 531 | | var payload = new UpdateFulfillHtlcPayload(channelId, id, preimage); |
| | 532 | |
|
| 0 | 533 | | return new UpdateFulfillHtlcMessage(payload); |
| | 534 | | } |
| | 535 | |
|
| | 536 | | /// <summary> |
| | 537 | | /// Create a UpdateFailHtlc message. |
| | 538 | | /// </summary> |
| | 539 | | /// <param name="channelId">The channel id.</param> |
| | 540 | | /// <param name="id">The htlc id.</param> |
| | 541 | | /// <param name="reason">The reason for failure.</param> |
| | 542 | | /// <returns>The UpdateFailHtlc message.</returns> |
| | 543 | | /// <seealso cref="UpdateFailHtlcMessage"/> |
| | 544 | | /// <seealso cref="ChannelId"/> |
| | 545 | | /// <seealso cref="UpdateFailHtlcPayload"/> |
| | 546 | | public UpdateFailHtlcMessage CreateUpdateFailHtlcMessage(ChannelId channelId, ulong id, ReadOnlyMemory<byte> reason) |
| | 547 | | { |
| 0 | 548 | | var payload = new UpdateFailHtlcPayload(channelId, id, reason); |
| | 549 | |
|
| 0 | 550 | | return new UpdateFailHtlcMessage(payload); |
| | 551 | | } |
| | 552 | |
|
| | 553 | | /// <summary> |
| | 554 | | /// Create a CommitmentSigned message. |
| | 555 | | /// </summary> |
| | 556 | | /// <param name="channelId">The channel id.</param> |
| | 557 | | /// <param name="signature">The signature for the commitment transaction.</param> |
| | 558 | | /// <param name="htlcSignatures">The signatures for each open htlc.</param> |
| | 559 | | /// <returns>The CommitmentSigned message.</returns> |
| | 560 | | /// <seealso cref="CommitmentSignedMessage"/> |
| | 561 | | /// <seealso cref="ChannelId"/> |
| | 562 | | /// <seealso cref="ECDSASignature"/> |
| | 563 | | /// <seealso cref="CommitmentSignedPayload"/> |
| | 564 | | public CommitmentSignedMessage CreateCommitmentSignedMessage(ChannelId channelId, ECDSASignature signature, |
| | 565 | | IEnumerable<ECDSASignature> htlcSignatures) |
| | 566 | | { |
| 0 | 567 | | var payload = new CommitmentSignedPayload(channelId, htlcSignatures, signature); |
| | 568 | |
|
| 0 | 569 | | return new CommitmentSignedMessage(payload); |
| | 570 | | } |
| | 571 | |
|
| | 572 | | /// <summary> |
| | 573 | | /// Create a RevokeAndAck message. |
| | 574 | | /// </summary> |
| | 575 | | /// <param name="channelId">The channel id.</param> |
| | 576 | | /// <param name="perCommitmentSecret">The secret for the commitment transaction.</param> |
| | 577 | | /// <param name="nextPerCommitmentPoint">The next per commitment point.</param> |
| | 578 | | /// <returns>The RevokeAndAck message.</returns> |
| | 579 | | /// <seealso cref="RevokeAndAckMessage"/> |
| | 580 | | /// <seealso cref="ChannelId"/> |
| | 581 | | /// <seealso cref="ECDSASignature"/> |
| | 582 | | /// <seealso cref="RevokeAndAckPayload"/> |
| | 583 | | public RevokeAndAckMessage CreateRevokeAndAckMessage(ChannelId channelId, ReadOnlyMemory<byte> perCommitmentSecret, |
| | 584 | | PubKey nextPerCommitmentPoint) |
| | 585 | | { |
| 0 | 586 | | var payload = new RevokeAndAckPayload(channelId, nextPerCommitmentPoint, perCommitmentSecret); |
| | 587 | |
|
| 0 | 588 | | return new RevokeAndAckMessage(payload); |
| | 589 | | } |
| | 590 | |
|
| | 591 | | /// <summary> |
| | 592 | | /// Create a UpdateFee message. |
| | 593 | | /// </summary> |
| | 594 | | /// <param name="channelId">The channel id.</param> |
| | 595 | | /// <param name="feeratePerKw">The fee rate for the commitment transaction.</param> |
| | 596 | | /// <returns>The UpdateFee message.</returns> |
| | 597 | | /// <seealso cref="UpdateFeeMessage"/> |
| | 598 | | /// <seealso cref="ChannelId"/> |
| | 599 | | /// <seealso cref="UpdateFeePayload"/> |
| | 600 | | public UpdateFeeMessage CreateUpdateFeeMessage(ChannelId channelId, uint feeratePerKw) |
| | 601 | | { |
| 0 | 602 | | var payload = new UpdateFeePayload(channelId, feeratePerKw); |
| | 603 | |
|
| 0 | 604 | | return new UpdateFeeMessage(payload); |
| | 605 | | } |
| | 606 | |
|
| | 607 | | /// <summary> |
| | 608 | | /// Create a UpdateFailMalformedHtlc message. |
| | 609 | | /// </summary> |
| | 610 | | /// <param name="channelId">The channel id.</param> |
| | 611 | | /// <param name="id">The htlc id.</param> |
| | 612 | | /// <param name="sha256OfOnion">The sha256OfOnion for this htlc.</param> |
| | 613 | | /// <param name="failureCode">The failureCode.</param> |
| | 614 | | /// <returns>The UpdateFailMalformedHtlc message.</returns> |
| | 615 | | /// <seealso cref="UpdateFailMalformedHtlcMessage"/> |
| | 616 | | /// <seealso cref="ChannelId"/> |
| | 617 | | /// <seealso cref="UpdateFailMalformedHtlcPayload"/> |
| | 618 | | public UpdateFailMalformedHtlcMessage CreateUpdateFailMalformedHtlcMessage(ChannelId channelId, ulong id, |
| | 619 | | ReadOnlyMemory<byte> sha256OfOnion, ushort failureCode) |
| | 620 | | { |
| 0 | 621 | | var payload = new UpdateFailMalformedHtlcPayload(channelId, failureCode, id, sha256OfOnion); |
| | 622 | |
|
| 0 | 623 | | return new UpdateFailMalformedHtlcMessage(payload); |
| | 624 | | } |
| | 625 | |
|
| | 626 | | /// <summary> |
| | 627 | | /// Create a ChannelReestablish message. |
| | 628 | | /// </summary> |
| | 629 | | /// <param name="channelId">The channel id.</param> |
| | 630 | | /// <param name="nextCommitmentNumber">The next commitment number.</param> |
| | 631 | | /// <param name="nextRevocationNumber">The next revocation number.</param> |
| | 632 | | /// <param name="yourLastPerCommitmentSecret">The peer last per commitment secret.</param> |
| | 633 | | /// <param name="myCurrentPerCommitmentPoint">Our current per commitment point.</param> |
| | 634 | | /// <returns>The ChannelReestablish message.</returns> |
| | 635 | | /// <seealso cref="ChannelReestablishMessage"/> |
| | 636 | | /// <seealso cref="ChannelId"/> |
| | 637 | | /// <seealso cref="ChannelReestablishPayload"/> |
| | 638 | | public ChannelReestablishMessage CreateChannelReestablishMessage(ChannelId channelId, ulong nextCommitmentNumber, |
| | 639 | | ulong nextRevocationNumber, |
| | 640 | | ReadOnlyMemory<byte> yourLastPerCommitmentSecret, |
| | 641 | | PubKey myCurrentPerCommitmentPoint) |
| | 642 | | { |
| 0 | 643 | | var payload = new ChannelReestablishPayload(channelId, myCurrentPerCommitmentPoint, nextCommitmentNumber, |
| 0 | 644 | | nextRevocationNumber, yourLastPerCommitmentSecret); |
| | 645 | |
|
| 0 | 646 | | return new ChannelReestablishMessage(payload); |
| | 647 | | } |
| | 648 | | #endregion |
| | 649 | | } |