What changed, and why it matters
This commit adds two small helper functions for working with optional data fields in Lightning Network messages. It is purely additive and contains no security fix or behavior change to existing code. There is no indication it addresses a vulnerability.
No security action needed. Review as normal code-quality change.
Security signals we found
No strong security signals were identified.
Evidence from the diff
The patch introduces AddOpt and SetOptFromMap in lnwire/custom_records.go, plus unit tests. AddOpt appends a tlv.RecordProducer only when an OptionalRecordT is Some. SetOptFromMap marks an optional record as Some based on presence in a decoded TypeMap. Both are generic utilities with no callers shown in this commit and no changes to existing logic.
Changed components
lnwire/custom_records.golnwire/custom_records_test.goInspect captured patch +69 / −0
diff --git a/lnwire/custom_records.go b/lnwire/custom_records.go
index de5ff4a..90f99d2 100644
--- a/lnwire/custom_records.go
+++ b/lnwire/custom_records.go
@@ -263,6 +263,32 @@ func DecodeRecordsP2P(r *bytes.Reader,
return tlvStream.DecodeWithParsedTypesP2P(r)
}
+// AddOpt appends a record producer for the given optional record to producers
+// when the optional is set, leaving producers unchanged otherwise.
+func AddOpt[T tlv.TlvType, V any](producers *[]tlv.RecordProducer,
+ opt tlv.OptionalRecordT[T, V]) {
+
+ opt.WhenSome(
+ func(r tlv.RecordT[T, V]) {
+ *producers = append(*producers, &r)
+ },
+ )
+}
+
+// SetOptFromMap marks target as Some(record) when record's TLV type appeared
+// on the wire (i.e., is a key in the decoded TypeMap).
+//
+// The caller must have passed record to the underlying Stream before decoding;
+// otherwise record.Val will not have been populated, and wrapping it as Some
+// would yield a zero-valued field.
+func SetOptFromMap[T tlv.TlvType, V any](typeMap tlv.TypeMap,
+ target *tlv.OptionalRecordT[T, V], record tlv.RecordT[T, V]) {
+
+ if _, ok := typeMap[record.TlvType()]; ok {
+ *target = tlv.SomeRecordT(record)
+ }
+}
+
// AssertUniqueTypes asserts that the given records have unique types.
func AssertUniqueTypes(r []tlv.Record) error {
seen := make(fn.Set[tlv.Type], len(r))
diff --git a/lnwire/custom_records_test.go b/lnwire/custom_records_test.go
index d4aad2e..d14586b 100644
--- a/lnwire/custom_records_test.go
+++ b/lnwire/custom_records_test.go
@@ -249,3 +249,46 @@ func TestCustomRecordsMergedCopy(t *testing.T) {
})
}
}
+
+// TestAddOptAppendsOnlyWhenSet checks that AddOpt is a no-op for an empty
+// optional and appends a producer when the optional is populated.
+func TestAddOptAppendsOnlyWhenSet(t *testing.T) {
+ t.Parallel()
+
+ var producers []tlv.RecordProducer
+
+ emptyOpt := tlv.OptionalRecordT[tlv.TlvType1, uint16]{}
+ AddOpt(&producers, emptyOpt)
+ require.Empty(t, producers)
+
+ setOpt := tlv.SomeRecordT(
+ tlv.NewPrimitiveRecord[tlv.TlvType1, uint16](42),
+ )
+ AddOpt(&producers, setOpt)
+ require.Len(t, producers, 1)
+
+ rec := producers[0].Record()
+ require.Equal(t, tlv.Type(1), rec.Type())
+}
+
+// TestSetOptFromMapUsesTypeMapPresence verifies that SetOptFromMap populates
+// only when the TLV type is present in the TypeMap.
+func TestSetOptFromMapUsesTypeMapPresence(t *testing.T) {
+ t.Parallel()
+
+ present := tlv.TypeMap{tlv.Type(1): nil}
+ missing := tlv.TypeMap{}
+
+ var target tlv.OptionalRecordT[tlv.TlvType1, uint16]
+ SetOptFromMap(
+ missing, &target,
+ tlv.NewPrimitiveRecord[tlv.TlvType1, uint16](7),
+ )
+ require.True(t, target.IsNone())
+
+ SetOptFromMap(
+ present, &target,
+ tlv.NewPrimitiveRecord[tlv.TlvType1, uint16](7),
+ )
+ require.True(t, target.IsSome())
+}
Why this scored 15/100
Community notes
Notes can correct, qualify, or add evidence to the AI analysis. Every note shown here has been validated by a human moderator.
The AI analysis stands alone for now. Submit a note if you can add evidence or important context.