-
Notifications
You must be signed in to change notification settings - Fork 1
/
LightningMessageTypes.scala
476 lines (428 loc) · 14.8 KB
/
LightningMessageTypes.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
package scoin.ln
import java.nio.charset.StandardCharsets
import scala.util.Try
import scodec.bits.ByteVector
import com.comcast.ip4s.{Ipv4Address, Ipv6Address, IpAddress}
import scoin._
import scoin.Crypto.{PrivateKey, PublicKey}
// @formatter:off
trait LightningMessage extends Serializable
sealed trait SetupMessage extends LightningMessage
sealed trait ChannelMessage extends LightningMessage
sealed trait HtlcMessage extends LightningMessage
sealed trait RoutingMessage extends LightningMessage
sealed trait AnnouncementMessage extends RoutingMessage // <- not in the spec
sealed trait HasTimestamp extends LightningMessage { def timestamp: TimestampSecond }
sealed trait HasTemporaryChannelId extends LightningMessage { def temporaryChannelId: ByteVector32 } // <- not in the spec
sealed trait HasChannelId extends LightningMessage { def channelId: ByteVector32 } // <- not in the spec
sealed trait HasChainHash extends LightningMessage { def chainHash: ByteVector32 } // <- not in the spec
sealed trait UpdateMessage extends HtlcMessage // <- not in the spec
sealed trait HtlcSettlementMessage extends UpdateMessage { def id: Long } // <- not in the spec
// @formatter:on
case class Init(
features: Features[InitFeature],
tlvStream: TlvStream[InitTlv] = TlvStream.empty
) extends SetupMessage {
val networks =
tlvStream.get[InitTlv.Networks].map(_.chainHashes).getOrElse(Nil)
val remoteAddressOpt = tlvStream.get[InitTlv.RemoteAddress].map(_.address)
}
case class Warning(
channelId: ByteVector32,
data: ByteVector,
tlvStream: TlvStream[WarningTlv] = TlvStream.empty
) extends SetupMessage
with HasChannelId {
val isGlobal: Boolean = channelId == ByteVector32.Zeroes
def toAscii: String =
if (data.toArray.forall(ch => ch >= 32 && ch < 127))
new String(data.toArray, StandardCharsets.US_ASCII)
else "n/a"
def asText: String = new String(data.toArray, StandardCharsets.UTF_8)
}
object Warning {
def apply(channelId: ByteVector32, msg: String): Warning =
Warning(channelId, ByteVector.view(msg.getBytes(StandardCharsets.US_ASCII)))
def apply(msg: String): Warning = Warning(
ByteVector32.Zeroes,
ByteVector.view(msg.getBytes(StandardCharsets.US_ASCII))
)
}
case class Error(
channelId: ByteVector32,
data: ByteVector,
tlvStream: TlvStream[ErrorTlv] = TlvStream.empty
) extends SetupMessage
with HasChannelId {
def toAscii: String =
if (data.toArray.forall(ch => ch >= 32 && ch < 127))
new String(data.toArray, StandardCharsets.US_ASCII)
else "n/a"
def asText: String = new String(data.toArray, StandardCharsets.UTF_8)
}
object Error {
def apply(channelId: ByteVector32, msg: String): Error =
Error(channelId, ByteVector.view(msg.getBytes(StandardCharsets.US_ASCII)))
}
case class Ping(
pongLength: Int,
data: ByteVector,
tlvStream: TlvStream[PingTlv] = TlvStream.empty
) extends SetupMessage
case class Pong(
data: ByteVector,
tlvStream: TlvStream[PongTlv] = TlvStream.empty
) extends SetupMessage
case class ChannelReestablish(
channelId: ByteVector32,
nextLocalCommitmentNumber: Long,
nextRemoteRevocationNumber: Long,
yourLastPerCommitmentSecret: PrivateKey,
myCurrentPerCommitmentPoint: PublicKey,
tlvStream: TlvStream[ChannelReestablishTlv] = TlvStream.empty
) extends ChannelMessage
with HasChannelId
case class OpenChannel(
chainHash: ByteVector32,
temporaryChannelId: ByteVector32,
fundingSatoshis: Satoshi,
pushMsat: MilliSatoshi,
dustLimitSatoshis: Satoshi,
maxHtlcValueInFlightMsat: UInt64, // this is not MilliSatoshi because it can exceed the total amount of MilliSatoshi
channelReserveSatoshis: Satoshi,
htlcMinimumMsat: MilliSatoshi,
feeratePerKw: FeeratePerKw,
toSelfDelay: CltvExpiryDelta,
maxAcceptedHtlcs: Int,
fundingPubkey: PublicKey,
revocationBasepoint: PublicKey,
paymentBasepoint: PublicKey,
delayedPaymentBasepoint: PublicKey,
htlcBasepoint: PublicKey,
firstPerCommitmentPoint: PublicKey,
channelFlags: OpenChannel.ChannelFlags,
tlvStream: TlvStream[OpenChannelTlv] = TlvStream.empty
) extends ChannelMessage
with HasTemporaryChannelId
with HasChainHash {
val upfrontShutdownScriptOpt: Option[ByteVector] =
tlvStream.get[ChannelTlv.UpfrontShutdownScriptTlv].map(_.script)
}
object OpenChannel {
case class ChannelFlags(announceChannel: Boolean)
}
case class AcceptChannel(
temporaryChannelId: ByteVector32,
dustLimitSatoshis: Satoshi,
maxHtlcValueInFlightMsat: UInt64, // this is not MilliSatoshi because it can exceed the total amount of MilliSatoshi
channelReserveSatoshis: Satoshi,
htlcMinimumMsat: MilliSatoshi,
minimumDepth: Long,
toSelfDelay: CltvExpiryDelta,
maxAcceptedHtlcs: Int,
fundingPubkey: PublicKey,
revocationBasepoint: PublicKey,
paymentBasepoint: PublicKey,
delayedPaymentBasepoint: PublicKey,
htlcBasepoint: PublicKey,
firstPerCommitmentPoint: PublicKey,
tlvStream: TlvStream[AcceptChannelTlv] = TlvStream.empty
) extends ChannelMessage
with HasTemporaryChannelId {
val upfrontShutdownScriptOpt: Option[ByteVector] =
tlvStream.get[ChannelTlv.UpfrontShutdownScriptTlv].map(_.script)
}
case class FundingCreated(
temporaryChannelId: ByteVector32,
fundingTxid: ByteVector32,
fundingOutputIndex: Int,
signature: ByteVector64,
tlvStream: TlvStream[FundingCreatedTlv] = TlvStream.empty
) extends ChannelMessage
with HasTemporaryChannelId
case class FundingSigned(
channelId: ByteVector32,
signature: ByteVector64,
tlvStream: TlvStream[FundingSignedTlv] = TlvStream.empty
) extends ChannelMessage
with HasChannelId
case class FundingLocked(
channelId: ByteVector32,
nextPerCommitmentPoint: PublicKey,
tlvStream: TlvStream[FundingLockedTlv] = TlvStream.empty
) extends ChannelMessage
with HasChannelId
case class Shutdown(
channelId: ByteVector32,
scriptPubKey: ByteVector,
tlvStream: TlvStream[ShutdownTlv] = TlvStream.empty
) extends ChannelMessage
with HasChannelId
case class ClosingSigned(
channelId: ByteVector32,
feeSatoshis: Satoshi,
signature: ByteVector64,
tlvStream: TlvStream[ClosingSignedTlv] = TlvStream.empty
) extends ChannelMessage
with HasChannelId {
val feeRangeOpt = tlvStream.get[ClosingSignedTlv.FeeRange]
}
case class UpdateAddHtlc(
channelId: ByteVector32,
id: Long,
amountMsat: MilliSatoshi,
paymentHash: ByteVector32,
cltvExpiry: CltvExpiry,
onionRoutingPacket: OnionRoutingPacket,
tlvStream: TlvStream[UpdateAddHtlcTlv] = TlvStream.empty
) extends HtlcMessage
with UpdateMessage
with HasChannelId {
val blindingOpt: Option[PublicKey] =
tlvStream.get[UpdateAddHtlcTlv.BlindingPoint].map(_.publicKey)
}
case class UpdateFulfillHtlc(
channelId: ByteVector32,
id: Long,
paymentPreimage: ByteVector32,
tlvStream: TlvStream[UpdateFulfillHtlcTlv] = TlvStream.empty
) extends HtlcMessage
with UpdateMessage
with HasChannelId
with HtlcSettlementMessage {
lazy val paymentHash: ByteVector32 = Crypto.sha256(paymentPreimage)
}
case class UpdateFailHtlc(
channelId: ByteVector32,
id: Long,
reason: ByteVector,
tlvStream: TlvStream[UpdateFailHtlcTlv] = TlvStream.empty
) extends HtlcMessage
with UpdateMessage
with HasChannelId
with HtlcSettlementMessage
case class UpdateFailMalformedHtlc(
channelId: ByteVector32,
id: Long,
onionHash: ByteVector32,
failureCode: Int,
tlvStream: TlvStream[UpdateFailMalformedHtlcTlv] = TlvStream.empty
) extends HtlcMessage
with UpdateMessage
with HasChannelId
with HtlcSettlementMessage
case class CommitSig(
channelId: ByteVector32,
signature: ByteVector64,
htlcSignatures: List[ByteVector64],
tlvStream: TlvStream[CommitSigTlv] = TlvStream.empty
) extends HtlcMessage
with HasChannelId
case class RevokeAndAck(
channelId: ByteVector32,
perCommitmentSecret: PrivateKey,
nextPerCommitmentPoint: PublicKey,
tlvStream: TlvStream[RevokeAndAckTlv] = TlvStream.empty
) extends HtlcMessage
with HasChannelId
case class UpdateFee(
channelId: ByteVector32,
feeratePerKw: FeeratePerKw,
tlvStream: TlvStream[UpdateFeeTlv] = TlvStream.empty
) extends ChannelMessage
with UpdateMessage
with HasChannelId
case class AnnouncementSignatures(
channelId: ByteVector32,
shortChannelId: ShortChannelId,
nodeSignature: ByteVector64,
bitcoinSignature: ByteVector64,
tlvStream: TlvStream[AnnouncementSignaturesTlv] = TlvStream.empty
) extends RoutingMessage
with HasChannelId
case class ChannelAnnouncement(
nodeSignature1: ByteVector64,
nodeSignature2: ByteVector64,
bitcoinSignature1: ByteVector64,
bitcoinSignature2: ByteVector64,
features: Features[Feature],
chainHash: ByteVector32,
shortChannelId: ShortChannelId,
nodeId1: PublicKey,
nodeId2: PublicKey,
bitcoinKey1: PublicKey,
bitcoinKey2: PublicKey,
tlvStream: TlvStream[ChannelAnnouncementTlv] = TlvStream.empty
) extends RoutingMessage
with AnnouncementMessage
with HasChainHash
case class Color(r: Byte, g: Byte, b: Byte) {
override def toString: String =
f"#$r%02x$g%02x$b%02x" // to hexa s"# ${r}%02x ${r & 0xFF}${g & 0xFF}${b & 0xFF}"
}
sealed trait NodeAddress {
def host: String; def port: Int;
override def toString: String = s"$host:$port"
}
sealed trait OnionAddress extends NodeAddress
sealed trait IPAddress extends NodeAddress
object NodeAddress {
def fromParts(host: String, port: Int): Try[NodeAddress] = Try {
host match {
case _ if host.endsWith(".onion") && host.length == 22 =>
Tor2(host.dropRight(6), port)
case _ if host.endsWith(".onion") && host.length == 62 =>
Tor3(host.dropRight(6), port)
case _ => IPAddress(IpAddress.fromString(host).get, port)
}
}
}
object IPAddress {
def apply(ipAddress: IpAddress, port: Int): IPAddress =
ipAddress match {
case address: Ipv4Address => IPv4(address, port)
case address: Ipv6Address => IPv6(address, port)
}
}
case class IPv4(ipv4: Ipv4Address, port: Int) extends IPAddress {
override def host: String = ipv4.toString()
}
case class IPv6(ipv6: Ipv6Address, port: Int) extends IPAddress {
override def host: String = ipv6.toString()
}
case class Tor2(tor2: String, port: Int) extends OnionAddress {
override def host: String = tor2 + ".onion"
}
case class Tor3(tor3: String, port: Int) extends OnionAddress {
override def host: String = tor3 + ".onion"
}
case class NodeAnnouncement(
signature: ByteVector64,
features: Features[Feature],
timestamp: TimestampSecond,
nodeId: PublicKey,
rgbColor: Color,
alias: String,
addresses: List[NodeAddress],
tlvStream: TlvStream[NodeAnnouncementTlv] = TlvStream.empty
) extends RoutingMessage
with AnnouncementMessage
with HasTimestamp
case class ChannelUpdate(
signature: ByteVector64,
chainHash: ByteVector32,
shortChannelId: ShortChannelId,
timestamp: TimestampSecond,
channelFlags: ChannelUpdate.ChannelFlags,
cltvExpiryDelta: CltvExpiryDelta,
htlcMinimumMsat: MilliSatoshi,
feeBaseMsat: MilliSatoshi,
feeProportionalMillionths: Long,
htlcMaximumMsat: MilliSatoshi,
tlvStream: TlvStream[ChannelUpdateTlv] = TlvStream.empty
) extends RoutingMessage
with AnnouncementMessage
with HasTimestamp
with HasChainHash {
def messageFlags: Byte = 1
def toStringShort: String =
s"cltvExpiryDelta=$cltvExpiryDelta,feeBase=$feeBaseMsat,feeProportionalMillionths=$feeProportionalMillionths"
}
object ChannelUpdate {
case class ChannelFlags(isEnabled: Boolean, isNode1: Boolean)
case class Checksum(
chainHash: ByteVector32,
shortChannelId: ShortChannelId,
channelFlags: ChannelUpdate.ChannelFlags,
cltvExpiryDelta: CltvExpiryDelta,
htlcMinimumMsat: MilliSatoshi,
feeBaseMsat: MilliSatoshi,
feeProportionalMillionths: Long,
htlcMaximumMsat: MilliSatoshi
)
}
sealed trait EncodingType
object EncodingType {
case object UNCOMPRESSED extends EncodingType
case object COMPRESSED_ZLIB extends EncodingType
}
case class EncodedShortChannelIds(
encoding: EncodingType,
array: List[ShortChannelId]
) {
/** custom toString because it can get huge in logs */
override def toString: String =
s"EncodedShortChannelIds($encoding,${array.headOption
.getOrElse("")}->${array.lastOption.getOrElse("")} size=${array.size})"
}
case class QueryShortChannelIds(
chainHash: ByteVector32,
shortChannelIds: EncodedShortChannelIds,
tlvStream: TlvStream[QueryShortChannelIdsTlv] = TlvStream.empty
) extends RoutingMessage
with HasChainHash {
val queryFlagsOpt: Option[QueryShortChannelIdsTlv.EncodedQueryFlags] =
tlvStream.get[QueryShortChannelIdsTlv.EncodedQueryFlags]
}
case class ReplyShortChannelIdsEnd(
chainHash: ByteVector32,
complete: Byte,
tlvStream: TlvStream[ReplyShortChannelIdsEndTlv] = TlvStream.empty
) extends RoutingMessage
with HasChainHash
case class QueryChannelRange(
chainHash: ByteVector32,
firstBlock: BlockHeight,
numberOfBlocks: Long,
tlvStream: TlvStream[QueryChannelRangeTlv] = TlvStream.empty
) extends RoutingMessage {
val queryFlagsOpt: Option[QueryChannelRangeTlv.QueryFlags] =
tlvStream.get[QueryChannelRangeTlv.QueryFlags]
}
case class ReplyChannelRange(
chainHash: ByteVector32,
firstBlock: BlockHeight,
numberOfBlocks: Long,
syncComplete: Byte,
shortChannelIds: EncodedShortChannelIds,
tlvStream: TlvStream[ReplyChannelRangeTlv] = TlvStream.empty
) extends RoutingMessage {
val timestampsOpt: Option[ReplyChannelRangeTlv.EncodedTimestamps] =
tlvStream.get[ReplyChannelRangeTlv.EncodedTimestamps]
val checksumsOpt: Option[ReplyChannelRangeTlv.EncodedChecksums] =
tlvStream.get[ReplyChannelRangeTlv.EncodedChecksums]
}
object ReplyChannelRange {
def apply(
chainHash: ByteVector32,
firstBlock: BlockHeight,
numberOfBlocks: Long,
syncComplete: Byte,
shortChannelIds: EncodedShortChannelIds,
timestamps: Option[ReplyChannelRangeTlv.EncodedTimestamps],
checksums: Option[ReplyChannelRangeTlv.EncodedChecksums]
): ReplyChannelRange = {
timestamps.foreach(ts =>
require(ts.timestamps.length == shortChannelIds.array.length)
)
checksums.foreach(cs =>
require(cs.checksums.length == shortChannelIds.array.length)
)
new ReplyChannelRange(
chainHash,
firstBlock,
numberOfBlocks,
syncComplete,
shortChannelIds,
TlvStream(timestamps.toList ::: checksums.toList)
)
}
}
case class GossipTimestampFilter(
chainHash: ByteVector32,
firstTimestamp: TimestampSecond,
timestampRange: Long,
tlvStream: TlvStream[GossipTimestampFilterTlv] = TlvStream.empty
) extends RoutingMessage
with HasChainHash
case class UnknownMessage(tag: Int, data: ByteVector) extends LightningMessage