-
Notifications
You must be signed in to change notification settings - Fork 1
/
PaymentPacket.scala
292 lines (277 loc) · 11.9 KB
/
PaymentPacket.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
package scoin.ln
import scala.util.{Failure, Success, Try}
import scodec.bits.ByteVector
import scodec.{Attempt, Codec, DecodeResult}
import scoin._
import scoin.Crypto.{PrivateKey, PublicKey}
import scoin.ln._
sealed trait IncomingPaymentPacket
/** Helpers to handle incoming payment packets. */
object IncomingPaymentPacket {
/** We are the final recipient. */
case class FinalPacket(
add: UpdateAddHtlc,
payload: PaymentOnion.FinalTlvPayload
) extends IncomingPaymentPacket
/** We are an intermediate node. */
sealed trait RelayPacket extends IncomingPaymentPacket
/** We must relay the payment to a direct peer. */
case class ChannelRelayPacket(
add: UpdateAddHtlc,
payload: PaymentOnion.ChannelRelayData,
nextPacket: OnionRoutingPacket,
nextBlindingKeyOpt: Option[PublicKey]
) extends RelayPacket {
val relayFeeMsat: MilliSatoshi = add.amountMsat - payload.amountToForward
val expiryDelta: CltvExpiryDelta = add.cltvExpiry - payload.outgoingCltv
}
/** We must relay the payment to a remote node. */
case class NodeRelayPacket(
add: UpdateAddHtlc,
outerPayload: PaymentOnion.FinalTlvPayload,
innerPayload: PaymentOnion.NodeRelayPayload,
nextPacket: OnionRoutingPacket
) extends RelayPacket
case class DecodedOnionPacket[T <: PaymentOnion.PacketType](
payload: T,
next: OnionRoutingPacket
)
private def decryptOnion[T <: PaymentOnion.PacketType](
paymentHash: ByteVector32,
privateKey: PrivateKey,
packet: OnionRoutingPacket,
perHopPayloadCodec: Boolean => Codec[T]
): Either[FailureMessage, DecodedOnionPacket[T]] =
Sphinx.peel(privateKey, Some(paymentHash), packet) match {
case Right(p @ Sphinx.DecryptedPacket(payload, nextPacket, _)) =>
perHopPayloadCodec(p.isLastPacket).decode(payload.bits) match {
case Attempt.Successful(DecodeResult(perHopPayload, _)) =>
Right(DecodedOnionPacket(perHopPayload, nextPacket))
case Attempt.Failure(e: OnionRoutingCodecs.MissingRequiredTlv) =>
Left(e.failureMessage)
case Attempt.Failure(e: OnionRoutingCodecs.ForbiddenTlv) =>
Left(e.failureMessage)
// Onion is correctly encrypted but the content of the per-hop payload couldn't be parsed.
// It's hard to provide tag and offset information from scodec failures, so we currently don't do it.
case Attempt.Failure(_) => Left(InvalidOnionPayload(UInt64(0), 0))
}
case Left(badOnion) => Left(badOnion)
}
/** Decrypt the onion packet of a received htlc. If we are the final
* recipient, we validate that the HTLC fields match the onion fields (this
* prevents intermediate nodes from sending an invalid amount or expiry).
*
* NB: we can't fully validate RelayPackets because it requires knowing the
* channel/route we'll be using, which we don't know yet. Such validation is
* the responsibility of downstream components.
*
* @param add
* incoming htlc
* @param privateKey
* this node's private key
* @return
* whether the payment is to be relayed or if our node is the final
* recipient (or an error).
*/
def decrypt(
add: UpdateAddHtlc,
privateKey: PrivateKey
): Either[FailureMessage, IncomingPaymentPacket] = {
// We first derive the decryption key used to peel the onion.
val outerOnionDecryptionKey = add.blindingOpt match {
case Some(blinding) =>
Sphinx.RouteBlinding.derivePrivateKey(privateKey, blinding)
case None => privateKey
}
decryptOnion(
add.paymentHash,
outerOnionDecryptionKey,
add.onionRoutingPacket,
PaymentOnionCodecs.paymentOnionPerHopPayloadCodec
) match {
case Left(failure) => Left(failure)
case Right(
DecodedOnionPacket(payload: PaymentOnion.ChannelRelayPayload, next)
) =>
payload match {
case payload: PaymentOnion.BlindedChannelRelayPayload =>
if (add.blindingOpt.isDefined && payload.blindingOpt.isDefined) {
Left(InvalidOnionPayload(UInt64(12), 0))
} else {
add.blindingOpt.orElse(payload.blindingOpt) match {
case Some(blinding) =>
RouteBlindingEncryptedDataCodecs.decode(
privateKey,
blinding,
payload.encryptedRecipientData,
RouteBlindingEncryptedDataCodecs.paymentRelayDataCodec
) match {
case Failure(_) =>
// There are two possibilities in this case:
// - the blinding point is invalid: the sender or the previous node is buggy or malicious
// - the encrypted data is invalid: the recipient is buggy or malicious
// TODO: return an unparseable error
Left(InvalidOnionPayload(UInt64(12), 0))
case Success((relayData, nextBlinding)) =>
if (
isValidBlindedPayment(
relayData,
add.amountMsat,
add.cltvExpiry,
Features.empty
)
) {
// TODO: If we build routes with several copies of our node at the end to hide the true length of the
// route, then we should add some code here to continue decrypting onions until we reach the final packet.
Right(
ChannelRelayPacket(
add,
PaymentOnion.BlindedChannelRelayData(
relayData,
add.amountMsat,
add.cltvExpiry
),
next,
Some(nextBlinding)
)
)
} else {
// The sender is buggy or malicious, probably trying to probe the blinded route.
// TODO: return an unparseable error
Left(InvalidOnionPayload(UInt64(12), 0))
}
}
case None =>
// The sender is trying to use route blinding, but we didn't receive the blinding point used to derive
// the decryption key. The sender or the previous peer is buggy or malicious.
// TODO: return an unparseable error
Left(InvalidOnionPayload(UInt64(12), 0))
}
}
case _ if add.blindingOpt.isDefined =>
Left(InvalidOnionPayload(UInt64(12), 0))
// NB: we don't validate the ChannelRelayPacket here because its fees and cltv depend on what channel we'll choose to use.
case payload: PaymentOnion.ChannelRelayTlvPayload =>
Right(ChannelRelayPacket(add, payload, next, None))
}
case Right(DecodedOnionPacket(payload: PaymentOnion.FinalPayload, _)) =>
payload match {
case payload: PaymentOnion.FinalTlvPayload =>
// We check if the payment is using trampoline: if it is, we may not be the final recipient.
payload.records.get[OnionPaymentPayloadTlv.TrampolineOnion] match {
case Some(
OnionPaymentPayloadTlv.TrampolineOnion(trampolinePacket)
) =>
// NB: when we enable blinded trampoline routes, we will need to check if the outer onion contains a blinding
// point and use it to derive the decryption key for the blinded trampoline onion.
decryptOnion(
add.paymentHash,
privateKey,
trampolinePacket,
PaymentOnionCodecs.trampolineOnionPerHopPayloadCodec
) match {
case Left(failure) => Left(failure)
case Right(
DecodedOnionPacket(
innerPayload: PaymentOnion.NodeRelayPayload,
next
)
) =>
validateNodeRelay(add, payload, innerPayload, next)
case Right(
DecodedOnionPacket(
innerPayload: PaymentOnion.FinalTlvPayload,
_
)
) =>
validateFinal(add, payload, innerPayload)
case Right(
DecodedOnionPacket(
_: PaymentOnion.BlindedFinalPayload,
_
)
) =>
Left(
InvalidOnionPayload(UInt64(12), 0)
) // trampoline blinded routes are not supported yet
}
case None => validateFinal(add, payload)
}
case _: PaymentOnion.BlindedFinalPayload =>
// TODO: receiving through blinded routes is not supported yet.
Left(InvalidOnionPayload(UInt64(12), 0))
}
}
}
private def isValidBlindedPayment(
data: BlindedRouteData.PaymentData,
amount: MilliSatoshi,
cltvExpiry: CltvExpiry,
features: Features[Feature]
): Boolean = {
val amountOk = amount >= data.paymentConstraints.minAmount
val expiryOk = cltvExpiry <= data.paymentConstraints.maxCltvExpiry
val featuresOk = Features.areCompatible(features, data.allowedFeatures)
amountOk && expiryOk && featuresOk
}
private def validateFinal(
add: UpdateAddHtlc,
payload: PaymentOnion.FinalTlvPayload
): Either[FailureMessage, IncomingPaymentPacket] = {
if (add.amountMsat != payload.amount) {
Left(FinalIncorrectHtlcAmount(add.amountMsat))
} else if (add.cltvExpiry != payload.expiry) {
Left(FinalIncorrectCltvExpiry(add.cltvExpiry))
} else {
Right(FinalPacket(add, payload))
}
}
private def validateFinal(
add: UpdateAddHtlc,
outerPayload: PaymentOnion.FinalTlvPayload,
innerPayload: PaymentOnion.FinalTlvPayload
): Either[FailureMessage, IncomingPaymentPacket] = {
if (add.amountMsat != outerPayload.amount) {
Left(FinalIncorrectHtlcAmount(add.amountMsat))
} else if (add.cltvExpiry != outerPayload.expiry) {
Left(FinalIncorrectCltvExpiry(add.cltvExpiry))
} else if (outerPayload.expiry != innerPayload.expiry) {
Left(
FinalIncorrectCltvExpiry(add.cltvExpiry)
) // previous trampoline didn't forward the right expiry
} else if (outerPayload.totalAmount != innerPayload.amount) {
Left(
FinalIncorrectHtlcAmount(outerPayload.totalAmount)
) // previous trampoline didn't forward the right amount
} else {
// We merge contents from the outer and inner payloads.
// We must use the inner payload's total amount and payment secret because the payment may be split between multiple trampoline payments (#reckless).
Right(
FinalPacket(
add,
PaymentOnion.createMultiPartPayload(
outerPayload.amount,
innerPayload.totalAmount,
outerPayload.expiry,
innerPayload.paymentSecret,
innerPayload.paymentMetadata
)
)
)
}
}
private def validateNodeRelay(
add: UpdateAddHtlc,
outerPayload: PaymentOnion.FinalTlvPayload,
innerPayload: PaymentOnion.NodeRelayPayload,
next: OnionRoutingPacket
): Either[FailureMessage, IncomingPaymentPacket] = {
if (add.amountMsat < outerPayload.amount) {
Left(FinalIncorrectHtlcAmount(add.amountMsat))
} else if (add.cltvExpiry != outerPayload.expiry) {
Left(FinalIncorrectCltvExpiry(add.cltvExpiry))
} else {
Right(NodeRelayPacket(add, outerPayload, innerPayload, next))
}
}
}