Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update TLV ranges for Offers #716

Merged
merged 3 commits into from
Oct 16, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions src/commonMain/kotlin/fr/acinq/lightning/wire/OfferTypes.kt
Original file line number Diff line number Diff line change
Expand Up @@ -648,19 +648,27 @@ object OfferTypes {
}
}

private fun isOfferTlv(tlv: GenericTlv): Boolean {
// Offer TLVs are in the range [1, 79] or [1000000000, 1999999999].
return tlv.tag in 1..79 || tlv.tag in 1000000000..1999999999
}

private fun isInvoiceRequestTlv(tlv: GenericTlv): Boolean {
// Offer TLVs are in the range [0, 159] or [1000000000, 2999999999].
t-bast marked this conversation as resolved.
Show resolved Hide resolved
return tlv.tag in 0..159 || tlv.tag in 1000000000..2999999999
}

fun filterOfferFields(tlvs: TlvStream<InvoiceRequestTlv>): TlvStream<OfferTlv> {
// Offer TLVs are in the range (0, 80).
return TlvStream(
tlvs.records.filterIsInstance<OfferTlv>().toSet(),
tlvs.unknown.filter { it.tag < 80 }.toSet()
tlvs.unknown.filter { isOfferTlv(it) }.toSet()
)
}

fun filterInvoiceRequestFields(tlvs: TlvStream<InvoiceTlv>): TlvStream<InvoiceRequestTlv> {
// Invoice request TLVs are in the range [0, 160): invoice request metadata (tag 0), offer TLVs, and additional invoice request TLVs in the range [80, 160).
return TlvStream(
tlvs.records.filterIsInstance<InvoiceRequestTlv>().toSet(),
tlvs.unknown.filter { it.tag < 160 }.toSet()
tlvs.unknown.filter { isInvoiceRequestTlv(it) }.toSet()
)
}

Expand Down Expand Up @@ -806,7 +814,7 @@ object OfferTypes {
fun validate(records: TlvStream<OfferTlv>): Either<InvalidTlvPayload, Offer> {
if (records.get<OfferDescription>() == null && records.get<OfferAmount>() != null) return Left(MissingRequiredTlv(10))
if (records.get<OfferNodeId>() == null && records.get<OfferPaths>() == null) return Left(MissingRequiredTlv(22))
if (records.unknown.any { it.tag >= 80 }) return Left(ForbiddenTlv(records.unknown.find { it.tag >= 80 }!!.tag))
if (records.unknown.any { !isOfferTlv(it) }) return Left(ForbiddenTlv(records.unknown.find { !isOfferTlv(it) }!!.tag))
return Right(Offer(records))
}

Expand Down Expand Up @@ -932,7 +940,7 @@ object OfferTypes {
if (records.get<InvoiceRequestMetadata>() == null) return Left(MissingRequiredTlv(0L))
if (records.get<InvoiceRequestPayerId>() == null) return Left(MissingRequiredTlv(88))
if (records.get<Signature>() == null) return Left(MissingRequiredTlv(240))
if (records.unknown.any { it.tag >= 160 }) return Left(ForbiddenTlv(records.unknown.find { it.tag >= 160 }!!.tag))
if (records.unknown.any { !isInvoiceRequestTlv(it) }) return Left(ForbiddenTlv(records.unknown.find { !isInvoiceRequestTlv(it) }!!.tag))
return Right(InvoiceRequest(records))
}

Expand Down